Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions Lib/symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ def get_frees(self):
self.__frees = self.__idents_matching(is_free)
return self.__frees

def get_cells(self):
"""Return a list of cell variable names in the table."""
return [s.get_name() for s in self.get_symbols() if s.is_cell()]
Copy link
Member

Choose a reason for hiding this comment

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

Why does this not follow the pattern of get_frees and get_nonlocals?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We'll have to use the _cells variable then, I'll add it.



class Class(SymbolTable):

Expand Down Expand Up @@ -342,6 +346,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
14 changes: 14 additions & 0 deletions Lib/test/test_symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,20 @@ def test_local(self):
def test_free(self):
self.assertTrue(self.internal.lookup("x").is_free())

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"""
Copy link
Member

Choose a reason for hiding this comment

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

the other tests don't do all this. Can we follow the same pattern in this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iritkatriel I've implemented the change in symtable.py with get_cells

But the current test_cells seems to be the only one that keeps working after I've change it up quite a few times.

Is it really a requirement for it all to have the same pattern?


top=symtable.symtable(code,"?","exec")
outer = find_block(top, "outer")
self.assertIn("x",outer.get_cells())
self.assertTrue(outer.lookup("x").is_cell())
self.assertFalse(outer.lookup("inner").is_cell())

def test_referenced(self):
self.assertTrue(self.internal.lookup("x").is_referenced())
self.assertTrue(self.spam.lookup("internal").is_referenced())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add symtable.is_cell() and get_cells() methods for cell variable analysis.
Loading