You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
7.3 KiB

Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r73376 | benjamin.peterson | 2009-06-11 17:29:23 -0500 (Thu, 11 Jun 2009) | 1 line remove check for case handled in sub-function ........ r73393 | alexandre.vassalotti | 2009-06-12 13:56:57 -0500 (Fri, 12 Jun 2009) | 2 lines Clear reference to the static PyExc_RecursionErrorInst in _PyExc_Fini. ........ r73398 | alexandre.vassalotti | 2009-06-12 15:57:12 -0500 (Fri, 12 Jun 2009) | 3 lines Add const qualifier to PyErr_SetFromErrnoWithFilename and to PyErr_SetFromErrnoWithUnicodeFilename. ........ r73400 | alexandre.vassalotti | 2009-06-12 16:43:47 -0500 (Fri, 12 Jun 2009) | 2 lines Delete outdated make file for building the parser with MSVC 6. ........ r73404 | benjamin.peterson | 2009-06-12 20:40:00 -0500 (Fri, 12 Jun 2009) | 1 line keep the slice.step field as NULL if no step expression is given ........ r73405 | benjamin.peterson | 2009-06-12 22:46:30 -0500 (Fri, 12 Jun 2009) | 1 line prevent import statements from assigning to None ........ r73409 | benjamin.peterson | 2009-06-13 08:06:21 -0500 (Sat, 13 Jun 2009) | 1 line allow importing from a module named None if it has an 'as' clause ........ r73419 | benjamin.peterson | 2009-06-13 11:19:19 -0500 (Sat, 13 Jun 2009) | 1 line set Print.values to NULL if there are no values ........ r73420 | benjamin.peterson | 2009-06-13 12:08:53 -0500 (Sat, 13 Jun 2009) | 1 line give a better error message when deleting () ........ r73421 | benjamin.peterson | 2009-06-13 15:23:33 -0500 (Sat, 13 Jun 2009) | 1 line when no module is given in a 'from' relative import, make ImportFrom.module NULL ........ r73432 | amaury.forgeotdarc | 2009-06-14 16:20:40 -0500 (Sun, 14 Jun 2009) | 3 lines #6227: Because of a wrong indentation, the test was not testing what it should. Ensure that the snippet in doctest_aliases actually contains aliases. ........ r73457 | benjamin.peterson | 2009-06-16 18:13:09 -0500 (Tue, 16 Jun 2009) | 1 line add underscores ........ r73460 | benjamin.peterson | 2009-06-16 22:23:04 -0500 (Tue, 16 Jun 2009) | 1 line remove unused 'encoding' member from the compiler struct ........ r73485 | benjamin.peterson | 2009-06-19 17:07:47 -0500 (Fri, 19 Jun 2009) | 1 line remove duplicate test ........ r73486 | benjamin.peterson | 2009-06-19 17:09:17 -0500 (Fri, 19 Jun 2009) | 1 line add missing assertion #6313 ........ r73488 | benjamin.peterson | 2009-06-19 17:16:28 -0500 (Fri, 19 Jun 2009) | 1 line show that this one isn't used ........ r73489 | benjamin.peterson | 2009-06-19 17:21:12 -0500 (Fri, 19 Jun 2009) | 1 line use closures ........ r73501 | benjamin.peterson | 2009-06-21 18:01:07 -0500 (Sun, 21 Jun 2009) | 1 line don't need to add the name 'lambda' as assigned ........ r73502 | benjamin.peterson | 2009-06-21 18:03:36 -0500 (Sun, 21 Jun 2009) | 1 line remove tmpname support since it's no longer used ........ r73513 | benjamin.peterson | 2009-06-22 20:18:57 -0500 (Mon, 22 Jun 2009) | 1 line fix grammar ........ r73514 | benjamin.peterson | 2009-06-22 22:01:56 -0500 (Mon, 22 Jun 2009) | 1 line remove some unused symtable constants ........
17 years ago
18 years ago
Merged revisions 73376,73393,73398,73400,73404-73405,73409,73419-73421,73432,73457,73460,73485-73486,73488-73489,73501-73502,73513-73514 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r73376 | benjamin.peterson | 2009-06-11 17:29:23 -0500 (Thu, 11 Jun 2009) | 1 line remove check for case handled in sub-function ........ r73393 | alexandre.vassalotti | 2009-06-12 13:56:57 -0500 (Fri, 12 Jun 2009) | 2 lines Clear reference to the static PyExc_RecursionErrorInst in _PyExc_Fini. ........ r73398 | alexandre.vassalotti | 2009-06-12 15:57:12 -0500 (Fri, 12 Jun 2009) | 3 lines Add const qualifier to PyErr_SetFromErrnoWithFilename and to PyErr_SetFromErrnoWithUnicodeFilename. ........ r73400 | alexandre.vassalotti | 2009-06-12 16:43:47 -0500 (Fri, 12 Jun 2009) | 2 lines Delete outdated make file for building the parser with MSVC 6. ........ r73404 | benjamin.peterson | 2009-06-12 20:40:00 -0500 (Fri, 12 Jun 2009) | 1 line keep the slice.step field as NULL if no step expression is given ........ r73405 | benjamin.peterson | 2009-06-12 22:46:30 -0500 (Fri, 12 Jun 2009) | 1 line prevent import statements from assigning to None ........ r73409 | benjamin.peterson | 2009-06-13 08:06:21 -0500 (Sat, 13 Jun 2009) | 1 line allow importing from a module named None if it has an 'as' clause ........ r73419 | benjamin.peterson | 2009-06-13 11:19:19 -0500 (Sat, 13 Jun 2009) | 1 line set Print.values to NULL if there are no values ........ r73420 | benjamin.peterson | 2009-06-13 12:08:53 -0500 (Sat, 13 Jun 2009) | 1 line give a better error message when deleting () ........ r73421 | benjamin.peterson | 2009-06-13 15:23:33 -0500 (Sat, 13 Jun 2009) | 1 line when no module is given in a 'from' relative import, make ImportFrom.module NULL ........ r73432 | amaury.forgeotdarc | 2009-06-14 16:20:40 -0500 (Sun, 14 Jun 2009) | 3 lines #6227: Because of a wrong indentation, the test was not testing what it should. Ensure that the snippet in doctest_aliases actually contains aliases. ........ r73457 | benjamin.peterson | 2009-06-16 18:13:09 -0500 (Tue, 16 Jun 2009) | 1 line add underscores ........ r73460 | benjamin.peterson | 2009-06-16 22:23:04 -0500 (Tue, 16 Jun 2009) | 1 line remove unused 'encoding' member from the compiler struct ........ r73485 | benjamin.peterson | 2009-06-19 17:07:47 -0500 (Fri, 19 Jun 2009) | 1 line remove duplicate test ........ r73486 | benjamin.peterson | 2009-06-19 17:09:17 -0500 (Fri, 19 Jun 2009) | 1 line add missing assertion #6313 ........ r73488 | benjamin.peterson | 2009-06-19 17:16:28 -0500 (Fri, 19 Jun 2009) | 1 line show that this one isn't used ........ r73489 | benjamin.peterson | 2009-06-19 17:21:12 -0500 (Fri, 19 Jun 2009) | 1 line use closures ........ r73501 | benjamin.peterson | 2009-06-21 18:01:07 -0500 (Sun, 21 Jun 2009) | 1 line don't need to add the name 'lambda' as assigned ........ r73502 | benjamin.peterson | 2009-06-21 18:03:36 -0500 (Sun, 21 Jun 2009) | 1 line remove tmpname support since it's no longer used ........ r73513 | benjamin.peterson | 2009-06-22 20:18:57 -0500 (Mon, 22 Jun 2009) | 1 line fix grammar ........ r73514 | benjamin.peterson | 2009-06-22 22:01:56 -0500 (Mon, 22 Jun 2009) | 1 line remove some unused symtable constants ........
17 years ago
18 years ago
  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, 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. raw = _symtable.symtable(code, filename, compile_type)
  10. for top in raw.values():
  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. Deprecated method."""
  70. return False
  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())