Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 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
10 changes: 10 additions & 0 deletions Doc/library/symtable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ Examining Symbol Tables
Return a tuple containing names of :term:`free (closure) variables <closure variable>`
in this function.

.. method:: get_cells()
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.

Please add the versionadded directive as well


Return a tuple containing names of :term:`cell variables <closure variable>` in this table.


.. class:: Class

Expand Down Expand Up @@ -291,6 +295,12 @@ Examining Symbol Tables
Return ``True`` if the symbol is referenced in its block, but not assigned
to.

.. method:: is_cell()

Return ``True`` if the symbol is a cell variable.
Comment thread
Yashp002 marked this conversation as resolved.
Outdated

.. versionadded:: 3.15
Comment thread
Yashp002 marked this conversation as resolved.
Outdated

.. method:: is_free_class()

Return *True* if a class-scoped symbol is free from
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,14 @@ ssl
(Contributed by Ron Frederick in :gh:`138252`.)


symtable
--------

* Add :meth:`symtable.Function.get_cells` and :meth:`symtable.Symbol.is_cell` methods.

Comment thread
Yashp002 marked this conversation as resolved.
Outdated
(Contributed by Yashp002 in :gh:`143504`.)


sys
---

Expand Down
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 cell variable names in the table.
Comment thread
Yashp002 marked this conversation as resolved.
Outdated
"""
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
4 changes: 4 additions & 0 deletions Lib/test/test_symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ def test_function_info(self):
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())
Expand Down Expand Up @@ -284,6 +285,9 @@ def test_local(self):
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())
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.
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.

There are two News files now

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :meth:`symtable.SymbolTable.get_cells` and
:meth:`symtable.Symbol.is_cell` methods.
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
Loading