2001-03-23 07:32:22 +08:00
|
|
|
"""Interface to the compiler's internal symbol tables"""
|
|
|
|
|
|
|
|
import _symtable
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
from _symtable import (USE, DEF_GLOBAL, DEF_LOCAL, DEF_PARAM,
|
2016-09-09 11:50:03 +08:00
|
|
|
DEF_IMPORT, DEF_BOUND, DEF_ANNOT, SCOPE_OFF, SCOPE_MASK, FREE,
|
2009-06-29 03:30:36 +08:00
|
|
|
LOCAL, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL)
|
2001-03-23 07:32:22 +08:00
|
|
|
|
|
|
|
import weakref
|
|
|
|
|
2008-08-20 10:33:00 +08:00
|
|
|
__all__ = ["symtable", "SymbolTable", "Class", "Function", "Symbol"]
|
2001-03-23 07:32:22 +08:00
|
|
|
|
|
|
|
def symtable(code, filename, compile_type):
|
2013-10-27 01:13:51 +08:00
|
|
|
top = _symtable.symtable(code, filename, compile_type)
|
2008-08-20 10:33:00 +08:00
|
|
|
return _newSymbolTable(top, filename)
|
2001-03-23 07:32:22 +08:00
|
|
|
|
|
|
|
class SymbolTableFactory:
|
|
|
|
def __init__(self):
|
|
|
|
self.__memo = weakref.WeakValueDictionary()
|
|
|
|
|
|
|
|
def new(self, table, filename):
|
|
|
|
if table.type == _symtable.TYPE_FUNCTION:
|
|
|
|
return Function(table, filename)
|
|
|
|
if table.type == _symtable.TYPE_CLASS:
|
|
|
|
return Class(table, filename)
|
|
|
|
return SymbolTable(table, filename)
|
|
|
|
|
|
|
|
def __call__(self, table, filename):
|
|
|
|
key = table, filename
|
|
|
|
obj = self.__memo.get(key, None)
|
|
|
|
if obj is None:
|
|
|
|
obj = self.__memo[key] = self.new(table, filename)
|
|
|
|
return obj
|
|
|
|
|
2008-08-20 10:33:00 +08:00
|
|
|
_newSymbolTable = SymbolTableFactory()
|
2001-03-29 12:36:09 +08:00
|
|
|
|
2001-03-23 07:32:22 +08:00
|
|
|
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
class SymbolTable(object):
|
|
|
|
|
2001-03-23 07:32:22 +08:00
|
|
|
def __init__(self, raw_table, filename):
|
|
|
|
self._table = raw_table
|
|
|
|
self._filename = filename
|
|
|
|
self._symbols = {}
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
if self.__class__ == SymbolTable:
|
|
|
|
kind = ""
|
|
|
|
else:
|
|
|
|
kind = "%s " % self.__class__.__name__
|
2001-03-29 12:36:09 +08:00
|
|
|
|
2001-03-23 07:32:22 +08:00
|
|
|
if self._table.name == "global":
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
return "<{0}SymbolTable for module {1}>".format(kind, self._filename)
|
2001-03-23 07:32:22 +08:00
|
|
|
else:
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
return "<{0}SymbolTable for {1} in {2}>".format(kind,
|
|
|
|
self._table.name,
|
|
|
|
self._filename)
|
2001-03-23 07:32:22 +08:00
|
|
|
|
|
|
|
def get_type(self):
|
|
|
|
if self._table.type == _symtable.TYPE_MODULE:
|
|
|
|
return "module"
|
|
|
|
if self._table.type == _symtable.TYPE_FUNCTION:
|
|
|
|
return "function"
|
|
|
|
if self._table.type == _symtable.TYPE_CLASS:
|
|
|
|
return "class"
|
|
|
|
assert self._table.type in (1, 2, 3), \
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
"unexpected type: {0}".format(self._table.type)
|
2001-03-23 07:32:22 +08:00
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return self._table.id
|
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
return self._table.name
|
|
|
|
|
|
|
|
def get_lineno(self):
|
|
|
|
return self._table.lineno
|
|
|
|
|
|
|
|
def is_optimized(self):
|
2015-04-28 09:44:22 +08:00
|
|
|
return bool(self._table.type == _symtable.TYPE_FUNCTION)
|
2001-03-23 07:32:22 +08:00
|
|
|
|
|
|
|
def is_nested(self):
|
|
|
|
return bool(self._table.nested)
|
|
|
|
|
|
|
|
def has_children(self):
|
|
|
|
return bool(self._table.children)
|
|
|
|
|
|
|
|
def has_exec(self):
|
2006-09-07 06:37:15 +08:00
|
|
|
"""Return true if the scope uses exec. Deprecated method."""
|
|
|
|
return False
|
2001-04-17 02:43:18 +08:00
|
|
|
|
2001-03-23 07:32:22 +08:00
|
|
|
def get_identifiers(self):
|
|
|
|
return self._table.symbols.keys()
|
|
|
|
|
|
|
|
def lookup(self, name):
|
|
|
|
sym = self._symbols.get(name)
|
|
|
|
if sym is None:
|
|
|
|
flags = self._table.symbols[name]
|
|
|
|
namespaces = self.__check_children(name)
|
|
|
|
sym = self._symbols[name] = Symbol(name, flags, namespaces)
|
|
|
|
return sym
|
|
|
|
|
|
|
|
def get_symbols(self):
|
|
|
|
return [self.lookup(ident) for ident in self.get_identifiers()]
|
|
|
|
|
|
|
|
def __check_children(self, name):
|
2008-08-20 10:33:00 +08:00
|
|
|
return [_newSymbolTable(st, self._filename)
|
2001-03-23 07:32:22 +08:00
|
|
|
for st in self._table.children
|
|
|
|
if st.name == name]
|
|
|
|
|
2001-03-23 23:41:14 +08:00
|
|
|
def get_children(self):
|
2008-08-20 10:33:00 +08:00
|
|
|
return [_newSymbolTable(st, self._filename)
|
2001-03-23 23:41:14 +08:00
|
|
|
for st in self._table.children]
|
|
|
|
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
|
2001-03-23 07:32:22 +08:00
|
|
|
class Function(SymbolTable):
|
|
|
|
|
|
|
|
# Default values for instance variables
|
|
|
|
__params = None
|
|
|
|
__locals = None
|
|
|
|
__frees = None
|
|
|
|
__globals = None
|
|
|
|
|
|
|
|
def __idents_matching(self, test_func):
|
2017-05-18 22:35:54 +08:00
|
|
|
return tuple(ident for ident in self.get_identifiers()
|
|
|
|
if test_func(self._table.symbols[ident]))
|
2001-03-23 07:32:22 +08:00
|
|
|
|
|
|
|
def get_parameters(self):
|
|
|
|
if self.__params is None:
|
|
|
|
self.__params = self.__idents_matching(lambda x:x & DEF_PARAM)
|
|
|
|
return self.__params
|
|
|
|
|
|
|
|
def get_locals(self):
|
|
|
|
if self.__locals is None:
|
2009-06-29 03:30:36 +08:00
|
|
|
locs = (LOCAL, CELL)
|
|
|
|
test = lambda x: ((x >> SCOPE_OFF) & SCOPE_MASK) in locs
|
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
........
2009-06-29 03:19:51 +08:00
|
|
|
self.__locals = self.__idents_matching(test)
|
2001-03-23 07:32:22 +08:00
|
|
|
return self.__locals
|
2001-03-29 12:36:09 +08:00
|
|
|
|
2001-03-23 07:32:22 +08:00
|
|
|
def get_globals(self):
|
|
|
|
if self.__globals is None:
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
|
|
|
|
test = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) in glob
|
|
|
|
self.__globals = self.__idents_matching(test)
|
2001-03-23 07:32:22 +08:00
|
|
|
return self.__globals
|
|
|
|
|
|
|
|
def get_frees(self):
|
|
|
|
if self.__frees is None:
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
is_free = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) == FREE
|
2001-03-23 07:32:22 +08:00
|
|
|
self.__frees = self.__idents_matching(is_free)
|
|
|
|
return self.__frees
|
|
|
|
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
|
2001-03-23 07:32:22 +08:00
|
|
|
class Class(SymbolTable):
|
|
|
|
|
|
|
|
__methods = None
|
|
|
|
|
|
|
|
def get_methods(self):
|
|
|
|
if self.__methods is None:
|
|
|
|
d = {}
|
|
|
|
for st in self._table.children:
|
|
|
|
d[st.name] = 1
|
2008-08-20 20:55:31 +08:00
|
|
|
self.__methods = tuple(d)
|
2001-03-23 07:32:22 +08:00
|
|
|
return self.__methods
|
|
|
|
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
|
|
|
|
class Symbol(object):
|
|
|
|
|
2001-03-23 07:32:22 +08:00
|
|
|
def __init__(self, name, flags, namespaces=None):
|
|
|
|
self.__name = name
|
|
|
|
self.__flags = flags
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
self.__scope = (flags >> SCOPE_OFF) & SCOPE_MASK # like PyST_GetScope()
|
2001-03-23 07:32:22 +08:00
|
|
|
self.__namespaces = namespaces or ()
|
|
|
|
|
|
|
|
def __repr__(self):
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
return "<symbol {0!r}>".format(self.__name)
|
2001-03-23 07:32:22 +08:00
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
return self.__name
|
|
|
|
|
|
|
|
def is_referenced(self):
|
|
|
|
return bool(self.__flags & _symtable.USE)
|
|
|
|
|
|
|
|
def is_parameter(self):
|
|
|
|
return bool(self.__flags & DEF_PARAM)
|
|
|
|
|
|
|
|
def is_global(self):
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT))
|
2001-03-23 07:32:22 +08:00
|
|
|
|
2009-03-31 23:26:37 +08:00
|
|
|
def is_declared_global(self):
|
|
|
|
return bool(self.__scope == GLOBAL_EXPLICIT)
|
|
|
|
|
2001-03-23 07:32:22 +08:00
|
|
|
def is_local(self):
|
|
|
|
return bool(self.__flags & DEF_BOUND)
|
|
|
|
|
2016-09-09 11:50:03 +08:00
|
|
|
def is_annotated(self):
|
|
|
|
return bool(self.__flags & DEF_ANNOT)
|
|
|
|
|
2001-03-23 07:32:22 +08:00
|
|
|
def is_free(self):
|
Merged revisions 65715,65724,65726,65732,65736-65739,65775 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65715 | benjamin.peterson | 2008-08-16 16:04:16 -0500 (Sat, 16 Aug 2008) | 1 line
add some documentation for symtable
........
r65724 | benjamin.peterson | 2008-08-16 17:11:33 -0500 (Sat, 16 Aug 2008) | 2 lines
include filename and line number in SyntaxError
........
r65726 | georg.brandl | 2008-08-16 17:37:05 -0500 (Sat, 16 Aug 2008) | 2 lines
Review symtable docs.
........
r65732 | benjamin.peterson | 2008-08-16 18:29:40 -0500 (Sat, 16 Aug 2008) | 1 line
PySTEntry's constructor is static; there's no point in a fancy API name
........
r65736 | benjamin.peterson | 2008-08-16 20:09:17 -0500 (Sat, 16 Aug 2008) | 1 line
expose PySTEntry.nested so the symtable module will work
........
r65737 | benjamin.peterson | 2008-08-16 20:17:15 -0500 (Sat, 16 Aug 2008) | 1 line
a few improvements
........
r65738 | benjamin.peterson | 2008-08-16 20:27:30 -0500 (Sat, 16 Aug 2008) | 1 line
fix compile errors
........
r65739 | benjamin.peterson | 2008-08-16 21:23:43 -0500 (Sat, 16 Aug 2008) | 1 line
uhh PySTEntry->ste_unoptimized has to be exposed too
........
r65775 | benjamin.peterson | 2008-08-17 12:13:26 -0500 (Sun, 17 Aug 2008) | 5 lines
get the symtable module back in working order
- Fix broken functions
- Add (hopefully) extensive tests
- Modernize a little
........
2008-08-18 02:02:44 +08:00
|
|
|
return bool(self.__scope == FREE)
|
2001-03-23 07:32:22 +08:00
|
|
|
|
|
|
|
def is_imported(self):
|
|
|
|
return bool(self.__flags & DEF_IMPORT)
|
|
|
|
|
|
|
|
def is_assigned(self):
|
|
|
|
return bool(self.__flags & DEF_LOCAL)
|
|
|
|
|
|
|
|
def is_namespace(self):
|
|
|
|
"""Returns true if name binding introduces new namespace.
|
|
|
|
|
|
|
|
If the name is used as the target of a function or class
|
|
|
|
statement, this will be true.
|
|
|
|
|
|
|
|
Note that a single name can be bound to multiple objects. If
|
|
|
|
is_namespace() is true, the name may also be bound to other
|
|
|
|
objects, like an int or list, that does not introduce a new
|
|
|
|
namespace.
|
|
|
|
"""
|
|
|
|
return bool(self.__namespaces)
|
|
|
|
|
|
|
|
def get_namespaces(self):
|
|
|
|
"""Return a list of namespaces bound to this name"""
|
|
|
|
return self.__namespaces
|
|
|
|
|
|
|
|
def get_namespace(self):
|
|
|
|
"""Returns the single namespace bound to this name.
|
|
|
|
|
|
|
|
Raises ValueError if the name is bound to multiple namespaces.
|
|
|
|
"""
|
|
|
|
if len(self.__namespaces) != 1:
|
2007-08-30 09:19:48 +08:00
|
|
|
raise ValueError("name is bound to multiple namespaces")
|
2001-03-23 07:32:22 +08:00
|
|
|
return self.__namespaces[0]
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import os, sys
|
2013-02-12 09:04:27 +08:00
|
|
|
with open(sys.argv[0]) as f:
|
|
|
|
src = f.read()
|
2001-03-23 07:32:22 +08:00
|
|
|
mod = symtable(src, os.path.split(sys.argv[0])[1], "exec")
|
|
|
|
for ident in mod.get_identifiers():
|
|
|
|
info = mod.lookup(ident)
|
2007-02-09 13:37:30 +08:00
|
|
|
print(info, info.is_local(), info.is_namespace())
|