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",
15 raw = _symtable.symtable(code, filename, compile_type)
22 def new(self, table, filename):
23 if table.type == _symtable.TYPE_FUNCTION:
25 if table.type == _symtable.TYPE_CLASS:
26 return Class(table, filename)
31 obj = self.__memo.get(key,
None)
33 obj = self.
__memo[key] = self.
new(table, filename)
39 """Helper to force boolean result to 1 or 0"""
45 if (flags & (USE | DEF_FREE)) \
46 and (flags & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)):
48 if flags & DEF_FREE_CLASS:
62 kind =
"%s " % self.__class__.__name__
64 if self._table.name ==
"global":
65 return "<%sSymbolTable for module %s>" % (kind, self.
_filename)
67 return "<%sSymbolTable for %s in %s>" % (kind, self._table.name,
71 if self._table.type == _symtable.TYPE_MODULE:
73 if self._table.type == _symtable.TYPE_FUNCTION:
75 if self._table.type == _symtable.TYPE_CLASS:
77 assert self._table.type
in (1, 2, 3), \
78 "unexpected type: %s" % self._table.type
84 return self._table.name
87 return self._table.lineno
90 return bool(self._table.type == _symtable.TYPE_FUNCTION
91 and not self._table.optimized)
94 return bool(self._table.nested)
97 return bool(self._table.children)
100 """Return true if the scope uses exec"""
101 return bool(self._table.optimized & (OPT_EXEC | OPT_BARE_EXEC))
104 """Return true if the scope uses import *"""
105 return bool(self._table.optimized & OPT_IMPORT_STAR)
108 return self._table.symbols.keys()
111 sym = self._symbols.get(name)
113 flags = self._table.symbols[name]
121 def __check_children(self, name):
123 for st
in self._table.children
128 for st
in self._table.children]
138 def __idents_matching(self, test_func):
140 if test_func(self._table.symbols[ident])])
154 glob = DEF_GLOBAL | DEF_FREE_GLOBAL
170 for st
in self._table.children:
182 return "<symbol '%s'>" % self.
__name
195 or (self.
__flags & DEF_FREE_GLOBAL))
207 if (self.
__flags & (USE | DEF_FREE)) \
208 and (self.
__flags & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)):
210 if self.
__flags & DEF_FREE_CLASS:
224 """Returns true if name binding introduces new namespace.
226 If the name is used as the target of a function or class
227 statement, this will be true.
229 Note that a single name can be bound to multiple objects. If
230 is_namespace() is true, the name may also be bound to other
231 objects, like an int or list, that does not introduce a new
237 """Return a list of namespaces bound to this name"""
241 """Returns the single namespace bound to this name.
243 Raises ValueError if the name is bound to multiple namespaces.
246 raise ValueError,
"name is bound to multiple namespaces"
249 if __name__ ==
"__main__":
252 mod =
symtable(src, os.path.split(sys.argv[0])[1],
"exec")
253 for ident
in mod.get_identifiers():
254 info = mod.lookup(ident)
255 print info, info.is_local(), info.is_namespace()