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:
Pablo Galindo 2018-10-20 01:46:00 +01:00 committed by GitHub
parent 6395844e6a
commit d5b4f1b5a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 3 deletions

View file

@ -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)