Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python3....
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, DEF_ANNOT, SCOPE_OFF, SCOPE_MASK, FREE,
[4] Fix | Delete
LOCAL, GLOBAL_IMPLICIT, GLOBAL_EXPLICIT, CELL)
[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
[77] Fix | Delete
def is_nested(self):
[78] Fix | Delete
return bool(self._table.nested)
[79] Fix | Delete
[80] Fix | Delete
def has_children(self):
[81] Fix | Delete
return bool(self._table.children)
[82] Fix | Delete
[83] Fix | Delete
def has_exec(self):
[84] Fix | Delete
"""Return true if the scope uses exec. Deprecated method."""
[85] Fix | Delete
return False
[86] Fix | Delete
[87] Fix | Delete
def get_identifiers(self):
[88] Fix | Delete
return self._table.symbols.keys()
[89] Fix | Delete
[90] Fix | Delete
def lookup(self, name):
[91] Fix | Delete
sym = self._symbols.get(name)
[92] Fix | Delete
if sym is None:
[93] Fix | Delete
flags = self._table.symbols[name]
[94] Fix | Delete
namespaces = self.__check_children(name)
[95] Fix | Delete
sym = self._symbols[name] = Symbol(name, flags, namespaces)
[96] Fix | Delete
return sym
[97] Fix | Delete
[98] Fix | Delete
def get_symbols(self):
[99] Fix | Delete
return [self.lookup(ident) for ident in self.get_identifiers()]
[100] Fix | Delete
[101] Fix | Delete
def __check_children(self, name):
[102] Fix | Delete
return [_newSymbolTable(st, self._filename)
[103] Fix | Delete
for st in self._table.children
[104] Fix | Delete
if st.name == name]
[105] Fix | Delete
[106] Fix | Delete
def get_children(self):
[107] Fix | Delete
return [_newSymbolTable(st, self._filename)
[108] Fix | Delete
for st in self._table.children]
[109] Fix | Delete
[110] Fix | Delete
[111] Fix | Delete
class Function(SymbolTable):
[112] Fix | Delete
[113] Fix | Delete
# Default values for instance variables
[114] Fix | Delete
__params = None
[115] Fix | Delete
__locals = None
[116] Fix | Delete
__frees = None
[117] Fix | Delete
__globals = None
[118] Fix | Delete
[119] Fix | Delete
def __idents_matching(self, test_func):
[120] Fix | Delete
return tuple([ident for ident in self.get_identifiers()
[121] Fix | Delete
if test_func(self._table.symbols[ident])])
[122] Fix | Delete
[123] Fix | Delete
def get_parameters(self):
[124] Fix | Delete
if self.__params is None:
[125] Fix | Delete
self.__params = self.__idents_matching(lambda x:x & DEF_PARAM)
[126] Fix | Delete
return self.__params
[127] Fix | Delete
[128] Fix | Delete
def get_locals(self):
[129] Fix | Delete
if self.__locals is None:
[130] Fix | Delete
locs = (LOCAL, CELL)
[131] Fix | Delete
test = lambda x: ((x >> SCOPE_OFF) & SCOPE_MASK) in locs
[132] Fix | Delete
self.__locals = self.__idents_matching(test)
[133] Fix | Delete
return self.__locals
[134] Fix | Delete
[135] Fix | Delete
def get_globals(self):
[136] Fix | Delete
if self.__globals is None:
[137] Fix | Delete
glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT)
[138] Fix | Delete
test = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) in glob
[139] Fix | Delete
self.__globals = self.__idents_matching(test)
[140] Fix | Delete
return self.__globals
[141] Fix | Delete
[142] Fix | Delete
def get_frees(self):
[143] Fix | Delete
if self.__frees is None:
[144] Fix | Delete
is_free = lambda x:((x >> SCOPE_OFF) & SCOPE_MASK) == FREE
[145] Fix | Delete
self.__frees = self.__idents_matching(is_free)
[146] Fix | Delete
return self.__frees
[147] Fix | Delete
[148] Fix | Delete
[149] Fix | Delete
class Class(SymbolTable):
[150] Fix | Delete
[151] Fix | Delete
__methods = None
[152] Fix | Delete
[153] Fix | Delete
def get_methods(self):
[154] Fix | Delete
if self.__methods is None:
[155] Fix | Delete
d = {}
[156] Fix | Delete
for st in self._table.children:
[157] Fix | Delete
d[st.name] = 1
[158] Fix | Delete
self.__methods = tuple(d)
[159] Fix | Delete
return self.__methods
[160] Fix | Delete
[161] Fix | Delete
[162] Fix | Delete
class Symbol(object):
[163] Fix | Delete
[164] Fix | Delete
def __init__(self, name, flags, namespaces=None):
[165] Fix | Delete
self.__name = name
[166] Fix | Delete
self.__flags = flags
[167] Fix | Delete
self.__scope = (flags >> SCOPE_OFF) & SCOPE_MASK # like PyST_GetScope()
[168] Fix | Delete
self.__namespaces = namespaces or ()
[169] Fix | Delete
[170] Fix | Delete
def __repr__(self):
[171] Fix | Delete
return "<symbol {0!r}>".format(self.__name)
[172] Fix | Delete
[173] Fix | Delete
def get_name(self):
[174] Fix | Delete
return self.__name
[175] Fix | Delete
[176] Fix | Delete
def is_referenced(self):
[177] Fix | Delete
return bool(self.__flags & _symtable.USE)
[178] Fix | Delete
[179] Fix | Delete
def is_parameter(self):
[180] Fix | Delete
return bool(self.__flags & DEF_PARAM)
[181] Fix | Delete
[182] Fix | Delete
def is_global(self):
[183] Fix | Delete
return bool(self.__scope in (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT))
[184] Fix | Delete
[185] Fix | Delete
def is_declared_global(self):
[186] Fix | Delete
return bool(self.__scope == GLOBAL_EXPLICIT)
[187] Fix | Delete
[188] Fix | Delete
def is_local(self):
[189] Fix | Delete
return bool(self.__flags & DEF_BOUND)
[190] Fix | Delete
[191] Fix | Delete
def is_annotated(self):
[192] Fix | Delete
return bool(self.__flags & DEF_ANNOT)
[193] Fix | Delete
[194] Fix | Delete
def is_free(self):
[195] Fix | Delete
return bool(self.__scope == FREE)
[196] Fix | Delete
[197] Fix | Delete
def is_imported(self):
[198] Fix | Delete
return bool(self.__flags & DEF_IMPORT)
[199] Fix | Delete
[200] Fix | Delete
def is_assigned(self):
[201] Fix | Delete
return bool(self.__flags & DEF_LOCAL)
[202] Fix | Delete
[203] Fix | Delete
def is_namespace(self):
[204] Fix | Delete
"""Returns true if name binding introduces new namespace.
[205] Fix | Delete
[206] Fix | Delete
If the name is used as the target of a function or class
[207] Fix | Delete
statement, this will be true.
[208] Fix | Delete
[209] Fix | Delete
Note that a single name can be bound to multiple objects. If
[210] Fix | Delete
is_namespace() is true, the name may also be bound to other
[211] Fix | Delete
objects, like an int or list, that does not introduce a new
[212] Fix | Delete
namespace.
[213] Fix | Delete
"""
[214] Fix | Delete
return bool(self.__namespaces)
[215] Fix | Delete
[216] Fix | Delete
def get_namespaces(self):
[217] Fix | Delete
"""Return a list of namespaces bound to this name"""
[218] Fix | Delete
return self.__namespaces
[219] Fix | Delete
[220] Fix | Delete
def get_namespace(self):
[221] Fix | Delete
"""Returns the single namespace bound to this name.
[222] Fix | Delete
[223] Fix | Delete
Raises ValueError if the name is bound to multiple namespaces.
[224] Fix | Delete
"""
[225] Fix | Delete
if len(self.__namespaces) != 1:
[226] Fix | Delete
raise ValueError("name is bound to multiple namespaces")
[227] Fix | Delete
return self.__namespaces[0]
[228] Fix | Delete
[229] Fix | Delete
if __name__ == "__main__":
[230] Fix | Delete
import os, sys
[231] Fix | Delete
with open(sys.argv[0]) as f:
[232] Fix | Delete
src = f.read()
[233] Fix | Delete
mod = symtable(src, os.path.split(sys.argv[0])[1], "exec")
[234] Fix | Delete
for ident in mod.get_identifiers():
[235] Fix | Delete
info = mod.lookup(ident)
[236] Fix | Delete
print(info, info.is_local(), info.is_namespace())
[237] Fix | Delete
[238] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function