diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2020-10-03 20:23:03 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-03 20:23:03 (GMT) |
commit | 7b7aa94e51a396365df41f86b7b64a3b74481a6c (patch) | |
tree | dfb094c2b3cd3d04b2ce3b95b3b1c2e274ccfa95 /Lib/symtable.py | |
parent | e89253756c00ca71ff6476114a052d30d6de3af2 (diff) | |
download | cpython-7b7aa94e51a396365df41f86b7b64a3b74481a6c.zip cpython-7b7aa94e51a396365df41f86b7b64a3b74481a6c.tar.gz cpython-7b7aa94e51a396365df41f86b7b64a3b74481a6c.tar.bz2 |
[3.9] bpo-41840: Report module-level globals as both local and global in the symtable module (GH-22391). (GH-22528)
(cherry picked from commit fb0a4651f1be4ad936f8277478f73f262d8eeb72)
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Diffstat (limited to 'Lib/symtable.py')
-rw-r--r-- | Lib/symtable.py | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/Lib/symtable.py b/Lib/symtable.py index a711676..521540f 100644 --- a/Lib/symtable.py +++ b/Lib/symtable.py @@ -34,7 +34,7 @@ class SymbolTableFactory: _newSymbolTable = SymbolTableFactory() -class SymbolTable(object): +class SymbolTable: def __init__(self, raw_table, filename): self._table = raw_table @@ -47,7 +47,7 @@ class SymbolTable(object): else: kind = "%s " % self.__class__.__name__ - if self._table.name == "global": + if self._table.name == "top": return "<{0}SymbolTable for module {1}>".format(kind, self._filename) else: return "<{0}SymbolTable for {1} in {2}>".format(kind, @@ -90,7 +90,9 @@ class SymbolTable(object): if sym is None: flags = self._table.symbols[name] namespaces = self.__check_children(name) - sym = self._symbols[name] = Symbol(name, flags, namespaces) + module_scope = (self._table.name == "top") + sym = self._symbols[name] = Symbol(name, flags, namespaces, + module_scope=module_scope) return sym def get_symbols(self): @@ -163,13 +165,14 @@ class Class(SymbolTable): return self.__methods -class Symbol(object): +class Symbol: - def __init__(self, name, flags, namespaces=None): + def __init__(self, name, flags, namespaces=None, *, module_scope=False): self.__name = name self.__flags = flags self.__scope = (flags >> SCOPE_OFF) & SCOPE_MASK # like PyST_GetScope() self.__namespaces = namespaces or () + self.__module_scope = module_scope def __repr__(self): return "<symbol {0!r}>".format(self.__name) @@ -184,7 +187,10 @@ class Symbol(object): return bool(self.__flags & DEF_PARAM) def is_global(self): - return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)) + """Return *True* if the sysmbol is global. + """ + return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT) + or (self.__module_scope and self.__flags & DEF_BOUND)) def is_nonlocal(self): return bool(self.__flags & DEF_NONLOCAL) @@ -193,7 +199,10 @@ class Symbol(object): return bool(self.__scope == GLOBAL_EXPLICIT) def is_local(self): - return bool(self.__scope in (LOCAL, CELL)) + """Return *True* if the symbol is local. + """ + return bool(self.__scope in (LOCAL, CELL) + or (self.__module_scope and self.__flags & DEF_BOUND)) def is_annotated(self): return bool(self.__flags & DEF_ANNOT) |