-
-
Notifications
You must be signed in to change notification settings - Fork 32k
Unexpected behaviour of "compile" function #103375
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 second one is correct. Your first example has print inside a function, where it attempts to print the value of an unbound local variable: the local variable x is not bound until after The second example is not executed inside a function, it is just the top-level (module level) code To be clear: just because you are calling |
To get the same result as your
To execute the source code
|
@wrongnull The fact that you did not expect the behavior of @stevendaprano and everyone else: When two separate namespaces are passed to exec, the effect is as the code source were embedded in a class statement code that creates the passed in globals() and locals()
Adding At the point of 'exec' in example b, locals() has two entries {'source': 'print(x)', 'co': |
@terryjreedy OK, I got it. But such a semantics of this function raises an exception if used in pdb. What to do with it? |
BTW, talking of class definition... x = 0
y = 0
def foo():
x = 1
y = 1
class X:
print(x, y)
x = 2
foo() |
As already mentioned in #100185 builtin function
compile
does not take into account their enclosing scope. For instance:Invocation of the first function works correctly and generates an exception, what can not be said about the second one. This example and one in issue above are also relevant for all currently supported python versions. I bet this can be fixed by promoting the symbol table of the scope to the function.
The text was updated successfully, but these errors were encountered: