symtable.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. """Interface to the compiler's internal symbol tables"""
  2. import _symtable
  3. from _symtable import (USE, DEF_GLOBAL, DEF_NONLOCAL, DEF_LOCAL, DEF_PARAM,
  4. DEF_IMPORT, DEF_BOUND, DEF_ANNOT, SCOPE_OFF, SCOPE_MASK, FREE,
  5. LOCAL, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL)
  6. import weakref
  7. __all__ = ["symtable", "SymbolTable", "Class", "Function", "Symbol"]
  8. def symtable(code, filename, compile_type):
  9. """ Return the toplevel *SymbolTable* for the source code.
  10. *filename* is the name of the file with the code
  11. and *compile_type* is the *compile()* mode argument.
  12. """
  13. top = _symtable.symtable(code, filename, compile_type)
  14. return _newSymbolTable(top, filename)
  15. class SymbolTableFactory:
  16. def __init__(self):
  17. self.__memo = weakref.WeakValueDictionary()
  18. def new(self, table, filename):
  19. if table.type == _symtable.TYPE_FUNCTION:
  20. return Function(table, filename)
  21. if table.type == _symtable.TYPE_CLASS:
  22. return Class(table, filename)
  23. return SymbolTable(table, filename)
  24. def __call__(self, table, filename):
  25. key = table, filename
  26. obj = self.__memo.get(key, None)
  27. if obj is None:
  28. obj = self.__memo[key] = self.new(table, filename)
  29. return obj
  30. _newSymbolTable = SymbolTableFactory()
  31. class SymbolTable:
  32. def __init__(self, raw_table, filename):
  33. self._table = raw_table
  34. self._filename = filename
  35. self._symbols = {}
  36. def __repr__(self):
  37. if self.__class__ == SymbolTable:
  38. kind = ""
  39. else:
  40. kind = "%s " % self.__class__.__name__
  41. if self._table.name == "top":
  42. return "<{0}SymbolTable for module {1}>".format(kind, self._filename)
  43. else:
  44. return "<{0}SymbolTable for {1} in {2}>".format(kind,
  45. self._table.name,
  46. self._filename)
  47. def get_type(self):
  48. """Return the type of the symbol table.
  49. The values returned are 'class', 'module', 'function',
  50. 'annotation', 'TypeVar bound', 'type alias', and 'type parameter'.
  51. """
  52. if self._table.type == _symtable.TYPE_MODULE:
  53. return "module"
  54. if self._table.type == _symtable.TYPE_FUNCTION:
  55. return "function"
  56. if self._table.type == _symtable.TYPE_CLASS:
  57. return "class"
  58. if self._table.type == _symtable.TYPE_ANNOTATION:
  59. return "annotation"
  60. if self._table.type == _symtable.TYPE_TYPE_VAR_BOUND:
  61. return "TypeVar bound"
  62. if self._table.type == _symtable.TYPE_TYPE_ALIAS:
  63. return "type alias"
  64. if self._table.type == _symtable.TYPE_TYPE_PARAM:
  65. return "type parameter"
  66. assert False, f"unexpected type: {self._table.type}"
  67. def get_id(self):
  68. """Return an identifier for the table.
  69. """
  70. return self._table.id
  71. def get_name(self):
  72. """Return the table's name.
  73. This corresponds to the name of the class, function
  74. or 'top' if the table is for a class, function or
  75. global respectively.
  76. """
  77. return self._table.name
  78. def get_lineno(self):
  79. """Return the number of the first line in the
  80. block for the table.
  81. """
  82. return self._table.lineno
  83. def is_optimized(self):
  84. """Return *True* if the locals in the table
  85. are optimizable.
  86. """
  87. return bool(self._table.type == _symtable.TYPE_FUNCTION)
  88. def is_nested(self):
  89. """Return *True* if the block is a nested class
  90. or function."""
  91. return bool(self._table.nested)
  92. def has_children(self):
  93. """Return *True* if the block has nested namespaces.
  94. """
  95. return bool(self._table.children)
  96. def get_identifiers(self):
  97. """Return a view object containing the names of symbols in the table.
  98. """
  99. return self._table.symbols.keys()
  100. def lookup(self, name):
  101. """Lookup a *name* in the table.
  102. Returns a *Symbol* instance.
  103. """
  104. sym = self._symbols.get(name)
  105. if sym is None:
  106. flags = self._table.symbols[name]
  107. namespaces = self.__check_children(name)
  108. module_scope = (self._table.name == "top")
  109. sym = self._symbols[name] = Symbol(name, flags, namespaces,
  110. module_scope=module_scope)
  111. return sym
  112. def get_symbols(self):
  113. """Return a list of *Symbol* instances for
  114. names in the table.
  115. """
  116. return [self.lookup(ident) for ident in self.get_identifiers()]
  117. def __check_children(self, name):
  118. return [_newSymbolTable(st, self._filename)
  119. for st in self._table.children
  120. if st.name == name]
  121. def get_children(self):
  122. """Return a list of the nested symbol tables.
  123. """
  124. return [_newSymbolTable(st, self._filename)
  125. for st in self._table.children]
  126. class Function(SymbolTable):
  127. # Default values for instance variables
  128. __params = None
  129. __locals = None
  130. __frees = None
  131. __globals = None
  132. __nonlocals = None
  133. def __idents_matching(self, test_func):
  134. return tuple(ident for ident in self.get_identifiers()
  135. if test_func(self._table.symbols[ident]))
  136. def get_parameters(self):
  137. """Return a tuple of parameters to the function.
  138. """
  139. if self.__params is None:
  140. self.__params = self.__idents_matching(lambda x:x & DEF_PARAM)
  141. return self.__params
  142. def get_locals(self):
  143. """Return a tuple of locals in the function.
  144. """
  145. if self.__locals is None:
  146. locs = (LOCAL, CELL)
  147. test = lambda x: ((x >> SCOPE_OFF) & SCOPE_MASK) in locs
  148. self.__locals = self.__idents_matching(test)
  149. return self.__locals
  150. def get_globals(self):
  151. """Return a tuple of globals in the function.
  152. """
  153. if self.__globals is None:
  154. glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
  155. test = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) in glob
  156. self.__globals = self.__idents_matching(test)
  157. return self.__globals
  158. def get_nonlocals(self):
  159. """Return a tuple of nonlocals in the function.
  160. """
  161. if self.__nonlocals is None:
  162. self.__nonlocals = self.__idents_matching(lambda x:x & DEF_NONLOCAL)
  163. return self.__nonlocals
  164. def get_frees(self):
  165. """Return a tuple of free variables in the function.
  166. """
  167. if self.__frees is None:
  168. is_free = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) == FREE
  169. self.__frees = self.__idents_matching(is_free)
  170. return self.__frees
  171. class Class(SymbolTable):
  172. __methods = None
  173. def get_methods(self):
  174. """Return a tuple of methods declared in the class.
  175. """
  176. if self.__methods is None:
  177. d = {}
  178. for st in self._table.children:
  179. d[st.name] = 1
  180. self.__methods = tuple(d)
  181. return self.__methods
  182. class Symbol:
  183. def __init__(self, name, flags, namespaces=None, *, module_scope=False):
  184. self.__name = name
  185. self.__flags = flags
  186. self.__scope = (flags >> SCOPE_OFF) & SCOPE_MASK # like PyST_GetScope()
  187. self.__namespaces = namespaces or ()
  188. self.__module_scope = module_scope
  189. def __repr__(self):
  190. return "<symbol {0!r}>".format(self.__name)
  191. def get_name(self):
  192. """Return a name of a symbol.
  193. """
  194. return self.__name
  195. def is_referenced(self):
  196. """Return *True* if the symbol is used in
  197. its block.
  198. """
  199. return bool(self.__flags & _symtable.USE)
  200. def is_parameter(self):
  201. """Return *True* if the symbol is a parameter.
  202. """
  203. return bool(self.__flags & DEF_PARAM)
  204. def is_global(self):
  205. """Return *True* if the symbol is global.
  206. """
  207. return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
  208. or (self.__module_scope and self.__flags & DEF_BOUND))
  209. def is_nonlocal(self):
  210. """Return *True* if the symbol is nonlocal."""
  211. return bool(self.__flags & DEF_NONLOCAL)
  212. def is_declared_global(self):
  213. """Return *True* if the symbol is declared global
  214. with a global statement."""
  215. return bool(self.__scope == GLOBAL_EXPLICIT)
  216. def is_local(self):
  217. """Return *True* if the symbol is local.
  218. """
  219. return bool(self.__scope in (LOCAL, CELL)
  220. or (self.__module_scope and self.__flags & DEF_BOUND))
  221. def is_annotated(self):
  222. """Return *True* if the symbol is annotated.
  223. """
  224. return bool(self.__flags & DEF_ANNOT)
  225. def is_free(self):
  226. """Return *True* if a referenced symbol is
  227. not assigned to.
  228. """
  229. return bool(self.__scope == FREE)
  230. def is_imported(self):
  231. """Return *True* if the symbol is created from
  232. an import statement.
  233. """
  234. return bool(self.__flags & DEF_IMPORT)
  235. def is_assigned(self):
  236. """Return *True* if a symbol is assigned to."""
  237. return bool(self.__flags & DEF_LOCAL)
  238. def is_namespace(self):
  239. """Returns *True* if name binding introduces new namespace.
  240. If the name is used as the target of a function or class
  241. statement, this will be true.
  242. Note that a single name can be bound to multiple objects. If
  243. is_namespace() is true, the name may also be bound to other
  244. objects, like an int or list, that does not introduce a new
  245. namespace.
  246. """
  247. return bool(self.__namespaces)
  248. def get_namespaces(self):
  249. """Return a list of namespaces bound to this name"""
  250. return self.__namespaces
  251. def get_namespace(self):
  252. """Return the single namespace bound to this name.
  253. Raises ValueError if the name is bound to multiple namespaces
  254. or no namespace.
  255. """
  256. if len(self.__namespaces) == 0:
  257. raise ValueError("name is not bound to any namespaces")
  258. elif len(self.__namespaces) > 1:
  259. raise ValueError("name is bound to multiple namespaces")
  260. else:
  261. return self.__namespaces[0]
  262. if __name__ == "__main__":
  263. import os, sys
  264. with open(sys.argv[0]) as f:
  265. src = f.read()
  266. mod = symtable(src, os.path.split(sys.argv[0])[1], "exec")
  267. for ident in mod.get_identifiers():
  268. info = mod.lookup(ident)
  269. print(info, info.is_local(), info.is_namespace())