implement 'globals' option

This commit is contained in:
Mike Fährmann
2023-02-28 18:18:55 +01:00
parent b14f8d5817
commit d788e6c60c
4 changed files with 101 additions and 3 deletions

View File

@@ -619,9 +619,28 @@ GLOBALS = {
}
def compile_expression(expr, name="<expr>", globals=GLOBALS):
def compile_expression(expr, name="<expr>", globals=None):
code_object = compile(expr, name, "eval")
return functools.partial(eval, code_object, globals)
return functools.partial(eval, code_object, globals or GLOBALS)
def import_file(path):
"""Import a Python module from a filesystem path"""
path, name = os.path.split(path)
name, sep, ext = name.rpartition(".")
if not sep:
name = ext
if path:
path = expand_path(path)
sys.path.insert(0, path)
try:
return __import__(name)
finally:
del sys.path[0]
else:
return __import__(name)
def build_duration_func(duration, min=0.0):