/extern/python/reloop-closured/lib/python2.7/symtable.py

https://github.com/atoun/jsrepl
Python | 242 lines | 233 code | 3 blank | 6 comment | 6 complexity | 7e8a0c1d223b47f5e93fd5b941b7f6ce MD5 | raw file
  1. """Interface to the compiler's internal symbol tables"""
  2. import _symtable
  3. from _symtable import (USE, DEF_GLOBAL, DEF_LOCAL, DEF_PARAM,
  4. DEF_IMPORT, DEF_BOUND, OPT_IMPORT_STAR, OPT_EXEC, OPT_BARE_EXEC,
  5. SCOPE_OFF, SCOPE_MASK, FREE, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL, LOCAL)
  6. import weakref
  7. __all__ = ["symtable", "SymbolTable", "Class", "Function", "Symbol"]
  8. def symtable(code, filename, compile_type):
  9. raw = _symtable.symtable(code, filename, compile_type)
  10. for top in raw.itervalues():
  11. if top.name == 'top':
  12. break
  13. return _newSymbolTable(top, filename)
  14. class SymbolTableFactory:
  15. def __init__(self):
  16. self.__memo = weakref.WeakValueDictionary()
  17. def new(self, table, filename):
  18. if table.type == _symtable.TYPE_FUNCTION:
  19. return Function(table, filename)
  20. if table.type == _symtable.TYPE_CLASS:
  21. return Class(table, filename)
  22. return SymbolTable(table, filename)
  23. def __call__(self, table, filename):
  24. key = table, filename
  25. obj = self.__memo.get(key, None)
  26. if obj is None:
  27. obj = self.__memo[key] = self.new(table, filename)
  28. return obj
  29. _newSymbolTable = SymbolTableFactory()
  30. class SymbolTable(object):
  31. def __init__(self, raw_table, filename):
  32. self._table = raw_table
  33. self._filename = filename
  34. self._symbols = {}
  35. def __repr__(self):
  36. if self.__class__ == SymbolTable:
  37. kind = ""
  38. else:
  39. kind = "%s " % self.__class__.__name__
  40. if self._table.name == "global":
  41. return "<{0}SymbolTable for module {1}>".format(kind, self._filename)
  42. else:
  43. return "<{0}SymbolTable for {1} in {2}>".format(kind,
  44. self._table.name,
  45. self._filename)
  46. def get_type(self):
  47. if self._table.type == _symtable.TYPE_MODULE:
  48. return "module"
  49. if self._table.type == _symtable.TYPE_FUNCTION:
  50. return "function"
  51. if self._table.type == _symtable.TYPE_CLASS:
  52. return "class"
  53. assert self._table.type in (1, 2, 3), \
  54. "unexpected type: {0}".format(self._table.type)
  55. def get_id(self):
  56. return self._table.id
  57. def get_name(self):
  58. return self._table.name
  59. def get_lineno(self):
  60. return self._table.lineno
  61. def is_optimized(self):
  62. return bool(self._table.type == _symtable.TYPE_FUNCTION
  63. and not self._table.optimized)
  64. def is_nested(self):
  65. return bool(self._table.nested)
  66. def has_children(self):
  67. return bool(self._table.children)
  68. def has_exec(self):
  69. """Return true if the scope uses exec"""
  70. return bool(self._table.optimized & (OPT_EXEC | OPT_BARE_EXEC))
  71. def has_import_star(self):
  72. """Return true if the scope uses import *"""
  73. return bool(self._table.optimized & OPT_IMPORT_STAR)
  74. def get_identifiers(self):
  75. return self._table.symbols.keys()
  76. def lookup(self, name):
  77. sym = self._symbols.get(name)
  78. if sym is None:
  79. flags = self._table.symbols[name]
  80. namespaces = self.__check_children(name)
  81. sym = self._symbols[name] = Symbol(name, flags, namespaces)
  82. return sym
  83. def get_symbols(self):
  84. return [self.lookup(ident) for ident in self.get_identifiers()]
  85. def __check_children(self, name):
  86. return [_newSymbolTable(st, self._filename)
  87. for st in self._table.children
  88. if st.name == name]
  89. def get_children(self):
  90. return [_newSymbolTable(st, self._filename)
  91. for st in self._table.children]
  92. class Function(SymbolTable):
  93. # Default values for instance variables
  94. __params = None
  95. __locals = None
  96. __frees = None
  97. __globals = None
  98. def __idents_matching(self, test_func):
  99. return tuple([ident for ident in self.get_identifiers()
  100. if test_func(self._table.symbols[ident])])
  101. def get_parameters(self):
  102. if self.__params is None:
  103. self.__params = self.__idents_matching(lambda x:x & DEF_PARAM)
  104. return self.__params
  105. def get_locals(self):
  106. if self.__locals is None:
  107. locs = (LOCAL, CELL)
  108. test = lambda x: ((x >> SCOPE_OFF) & SCOPE_MASK) in locs
  109. self.__locals = self.__idents_matching(test)
  110. return self.__locals
  111. def get_globals(self):
  112. if self.__globals is None:
  113. glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
  114. test = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) in glob
  115. self.__globals = self.__idents_matching(test)
  116. return self.__globals
  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(object):
  132. def __init__(self, name, flags, namespaces=None):
  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. def __repr__(self):
  138. return "<symbol {0!r}>".format(self.__name)
  139. def get_name(self):
  140. return self.__name
  141. def is_referenced(self):
  142. return bool(self.__flags & _symtable.USE)
  143. def is_parameter(self):
  144. return bool(self.__flags & DEF_PARAM)
  145. def is_global(self):
  146. return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT))
  147. def is_declared_global(self):
  148. return bool(self.__scope == GLOBAL_EXPLICIT)
  149. def is_local(self):
  150. return bool(self.__flags & DEF_BOUND)
  151. def is_free(self):
  152. return bool(self.__scope == FREE)
  153. def is_imported(self):
  154. return bool(self.__flags & DEF_IMPORT)
  155. def is_assigned(self):
  156. return bool(self.__flags & DEF_LOCAL)
  157. def is_namespace(self):
  158. """Returns true if name binding introduces new namespace.
  159. If the name is used as the target of a function or class
  160. statement, this will be true.
  161. Note that a single name can be bound to multiple objects. If
  162. is_namespace() is true, the name may also be bound to other
  163. objects, like an int or list, that does not introduce a new
  164. namespace.
  165. """
  166. return bool(self.__namespaces)
  167. def get_namespaces(self):
  168. """Return a list of namespaces bound to this name"""
  169. return self.__namespaces
  170. def get_namespace(self):
  171. """Returns the single namespace bound to this name.
  172. Raises ValueError if the name is bound to multiple namespaces.
  173. """
  174. if len(self.__namespaces) != 1:
  175. raise ValueError, "name is bound to multiple namespaces"
  176. return self.__namespaces[0]
  177. if __name__ == "__main__":
  178. import os, sys
  179. src = open(sys.argv[0]).read()
  180. mod = symtable(src, os.path.split(sys.argv[0])[1], "exec")
  181. for ident in mod.get_identifiers():
  182. info = mod.lookup(ident)
  183. print info, info.is_local(), info.is_namespace()