-
-
Notifications
You must be signed in to change notification settings - Fork 32k
exec fails to take locals into account when running list comprehensions or functions #86084
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
Comments
The exec function fails to take locals into account when executing a list comprehension:
This is the cause of https://bugs.python.org/issue21161 |
Fails for functions as well:
|
This seems to happen only when both arguments to exec are used:
|
This issue also occurs in Python 3.8.6 |
Also occurs in Python 3.6.9 |
This also occurs in Python 3.10.4 !!! |
And 3.11.9 Example: example = """
fields_a = ['testa0', 'test1']
fields_b = ['testb0', 'test1']
print(fields_a)
print(fields_b)
print([field for field in fields_a if field in fields_b])
"""
locals = {}
globals = {}
exec(example, globals, locals) Gives error: ['testa0', 'test1']
['testb0', 'test1']
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<string>", line 6, in <module>
File "<string>", line 6, in <listcomp>
NameError: name 'fields_b' is not defined I used this as a workaround: locals = {}
globals = {}
combined_context = globals.copy()
combined_context.update(locals)
exec(example, combined_context, combined_context) See also this more recent issue: #121306 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: