4
4
from _symtable import (
5
5
USE ,
6
6
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 ,
8
11
SCOPE_OFF , SCOPE_MASK ,
9
12
FREE , LOCAL , GLOBAL_IMPLICIT , GLOBAL_EXPLICIT , CELL
10
13
)
@@ -158,6 +161,10 @@ def get_children(self):
158
161
for st in self ._table .children ]
159
162
160
163
164
+ def _get_scope (flags ): # like _PyST_GetScope()
165
+ return (flags >> SCOPE_OFF ) & SCOPE_MASK
166
+
167
+
161
168
class Function (SymbolTable ):
162
169
163
170
# Default values for instance variables
@@ -183,7 +190,7 @@ def get_locals(self):
183
190
"""
184
191
if self .__locals is None :
185
192
locs = (LOCAL , CELL )
186
- test = lambda x : (( x >> SCOPE_OFF ) & SCOPE_MASK ) in locs
193
+ test = lambda x : _get_scope ( x ) in locs
187
194
self .__locals = self .__idents_matching (test )
188
195
return self .__locals
189
196
@@ -192,7 +199,7 @@ def get_globals(self):
192
199
"""
193
200
if self .__globals is None :
194
201
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
196
203
self .__globals = self .__idents_matching (test )
197
204
return self .__globals
198
205
@@ -207,7 +214,7 @@ def get_frees(self):
207
214
"""Return a tuple of free variables in the function.
208
215
"""
209
216
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
211
218
self .__frees = self .__idents_matching (is_free )
212
219
return self .__frees
213
220
@@ -234,7 +241,7 @@ class Symbol:
234
241
def __init__ (self , name , flags , namespaces = None , * , module_scope = False ):
235
242
self .__name = name
236
243
self .__flags = flags
237
- self .__scope = (flags >> SCOPE_OFF ) & SCOPE_MASK # like PyST_GetScope( )
244
+ self .__scope = _get_scope (flags )
238
245
self .__namespaces = namespaces or ()
239
246
self .__module_scope = module_scope
240
247
@@ -303,6 +310,11 @@ def is_free(self):
303
310
"""
304
311
return bool (self .__scope == FREE )
305
312
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
+
306
318
def is_imported (self ):
307
319
"""Return *True* if the symbol is created from
308
320
an import statement.
@@ -313,6 +325,16 @@ def is_assigned(self):
313
325
"""Return *True* if a symbol is assigned to."""
314
326
return bool (self .__flags & DEF_LOCAL )
315
327
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
+
316
338
def is_namespace (self ):
317
339
"""Returns *True* if name binding introduces new namespace.
318
340
0 commit comments