summaryrefslogtreecommitdiffstats
path: root/Lib/symtable.py
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2018-10-20 00:46:00 (GMT)
committerGitHub <noreply@github.com>2018-10-20 00:46:00 (GMT)
commitd5b4f1b5a064c0d858352100fcddb91c363afa51 (patch)
treecae99173cc824e62289c7ebd8e5be290a206d0c1 /Lib/symtable.py
parent6395844e6adebc12c4eba1fb75c5e7c9c8b89f85 (diff)
downloadcpython-d5b4f1b5a064c0d858352100fcddb91c363afa51.zip
cpython-d5b4f1b5a064c0d858352100fcddb91c363afa51.tar.gz
cpython-d5b4f1b5a064c0d858352100fcddb91c363afa51.tar.bz2
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.
Diffstat (limited to 'Lib/symtable.py')
-rw-r--r--Lib/symtable.py11
1 files changed, 10 insertions, 1 deletions
diff --git a/Lib/symtable.py b/Lib/symtable.py
index c7627a6..5bea7cf 100644
--- a/Lib/symtable.py
+++ b/Lib/symtable.py
@@ -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)