Return a tuple containing names of :term:`free (closure) variables <closure variable>`
in this function.
+ .. method:: get_cells()
+
+ Return a tuple containing names of :term:`cell variables <closure variable>` in this table.
+
+ .. versionadded:: next
+
.. class:: Class
Return ``True`` if the symbol is referenced in its block, but not assigned
to.
+ .. method:: is_cell()
+
+ Return ``True`` if the symbol is referenced but not assigned in a nested block.
+
+ .. versionadded:: next
+
.. method:: is_free_class()
Return *True* if a class-scoped symbol is free from
(Contributed by Ron Frederick in :gh:`138252`.)
+symtable
+--------
+
+* Add :meth:`symtable.Function.get_cells` and :meth:`symtable.Symbol.is_cell` methods.
+ (Contributed by Yashp002 in :gh:`143504`.)
+
+
sys
---
__frees = None
__globals = None
__nonlocals = None
+ __cells = None
def __idents_matching(self, test_func):
return tuple(ident for ident in self.get_identifiers()
self.__frees = self.__idents_matching(is_free)
return self.__frees
+ def get_cells(self):
+ """Return a tuple of cell variables 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
+
class Class(SymbolTable):
"""
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."""
self.assertEqual(sorted(func.get_locals()), expected)
self.assertEqual(sorted(func.get_globals()), ["bar", "glob", "some_assigned_global_var"])
self.assertEqual(self.internal.get_frees(), ("x",))
+ self.assertEqual(self.spam.get_cells(), ("some_var", "x",))
def test_globals(self):
self.assertTrue(self.spam.lookup("glob").is_global())
def test_free(self):
self.assertTrue(self.internal.lookup("x").is_free())
+ def test_cells(self):
+ self.assertTrue(self.spam.lookup("x").is_cell())
+
def test_referenced(self):
self.assertTrue(self.internal.lookup("x").is_referenced())
self.assertTrue(self.spam.lookup("internal").is_referenced())
--- /dev/null
+Add :meth:`symtable.Function.get_cells` and :meth:`symtable.Symbol.is_cell` methods.