mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
bpo-34983: Expose symtable.Symbol.is_nonlocal() in the symtable module (GH-9872)
The symbol table was not exposing functionality to query the nonlocal symbols in a function or to check if a particular symbol is nonlocal.
This commit is contained in:
parent
6395844e6a
commit
d5b4f1b5a0
5 changed files with 36 additions and 3 deletions
|
@ -1,7 +1,7 @@
|
|||
"""Interface to the compiler's internal symbol tables"""
|
||||
|
||||
import _symtable
|
||||
from _symtable import (USE, DEF_GLOBAL, DEF_LOCAL, DEF_PARAM,
|
||||
from _symtable import (USE, DEF_GLOBAL, DEF_NONLOCAL, DEF_LOCAL, DEF_PARAM,
|
||||
DEF_IMPORT, DEF_BOUND, DEF_ANNOT, SCOPE_OFF, SCOPE_MASK, FREE,
|
||||
LOCAL, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL)
|
||||
|
||||
|
@ -117,6 +117,7 @@ class Function(SymbolTable):
|
|||
__locals = None
|
||||
__frees = None
|
||||
__globals = None
|
||||
__nonlocals = None
|
||||
|
||||
def __idents_matching(self, test_func):
|
||||
return tuple(ident for ident in self.get_identifiers()
|
||||
|
@ -141,6 +142,11 @@ class Function(SymbolTable):
|
|||
self.__globals = self.__idents_matching(test)
|
||||
return self.__globals
|
||||
|
||||
def get_nonlocals(self):
|
||||
if self.__nonlocals is None:
|
||||
self.__nonlocals = self.__idents_matching(lambda x:x & DEF_NONLOCAL)
|
||||
return self.__nonlocals
|
||||
|
||||
def get_frees(self):
|
||||
if self.__frees is None:
|
||||
is_free = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) == FREE
|
||||
|
@ -184,6 +190,9 @@ class Symbol(object):
|
|||
def is_global(self):
|
||||
return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT))
|
||||
|
||||
def is_nonlocal(self):
|
||||
return bool(self.__flags & DEF_NONLOCAL)
|
||||
|
||||
def is_declared_global(self):
|
||||
return bool(self.__scope == GLOBAL_EXPLICIT)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue