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
symtable.Symbol.is_local() can be True for global symbols #84377
Comments
Consider this function: def f():
global e
e = 1 When inspecting symbols with symtable, symbol 'e' will be global and local, whereas is_local() should return False. See the attached file for reproducing. It will output to stdout: symbol 'e' in function scope: is_global() = True, is_local() = True |
see https://stackoverflow.com/a/61040435/1725562 for a proposed fix |
That fix is not correct. For instance consider: >>> code2 = """\
... def foo():
... x = 42
... def bar():
... return -1
... """
>>> top.get_children()[0]
<Function SymbolTable for foo in ?>
>>> top = symtable.symtable(code2, "?", "exec")
>>> top.get_children()[0].lookup('x')._Symbol__scope == symtable.LOCAL
True but if we return x from bar: >>> code = """\
... def foo():
... x = 42
... def bar():
... return x
... """
>>> import symtable
>>> top = symtable.symtable(code, "?", "exec")
>>> top.get_children()[0].lookup('x')._Symbol__scope == symtable.LOCAL
False |
In symtable.Function.get_locals() symbols with scopes in (LOCAL, CELL) are selected. Also >>> code = """\
... def foo():
... x = 42
... def bar():
... return x
... """
>>> import symtable
>>> top = symtable.symtable(code, "?", "exec")
>>> top.get_children()[0].lookup('x')._Symbol__scope == symtable.CELL
True So I guess this would be the correct fix then: def is_local(self):
return self.__scope in (LOCAL, CELL) |
Thanks for pointing that out. I will simplify PR 19391. |
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: