symtable.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. top = _symtable.symtable(code, filename, compile_type)
  10. return _newSymbolTable(top, filename)
  11. class SymbolTableFactory:
  12. def __init__(self):
  13. self.__memo = weakref.WeakValueDictionary()
  14. def new(self, table, filename):
  15. if table.type == _symtable.TYPE_FUNCTION:
  16. return Function(table, filename)
  17. if table.type == _symtable.TYPE_CLASS:
  18. return Class(table, filename)
  19. return SymbolTable(table, filename)
  20. def __call__(self, table, filename):
  21. key = table, filename
  22. obj = self.__memo.get(key, None)
  23. if obj is None:
  24. obj = self.__memo[key] = self.new(table, filename)
  25. return obj
  26. _newSymbolTable = SymbolTableFactory()
  27. class SymbolTable:
  28. def __init__(self, raw_table, filename):
  29. self._table = raw_table
  30. self._filename = filename
  31. self._symbols = {}
  32. def __repr__(self):
  33. if self.__class__ == SymbolTable:
  34. kind = ""
  35. else:
  36. kind = "%s " % self.__class__.__name__
  37. if self._table.name == "top":
  38. return "<{0}SymbolTable for module {1}>".format(kind, self._filename)
  39. else:
  40. return "<{0}SymbolTable for {1} in {2}>".format(kind,
  41. self._table.name,
  42. self._filename)
  43. def get_type(self):
  44. if self._table.type == _symtable.TYPE_MODULE:
  45. return "module"
  46. if self._table.type == _symtable.TYPE_FUNCTION:
  47. return "function"
  48. if self._table.type == _symtable.TYPE_CLASS:
  49. return "class"
  50. assert self._table.type in (1, 2, 3), \
  51. "unexpected type: {0}".format(self._table.type)
  52. def get_id(self):
  53. return self._table.id
  54. def get_name(self):
  55. return self._table.name
  56. def get_lineno(self):
  57. return self._table.lineno
  58. def is_optimized(self):
  59. return bool(self._table.type == _symtable.TYPE_FUNCTION)
  60. def is_nested(self):
  61. return bool(self._table.nested)
  62. def has_children(self):
  63. return bool(self._table.children)
  64. def has_exec(self):
  65. """Return true if the scope uses exec. Deprecated method."""
  66. return False
  67. def get_identifiers(self):
  68. return self._table.symbols.keys()
  69. def lookup(self, name):
  70. sym = self._symbols.get(name)
  71. if sym is None:
  72. flags = self._table.symbols[name]
  73. namespaces = self.__check_children(name)
  74. module_scope = (self._table.name == "top")
  75. sym = self._symbols[name] = Symbol(name, flags, namespaces,
  76. module_scope=module_scope)
  77. return sym
  78. def get_symbols(self):
  79. return [self.lookup(ident) for ident in self.get_identifiers()]
  80. def __check_children(self, name):
  81. return [_newSymbolTable(st, self._filename)
  82. for st in self._table.children
  83. if st.name == name]
  84. def get_children(self):
  85. return [_newSymbolTable(st, self._filename)
  86. for st in self._table.children]
  87. class Function(SymbolTable):
  88. # Default values for instance variables
  89. __params = None
  90. __locals = None
  91. __frees = None
  92. __globals = None
  93. __nonlocals = None
  94. def __idents_matching(self, test_func):
  95. return tuple(ident for ident in self.get_identifiers()
  96. if test_func(self._table.symbols[ident]))
  97. def get_parameters(self):
  98. if self.__params is None:
  99. self.__params = self.__idents_matching(lambda x:x & DEF_PARAM)
  100. return self.__params
  101. def get_locals(self):
  102. if self.__locals is None:
  103. locs = (LOCAL, CELL)
  104. test = lambda x: ((x >> SCOPE_OFF) & SCOPE_MASK) in locs
  105. self.__locals = self.__idents_matching(test)
  106. return self.__locals
  107. def get_globals(self):
  108. if self.__globals is None:
  109. glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
  110. test = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) in glob
  111. self.__globals = self.__idents_matching(test)
  112. return self.__globals
  113. def get_nonlocals(self):
  114. if self.__nonlocals is None:
  115. self.__nonlocals = self.__idents_matching(lambda x:x & DEF_NONLOCAL)
  116. return self.__nonlocals
  117. def get_frees(self):
  118. if self.__frees is None:
  119. is_free = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) == FREE
  120. self.__frees = self.__idents_matching(is_free)
  121. return self.__frees
  122. class Class(SymbolTable):
  123. __methods = None
  124. def get_methods(self):
  125. if self.__methods is None:
  126. d = {}
  127. for st in self._table.children:
  128. d[st.name] = 1
  129. self.__methods = tuple(d)
  130. return self.__methods
  131. class Symbol:
  132. def __init__(self, name, flags, namespaces=None, *, module_scope=False):
  133. self.__name = name
  134. self.__flags = flags
  135. self.__scope = (flags >> SCOPE_OFF) & SCOPE_MASK # like PyST_GetScope()
  136. self.__namespaces = namespaces or ()
  137. self.__module_scope = module_scope
  138. def __repr__(self):
  139. return "<symbol {0!r}>".format(self.__name)
  140. def get_name(self):
  141. return self.__name
  142. def is_referenced(self):
  143. return bool(self.__flags & _symtable.USE)
  144. def is_parameter(self):
  145. return bool(self.__flags & DEF_PARAM)
  146. def is_global(self):
  147. """Return *True* if the sysmbol is global.
  148. """
  149. return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
  150. or (self.__module_scope and self.__flags & DEF_BOUND))
  151. def is_nonlocal(self):
  152. return bool(self.__flags & DEF_NONLOCAL)
  153. def is_declared_global(self):
  154. return bool(self.__scope == GLOBAL_EXPLICIT)
  155. def is_local(self):
  156. """Return *True* if the symbol is local.
  157. """
  158. return bool(self.__scope in (LOCAL, CELL)
  159. or (self.__module_scope and self.__flags & DEF_BOUND))
  160. def is_annotated(self):
  161. return bool(self.__flags & DEF_ANNOT)
  162. def is_free(self):
  163. return bool(self.__scope == FREE)
  164. def is_imported(self):
  165. return bool(self.__flags & DEF_IMPORT)
  166. def is_assigned(self):
  167. return bool(self.__flags & DEF_LOCAL)
  168. def is_namespace(self):
  169. """Returns true if name binding introduces new namespace.
  170. If the name is used as the target of a function or class
  171. statement, this will be true.
  172. Note that a single name can be bound to multiple objects. If
  173. is_namespace() is true, the name may also be bound to other
  174. objects, like an int or list, that does not introduce a new
  175. namespace.
  176. """
  177. return bool(self.__namespaces)
  178. def get_namespaces(self):
  179. """Return a list of namespaces bound to this name"""
  180. return self.__namespaces
  181. def get_namespace(self):
  182. """Returns the single namespace bound to this name.
  183. Raises ValueError if the name is bound to multiple namespaces.
  184. """
  185. if len(self.__namespaces) != 1:
  186. raise ValueError("name is bound to multiple namespaces")
  187. return self.__namespaces[0]
  188. if __name__ == "__main__":
  189. import os, sys
  190. with open(sys.argv[0]) as f:
  191. src = f.read()
  192. mod = symtable(src, os.path.split(sys.argv[0])[1], "exec")
  193. for ident in mod.get_identifiers():
  194. info = mod.lookup(ident)
  195. print(info, info.is_local(), info.is_namespace())