Skip to content

Commit a9e5bf7

Browse files
committed
symtable fix 6
1 parent 19cd54d commit a9e5bf7

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

Lib/symtable.py

Lines changed: 6 additions & 2 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()
@@ -230,8 +231,11 @@ def get_frees(self):
230231
return self.__frees
231232

232233
def get_cells(self):
233-
"""Return a list of cell variable names in the table."""
234-
return [s.get_name() for s in self.get_symbols() if s.is_cell()]
234+
"""Return a list of cell variable names in the table.
235+
"""
236+
if self.__cells is None:
237+
self.__cells = [s.get_name() for s in self.get_symbols() if s.is_cell()]
238+
return self.__cells
235239

236240

237241
class Class(SymbolTable):

Lib/test/test_symtable.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,15 @@ def test_free(self):
285285
self.assertTrue(self.internal.lookup("x").is_free())
286286

287287
def test_cells(self):
288-
#test for addition of is_cell() and get_cells()
289-
#see https://github.com/python/cpython/issues/143504
288+
290289
code="""def outer():
291290
x=1
292291
def inner():
293292
return x"""
294293

295294
top=symtable.symtable(code,"?","exec")
296295
outer = find_block(top, "outer")
297-
self.assertIn("x",outer.get_cells())
296+
self.assertEqual(outer.get_cells(), ["x"])
298297
self.assertTrue(outer.lookup("x").is_cell())
299298
self.assertFalse(outer.lookup("inner").is_cell())
300299

0 commit comments

Comments
 (0)