44from _symtable import (
55 USE ,
66 DEF_GLOBAL , DEF_NONLOCAL , DEF_LOCAL ,
7- DEF_PARAM , DEF_TYPE_PARAM , DEF_IMPORT , DEF_BOUND , DEF_ANNOT ,
7+ DEF_PARAM , DEF_TYPE_PARAM ,
8+ DEF_FREE_CLASS ,
9+ DEF_IMPORT , DEF_BOUND , DEF_ANNOT ,
10+ DEF_COMP_ITER , DEF_COMP_CELL ,
811 SCOPE_OFF , SCOPE_MASK ,
912 FREE , LOCAL , GLOBAL_IMPLICIT , GLOBAL_EXPLICIT , CELL
1013)
@@ -158,6 +161,10 @@ def get_children(self):
158161 for st in self ._table .children ]
159162
160163
164+ def _get_scope (flags ): # like _PyST_GetScope()
165+ return (flags >> SCOPE_OFF ) & SCOPE_MASK
166+
167+
161168class Function (SymbolTable ):
162169
163170 # Default values for instance variables
@@ -183,7 +190,7 @@ def get_locals(self):
183190 """
184191 if self .__locals is None :
185192 locs = (LOCAL , CELL )
186- test = lambda x : (( x >> SCOPE_OFF ) & SCOPE_MASK ) in locs
193+ test = lambda x : _get_scope ( x ) in locs
187194 self .__locals = self .__idents_matching (test )
188195 return self .__locals
189196
@@ -192,7 +199,7 @@ def get_globals(self):
192199 """
193200 if self .__globals is None :
194201 glob = (GLOBAL_IMPLICIT , GLOBAL_EXPLICIT )
195- test = lambda x :(( x >> SCOPE_OFF ) & SCOPE_MASK ) in glob
202+ test = lambda x : _get_scope ( x ) in glob
196203 self .__globals = self .__idents_matching (test )
197204 return self .__globals
198205
@@ -207,7 +214,7 @@ def get_frees(self):
207214 """Return a tuple of free variables in the function.
208215 """
209216 if self .__frees is None :
210- is_free = lambda x :(( x >> SCOPE_OFF ) & SCOPE_MASK ) == FREE
217+ is_free = lambda x : _get_scope ( x ) == FREE
211218 self .__frees = self .__idents_matching (is_free )
212219 return self .__frees
213220
@@ -234,7 +241,7 @@ class Symbol:
234241 def __init__ (self , name , flags , namespaces = None , * , module_scope = False ):
235242 self .__name = name
236243 self .__flags = flags
237- self .__scope = (flags >> SCOPE_OFF ) & SCOPE_MASK # like PyST_GetScope( )
244+ self .__scope = _get_scope (flags )
238245 self .__namespaces = namespaces or ()
239246 self .__module_scope = module_scope
240247
@@ -303,6 +310,11 @@ def is_free(self):
303310 """
304311 return bool (self .__scope == FREE )
305312
313+ def is_free_class (self ):
314+ """Return *True* if a class-scoped symbol is free from
315+ the perspective of a method."""
316+ return bool (self .__flags & DEF_FREE_CLASS )
317+
306318 def is_imported (self ):
307319 """Return *True* if the symbol is created from
308320 an import statement.
@@ -313,6 +325,16 @@ def is_assigned(self):
313325 """Return *True* if a symbol is assigned to."""
314326 return bool (self .__flags & DEF_LOCAL )
315327
328+ def is_comp_iter (self ):
329+ """Return *True* if the symbol is a comprehension iteration variable.
330+ """
331+ return bool (self .__flags & DEF_COMP_ITER )
332+
333+ def is_comp_cell (self ):
334+ """Return *True* if the symbol is a cell in an inlined comprehension.
335+ """
336+ return bool (self .__flags & DEF_COMP_CELL )
337+
316338 def is_namespace (self ):
317339 """Returns *True* if name binding introduces new namespace.
318340
0 commit comments