Return a tuple containing names of globals in this function.
+ .. method:: get_nonlocals()
+
+ Return a tuple containing names of nonlocals in this function.
+
.. method:: get_frees()
Return a tuple containing names of free variables in this function.
Return ``True`` if the symbol is global.
+ .. method:: is_nonlocal()
+
+ Return ``True`` if the symbol is nonlocal.
+
.. method:: is_declared_global()
Return ``True`` if the symbol is declared global with a global statement.
"""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)
__locals = None
__frees = None
__globals = None
+ __nonlocals = None
def __idents_matching(self, test_func):
return tuple(ident for ident in self.get_identifiers()
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
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)
import sys
glob = 42
+some_var = 12
class Mine:
instance_var = 24
def spam(a, b, *var, **kw):
global bar
bar = 47
+ some_var = 10
x = 23
glob
def internal():
return x
+ def other_internal():
+ nonlocal some_var
+ some_var = 3
+ return some_var
return internal
def foo():
a_method = find_block(Mine, "a_method")
spam = find_block(top, "spam")
internal = find_block(spam, "internal")
+ other_internal = find_block(spam, "other_internal")
foo = find_block(top, "foo")
def test_type(self):
def test_lineno(self):
self.assertEqual(self.top.get_lineno(), 0)
- self.assertEqual(self.spam.get_lineno(), 11)
+ self.assertEqual(self.spam.get_lineno(), 12)
def test_function_info(self):
func = self.spam
self.assertEqual(sorted(func.get_parameters()), ["a", "b", "kw", "var"])
- expected = ["a", "b", "internal", "kw", "var", "x"]
+ expected = ['a', 'b', 'internal', 'kw', 'other_internal', 'some_var', 'var', 'x']
self.assertEqual(sorted(func.get_locals()), expected)
self.assertEqual(sorted(func.get_globals()), ["bar", "glob"])
self.assertEqual(self.internal.get_frees(), ("x",))
self.assertFalse(self.internal.lookup("x").is_global())
self.assertFalse(self.Mine.lookup("instance_var").is_global())
+ def test_nonlocal(self):
+ self.assertFalse(self.spam.lookup("some_var").is_nonlocal())
+ self.assertTrue(self.other_internal.lookup("some_var").is_nonlocal())
+ expected = ("some_var",)
+ self.assertEqual(self.other_internal.get_nonlocals(), expected)
+
def test_local(self):
self.assertTrue(self.spam.lookup("x").is_local())
self.assertFalse(self.internal.lookup("x").is_local())
--- /dev/null
+Expose :meth:`symtable.Symbol.is_nonlocal` in the symtable module. Patch by
+Pablo Galindo.
return NULL;
PyModule_AddIntMacro(m, USE);
PyModule_AddIntMacro(m, DEF_GLOBAL);
+ PyModule_AddIntMacro(m, DEF_NONLOCAL);
PyModule_AddIntMacro(m, DEF_LOCAL);
PyModule_AddIntMacro(m, DEF_PARAM);
PyModule_AddIntMacro(m, DEF_FREE);