1 """Interface to the compiler's internal symbol tables"""
4 from _symtable
import USE
, DEF_GLOBAL
, DEF_LOCAL
, DEF_PARAM
, \
5 DEF_STAR
, DEF_DOUBLESTAR
, DEF_INTUPLE
, DEF_FREE
, \
6 DEF_FREE_GLOBAL
, DEF_FREE_CLASS
, DEF_IMPORT
, DEF_BOUND
, \
7 OPT_IMPORT_STAR
, OPT_EXEC
, OPT_BARE_EXEC
11 __all__
= ["symtable", "SymbolTable", "newSymbolTable", "Class",
14 def symtable(code
, filename
, compile_type
):
15 raw
= _symtable
.symtable(code
, filename
, compile_type
)
16 for top
in raw
.itervalues():
19 return newSymbolTable(top
, filename
)
21 class SymbolTableFactory
:
23 self
.__memo
= weakref
.WeakValueDictionary()
25 def new(self
, table
, filename
):
26 if table
.type == _symtable
.TYPE_FUNCTION
:
27 return Function(table
, filename
)
28 if table
.type == _symtable
.TYPE_CLASS
:
29 return Class(table
, filename
)
30 return SymbolTable(table
, filename
)
32 def __call__(self
, table
, filename
):
34 obj
= self
.__memo
.get(key
, None)
36 obj
= self
.__memo
[key
] = self
.new(table
, filename
)
39 newSymbolTable
= SymbolTableFactory()
42 if (flags
& (USE | DEF_FREE
)) \
43 and (flags
& (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL
)):
45 if flags
& DEF_FREE_CLASS
:
50 def __init__(self
, raw_table
, filename
):
51 self
._table
= raw_table
52 self
._filename
= filename
56 if self
.__class
__ == SymbolTable
:
59 kind
= "%s " % self
.__class
__.__name
__
61 if self
._table
.name
== "global":
62 return "<%sSymbolTable for module %s>" % (kind
, self
._filename
)
64 return "<%sSymbolTable for %s in %s>" % (kind
, self
._table
.name
,
68 if self
._table
.type == _symtable
.TYPE_MODULE
:
70 if self
._table
.type == _symtable
.TYPE_FUNCTION
:
72 if self
._table
.type == _symtable
.TYPE_CLASS
:
74 assert self
._table
.type in (1, 2, 3), \
75 "unexpected type: %s" % self
._table
.type
81 return self
._table
.name
84 return self
._table
.lineno
86 def is_optimized(self
):
87 return bool(self
._table
.type == _symtable
.TYPE_FUNCTION
88 and not self
._table
.optimized
)
91 return bool(self
._table
.nested
)
93 def has_children(self
):
94 return bool(self
._table
.children
)
97 """Return true if the scope uses exec"""
98 return bool(self
._table
.optimized
& (OPT_EXEC | OPT_BARE_EXEC
))
100 def has_import_star(self
):
101 """Return true if the scope uses import *"""
102 return bool(self
._table
.optimized
& OPT_IMPORT_STAR
)
104 def get_identifiers(self
):
105 return self
._table
.symbols
.keys()
107 def lookup(self
, name
):
108 sym
= self
._symbols
.get(name
)
110 flags
= self
._table
.symbols
[name
]
111 namespaces
= self
.__check
_children
(name
)
112 sym
= self
._symbols
[name
] = Symbol(name
, flags
, namespaces
)
115 def get_symbols(self
):
116 return [self
.lookup(ident
) for ident
in self
.get_identifiers()]
118 def __check_children(self
, name
):
119 return [newSymbolTable(st
, self
._filename
)
120 for st
in self
._table
.children
123 def get_children(self
):
124 return [newSymbolTable(st
, self
._filename
)
125 for st
in self
._table
.children
]
127 class Function(SymbolTable
):
129 # Default values for instance variables
135 def __idents_matching(self
, test_func
):
136 return tuple([ident
for ident
in self
.get_identifiers()
137 if test_func(self
._table
.symbols
[ident
])])
139 def get_parameters(self
):
140 if self
.__params
is None:
141 self
.__params
= self
.__idents
_matching
(lambda x
:x
& DEF_PARAM
)
144 def get_locals(self
):
145 if self
.__locals
is None:
146 self
.__locals
= self
.__idents
_matching
(lambda x
:x
& DEF_BOUND
)
149 def get_globals(self
):
150 if self
.__globals
is None:
151 glob
= DEF_GLOBAL | DEF_FREE_GLOBAL
152 self
.__globals
= self
.__idents
_matching
(lambda x
:x
& glob
)
153 return self
.__globals
156 if self
.__frees
is None:
157 self
.__frees
= self
.__idents
_matching
(is_free
)
160 class Class(SymbolTable
):
164 def get_methods(self
):
165 if self
.__methods
is None:
167 for st
in self
._table
.children
:
169 self
.__methods
= tuple(d
)
170 return self
.__methods
173 def __init__(self
, name
, flags
, namespaces
=None):
176 self
.__namespaces
= namespaces
or ()
179 return "<symbol '%s'>" % self
.__name
184 def is_referenced(self
):
185 return bool(self
.__flags
& _symtable
.USE
)
187 def is_parameter(self
):
188 return bool(self
.__flags
& DEF_PARAM
)
191 return bool((self
.__flags
& DEF_GLOBAL
)
192 or (self
.__flags
& DEF_FREE_GLOBAL
))
195 return bool(self
.__flags
& DEF_STAR
)
197 def is_keywordarg(self
):
198 return bool(self
.__flags
& DEF_DOUBLESTAR
)
201 return bool(self
.__flags
& DEF_BOUND
)
204 if (self
.__flags
& (USE | DEF_FREE
)) \
205 and (self
.__flags
& (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL
)):
207 if self
.__flags
& DEF_FREE_CLASS
:
211 def is_imported(self
):
212 return bool(self
.__flags
& DEF_IMPORT
)
214 def is_assigned(self
):
215 return bool(self
.__flags
& DEF_LOCAL
)
217 def is_in_tuple(self
):
218 return bool(self
.__flags
& DEF_INTUPLE
)
220 def is_namespace(self
):
221 """Returns true if name binding introduces new namespace.
223 If the name is used as the target of a function or class
224 statement, this will be true.
226 Note that a single name can be bound to multiple objects. If
227 is_namespace() is true, the name may also be bound to other
228 objects, like an int or list, that does not introduce a new
231 return bool(self
.__namespaces
)
233 def get_namespaces(self
):
234 """Return a list of namespaces bound to this name"""
235 return self
.__namespaces
237 def get_namespace(self
):
238 """Returns the single namespace bound to this name.
240 Raises ValueError if the name is bound to multiple namespaces.
242 if len(self
.__namespaces
) != 1:
243 raise ValueError, "name is bound to multiple namespaces"
244 return self
.__namespaces
[0]
246 if __name__
== "__main__":
248 src
= open(sys
.argv
[0]).read()
249 mod
= symtable(src
, os
.path
.split(sys
.argv
[0])[1], "exec")
250 for ident
in mod
.get_identifiers():
251 info
= mod
.lookup(ident
)
252 print info
, info
.is_local(), info
.is_namespace()