classification
Title: symtable.Symbol.is_local() can be True for global symbols
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: pablogsal Nosy List: coproc, miss-islington, pablogsal
Priority: normal Keywords: patch

Created on 2020-04-05 13:30 by coproc, last changed 2020-04-06 16:42 by pablogsal. This issue is now closed.

Files
File name Uploaded Description Edit
global_and_local.py coproc, 2020-04-05 13:30 console output (as in comment) shows that for global symbol 'e' also is_local() returns True
Pull Requests
URL Status Linked Edit
PR 19391 merged pablogsal, 2020-04-06 10:06
PR 19394 merged miss-islington, 2020-04-06 16:06
PR 19395 merged miss-islington, 2020-04-06 16:06
Messages (8)
msg365820 - (view) Author: Wolfgang Stöcher (coproc) Date: 2020-04-05 13:30
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
global scope: e = 1
msg365821 - (view) Author: Wolfgang Stöcher (coproc) Date: 2020-04-05 13:34
see https://stackoverflow.com/a/61040435/1725562 for a proposed fix
msg365845 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-04-06 10:07
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
msg365853 - (view) Author: Wolfgang Stöcher (coproc) Date: 2020-04-06 13:28
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)
msg365855 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-04-06 13:35
> In symtable.Function.get_locals() symbols with scopes in (LOCAL, CELL) are selected.

Thanks for pointing that out. I will simplify PR 19391.
msg365868 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-04-06 16:06
New changeset 799d7d61a91eb0ad3256ef9a45a90029cef93b7c by Pablo Galindo in branch 'master':
bpo-40196: Fix a bug in the symtable when reporting inspecting global variables (GH-19391)
https://github.com/python/cpython/commit/799d7d61a91eb0ad3256ef9a45a90029cef93b7c
msg365870 - (view) Author: miss-islington (miss-islington) Date: 2020-04-06 16:41
New changeset 717f1668b3455b498424577e194719f9beae13a1 by Miss Islington (bot) in branch '3.7':
bpo-40196: Fix a bug in the symtable when reporting inspecting global variables (GH-19391)
https://github.com/python/cpython/commit/717f1668b3455b498424577e194719f9beae13a1
msg365871 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-04-06 16:41
New changeset 8bd84e7f79a6cc7670a89a92edba3015aa781758 by Miss Islington (bot) in branch '3.8':
bpo-40196: Fix a bug in the symtable when reporting inspecting global variables (GH-19391) (GH-19394)
https://github.com/python/cpython/commit/8bd84e7f79a6cc7670a89a92edba3015aa781758
History
Date User Action Args
2020-04-06 16:42:07pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-04-06 16:41:59pablogsalsetmessages: + msg365871
2020-04-06 16:41:32miss-islingtonsetmessages: + msg365870
2020-04-06 16:06:50miss-islingtonsetpull_requests: + pull_request18757
2020-04-06 16:06:41miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request18756
2020-04-06 16:06:04pablogsalsetmessages: + msg365868
2020-04-06 13:35:19pablogsalsetmessages: + msg365855
2020-04-06 13:33:56pablogsalsetmessages: - msg365854
2020-04-06 13:33:08pablogsalsetmessages: + msg365854
2020-04-06 13:28:36coprocsetmessages: + msg365853
2020-04-06 10:07:07pablogsalsetmessages: + msg365845
2020-04-06 10:06:55pablogsalsetmessages: - msg365843
2020-04-06 10:06:05pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request18753
2020-04-06 09:49:56pablogsalsetmessages: + msg365843
2020-04-06 08:33:27pablogsalsetassignee: pablogsal

nosy: + pablogsal
2020-04-05 13:34:28coprocsettype: behavior
messages: + msg365821
2020-04-05 13:30:26coproccreate