symtable.py 7.7 KB

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