wrap filters/conditionals in a try-except block
allows accessing undefined fields without exception or locals().get(…) but hides mistakes/typos/etc by evaluating to False without feedback performance loss compared to the previous version without try-except is negligible (~20ns for me)
This commit is contained in:
@@ -105,6 +105,11 @@ def main():
|
||||
|
||||
output.ANSI = True
|
||||
|
||||
# filter environment
|
||||
filterenv = config.get((), "filters-environment", True)
|
||||
if not filterenv:
|
||||
util.compile_expression = util.compile_expression_raw
|
||||
|
||||
# format string separator
|
||||
separator = config.get((), "format-separator")
|
||||
if separator:
|
||||
|
||||
@@ -616,11 +616,28 @@ else:
|
||||
Popen = subprocess.Popen
|
||||
|
||||
|
||||
def compile_expression(expr, name="<expr>", globals=None):
|
||||
def compile_expression_raw(expr, name="<expr>", globals=None):
|
||||
code_object = compile(expr, name, "eval")
|
||||
return functools.partial(eval, code_object, globals or GLOBALS)
|
||||
|
||||
|
||||
def compile_expression_tryexcept(expr, name="<expr>", globals=None):
|
||||
code_object = compile(expr, name, "eval")
|
||||
|
||||
def _eval(locals=None, globals=(globals or GLOBALS), co=code_object):
|
||||
try:
|
||||
return eval(co, globals, locals)
|
||||
except exception.GalleryDLException:
|
||||
raise
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
return _eval
|
||||
|
||||
|
||||
compile_expression = compile_expression_tryexcept
|
||||
|
||||
|
||||
def import_file(path):
|
||||
"""Import a Python module from a filesystem path"""
|
||||
path, name = os.path.split(path)
|
||||
|
||||
Reference in New Issue
Block a user