10
10
__all__ = ["symtable" , "SymbolTable" , "Class" , "Function" , "Symbol" ]
11
11
12
12
def symtable (code , filename , compile_type ):
13
+ """ Return the toplevel *SymbolTable* for the source code.
14
+
15
+ *filename* is the name of the file with the code
16
+ and *compile_type* is the *compile()* mode argument.
17
+ """
13
18
top = _symtable .symtable (code , filename , compile_type )
14
19
return _newSymbolTable (top , filename )
15
20
@@ -55,6 +60,11 @@ def __repr__(self):
55
60
self ._filename )
56
61
57
62
def get_type (self ):
63
+ """Return the type of the symbol table.
64
+
65
+ The values retuned are 'class', 'module' and
66
+ 'function'.
67
+ """
58
68
if self ._table .type == _symtable .TYPE_MODULE :
59
69
return "module"
60
70
if self ._table .type == _symtable .TYPE_FUNCTION :
@@ -65,27 +75,51 @@ def get_type(self):
65
75
"unexpected type: {0}" .format (self ._table .type )
66
76
67
77
def get_id (self ):
78
+ """Return an identifier for the table.
79
+ """
68
80
return self ._table .id
69
81
70
82
def get_name (self ):
83
+ """Return the table's name.
84
+
85
+ This corresponds to the name of the class, function
86
+ or 'top' if the table is for a class, function or
87
+ global respectively.
88
+ """
71
89
return self ._table .name
72
90
73
91
def get_lineno (self ):
92
+ """Return the number of the first line in the
93
+ block for the table.
94
+ """
74
95
return self ._table .lineno
75
96
76
97
def is_optimized (self ):
98
+ """Return *True* if the locals in the table
99
+ are optimizable.
100
+ """
77
101
return bool (self ._table .type == _symtable .TYPE_FUNCTION )
78
102
79
103
def is_nested (self ):
104
+ """Return *True* if the block is a nested class
105
+ or function."""
80
106
return bool (self ._table .nested )
81
107
82
108
def has_children (self ):
109
+ """Return *True* if the block has nested namespaces.
110
+ """
83
111
return bool (self ._table .children )
84
112
85
113
def get_identifiers (self ):
114
+ """Return a list of names of symbols in the table.
115
+ """
86
116
return self ._table .symbols .keys ()
87
117
88
118
def lookup (self , name ):
119
+ """Lookup a *name* in the table.
120
+
121
+ Returns a *Symbol* instance.
122
+ """
89
123
sym = self ._symbols .get (name )
90
124
if sym is None :
91
125
flags = self ._table .symbols [name ]
@@ -94,6 +128,9 @@ def lookup(self, name):
94
128
return sym
95
129
96
130
def get_symbols (self ):
131
+ """Return a list of *Symbol* instances for
132
+ names in the table.
133
+ """
97
134
return [self .lookup (ident ) for ident in self .get_identifiers ()]
98
135
99
136
def __check_children (self , name ):
@@ -102,6 +139,8 @@ def __check_children(self, name):
102
139
if st .name == name ]
103
140
104
141
def get_children (self ):
142
+ """Return a list of the nested symbol tables.
143
+ """
105
144
return [_newSymbolTable (st , self ._filename )
106
145
for st in self ._table .children ]
107
146
@@ -120,30 +159,40 @@ def __idents_matching(self, test_func):
120
159
if test_func (self ._table .symbols [ident ]))
121
160
122
161
def get_parameters (self ):
162
+ """Return a tuple of parameters to the function.
163
+ """
123
164
if self .__params is None :
124
165
self .__params = self .__idents_matching (lambda x :x & DEF_PARAM )
125
166
return self .__params
126
167
127
168
def get_locals (self ):
169
+ """Return a tuple of locals in the function.
170
+ """
128
171
if self .__locals is None :
129
172
locs = (LOCAL , CELL )
130
173
test = lambda x : ((x >> SCOPE_OFF ) & SCOPE_MASK ) in locs
131
174
self .__locals = self .__idents_matching (test )
132
175
return self .__locals
133
176
134
177
def get_globals (self ):
178
+ """Return a tuple of globals in the function.
179
+ """
135
180
if self .__globals is None :
136
181
glob = (GLOBAL_IMPLICIT , GLOBAL_EXPLICIT )
137
182
test = lambda x :((x >> SCOPE_OFF ) & SCOPE_MASK ) in glob
138
183
self .__globals = self .__idents_matching (test )
139
184
return self .__globals
140
185
141
186
def get_nonlocals (self ):
187
+ """Return a tuple of nonlocals in the function.
188
+ """
142
189
if self .__nonlocals is None :
143
190
self .__nonlocals = self .__idents_matching (lambda x :x & DEF_NONLOCAL )
144
191
return self .__nonlocals
145
192
146
193
def get_frees (self ):
194
+ """Return a tuple of free variables in the function.
195
+ """
147
196
if self .__frees is None :
148
197
is_free = lambda x :((x >> SCOPE_OFF ) & SCOPE_MASK ) == FREE
149
198
self .__frees = self .__idents_matching (is_free )
@@ -155,6 +204,8 @@ class Class(SymbolTable):
155
204
__methods = None
156
205
157
206
def get_methods (self ):
207
+ """Return a tuple of methods declared in the class.
208
+ """
158
209
if self .__methods is None :
159
210
d = {}
160
211
for st in self ._table .children :
@@ -175,40 +226,63 @@ def __repr__(self):
175
226
return "<symbol {0!r}>" .format (self .__name )
176
227
177
228
def get_name (self ):
229
+ """Return a name of a symbol.
230
+ """
178
231
return self .__name
179
232
180
233
def is_referenced (self ):
234
+ """Return *True* if the symbol is used in
235
+ its block.
236
+ """
181
237
return bool (self .__flags & _symtable .USE )
182
238
183
239
def is_parameter (self ):
240
+ """Return *True* if the symbol is a parameter.
241
+ """
184
242
return bool (self .__flags & DEF_PARAM )
185
243
186
244
def is_global (self ):
245
+ """Return *True* if the sysmbol is global.
246
+ """
187
247
return bool (self .__scope in (GLOBAL_IMPLICIT , GLOBAL_EXPLICIT ))
188
248
189
249
def is_nonlocal (self ):
250
+ """Return *True* if the symbol is nonlocal."""
190
251
return bool (self .__flags & DEF_NONLOCAL )
191
252
192
253
def is_declared_global (self ):
254
+ """Return *True* if the symbol is declared global
255
+ with a global statement."""
193
256
return bool (self .__scope == GLOBAL_EXPLICIT )
194
257
195
258
def is_local (self ):
259
+ """Return *True* if the symbol is local.
260
+ """
196
261
return bool (self .__scope in (LOCAL , CELL ))
197
262
198
263
def is_annotated (self ):
264
+ """Return *True* if the symbol is annotated.
265
+ """
199
266
return bool (self .__flags & DEF_ANNOT )
200
267
201
268
def is_free (self ):
269
+ """Return *True* if a referenced symbol is
270
+ not assigned to.
271
+ """
202
272
return bool (self .__scope == FREE )
203
273
204
274
def is_imported (self ):
275
+ """Return *True* if the symbol is created from
276
+ an import statement.
277
+ """
205
278
return bool (self .__flags & DEF_IMPORT )
206
279
207
280
def is_assigned (self ):
281
+ """Return *True* if a symbol is assigned to."""
208
282
return bool (self .__flags & DEF_LOCAL )
209
283
210
284
def is_namespace (self ):
211
- """Returns true if name binding introduces new namespace.
285
+ """Returns *True* if name binding introduces new namespace.
212
286
213
287
If the name is used as the target of a function or class
214
288
statement, this will be true.
@@ -225,7 +299,7 @@ def get_namespaces(self):
225
299
return self .__namespaces
226
300
227
301
def get_namespace (self ):
228
- """Returns the single namespace bound to this name.
302
+ """Return the single namespace bound to this name.
229
303
230
304
Raises ValueError if the name is bound to multiple namespaces.
231
305
"""
0 commit comments