diff options
author | Benjamin Peterson <benjamin@python.org> | 2008-08-17 18:02:44 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2008-08-17 18:02:44 (GMT) |
commit | 55e00f279f6e662f0f7cfc53398fa854d9666e09 (patch) | |
tree | ea6b31b08ddcc6011d969e69feb52888e2890510 /Lib/symtable.py | |
parent | 8ba92f65021900ac088aed3f65f8650efab5d416 (diff) | |
download | cpython-55e00f279f6e662f0f7cfc53398fa854d9666e09.zip cpython-55e00f279f6e662f0f7cfc53398fa854d9666e09.tar.gz cpython-55e00f279f6e662f0f7cfc53398fa854d9666e09.tar.bz2 |
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
........
Diffstat (limited to 'Lib/symtable.py')
-rw-r--r-- | Lib/symtable.py | 53 |
1 files changed, 25 insertions, 28 deletions
diff --git a/Lib/symtable.py b/Lib/symtable.py index 089c8dc..ff332f2 100644 --- a/Lib/symtable.py +++ b/Lib/symtable.py @@ -1,10 +1,11 @@ """Interface to the compiler's internal symbol tables""" import _symtable -from _symtable import USE, DEF_GLOBAL, DEF_LOCAL, DEF_PARAM, \ - DEF_STAR, DEF_DOUBLESTAR, DEF_INTUPLE, DEF_FREE, \ - DEF_FREE_GLOBAL, DEF_FREE_CLASS, DEF_IMPORT, DEF_BOUND, \ - OPT_IMPORT_STAR +from _symtable import (USE, DEF_GLOBAL, DEF_LOCAL, DEF_PARAM, + DEF_STAR, DEF_DOUBLESTAR, DEF_INTUPLE, DEF_FREE, + DEF_FREE_GLOBAL, DEF_FREE_CLASS, DEF_IMPORT, DEF_BOUND, + OPT_IMPORT_STAR, SCOPE_OFF, SCOPE_MASK, FREE, + GLOBAL_IMPLICIT, GLOBAL_EXPLICIT) import weakref @@ -38,15 +39,9 @@ class SymbolTableFactory: newSymbolTable = SymbolTableFactory() -def is_free(flags): - if (flags & (USE | DEF_FREE)) \ - and (flags & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)): - return True - if flags & DEF_FREE_CLASS: - return True - return False -class SymbolTable: +class SymbolTable(object): + def __init__(self, raw_table, filename): self._table = raw_table self._filename = filename @@ -59,10 +54,11 @@ class SymbolTable: kind = "%s " % self.__class__.__name__ if self._table.name == "global": - return "<%sSymbolTable for module %s>" % (kind, self._filename) + return "<{0}SymbolTable for module {1}>".format(kind, self._filename) else: - return "<%sSymbolTable for %s in %s>" % (kind, self._table.name, - self._filename) + return "<{0}SymbolTable for {1} in {2}>".format(kind, + self._table.name, + self._filename) def get_type(self): if self._table.type == _symtable.TYPE_MODULE: @@ -72,7 +68,7 @@ class SymbolTable: if self._table.type == _symtable.TYPE_CLASS: return "class" assert self._table.type in (1, 2, 3), \ - "unexpected type: %s" % self._table.type + "unexpected type: {0}".format(self._table.type) def get_id(self): return self._table.id @@ -124,6 +120,7 @@ class SymbolTable: return [newSymbolTable(st, self._filename) for st in self._table.children] + class Function(SymbolTable): # Default values for instance variables @@ -148,15 +145,18 @@ class Function(SymbolTable): def get_globals(self): if self.__globals is None: - glob = DEF_GLOBAL | DEF_FREE_GLOBAL - self.__globals = self.__idents_matching(lambda x:x & glob) + glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT) + test = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) in glob + self.__globals = self.__idents_matching(test) return self.__globals def get_frees(self): if self.__frees is None: + is_free = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) == FREE self.__frees = self.__idents_matching(is_free) return self.__frees + class Class(SymbolTable): __methods = None @@ -169,14 +169,17 @@ class Class(SymbolTable): self.__methods = tuple(d) return self.__methods -class Symbol: + +class Symbol(object): + def __init__(self, name, flags, namespaces=None): self.__name = name self.__flags = flags + self.__scope = (flags >> SCOPE_OFF) & SCOPE_MASK # like PyST_GetScope() self.__namespaces = namespaces or () def __repr__(self): - return "<symbol '%s'>" % self.__name + return "<symbol {0!r}>".format(self.__name) def get_name(self): return self.__name @@ -188,8 +191,7 @@ class Symbol: return bool(self.__flags & DEF_PARAM) def is_global(self): - return bool((self.__flags & DEF_GLOBAL) - or (self.__flags & DEF_FREE_GLOBAL)) + return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)) def is_vararg(self): return bool(self.__flags & DEF_STAR) @@ -201,12 +203,7 @@ class Symbol: return bool(self.__flags & DEF_BOUND) def is_free(self): - if (self.__flags & (USE | DEF_FREE)) \ - and (self.__flags & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)): - return True - if self.__flags & DEF_FREE_CLASS: - return True - return False + return bool(self.__scope == FREE) def is_imported(self): return bool(self.__flags & DEF_IMPORT) |