Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New way to populate locals() for filter #247

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions fmf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,11 @@ def evaluate(expression, data, _node=None):
Expects data dictionary which will be used to populate local
namespace. Used to provide flexible conditions for filtering.
"""
locals().update(data)
# Since python3.13 and https://peps.python.org/pep-0667/
_locals = copy.deepcopy(locals())
Copy link
Collaborator

@happz happz Jun 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t believe deepcopy() is needed. locals() should regurn an independent snapshot of local namespace, i.e. we would be making (and updating) a copy of dictionary no other part of code can ever see. Seems superfluous.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other words, I agree we need to store locals() somewhere if we need to both update it and pass it to a function call, ut IIUIC, we don’t need to work with a copy of said dictionary.

_locals.update(data)
try:
return eval(expression)
return eval(expression, globals=globals(), locals=_locals)
except NameError as error:
raise FilterError("Key is not defined in data: {}".format(error))
except KeyError as error:
Expand Down
Loading