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,
|
|
|
|
DEF_FREE_GLOBAL, DEF_FREE_CLASS, DEF_IMPORT, DEF_BOUND,
|
|
|
|
OPT_IMPORT_STAR, SCOPE_OFF, SCOPE_MASK, FREE,
|
|
|
|
GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
|
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):
|
|
|
|
raw = _symtable.symtable(code, filename, compile_type)
|
2007-02-11 14:12:03 +08:00
|
|
|
for top in raw.values():
|
2005-11-25 11:14:00 +08:00
|
|
|
if top.name == 'top':
|
|
|
|
break
|
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):
|
|
|
|
return bool(self._table.type == _symtable.TYPE_FUNCTION
|
|
|
|
and not self._table.optimized)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def has_import_star(self):
|
|
|
|
"""Return true if the scope uses import *"""
|
|
|
|
return bool(self._table.optimized & OPT_IMPORT_STAR)
|
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):
|
2008-08-20 20:55:31 +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:
|
|
|
|
self.__locals = self.__idents_matching(lambda x:x & DEF_BOUND)
|
|
|
|
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
|
|
|
|
|
|
|
def is_local(self):
|
|
|
|
return bool(self.__flags & DEF_BOUND)
|
|
|
|
|
|
|
|
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
|
|
|
|
src = open(sys.argv[0]).read()
|
|
|
|
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())
|