Skip to content
Snippets Groups Projects
Commit a2c983e2 authored by David Schäfer's avatar David Schäfer
Browse files

bugfix: reserved constants (GOOD, BAD) as argument to flagGeneric

        failed, as they were evaluated during 'compile' time instead
        of execution time
parent 35187685
No related branches found
No related tags found
2 merge requests!193Release 1.4,!188Release 1.4
Pipeline #7387 passed with stages
in 9 minutes and 55 seconds
......@@ -160,7 +160,8 @@ class ConfigFunctionParser(ast.NodeVisitor):
k, v = node.arg, node.value
check_tree = True
# NOTE: not a constant or variable, should be function call
# NOTE: `node` is not a constant or a variable,
# so it should be a function call
try:
visitor = ConfigExpressionParser(v)
args = ast.arguments(
......@@ -189,9 +190,16 @@ class ConfigFunctionParser(ast.NodeVisitor):
# -> after all keywords where visited we end up with
# a dictionary holding all the passed arguments as
# real python objects
co = compile(ast.fix_missing_locations(ast.Interactive(body=[vnode])), "<ast>", mode="single")
# NOTE: only pass a copy to not clutter the ENVIRONMENT
exec(co, {**ENVIRONMENT}, self.kwargs)
if isinstance(v, ast.Name) and v.id in RESERVED:
# NOTE:
# the reserved constants needs to be evaluated during
# execution time, so dump the keyword directly and
# skip any further checks
self.kwargs[k] = v
else:
co = compile(ast.fix_missing_locations(ast.Interactive(body=[vnode])), "<ast>", mode="single")
# NOTE: only pass a copy to not clutter the ENVIRONMENT
exec(co, {**ENVIRONMENT}, self.kwargs)
# let's do some more validity checks
if check_tree:
......
......@@ -216,14 +216,14 @@ def test_isflagged(data, flagger):
tests = [
(f"isflagged({var1})", flagger.isFlagged(var1)),
(f"isflagged({var1}, BAD)", flagger.isFlagged(var1, flag=flagger.BAD, comparator=">=")),
(f"isflagged({var1}, flag=BAD)", flagger.isFlagged(var1, flag=flagger.BAD, comparator=">=")),
(f"isflagged({var1}, UNFLAGGED, '==')", flagger.isFlagged(var1, flag=flagger.UNFLAGGED, comparator="==")),
(f"~isflagged({var2})", ~flagger.isFlagged(var2)),
(f"~({var2}>999) & (~isflagged({var2}))", ~(data[var2] > 999) & (~flagger.isFlagged(var2))),
]
for test, expected in tests:
func = _compileGeneric(f"flagGeneric(func={test})")
func = _compileGeneric(f"flagGeneric(func={test}, flag=BAD)")
result = _execGeneric(flagger, data, func, field=None, nodata=np.nan)
assert np.all(result == expected)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment