2001-03-22 19:32:22 -04:00
|
|
|
"""Interface to the compiler's internal symbol tables"""
|
|
|
|
|
|
|
|
import _symtable
|
2024-06-04 11:24:22 -03:00
|
|
|
from _symtable import (
|
|
|
|
USE,
|
|
|
|
DEF_GLOBAL, DEF_NONLOCAL, DEF_LOCAL,
|
2024-06-12 08:14:50 -03:00
|
|
|
DEF_PARAM, DEF_TYPE_PARAM,
|
|
|
|
DEF_FREE_CLASS,
|
|
|
|
DEF_IMPORT, DEF_BOUND, DEF_ANNOT,
|
|
|
|
DEF_COMP_ITER, DEF_COMP_CELL,
|
2024-06-04 11:24:22 -03:00
|
|
|
SCOPE_OFF, SCOPE_MASK,
|
|
|
|
FREE, LOCAL, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL
|
|
|
|
)
|
2001-03-22 19:32:22 -04:00
|
|
|
|
|
|
|
import weakref
|
|
|
|
|
2008-08-19 23:33:00 -03:00
|
|
|
__all__ = ["symtable", "SymbolTable", "Class", "Function", "Symbol"]
|
2001-03-22 19:32:22 -04:00
|
|
|
|
|
|
|
def symtable(code, filename, compile_type):
|
2020-07-07 20:09:56 -03: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-26 14:13:51 -03:00
|
|
|
top = _symtable.symtable(code, filename, compile_type)
|
2008-08-19 23:33:00 -03:00
|
|
|
return _newSymbolTable(top, filename)
|
2001-03-22 19:32:22 -04: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-19 23:33:00 -03:00
|
|
|
_newSymbolTable = SymbolTableFactory()
|
2001-03-29 00:36:09 -04:00
|
|
|
|
2001-03-22 19:32:22 -04:00
|
|
|
|
2020-10-03 16:45:55 -03: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-17 15:02:44 -03:00
|
|
|
|
2001-03-22 19:32:22 -04: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 00:36:09 -04:00
|
|
|
|
2020-10-03 16:45:55 -03: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-17 15:02:44 -03:00
|
|
|
return "<{0}SymbolTable for module {1}>".format(kind, self._filename)
|
2001-03-22 19:32:22 -04: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-17 15:02:44 -03:00
|
|
|
return "<{0}SymbolTable for {1} in {2}>".format(kind,
|
|
|
|
self._table.name,
|
|
|
|
self._filename)
|
2001-03-22 19:32:22 -04:00
|
|
|
|
|
|
|
def get_type(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return the type of the symbol table.
|
|
|
|
|
2023-09-28 23:08:04 -03:00
|
|
|
The values returned are 'class', 'module', 'function',
|
|
|
|
'annotation', 'TypeVar bound', 'type alias', and 'type parameter'.
|
2020-07-07 20:09:56 -03:00
|
|
|
"""
|
2001-03-22 19:32:22 -04: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"
|
2023-09-28 23:08:04 -03:00
|
|
|
if self._table.type == _symtable.TYPE_ANNOTATION:
|
|
|
|
return "annotation"
|
|
|
|
if self._table.type == _symtable.TYPE_TYPE_VAR_BOUND:
|
|
|
|
return "TypeVar bound"
|
|
|
|
if self._table.type == _symtable.TYPE_TYPE_ALIAS:
|
|
|
|
return "type alias"
|
|
|
|
if self._table.type == _symtable.TYPE_TYPE_PARAM:
|
|
|
|
return "type parameter"
|
|
|
|
assert False, f"unexpected type: {self._table.type}"
|
2001-03-22 19:32:22 -04:00
|
|
|
|
|
|
|
def get_id(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return an identifier for the table.
|
|
|
|
"""
|
2001-03-22 19:32:22 -04:00
|
|
|
return self._table.id
|
|
|
|
|
|
|
|
def get_name(self):
|
2020-07-07 20:09:56 -03: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-22 19:32:22 -04:00
|
|
|
return self._table.name
|
|
|
|
|
|
|
|
def get_lineno(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return the number of the first line in the
|
|
|
|
block for the table.
|
|
|
|
"""
|
2001-03-22 19:32:22 -04:00
|
|
|
return self._table.lineno
|
|
|
|
|
|
|
|
def is_optimized(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return *True* if the locals in the table
|
|
|
|
are optimizable.
|
|
|
|
"""
|
2015-04-27 22:44:22 -03:00
|
|
|
return bool(self._table.type == _symtable.TYPE_FUNCTION)
|
2001-03-22 19:32:22 -04:00
|
|
|
|
|
|
|
def is_nested(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return *True* if the block is a nested class
|
|
|
|
or function."""
|
2001-03-22 19:32:22 -04:00
|
|
|
return bool(self._table.nested)
|
|
|
|
|
|
|
|
def has_children(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return *True* if the block has nested namespaces.
|
|
|
|
"""
|
2001-03-22 19:32:22 -04:00
|
|
|
return bool(self._table.children)
|
|
|
|
|
|
|
|
def get_identifiers(self):
|
2022-06-11 07:54:31 -03:00
|
|
|
"""Return a view object containing the names of symbols in the table.
|
2020-07-07 20:09:56 -03:00
|
|
|
"""
|
2001-03-22 19:32:22 -04:00
|
|
|
return self._table.symbols.keys()
|
|
|
|
|
|
|
|
def lookup(self, name):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Lookup a *name* in the table.
|
|
|
|
|
|
|
|
Returns a *Symbol* instance.
|
|
|
|
"""
|
2001-03-22 19:32:22 -04:00
|
|
|
sym = self._symbols.get(name)
|
|
|
|
if sym is None:
|
|
|
|
flags = self._table.symbols[name]
|
|
|
|
namespaces = self.__check_children(name)
|
2020-10-03 16:45:55 -03:00
|
|
|
module_scope = (self._table.name == "top")
|
|
|
|
sym = self._symbols[name] = Symbol(name, flags, namespaces,
|
|
|
|
module_scope=module_scope)
|
2001-03-22 19:32:22 -04:00
|
|
|
return sym
|
|
|
|
|
|
|
|
def get_symbols(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return a list of *Symbol* instances for
|
|
|
|
names in the table.
|
|
|
|
"""
|
2001-03-22 19:32:22 -04:00
|
|
|
return [self.lookup(ident) for ident in self.get_identifiers()]
|
|
|
|
|
|
|
|
def __check_children(self, name):
|
2008-08-19 23:33:00 -03:00
|
|
|
return [_newSymbolTable(st, self._filename)
|
2001-03-22 19:32:22 -04:00
|
|
|
for st in self._table.children
|
|
|
|
if st.name == name]
|
|
|
|
|
2001-03-23 11:41:14 -04:00
|
|
|
def get_children(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return a list of the nested symbol tables.
|
|
|
|
"""
|
2008-08-19 23:33:00 -03:00
|
|
|
return [_newSymbolTable(st, self._filename)
|
2001-03-23 11:41:14 -04: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-17 15:02:44 -03:00
|
|
|
|
2024-06-12 08:14:50 -03:00
|
|
|
def _get_scope(flags): # like _PyST_GetScope()
|
|
|
|
return (flags >> SCOPE_OFF) & SCOPE_MASK
|
|
|
|
|
|
|
|
|
2001-03-22 19:32:22 -04:00
|
|
|
class Function(SymbolTable):
|
|
|
|
|
|
|
|
# Default values for instance variables
|
|
|
|
__params = None
|
|
|
|
__locals = None
|
|
|
|
__frees = None
|
|
|
|
__globals = None
|
2018-10-19 21:46:00 -03:00
|
|
|
__nonlocals = None
|
2001-03-22 19:32:22 -04:00
|
|
|
|
|
|
|
def __idents_matching(self, test_func):
|
2017-05-18 11:35:54 -03:00
|
|
|
return tuple(ident for ident in self.get_identifiers()
|
|
|
|
if test_func(self._table.symbols[ident]))
|
2001-03-22 19:32:22 -04:00
|
|
|
|
|
|
|
def get_parameters(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return a tuple of parameters to the function.
|
|
|
|
"""
|
2001-03-22 19:32:22 -04: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-07 20:09:56 -03:00
|
|
|
"""Return a tuple of locals in the function.
|
|
|
|
"""
|
2001-03-22 19:32:22 -04:00
|
|
|
if self.__locals is None:
|
2009-06-28 16:30:36 -03:00
|
|
|
locs = (LOCAL, CELL)
|
2024-06-12 08:14:50 -03:00
|
|
|
test = lambda x: _get_scope(x) 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-28 16:19:51 -03:00
|
|
|
self.__locals = self.__idents_matching(test)
|
2001-03-22 19:32:22 -04:00
|
|
|
return self.__locals
|
2001-03-29 00:36:09 -04:00
|
|
|
|
2001-03-22 19:32:22 -04:00
|
|
|
def get_globals(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return a tuple of globals in the function.
|
|
|
|
"""
|
2001-03-22 19:32:22 -04: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-17 15:02:44 -03:00
|
|
|
glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
|
2024-06-12 08:14:50 -03:00
|
|
|
test = lambda x: _get_scope(x) in glob
|
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-17 15:02:44 -03:00
|
|
|
self.__globals = self.__idents_matching(test)
|
2001-03-22 19:32:22 -04:00
|
|
|
return self.__globals
|
|
|
|
|
2018-10-19 21:46:00 -03:00
|
|
|
def get_nonlocals(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return a tuple of nonlocals in the function.
|
|
|
|
"""
|
2018-10-19 21:46:00 -03:00
|
|
|
if self.__nonlocals is None:
|
|
|
|
self.__nonlocals = self.__idents_matching(lambda x:x & DEF_NONLOCAL)
|
|
|
|
return self.__nonlocals
|
|
|
|
|
2001-03-22 19:32:22 -04:00
|
|
|
def get_frees(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return a tuple of free variables in the function.
|
|
|
|
"""
|
2001-03-22 19:32:22 -04:00
|
|
|
if self.__frees is None:
|
2024-06-12 08:14:50 -03:00
|
|
|
is_free = lambda x: _get_scope(x) == FREE
|
2001-03-22 19:32:22 -04: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-17 15:02:44 -03:00
|
|
|
|
2001-03-22 19:32:22 -04:00
|
|
|
class Class(SymbolTable):
|
|
|
|
|
|
|
|
__methods = None
|
|
|
|
|
|
|
|
def get_methods(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return a tuple of methods declared in the class.
|
|
|
|
"""
|
2001-03-22 19:32:22 -04:00
|
|
|
if self.__methods is None:
|
|
|
|
d = {}
|
|
|
|
for st in self._table.children:
|
2024-06-11 10:06:49 -03:00
|
|
|
if st.type == _symtable.TYPE_ANNOTATION:
|
|
|
|
continue
|
2001-03-22 19:32:22 -04:00
|
|
|
d[st.name] = 1
|
2008-08-20 09:55:31 -03:00
|
|
|
self.__methods = tuple(d)
|
2001-03-22 19:32:22 -04: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-17 15:02:44 -03:00
|
|
|
|
2020-10-03 16:45:55 -03: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-17 15:02:44 -03:00
|
|
|
|
2020-10-03 16:45:55 -03:00
|
|
|
def __init__(self, name, flags, namespaces=None, *, module_scope=False):
|
2001-03-22 19:32:22 -04:00
|
|
|
self.__name = name
|
|
|
|
self.__flags = flags
|
2024-06-12 08:14:50 -03:00
|
|
|
self.__scope = _get_scope(flags)
|
2001-03-22 19:32:22 -04:00
|
|
|
self.__namespaces = namespaces or ()
|
2020-10-03 16:45:55 -03:00
|
|
|
self.__module_scope = module_scope
|
2001-03-22 19:32:22 -04:00
|
|
|
|
|
|
|
def __repr__(self):
|
2023-11-07 12:32:16 -04:00
|
|
|
flags_str = '|'.join(self._flags_str())
|
|
|
|
return f'<symbol {self.__name!r}: {self._scope_str()}, {flags_str}>'
|
|
|
|
|
|
|
|
def _scope_str(self):
|
|
|
|
return _scopes_value_to_name.get(self.__scope) or str(self.__scope)
|
|
|
|
|
|
|
|
def _flags_str(self):
|
|
|
|
for flagname, flagvalue in _flags:
|
|
|
|
if self.__flags & flagvalue == flagvalue:
|
|
|
|
yield flagname
|
2001-03-22 19:32:22 -04:00
|
|
|
|
|
|
|
def get_name(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return a name of a symbol.
|
|
|
|
"""
|
2001-03-22 19:32:22 -04:00
|
|
|
return self.__name
|
|
|
|
|
|
|
|
def is_referenced(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return *True* if the symbol is used in
|
|
|
|
its block.
|
|
|
|
"""
|
2024-06-04 11:24:22 -03:00
|
|
|
return bool(self.__flags & USE)
|
2001-03-22 19:32:22 -04:00
|
|
|
|
|
|
|
def is_parameter(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return *True* if the symbol is a parameter.
|
|
|
|
"""
|
2001-03-22 19:32:22 -04:00
|
|
|
return bool(self.__flags & DEF_PARAM)
|
|
|
|
|
2024-06-04 11:24:22 -03:00
|
|
|
def is_type_parameter(self):
|
|
|
|
"""Return *True* if the symbol is a type parameter.
|
|
|
|
"""
|
|
|
|
return bool(self.__flags & DEF_TYPE_PARAM)
|
|
|
|
|
2001-03-22 19:32:22 -04:00
|
|
|
def is_global(self):
|
2021-06-12 23:47:44 -03:00
|
|
|
"""Return *True* if the symbol is global.
|
2020-07-07 20:09:56 -03:00
|
|
|
"""
|
2020-10-03 16:45:55 -03:00
|
|
|
return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
|
|
|
|
or (self.__module_scope and self.__flags & DEF_BOUND))
|
2001-03-22 19:32:22 -04:00
|
|
|
|
2018-10-19 21:46:00 -03:00
|
|
|
def is_nonlocal(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return *True* if the symbol is nonlocal."""
|
2018-10-19 21:46:00 -03:00
|
|
|
return bool(self.__flags & DEF_NONLOCAL)
|
|
|
|
|
2009-03-31 12:26:37 -03:00
|
|
|
def is_declared_global(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return *True* if the symbol is declared global
|
|
|
|
with a global statement."""
|
2009-03-31 12:26:37 -03:00
|
|
|
return bool(self.__scope == GLOBAL_EXPLICIT)
|
|
|
|
|
2001-03-22 19:32:22 -04:00
|
|
|
def is_local(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return *True* if the symbol is local.
|
|
|
|
"""
|
2020-10-03 16:45:55 -03:00
|
|
|
return bool(self.__scope in (LOCAL, CELL)
|
|
|
|
or (self.__module_scope and self.__flags & DEF_BOUND))
|
2001-03-22 19:32:22 -04:00
|
|
|
|
2016-09-09 00:50:03 -03:00
|
|
|
def is_annotated(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return *True* if the symbol is annotated.
|
|
|
|
"""
|
2016-09-09 00:50:03 -03:00
|
|
|
return bool(self.__flags & DEF_ANNOT)
|
|
|
|
|
2001-03-22 19:32:22 -04:00
|
|
|
def is_free(self):
|
2020-07-07 20:09:56 -03: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-17 15:02:44 -03:00
|
|
|
return bool(self.__scope == FREE)
|
2001-03-22 19:32:22 -04:00
|
|
|
|
2024-06-12 08:14:50 -03:00
|
|
|
def is_free_class(self):
|
|
|
|
"""Return *True* if a class-scoped symbol is free from
|
|
|
|
the perspective of a method."""
|
|
|
|
return bool(self.__flags & DEF_FREE_CLASS)
|
|
|
|
|
2001-03-22 19:32:22 -04:00
|
|
|
def is_imported(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return *True* if the symbol is created from
|
|
|
|
an import statement.
|
|
|
|
"""
|
2001-03-22 19:32:22 -04:00
|
|
|
return bool(self.__flags & DEF_IMPORT)
|
|
|
|
|
|
|
|
def is_assigned(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Return *True* if a symbol is assigned to."""
|
2001-03-22 19:32:22 -04:00
|
|
|
return bool(self.__flags & DEF_LOCAL)
|
|
|
|
|
2024-06-12 08:14:50 -03:00
|
|
|
def is_comp_iter(self):
|
|
|
|
"""Return *True* if the symbol is a comprehension iteration variable.
|
|
|
|
"""
|
|
|
|
return bool(self.__flags & DEF_COMP_ITER)
|
|
|
|
|
|
|
|
def is_comp_cell(self):
|
|
|
|
"""Return *True* if the symbol is a cell in an inlined comprehension.
|
|
|
|
"""
|
|
|
|
return bool(self.__flags & DEF_COMP_CELL)
|
|
|
|
|
2001-03-22 19:32:22 -04:00
|
|
|
def is_namespace(self):
|
2020-07-07 20:09:56 -03:00
|
|
|
"""Returns *True* if name binding introduces new namespace.
|
2001-03-22 19:32:22 -04: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-07 20:09:56 -03:00
|
|
|
"""Return the single namespace bound to this name.
|
2001-03-22 19:32:22 -04:00
|
|
|
|
2021-07-18 09:56:09 -03:00
|
|
|
Raises ValueError if the name is bound to multiple namespaces
|
|
|
|
or no namespace.
|
2001-03-22 19:32:22 -04:00
|
|
|
"""
|
2021-07-18 09:56:09 -03:00
|
|
|
if len(self.__namespaces) == 0:
|
|
|
|
raise ValueError("name is not bound to any namespaces")
|
|
|
|
elif len(self.__namespaces) > 1:
|
2007-08-29 22:19:48 -03:00
|
|
|
raise ValueError("name is bound to multiple namespaces")
|
2021-07-18 09:56:09 -03:00
|
|
|
else:
|
|
|
|
return self.__namespaces[0]
|
2001-03-22 19:32:22 -04:00
|
|
|
|
2023-11-07 12:32:16 -04:00
|
|
|
|
|
|
|
_flags = [('USE', USE)]
|
|
|
|
_flags.extend(kv for kv in globals().items() if kv[0].startswith('DEF_'))
|
|
|
|
_scopes_names = ('FREE', 'LOCAL', 'GLOBAL_IMPLICIT', 'GLOBAL_EXPLICIT', 'CELL')
|
|
|
|
_scopes_value_to_name = {globals()[n]: n for n in _scopes_names}
|
|
|
|
|
|
|
|
|
|
|
|
def main(args):
|
|
|
|
import sys
|
|
|
|
def print_symbols(table, level=0):
|
|
|
|
indent = ' ' * level
|
|
|
|
nested = "nested " if table.is_nested() else ""
|
|
|
|
if table.get_type() == 'module':
|
|
|
|
what = f'from file {table._filename!r}'
|
|
|
|
else:
|
|
|
|
what = f'{table.get_name()!r}'
|
|
|
|
print(f'{indent}symbol table for {nested}{table.get_type()} {what}:')
|
|
|
|
for ident in table.get_identifiers():
|
|
|
|
symbol = table.lookup(ident)
|
|
|
|
flags = ', '.join(symbol._flags_str()).lower()
|
|
|
|
print(f' {indent}{symbol._scope_str().lower()} symbol {symbol.get_name()!r}: {flags}')
|
|
|
|
print()
|
|
|
|
|
|
|
|
for table2 in table.get_children():
|
|
|
|
print_symbols(table2, level + 1)
|
|
|
|
|
|
|
|
for filename in args or ['-']:
|
|
|
|
if filename == '-':
|
|
|
|
src = sys.stdin.read()
|
|
|
|
filename = '<stdin>'
|
|
|
|
else:
|
|
|
|
with open(filename, 'rb') as f:
|
|
|
|
src = f.read()
|
|
|
|
mod = symtable(src, filename, 'exec')
|
|
|
|
print_symbols(mod)
|
|
|
|
|
|
|
|
|
2001-03-22 19:32:22 -04:00
|
|
|
if __name__ == "__main__":
|
2023-11-07 12:32:16 -04:00
|
|
|
import sys
|
|
|
|
main(sys.argv[1:])
|