Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Lib/symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class Function(SymbolTable):
__frees = None
__globals = None
__nonlocals = None
__cells = None
Comment thread
iritkatriel marked this conversation as resolved.

def __idents_matching(self, test_func):
return tuple(ident for ident in self.get_identifiers()
Expand Down Expand Up @@ -229,6 +230,14 @@ def get_frees(self):
self.__frees = self.__idents_matching(is_free)
return self.__frees

def get_cells(self):
"""Return a tuple of cells in the function."""
if self.__cells is None:
is_cell = lambda x: _get_scope(x) == CELL
self.__cells = self.__idents_matching(is_cell)
return self.__cells


Comment thread
Yashp002 marked this conversation as resolved.

class Class(SymbolTable):

Expand Down Expand Up @@ -342,6 +351,10 @@ def is_free(self):
"""
return bool(self.__scope == FREE)

def is_cell(self):
"""Return *True* if the symbol is a cell variable."""
return bool(self.__scope == CELL)

def is_free_class(self):
"""Return *True* if a class-scoped symbol is free from
the perspective of a method."""
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,22 @@ def test_filter_syntax_warnings_by_module(self):
self.assertEqual(wm.filename, filename)
self.assertIs(wm.category, SyntaxWarning)

def test_cells(self):
#test for addition of is_cell() and get_cells()
#see https://github.com/python/cpython/issues/143504
code="""def outer():
x=1
def inner():
return x"""

top=symtable.symtable(code,"?","exec")
outer=top.get_children()[0]

self.assertIn("x",outer.get_cells())

self.assertTrue(outer.lookup("x").is_cell())
self.assertFalse(outer.lookup("inner").is_cell())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put this test next to test_free.



class ComprehensionTests(unittest.TestCase):
def get_identifiers_recursive(self, st, res):
Expand Down
Loading