2001-03-23 07:32:22 +08:00
|
|
|
"""Interface to the compiler's internal symbol tables"""
|
|
|
|
|
|
|
|
import _symtable
|
2018-10-20 08:46:00 +08:00
|
|
|
from _symtable import (USE, DEF_GLOBAL, DEF_NONLOCAL, 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):
|
2020-07-08 07:09:56 +08:00
|
|
|
""" Return the toplevel *SymbolTable* for the source code.
|
|
|
|
|
|
|
|
*filename* is the name of the file with the code
|
|
|
|
and *compile_type* is the *compile()* mode argument.
|
|
|
|
"""
|
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
|
|
|
|
2020-10-04 03:45:55 +08:00
|
|
|
class SymbolTable:
|
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
|
|
|
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
|
|
|
|
2020-10-04 03:45:55 +08:00
|
|
|
if self._table.name == "top":
|
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):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return the type of the symbol table.
|
|
|
|
|
2021-06-13 10:47:44 +08:00
|
|
|
The values returned are 'class', 'module' and
|
2020-07-08 07:09:56 +08:00
|
|
|
'function'.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
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):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return an identifier for the table.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
return self._table.id
|
|
|
|
|
|
|
|
def get_name(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return the table's name.
|
|
|
|
|
|
|
|
This corresponds to the name of the class, function
|
|
|
|
or 'top' if the table is for a class, function or
|
|
|
|
global respectively.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
return self._table.name
|
|
|
|
|
|
|
|
def get_lineno(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return the number of the first line in the
|
|
|
|
block for the table.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
return self._table.lineno
|
|
|
|
|
|
|
|
def is_optimized(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return *True* if the locals in the table
|
|
|
|
are optimizable.
|
|
|
|
"""
|
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):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return *True* if the block is a nested class
|
|
|
|
or function."""
|
2001-03-23 07:32:22 +08:00
|
|
|
return bool(self._table.nested)
|
|
|
|
|
|
|
|
def has_children(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return *True* if the block has nested namespaces.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
return bool(self._table.children)
|
|
|
|
|
|
|
|
def get_identifiers(self):
|
2022-06-11 18:54:31 +08:00
|
|
|
"""Return a view object containing the names of symbols in the table.
|
2020-07-08 07:09:56 +08:00
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
return self._table.symbols.keys()
|
|
|
|
|
|
|
|
def lookup(self, name):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Lookup a *name* in the table.
|
|
|
|
|
|
|
|
Returns a *Symbol* instance.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
sym = self._symbols.get(name)
|
|
|
|
if sym is None:
|
|
|
|
flags = self._table.symbols[name]
|
|
|
|
namespaces = self.__check_children(name)
|
2020-10-04 03:45:55 +08:00
|
|
|
module_scope = (self._table.name == "top")
|
|
|
|
sym = self._symbols[name] = Symbol(name, flags, namespaces,
|
|
|
|
module_scope=module_scope)
|
2001-03-23 07:32:22 +08:00
|
|
|
return sym
|
|
|
|
|
|
|
|
def get_symbols(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return a list of *Symbol* instances for
|
|
|
|
names in the table.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
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):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return a list of the nested symbol tables.
|
|
|
|
"""
|
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
|
2018-10-20 08:46:00 +08:00
|
|
|
__nonlocals = None
|
2001-03-23 07:32:22 +08:00
|
|
|
|
|
|
|
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):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return a tuple of parameters to the function.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
if self.__params is None:
|
|
|
|
self.__params = self.__idents_matching(lambda x:x & DEF_PARAM)
|
|
|
|
return self.__params
|
|
|
|
|
|
|
|
def get_locals(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return a tuple of locals in the function.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
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):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return a tuple of globals in the function.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
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
|
|
|
|
|
2018-10-20 08:46:00 +08:00
|
|
|
def get_nonlocals(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return a tuple of nonlocals in the function.
|
|
|
|
"""
|
2018-10-20 08:46:00 +08:00
|
|
|
if self.__nonlocals is None:
|
|
|
|
self.__nonlocals = self.__idents_matching(lambda x:x & DEF_NONLOCAL)
|
|
|
|
return self.__nonlocals
|
|
|
|
|
2001-03-23 07:32:22 +08:00
|
|
|
def get_frees(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return a tuple of free variables in the function.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
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):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return a tuple of methods declared in the class.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
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
|
|
|
|
2020-10-04 03:45:55 +08:00
|
|
|
class Symbol:
|
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
|
|
|
|
2020-10-04 03:45:55 +08:00
|
|
|
def __init__(self, name, flags, namespaces=None, *, module_scope=False):
|
2001-03-23 07:32:22 +08:00
|
|
|
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 ()
|
2020-10-04 03:45:55 +08:00
|
|
|
self.__module_scope = module_scope
|
2001-03-23 07:32:22 +08:00
|
|
|
|
|
|
|
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):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return a name of a symbol.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
return self.__name
|
|
|
|
|
|
|
|
def is_referenced(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return *True* if the symbol is used in
|
|
|
|
its block.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
return bool(self.__flags & _symtable.USE)
|
|
|
|
|
|
|
|
def is_parameter(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return *True* if the symbol is a parameter.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
return bool(self.__flags & DEF_PARAM)
|
|
|
|
|
|
|
|
def is_global(self):
|
2021-06-13 10:47:44 +08:00
|
|
|
"""Return *True* if the symbol is global.
|
2020-07-08 07:09:56 +08:00
|
|
|
"""
|
2020-10-04 03:45:55 +08:00
|
|
|
return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
|
|
|
|
or (self.__module_scope and self.__flags & DEF_BOUND))
|
2001-03-23 07:32:22 +08:00
|
|
|
|
2018-10-20 08:46:00 +08:00
|
|
|
def is_nonlocal(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return *True* if the symbol is nonlocal."""
|
2018-10-20 08:46:00 +08:00
|
|
|
return bool(self.__flags & DEF_NONLOCAL)
|
|
|
|
|
2009-03-31 23:26:37 +08:00
|
|
|
def is_declared_global(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return *True* if the symbol is declared global
|
|
|
|
with a global statement."""
|
2009-03-31 23:26:37 +08:00
|
|
|
return bool(self.__scope == GLOBAL_EXPLICIT)
|
|
|
|
|
2001-03-23 07:32:22 +08:00
|
|
|
def is_local(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return *True* if the symbol is local.
|
|
|
|
"""
|
2020-10-04 03:45:55 +08:00
|
|
|
return bool(self.__scope in (LOCAL, CELL)
|
|
|
|
or (self.__module_scope and self.__flags & DEF_BOUND))
|
2001-03-23 07:32:22 +08:00
|
|
|
|
2016-09-09 11:50:03 +08:00
|
|
|
def is_annotated(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return *True* if the symbol is annotated.
|
|
|
|
"""
|
2016-09-09 11:50:03 +08:00
|
|
|
return bool(self.__flags & DEF_ANNOT)
|
|
|
|
|
2001-03-23 07:32:22 +08:00
|
|
|
def is_free(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return *True* if a referenced symbol is
|
|
|
|
not assigned to.
|
|
|
|
"""
|
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):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return *True* if the symbol is created from
|
|
|
|
an import statement.
|
|
|
|
"""
|
2001-03-23 07:32:22 +08:00
|
|
|
return bool(self.__flags & DEF_IMPORT)
|
|
|
|
|
|
|
|
def is_assigned(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return *True* if a symbol is assigned to."""
|
2001-03-23 07:32:22 +08:00
|
|
|
return bool(self.__flags & DEF_LOCAL)
|
|
|
|
|
|
|
|
def is_namespace(self):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Returns *True* if name binding introduces new namespace.
|
2001-03-23 07:32:22 +08:00
|
|
|
|
|
|
|
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):
|
2020-07-08 07:09:56 +08:00
|
|
|
"""Return the single namespace bound to this name.
|
2001-03-23 07:32:22 +08:00
|
|
|
|
2021-07-18 20:56:09 +08:00
|
|
|
Raises ValueError if the name is bound to multiple namespaces
|
|
|
|
or no namespace.
|
2001-03-23 07:32:22 +08:00
|
|
|
"""
|
2021-07-18 20:56:09 +08:00
|
|
|
if len(self.__namespaces) == 0:
|
|
|
|
raise ValueError("name is not bound to any namespaces")
|
|
|
|
elif len(self.__namespaces) > 1:
|
2007-08-30 09:19:48 +08:00
|
|
|
raise ValueError("name is bound to multiple namespaces")
|
2021-07-18 20:56:09 +08:00
|
|
|
else:
|
|
|
|
return self.__namespaces[0]
|
2001-03-23 07:32:22 +08:00
|
|
|
|
|
|
|
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())
|