Skip to content

Commit b840e4e

Browse files
committed
symtable commit
1 parent 6c9f7b4 commit b840e4e

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Lib/symtable.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ class Function(SymbolTable):
184184
__frees = None
185185
__globals = None
186186
__nonlocals = None
187+
__cells = None
187188

188189
def __idents_matching(self, test_func):
189190
return tuple(ident for ident in self.get_identifiers()
@@ -229,6 +230,14 @@ def get_frees(self):
229230
self.__frees = self.__idents_matching(is_free)
230231
return self.__frees
231232

233+
def get_cells(self):
234+
"""Return a tuple of cells in the function."""
235+
if self.__cells is None:
236+
is_cell = lambda x: _get_scope(x) == CELL
237+
self.__cells = self.__idents_matching(is_cell)
238+
return self.__cells
239+
240+
232241

233242
class Class(SymbolTable):
234243

@@ -342,6 +351,10 @@ def is_free(self):
342351
"""
343352
return bool(self.__scope == FREE)
344353

354+
def is_cell(self):
355+
"""Return *True* if the symbol is a cell variable."""
356+
return bool(self.__scope == CELL)
357+
345358
def is_free_class(self):
346359
"""Return *True* if a class-scoped symbol is free from
347360
the perspective of a method."""

Lib/test/test_symtable.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,22 @@ def test_filter_syntax_warnings_by_module(self):
611611
self.assertEqual(wm.filename, filename)
612612
self.assertIs(wm.category, SyntaxWarning)
613613

614+
def test_cells(self):
615+
#test for addition of is_cell() and get_cells()
616+
#see https://github.com/python/cpython/issues/143504
617+
code="""def outer():
618+
x=1
619+
def inner():
620+
return x"""
621+
622+
top=symtable.symtable(code,"?","exec")
623+
outer=top.get_children()[0]
624+
625+
self.assertIn("x",outer.get_cells())
626+
627+
self.assertTrue(outer.lookup("x").is_cell())
628+
self.assertFalse(outer.lookup("inner").is_cell())
629+
614630

615631
class ComprehensionTests(unittest.TestCase):
616632
def get_identifiers_recursive(self, st, res):

0 commit comments

Comments
 (0)