Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/python27/lib64/python2....
File: symtable.py
"""Interface to the compiler's internal symbol tables"""
[0] Fix | Delete
[1] Fix | Delete
import _symtable
[2] Fix | Delete
from _symtable import (USE, DEF_GLOBAL, DEF_LOCAL, DEF_PARAM,
[3] Fix | Delete
DEF_IMPORT, DEF_BOUND, OPT_IMPORT_STAR, OPT_EXEC, OPT_BARE_EXEC,
[4] Fix | Delete
SCOPE_OFF, SCOPE_MASK, FREE, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL, LOCAL)
[5] Fix | Delete
[6] Fix | Delete
import weakref
[7] Fix | Delete
[8] Fix | Delete
__all__ = ["symtable", "SymbolTable", "Class", "Function", "Symbol"]
[9] Fix | Delete
[10] Fix | Delete
def symtable(code, filename, compile_type):
[11] Fix | Delete
top = _symtable.symtable(code, filename, compile_type)
[12] Fix | Delete
return _newSymbolTable(top, filename)
[13] Fix | Delete
[14] Fix | Delete
class SymbolTableFactory:
[15] Fix | Delete
def __init__(self):
[16] Fix | Delete
self.__memo = weakref.WeakValueDictionary()
[17] Fix | Delete
[18] Fix | Delete
def new(self, table, filename):
[19] Fix | Delete
if table.type == _symtable.TYPE_FUNCTION:
[20] Fix | Delete
return Function(table, filename)
[21] Fix | Delete
if table.type == _symtable.TYPE_CLASS:
[22] Fix | Delete
return Class(table, filename)
[23] Fix | Delete
return SymbolTable(table, filename)
[24] Fix | Delete
[25] Fix | Delete
def __call__(self, table, filename):
[26] Fix | Delete
key = table, filename
[27] Fix | Delete
obj = self.__memo.get(key, None)
[28] Fix | Delete
if obj is None:
[29] Fix | Delete
obj = self.__memo[key] = self.new(table, filename)
[30] Fix | Delete
return obj
[31] Fix | Delete
[32] Fix | Delete
_newSymbolTable = SymbolTableFactory()
[33] Fix | Delete
[34] Fix | Delete
[35] Fix | Delete
class SymbolTable(object):
[36] Fix | Delete
[37] Fix | Delete
def __init__(self, raw_table, filename):
[38] Fix | Delete
self._table = raw_table
[39] Fix | Delete
self._filename = filename
[40] Fix | Delete
self._symbols = {}
[41] Fix | Delete
[42] Fix | Delete
def __repr__(self):
[43] Fix | Delete
if self.__class__ == SymbolTable:
[44] Fix | Delete
kind = ""
[45] Fix | Delete
else:
[46] Fix | Delete
kind = "%s " % self.__class__.__name__
[47] Fix | Delete
[48] Fix | Delete
if self._table.name == "global":
[49] Fix | Delete
return "<{0}SymbolTable for module {1}>".format(kind, self._filename)
[50] Fix | Delete
else:
[51] Fix | Delete
return "<{0}SymbolTable for {1} in {2}>".format(kind,
[52] Fix | Delete
self._table.name,
[53] Fix | Delete
self._filename)
[54] Fix | Delete
[55] Fix | Delete
def get_type(self):
[56] Fix | Delete
if self._table.type == _symtable.TYPE_MODULE:
[57] Fix | Delete
return "module"
[58] Fix | Delete
if self._table.type == _symtable.TYPE_FUNCTION:
[59] Fix | Delete
return "function"
[60] Fix | Delete
if self._table.type == _symtable.TYPE_CLASS:
[61] Fix | Delete
return "class"
[62] Fix | Delete
assert self._table.type in (1, 2, 3), \
[63] Fix | Delete
"unexpected type: {0}".format(self._table.type)
[64] Fix | Delete
[65] Fix | Delete
def get_id(self):
[66] Fix | Delete
return self._table.id
[67] Fix | Delete
[68] Fix | Delete
def get_name(self):
[69] Fix | Delete
return self._table.name
[70] Fix | Delete
[71] Fix | Delete
def get_lineno(self):
[72] Fix | Delete
return self._table.lineno
[73] Fix | Delete
[74] Fix | Delete
def is_optimized(self):
[75] Fix | Delete
return bool(self._table.type == _symtable.TYPE_FUNCTION
[76] Fix | Delete
and not self._table.optimized)
[77] Fix | Delete
[78] Fix | Delete
def is_nested(self):
[79] Fix | Delete
return bool(self._table.nested)
[80] Fix | Delete
[81] Fix | Delete
def has_children(self):
[82] Fix | Delete
return bool(self._table.children)
[83] Fix | Delete
[84] Fix | Delete
def has_exec(self):
[85] Fix | Delete
"""Return true if the scope uses exec"""
[86] Fix | Delete
return bool(self._table.optimized & (OPT_EXEC | OPT_BARE_EXEC))
[87] Fix | Delete
[88] Fix | Delete
def has_import_star(self):
[89] Fix | Delete
"""Return true if the scope uses import *"""
[90] Fix | Delete
return bool(self._table.optimized & OPT_IMPORT_STAR)
[91] Fix | Delete
[92] Fix | Delete
def get_identifiers(self):
[93] Fix | Delete
return self._table.symbols.keys()
[94] Fix | Delete
[95] Fix | Delete
def lookup(self, name):
[96] Fix | Delete
sym = self._symbols.get(name)
[97] Fix | Delete
if sym is None:
[98] Fix | Delete
flags = self._table.symbols[name]
[99] Fix | Delete
namespaces = self.__check_children(name)
[100] Fix | Delete
sym = self._symbols[name] = Symbol(name, flags, namespaces)
[101] Fix | Delete
return sym
[102] Fix | Delete
[103] Fix | Delete
def get_symbols(self):
[104] Fix | Delete
return [self.lookup(ident) for ident in self.get_identifiers()]
[105] Fix | Delete
[106] Fix | Delete
def __check_children(self, name):
[107] Fix | Delete
return [_newSymbolTable(st, self._filename)
[108] Fix | Delete
for st in self._table.children
[109] Fix | Delete
if st.name == name]
[110] Fix | Delete
[111] Fix | Delete
def get_children(self):
[112] Fix | Delete
return [_newSymbolTable(st, self._filename)
[113] Fix | Delete
for st in self._table.children]
[114] Fix | Delete
[115] Fix | Delete
[116] Fix | Delete
class Function(SymbolTable):
[117] Fix | Delete
[118] Fix | Delete
# Default values for instance variables
[119] Fix | Delete
__params = None
[120] Fix | Delete
__locals = None
[121] Fix | Delete
__frees = None
[122] Fix | Delete
__globals = None
[123] Fix | Delete
[124] Fix | Delete
def __idents_matching(self, test_func):
[125] Fix | Delete
return tuple([ident for ident in self.get_identifiers()
[126] Fix | Delete
if test_func(self._table.symbols[ident])])
[127] Fix | Delete
[128] Fix | Delete
def get_parameters(self):
[129] Fix | Delete
if self.__params is None:
[130] Fix | Delete
self.__params = self.__idents_matching(lambda x:x & DEF_PARAM)
[131] Fix | Delete
return self.__params
[132] Fix | Delete
[133] Fix | Delete
def get_locals(self):
[134] Fix | Delete
if self.__locals is None:
[135] Fix | Delete
locs = (LOCAL, CELL)
[136] Fix | Delete
test = lambda x: ((x >> SCOPE_OFF) & SCOPE_MASK) in locs
[137] Fix | Delete
self.__locals = self.__idents_matching(test)
[138] Fix | Delete
return self.__locals
[139] Fix | Delete
[140] Fix | Delete
def get_globals(self):
[141] Fix | Delete
if self.__globals is None:
[142] Fix | Delete
glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
[143] Fix | Delete
test = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) in glob
[144] Fix | Delete
self.__globals = self.__idents_matching(test)
[145] Fix | Delete
return self.__globals
[146] Fix | Delete
[147] Fix | Delete
def get_frees(self):
[148] Fix | Delete
if self.__frees is None:
[149] Fix | Delete
is_free = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) == FREE
[150] Fix | Delete
self.__frees = self.__idents_matching(is_free)
[151] Fix | Delete
return self.__frees
[152] Fix | Delete
[153] Fix | Delete
[154] Fix | Delete
class Class(SymbolTable):
[155] Fix | Delete
[156] Fix | Delete
__methods = None
[157] Fix | Delete
[158] Fix | Delete
def get_methods(self):
[159] Fix | Delete
if self.__methods is None:
[160] Fix | Delete
d = {}
[161] Fix | Delete
for st in self._table.children:
[162] Fix | Delete
d[st.name] = 1
[163] Fix | Delete
self.__methods = tuple(d)
[164] Fix | Delete
return self.__methods
[165] Fix | Delete
[166] Fix | Delete
[167] Fix | Delete
class Symbol(object):
[168] Fix | Delete
[169] Fix | Delete
def __init__(self, name, flags, namespaces=None):
[170] Fix | Delete
self.__name = name
[171] Fix | Delete
self.__flags = flags
[172] Fix | Delete
self.__scope = (flags >> SCOPE_OFF) & SCOPE_MASK # like PyST_GetScope()
[173] Fix | Delete
self.__namespaces = namespaces or ()
[174] Fix | Delete
[175] Fix | Delete
def __repr__(self):
[176] Fix | Delete
return "<symbol {0!r}>".format(self.__name)
[177] Fix | Delete
[178] Fix | Delete
def get_name(self):
[179] Fix | Delete
return self.__name
[180] Fix | Delete
[181] Fix | Delete
def is_referenced(self):
[182] Fix | Delete
return bool(self.__flags & _symtable.USE)
[183] Fix | Delete
[184] Fix | Delete
def is_parameter(self):
[185] Fix | Delete
return bool(self.__flags & DEF_PARAM)
[186] Fix | Delete
[187] Fix | Delete
def is_global(self):
[188] Fix | Delete
return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT))
[189] Fix | Delete
[190] Fix | Delete
def is_declared_global(self):
[191] Fix | Delete
return bool(self.__scope == GLOBAL_EXPLICIT)
[192] Fix | Delete
[193] Fix | Delete
def is_local(self):
[194] Fix | Delete
return bool(self.__flags & DEF_BOUND)
[195] Fix | Delete
[196] Fix | Delete
def is_free(self):
[197] Fix | Delete
return bool(self.__scope == FREE)
[198] Fix | Delete
[199] Fix | Delete
def is_imported(self):
[200] Fix | Delete
return bool(self.__flags & DEF_IMPORT)
[201] Fix | Delete
[202] Fix | Delete
def is_assigned(self):
[203] Fix | Delete
return bool(self.__flags & DEF_LOCAL)
[204] Fix | Delete
[205] Fix | Delete
def is_namespace(self):
[206] Fix | Delete
"""Returns true if name binding introduces new namespace.
[207] Fix | Delete
[208] Fix | Delete
If the name is used as the target of a function or class
[209] Fix | Delete
statement, this will be true.
[210] Fix | Delete
[211] Fix | Delete
Note that a single name can be bound to multiple objects. If
[212] Fix | Delete
is_namespace() is true, the name may also be bound to other
[213] Fix | Delete
objects, like an int or list, that does not introduce a new
[214] Fix | Delete
namespace.
[215] Fix | Delete
"""
[216] Fix | Delete
return bool(self.__namespaces)
[217] Fix | Delete
[218] Fix | Delete
def get_namespaces(self):
[219] Fix | Delete
"""Return a list of namespaces bound to this name"""
[220] Fix | Delete
return self.__namespaces
[221] Fix | Delete
[222] Fix | Delete
def get_namespace(self):
[223] Fix | Delete
"""Returns the single namespace bound to this name.
[224] Fix | Delete
[225] Fix | Delete
Raises ValueError if the name is bound to multiple namespaces.
[226] Fix | Delete
"""
[227] Fix | Delete
if len(self.__namespaces) != 1:
[228] Fix | Delete
raise ValueError, "name is bound to multiple namespaces"
[229] Fix | Delete
return self.__namespaces[0]
[230] Fix | Delete
[231] Fix | Delete
if __name__ == "__main__":
[232] Fix | Delete
import os, sys
[233] Fix | Delete
src = open(sys.argv[0]).read()
[234] Fix | Delete
mod = symtable(src, os.path.split(sys.argv[0])[1], "exec")
[235] Fix | Delete
for ident in mod.get_identifiers():
[236] Fix | Delete
info = mod.lookup(ident)
[237] Fix | Delete
print info, info.is_local(), info.is_namespace()
[238] Fix | Delete
[239] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function