symtable.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 get_identifiers(self):
  65. return self._table.symbols.keys()
  66. def lookup(self, name):
  67. sym = self._symbols.get(name)
  68. if sym is None:
  69. flags = self._table.symbols[name]
  70. namespaces = self.__check_children(name)
  71. module_scope = (self._table.name == "top")
  72. sym = self._symbols[name] = Symbol(name, flags, namespaces,
  73. module_scope=module_scope)
  74. return sym
  75. def get_symbols(self):
  76. return [self.lookup(ident) for ident in self.get_identifiers()]
  77. def __check_children(self, name):
  78. return [_newSymbolTable(st, self._filename)
  79. for st in self._table.children
  80. if st.name == name]
  81. def get_children(self):
  82. return [_newSymbolTable(st, self._filename)
  83. for st in self._table.children]
  84. class Function(SymbolTable):
  85. # Default values for instance variables
  86. __params = None
  87. __locals = None
  88. __frees = None
  89. __globals = None
  90. __nonlocals = None
  91. def __idents_matching(self, test_func):
  92. return tuple(ident for ident in self.get_identifiers()
  93. if test_func(self._table.symbols[ident]))
  94. def get_parameters(self):
  95. if self.__params is None:
  96. self.__params = self.__idents_matching(lambda x:x & DEF_PARAM)
  97. return self.__params
  98. def get_locals(self):
  99. if self.__locals is None:
  100. locs = (LOCAL, CELL)
  101. test = lambda x: ((x >> SCOPE_OFF) & SCOPE_MASK) in locs
  102. self.__locals = self.__idents_matching(test)
  103. return self.__locals
  104. def get_globals(self):
  105. if self.__globals is None:
  106. glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
  107. test = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) in glob
  108. self.__globals = self.__idents_matching(test)
  109. return self.__globals
  110. def get_nonlocals(self):
  111. if self.__nonlocals is None:
  112. self.__nonlocals = self.__idents_matching(lambda x:x & DEF_NONLOCAL)
  113. return self.__nonlocals
  114. def get_frees(self):
  115. if self.__frees is None:
  116. is_free = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) == FREE
  117. self.__frees = self.__idents_matching(is_free)
  118. return self.__frees
  119. class Class(SymbolTable):
  120. __methods = None
  121. def get_methods(self):
  122. if self.__methods is None:
  123. d = {}
  124. for st in self._table.children:
  125. d[st.name] = 1
  126. self.__methods = tuple(d)
  127. return self.__methods
  128. class Symbol:
  129. def __init__(self, name, flags, namespaces=None, *, module_scope=False):
  130. self.__name = name
  131. self.__flags = flags
  132. self.__scope = (flags >> SCOPE_OFF) & SCOPE_MASK # like PyST_GetScope()
  133. self.__namespaces = namespaces or ()
  134. self.__module_scope = module_scope
  135. def __repr__(self):
  136. return "<symbol {0!r}>".format(self.__name)
  137. def get_name(self):
  138. return self.__name
  139. def is_referenced(self):
  140. return bool(self.__flags & _symtable.USE)
  141. def is_parameter(self):
  142. return bool(self.__flags & DEF_PARAM)
  143. def is_global(self):
  144. """Return *True* if the sysmbol is global.
  145. """
  146. return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
  147. or (self.__module_scope and self.__flags & DEF_BOUND))
  148. def is_nonlocal(self):
  149. return bool(self.__flags & DEF_NONLOCAL)
  150. def is_declared_global(self):
  151. return bool(self.__scope == GLOBAL_EXPLICIT)
  152. def is_local(self):
  153. """Return *True* if the symbol is local.
  154. """
  155. return bool(self.__scope in (LOCAL, CELL)
  156. or (self.__module_scope and self.__flags & DEF_BOUND))
  157. def is_annotated(self):
  158. return bool(self.__flags & DEF_ANNOT)
  159. def is_free(self):
  160. return bool(self.__scope == FREE)
  161. def is_imported(self):
  162. return bool(self.__flags & DEF_IMPORT)
  163. def is_assigned(self):
  164. return bool(self.__flags & DEF_LOCAL)
  165. def is_namespace(self):
  166. """Returns true if name binding introduces new namespace.
  167. If the name is used as the target of a function or class
  168. statement, this will be true.
  169. Note that a single name can be bound to multiple objects. If
  170. is_namespace() is true, the name may also be bound to other
  171. objects, like an int or list, that does not introduce a new
  172. namespace.
  173. """
  174. return bool(self.__namespaces)
  175. def get_namespaces(self):
  176. """Return a list of namespaces bound to this name"""
  177. return self.__namespaces
  178. def get_namespace(self):
  179. """Returns the single namespace bound to this name.
  180. Raises ValueError if the name is bound to multiple namespaces.
  181. """
  182. if len(self.__namespaces) != 1:
  183. raise ValueError("name is bound to multiple namespaces")
  184. return self.__namespaces[0]
  185. if __name__ == "__main__":
  186. import os, sys
  187. with open(sys.argv[0]) as f:
  188. src = f.read()
  189. mod = symtable(src, os.path.split(sys.argv[0])[1], "exec")
  190. for ident in mod.get_identifiers():
  191. info = mod.lookup(ident)
  192. print(info, info.is_local(), info.is_namespace())