-
-
Notifications
You must be signed in to change notification settings - Fork 32k
exec of list comprehension fails on NameError #57766
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
Calling exec() on code that includes a list comprehension that references a defined local variable x fails incorrectly on "NameError: global name 'x' not defined". |
This is expected and documented: http://docs.python.org/py3k/reference/executionmodel.html#interaction-with-dynamic-features Try using exec(code, locals()). It's a good habit anyway to always pass a namespace to exec(). |
Ah, thanks, there it is... I thought this must be dealt with somewhere but couldn't find it. Maybe should add something to the 'exec' statement docs http://docs.python.org/py3k/library/functions.html#exec to reference this (from a usability-of-docs standpoint). BTW, my code already sends the namespaces to exec (from current stack frame; it's part of a debugger) and probably would break other cases to alter this. Well, anyway, sorry for the invalid bug report... |
Would you like to make a doc patch? |
Here's a patch to the docs that notes the issue and refers the reader to the relevant execution model docs page. I have also attempted to clarify the "interaction with dynamic features" section of the execution model page. |
Issues like this, about exec, have come up multiple times. I just closed bpo-14049 as a duplicate of this, and listed there some other issues. So I think that the doc for exec (and execfile in 2.7) could be better still. I would like to see something like the following added, whether instead of or in addition to the current proposal -- perhaps after "If provided, locals can be any mapping object." (quote from the 3.2 exec entry) "At module level, globals and locals are the same dictionary. If one passes two separate objects as globals and locals, the code will be executed as if it were embedded in a class definition." To me, this is friendlier and less intimidating than 'free variable' and the Execution model doc chapter. It summarizes the essential problem with such exec calls. To illustrate, the following gives the same NameError. and for the same reason, as exec(code,{},{}), where code is the quoted unindented version with the class line deleted. class x:
x = 1
def incx():
return x+1
print(incx()) |
Stefan: This fell off my radar, sorry I haven’t reviewed your patch yet. Terry: +1 |
New changeset ab22ffa6fb2e by Terry Jan Reedy in branch '2.7': New changeset ea670d71a36d by Terry Jan Reedy in branch '3.2': New changeset b47ae7a9e685 by Terry Jan Reedy in branch 'default': |
Present in Python 3.10 but not in 3.12
Output (in its default above):
Expected output (seen if the exec() is outside of main(), for example):
|
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: