Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../compiler
File: symbols.py
"""Module symbol-table generator"""
[0] Fix | Delete
[1] Fix | Delete
from compiler import ast
[2] Fix | Delete
from compiler.consts import SC_LOCAL, SC_GLOBAL_IMPLICIT, SC_GLOBAL_EXPLICIT, \
[3] Fix | Delete
SC_FREE, SC_CELL, SC_UNKNOWN
[4] Fix | Delete
from compiler.misc import mangle
[5] Fix | Delete
import types
[6] Fix | Delete
[7] Fix | Delete
[8] Fix | Delete
import sys
[9] Fix | Delete
[10] Fix | Delete
MANGLE_LEN = 256
[11] Fix | Delete
[12] Fix | Delete
class Scope:
[13] Fix | Delete
# XXX how much information do I need about each name?
[14] Fix | Delete
def __init__(self, name, module, klass=None):
[15] Fix | Delete
self.name = name
[16] Fix | Delete
self.module = module
[17] Fix | Delete
self.defs = {}
[18] Fix | Delete
self.uses = {}
[19] Fix | Delete
self.globals = {}
[20] Fix | Delete
self.params = {}
[21] Fix | Delete
self.frees = {}
[22] Fix | Delete
self.cells = {}
[23] Fix | Delete
self.children = []
[24] Fix | Delete
# nested is true if the class could contain free variables,
[25] Fix | Delete
# i.e. if it is nested within another function.
[26] Fix | Delete
self.nested = None
[27] Fix | Delete
self.generator = None
[28] Fix | Delete
self.klass = None
[29] Fix | Delete
if klass is not None:
[30] Fix | Delete
for i in range(len(klass)):
[31] Fix | Delete
if klass[i] != '_':
[32] Fix | Delete
self.klass = klass[i:]
[33] Fix | Delete
break
[34] Fix | Delete
[35] Fix | Delete
def __repr__(self):
[36] Fix | Delete
return "<%s: %s>" % (self.__class__.__name__, self.name)
[37] Fix | Delete
[38] Fix | Delete
def mangle(self, name):
[39] Fix | Delete
if self.klass is None:
[40] Fix | Delete
return name
[41] Fix | Delete
return mangle(name, self.klass)
[42] Fix | Delete
[43] Fix | Delete
def add_def(self, name):
[44] Fix | Delete
self.defs[self.mangle(name)] = 1
[45] Fix | Delete
[46] Fix | Delete
def add_use(self, name):
[47] Fix | Delete
self.uses[self.mangle(name)] = 1
[48] Fix | Delete
[49] Fix | Delete
def add_global(self, name):
[50] Fix | Delete
name = self.mangle(name)
[51] Fix | Delete
if name in self.uses or name in self.defs:
[52] Fix | Delete
pass # XXX warn about global following def/use
[53] Fix | Delete
if name in self.params:
[54] Fix | Delete
raise SyntaxError, "%s in %s is global and parameter" % \
[55] Fix | Delete
(name, self.name)
[56] Fix | Delete
self.globals[name] = 1
[57] Fix | Delete
self.module.add_def(name)
[58] Fix | Delete
[59] Fix | Delete
def add_param(self, name):
[60] Fix | Delete
name = self.mangle(name)
[61] Fix | Delete
self.defs[name] = 1
[62] Fix | Delete
self.params[name] = 1
[63] Fix | Delete
[64] Fix | Delete
def get_names(self):
[65] Fix | Delete
d = {}
[66] Fix | Delete
d.update(self.defs)
[67] Fix | Delete
d.update(self.uses)
[68] Fix | Delete
d.update(self.globals)
[69] Fix | Delete
return d.keys()
[70] Fix | Delete
[71] Fix | Delete
def add_child(self, child):
[72] Fix | Delete
self.children.append(child)
[73] Fix | Delete
[74] Fix | Delete
def get_children(self):
[75] Fix | Delete
return self.children
[76] Fix | Delete
[77] Fix | Delete
def DEBUG(self):
[78] Fix | Delete
print >> sys.stderr, self.name, self.nested and "nested" or ""
[79] Fix | Delete
print >> sys.stderr, "\tglobals: ", self.globals
[80] Fix | Delete
print >> sys.stderr, "\tcells: ", self.cells
[81] Fix | Delete
print >> sys.stderr, "\tdefs: ", self.defs
[82] Fix | Delete
print >> sys.stderr, "\tuses: ", self.uses
[83] Fix | Delete
print >> sys.stderr, "\tfrees:", self.frees
[84] Fix | Delete
[85] Fix | Delete
def check_name(self, name):
[86] Fix | Delete
"""Return scope of name.
[87] Fix | Delete
[88] Fix | Delete
The scope of a name could be LOCAL, GLOBAL, FREE, or CELL.
[89] Fix | Delete
"""
[90] Fix | Delete
if name in self.globals:
[91] Fix | Delete
return SC_GLOBAL_EXPLICIT
[92] Fix | Delete
if name in self.cells:
[93] Fix | Delete
return SC_CELL
[94] Fix | Delete
if name in self.defs:
[95] Fix | Delete
return SC_LOCAL
[96] Fix | Delete
if self.nested and (name in self.frees or name in self.uses):
[97] Fix | Delete
return SC_FREE
[98] Fix | Delete
if self.nested:
[99] Fix | Delete
return SC_UNKNOWN
[100] Fix | Delete
else:
[101] Fix | Delete
return SC_GLOBAL_IMPLICIT
[102] Fix | Delete
[103] Fix | Delete
def get_free_vars(self):
[104] Fix | Delete
if not self.nested:
[105] Fix | Delete
return ()
[106] Fix | Delete
free = {}
[107] Fix | Delete
free.update(self.frees)
[108] Fix | Delete
for name in self.uses.keys():
[109] Fix | Delete
if name not in self.defs and name not in self.globals:
[110] Fix | Delete
free[name] = 1
[111] Fix | Delete
return free.keys()
[112] Fix | Delete
[113] Fix | Delete
def handle_children(self):
[114] Fix | Delete
for child in self.children:
[115] Fix | Delete
frees = child.get_free_vars()
[116] Fix | Delete
globals = self.add_frees(frees)
[117] Fix | Delete
for name in globals:
[118] Fix | Delete
child.force_global(name)
[119] Fix | Delete
[120] Fix | Delete
def force_global(self, name):
[121] Fix | Delete
"""Force name to be global in scope.
[122] Fix | Delete
[123] Fix | Delete
Some child of the current node had a free reference to name.
[124] Fix | Delete
When the child was processed, it was labelled a free
[125] Fix | Delete
variable. Now that all its enclosing scope have been
[126] Fix | Delete
processed, the name is known to be a global or builtin. So
[127] Fix | Delete
walk back down the child chain and set the name to be global
[128] Fix | Delete
rather than free.
[129] Fix | Delete
[130] Fix | Delete
Be careful to stop if a child does not think the name is
[131] Fix | Delete
free.
[132] Fix | Delete
"""
[133] Fix | Delete
self.globals[name] = 1
[134] Fix | Delete
if name in self.frees:
[135] Fix | Delete
del self.frees[name]
[136] Fix | Delete
for child in self.children:
[137] Fix | Delete
if child.check_name(name) == SC_FREE:
[138] Fix | Delete
child.force_global(name)
[139] Fix | Delete
[140] Fix | Delete
def add_frees(self, names):
[141] Fix | Delete
"""Process list of free vars from nested scope.
[142] Fix | Delete
[143] Fix | Delete
Returns a list of names that are either 1) declared global in the
[144] Fix | Delete
parent or 2) undefined in a top-level parent. In either case,
[145] Fix | Delete
the nested scope should treat them as globals.
[146] Fix | Delete
"""
[147] Fix | Delete
child_globals = []
[148] Fix | Delete
for name in names:
[149] Fix | Delete
sc = self.check_name(name)
[150] Fix | Delete
if self.nested:
[151] Fix | Delete
if sc == SC_UNKNOWN or sc == SC_FREE \
[152] Fix | Delete
or isinstance(self, ClassScope):
[153] Fix | Delete
self.frees[name] = 1
[154] Fix | Delete
elif sc == SC_GLOBAL_IMPLICIT:
[155] Fix | Delete
child_globals.append(name)
[156] Fix | Delete
elif isinstance(self, FunctionScope) and sc == SC_LOCAL:
[157] Fix | Delete
self.cells[name] = 1
[158] Fix | Delete
elif sc != SC_CELL:
[159] Fix | Delete
child_globals.append(name)
[160] Fix | Delete
else:
[161] Fix | Delete
if sc == SC_LOCAL:
[162] Fix | Delete
self.cells[name] = 1
[163] Fix | Delete
elif sc != SC_CELL:
[164] Fix | Delete
child_globals.append(name)
[165] Fix | Delete
return child_globals
[166] Fix | Delete
[167] Fix | Delete
def get_cell_vars(self):
[168] Fix | Delete
return self.cells.keys()
[169] Fix | Delete
[170] Fix | Delete
class ModuleScope(Scope):
[171] Fix | Delete
__super_init = Scope.__init__
[172] Fix | Delete
[173] Fix | Delete
def __init__(self):
[174] Fix | Delete
self.__super_init("global", self)
[175] Fix | Delete
[176] Fix | Delete
class FunctionScope(Scope):
[177] Fix | Delete
pass
[178] Fix | Delete
[179] Fix | Delete
class GenExprScope(Scope):
[180] Fix | Delete
__super_init = Scope.__init__
[181] Fix | Delete
[182] Fix | Delete
__counter = 1
[183] Fix | Delete
[184] Fix | Delete
def __init__(self, module, klass=None):
[185] Fix | Delete
i = self.__counter
[186] Fix | Delete
self.__counter += 1
[187] Fix | Delete
self.__super_init("generator expression<%d>"%i, module, klass)
[188] Fix | Delete
self.add_param('.0')
[189] Fix | Delete
[190] Fix | Delete
def get_names(self):
[191] Fix | Delete
keys = Scope.get_names(self)
[192] Fix | Delete
return keys
[193] Fix | Delete
[194] Fix | Delete
class LambdaScope(FunctionScope):
[195] Fix | Delete
__super_init = Scope.__init__
[196] Fix | Delete
[197] Fix | Delete
__counter = 1
[198] Fix | Delete
[199] Fix | Delete
def __init__(self, module, klass=None):
[200] Fix | Delete
i = self.__counter
[201] Fix | Delete
self.__counter += 1
[202] Fix | Delete
self.__super_init("lambda.%d" % i, module, klass)
[203] Fix | Delete
[204] Fix | Delete
class ClassScope(Scope):
[205] Fix | Delete
__super_init = Scope.__init__
[206] Fix | Delete
[207] Fix | Delete
def __init__(self, name, module):
[208] Fix | Delete
self.__super_init(name, module, name)
[209] Fix | Delete
[210] Fix | Delete
class SymbolVisitor:
[211] Fix | Delete
def __init__(self):
[212] Fix | Delete
self.scopes = {}
[213] Fix | Delete
self.klass = None
[214] Fix | Delete
[215] Fix | Delete
# node that define new scopes
[216] Fix | Delete
[217] Fix | Delete
def visitModule(self, node):
[218] Fix | Delete
scope = self.module = self.scopes[node] = ModuleScope()
[219] Fix | Delete
self.visit(node.node, scope)
[220] Fix | Delete
[221] Fix | Delete
visitExpression = visitModule
[222] Fix | Delete
[223] Fix | Delete
def visitFunction(self, node, parent):
[224] Fix | Delete
if node.decorators:
[225] Fix | Delete
self.visit(node.decorators, parent)
[226] Fix | Delete
parent.add_def(node.name)
[227] Fix | Delete
for n in node.defaults:
[228] Fix | Delete
self.visit(n, parent)
[229] Fix | Delete
scope = FunctionScope(node.name, self.module, self.klass)
[230] Fix | Delete
if parent.nested or isinstance(parent, FunctionScope):
[231] Fix | Delete
scope.nested = 1
[232] Fix | Delete
self.scopes[node] = scope
[233] Fix | Delete
self._do_args(scope, node.argnames)
[234] Fix | Delete
self.visit(node.code, scope)
[235] Fix | Delete
self.handle_free_vars(scope, parent)
[236] Fix | Delete
[237] Fix | Delete
def visitGenExpr(self, node, parent):
[238] Fix | Delete
scope = GenExprScope(self.module, self.klass);
[239] Fix | Delete
if parent.nested or isinstance(parent, FunctionScope) \
[240] Fix | Delete
or isinstance(parent, GenExprScope):
[241] Fix | Delete
scope.nested = 1
[242] Fix | Delete
[243] Fix | Delete
self.scopes[node] = scope
[244] Fix | Delete
self.visit(node.code, scope)
[245] Fix | Delete
[246] Fix | Delete
self.handle_free_vars(scope, parent)
[247] Fix | Delete
[248] Fix | Delete
def visitGenExprInner(self, node, scope):
[249] Fix | Delete
for genfor in node.quals:
[250] Fix | Delete
self.visit(genfor, scope)
[251] Fix | Delete
[252] Fix | Delete
self.visit(node.expr, scope)
[253] Fix | Delete
[254] Fix | Delete
def visitGenExprFor(self, node, scope):
[255] Fix | Delete
self.visit(node.assign, scope, 1)
[256] Fix | Delete
self.visit(node.iter, scope)
[257] Fix | Delete
for if_ in node.ifs:
[258] Fix | Delete
self.visit(if_, scope)
[259] Fix | Delete
[260] Fix | Delete
def visitGenExprIf(self, node, scope):
[261] Fix | Delete
self.visit(node.test, scope)
[262] Fix | Delete
[263] Fix | Delete
def visitLambda(self, node, parent, assign=0):
[264] Fix | Delete
# Lambda is an expression, so it could appear in an expression
[265] Fix | Delete
# context where assign is passed. The transformer should catch
[266] Fix | Delete
# any code that has a lambda on the left-hand side.
[267] Fix | Delete
assert not assign
[268] Fix | Delete
[269] Fix | Delete
for n in node.defaults:
[270] Fix | Delete
self.visit(n, parent)
[271] Fix | Delete
scope = LambdaScope(self.module, self.klass)
[272] Fix | Delete
if parent.nested or isinstance(parent, FunctionScope):
[273] Fix | Delete
scope.nested = 1
[274] Fix | Delete
self.scopes[node] = scope
[275] Fix | Delete
self._do_args(scope, node.argnames)
[276] Fix | Delete
self.visit(node.code, scope)
[277] Fix | Delete
self.handle_free_vars(scope, parent)
[278] Fix | Delete
[279] Fix | Delete
def _do_args(self, scope, args):
[280] Fix | Delete
for name in args:
[281] Fix | Delete
if type(name) == types.TupleType:
[282] Fix | Delete
self._do_args(scope, name)
[283] Fix | Delete
else:
[284] Fix | Delete
scope.add_param(name)
[285] Fix | Delete
[286] Fix | Delete
def handle_free_vars(self, scope, parent):
[287] Fix | Delete
parent.add_child(scope)
[288] Fix | Delete
scope.handle_children()
[289] Fix | Delete
[290] Fix | Delete
def visitClass(self, node, parent):
[291] Fix | Delete
parent.add_def(node.name)
[292] Fix | Delete
for n in node.bases:
[293] Fix | Delete
self.visit(n, parent)
[294] Fix | Delete
scope = ClassScope(node.name, self.module)
[295] Fix | Delete
if parent.nested or isinstance(parent, FunctionScope):
[296] Fix | Delete
scope.nested = 1
[297] Fix | Delete
if node.doc is not None:
[298] Fix | Delete
scope.add_def('__doc__')
[299] Fix | Delete
scope.add_def('__module__')
[300] Fix | Delete
self.scopes[node] = scope
[301] Fix | Delete
prev = self.klass
[302] Fix | Delete
self.klass = node.name
[303] Fix | Delete
self.visit(node.code, scope)
[304] Fix | Delete
self.klass = prev
[305] Fix | Delete
self.handle_free_vars(scope, parent)
[306] Fix | Delete
[307] Fix | Delete
# name can be a def or a use
[308] Fix | Delete
[309] Fix | Delete
# XXX a few calls and nodes expect a third "assign" arg that is
[310] Fix | Delete
# true if the name is being used as an assignment. only
[311] Fix | Delete
# expressions contained within statements may have the assign arg.
[312] Fix | Delete
[313] Fix | Delete
def visitName(self, node, scope, assign=0):
[314] Fix | Delete
if assign:
[315] Fix | Delete
scope.add_def(node.name)
[316] Fix | Delete
else:
[317] Fix | Delete
scope.add_use(node.name)
[318] Fix | Delete
[319] Fix | Delete
# operations that bind new names
[320] Fix | Delete
[321] Fix | Delete
def visitFor(self, node, scope):
[322] Fix | Delete
self.visit(node.assign, scope, 1)
[323] Fix | Delete
self.visit(node.list, scope)
[324] Fix | Delete
self.visit(node.body, scope)
[325] Fix | Delete
if node.else_:
[326] Fix | Delete
self.visit(node.else_, scope)
[327] Fix | Delete
[328] Fix | Delete
def visitFrom(self, node, scope):
[329] Fix | Delete
for name, asname in node.names:
[330] Fix | Delete
if name == "*":
[331] Fix | Delete
continue
[332] Fix | Delete
scope.add_def(asname or name)
[333] Fix | Delete
[334] Fix | Delete
def visitImport(self, node, scope):
[335] Fix | Delete
for name, asname in node.names:
[336] Fix | Delete
i = name.find(".")
[337] Fix | Delete
if i > -1:
[338] Fix | Delete
name = name[:i]
[339] Fix | Delete
scope.add_def(asname or name)
[340] Fix | Delete
[341] Fix | Delete
def visitGlobal(self, node, scope):
[342] Fix | Delete
for name in node.names:
[343] Fix | Delete
scope.add_global(name)
[344] Fix | Delete
[345] Fix | Delete
def visitAssign(self, node, scope):
[346] Fix | Delete
"""Propagate assignment flag down to child nodes.
[347] Fix | Delete
[348] Fix | Delete
The Assign node doesn't itself contains the variables being
[349] Fix | Delete
assigned to. Instead, the children in node.nodes are visited
[350] Fix | Delete
with the assign flag set to true. When the names occur in
[351] Fix | Delete
those nodes, they are marked as defs.
[352] Fix | Delete
[353] Fix | Delete
Some names that occur in an assignment target are not bound by
[354] Fix | Delete
the assignment, e.g. a name occurring inside a slice. The
[355] Fix | Delete
visitor handles these nodes specially; they do not propagate
[356] Fix | Delete
the assign flag to their children.
[357] Fix | Delete
"""
[358] Fix | Delete
for n in node.nodes:
[359] Fix | Delete
self.visit(n, scope, 1)
[360] Fix | Delete
self.visit(node.expr, scope)
[361] Fix | Delete
[362] Fix | Delete
def visitAssName(self, node, scope, assign=1):
[363] Fix | Delete
scope.add_def(node.name)
[364] Fix | Delete
[365] Fix | Delete
def visitAssAttr(self, node, scope, assign=0):
[366] Fix | Delete
self.visit(node.expr, scope, 0)
[367] Fix | Delete
[368] Fix | Delete
def visitSubscript(self, node, scope, assign=0):
[369] Fix | Delete
self.visit(node.expr, scope, 0)
[370] Fix | Delete
for n in node.subs:
[371] Fix | Delete
self.visit(n, scope, 0)
[372] Fix | Delete
[373] Fix | Delete
def visitSlice(self, node, scope, assign=0):
[374] Fix | Delete
self.visit(node.expr, scope, 0)
[375] Fix | Delete
if node.lower:
[376] Fix | Delete
self.visit(node.lower, scope, 0)
[377] Fix | Delete
if node.upper:
[378] Fix | Delete
self.visit(node.upper, scope, 0)
[379] Fix | Delete
[380] Fix | Delete
def visitAugAssign(self, node, scope):
[381] Fix | Delete
# If the LHS is a name, then this counts as assignment.
[382] Fix | Delete
# Otherwise, it's just use.
[383] Fix | Delete
self.visit(node.node, scope)
[384] Fix | Delete
if isinstance(node.node, ast.Name):
[385] Fix | Delete
self.visit(node.node, scope, 1) # XXX worry about this
[386] Fix | Delete
self.visit(node.expr, scope)
[387] Fix | Delete
[388] Fix | Delete
# prune if statements if tests are false
[389] Fix | Delete
[390] Fix | Delete
_const_types = types.StringType, types.IntType, types.FloatType
[391] Fix | Delete
[392] Fix | Delete
def visitIf(self, node, scope):
[393] Fix | Delete
for test, body in node.tests:
[394] Fix | Delete
if isinstance(test, ast.Const):
[395] Fix | Delete
if type(test.value) in self._const_types:
[396] Fix | Delete
if not test.value:
[397] Fix | Delete
continue
[398] Fix | Delete
self.visit(test, scope)
[399] Fix | Delete
self.visit(body, scope)
[400] Fix | Delete
if node.else_:
[401] Fix | Delete
self.visit(node.else_, scope)
[402] Fix | Delete
[403] Fix | Delete
# a yield statement signals a generator
[404] Fix | Delete
[405] Fix | Delete
def visitYield(self, node, scope):
[406] Fix | Delete
scope.generator = 1
[407] Fix | Delete
self.visit(node.value, scope)
[408] Fix | Delete
[409] Fix | Delete
def list_eq(l1, l2):
[410] Fix | Delete
return sorted(l1) == sorted(l2)
[411] Fix | Delete
[412] Fix | Delete
if __name__ == "__main__":
[413] Fix | Delete
import sys
[414] Fix | Delete
from compiler import parseFile, walk
[415] Fix | Delete
import symtable
[416] Fix | Delete
[417] Fix | Delete
def get_names(syms):
[418] Fix | Delete
return [s for s in [s.get_name() for s in syms.get_symbols()]
[419] Fix | Delete
if not (s.startswith('_[') or s.startswith('.'))]
[420] Fix | Delete
[421] Fix | Delete
for file in sys.argv[1:]:
[422] Fix | Delete
print file
[423] Fix | Delete
f = open(file)
[424] Fix | Delete
buf = f.read()
[425] Fix | Delete
f.close()
[426] Fix | Delete
syms = symtable.symtable(buf, file, "exec")
[427] Fix | Delete
mod_names = get_names(syms)
[428] Fix | Delete
tree = parseFile(file)
[429] Fix | Delete
s = SymbolVisitor()
[430] Fix | Delete
walk(tree, s)
[431] Fix | Delete
[432] Fix | Delete
# compare module-level symbols
[433] Fix | Delete
names2 = s.scopes[tree].get_names()
[434] Fix | Delete
[435] Fix | Delete
if not list_eq(mod_names, names2):
[436] Fix | Delete
print
[437] Fix | Delete
print "oops", file
[438] Fix | Delete
print sorted(mod_names)
[439] Fix | Delete
print sorted(names2)
[440] Fix | Delete
sys.exit(-1)
[441] Fix | Delete
[442] Fix | Delete
d = {}
[443] Fix | Delete
d.update(s.scopes)
[444] Fix | Delete
del d[tree]
[445] Fix | Delete
scopes = d.values()
[446] Fix | Delete
del d
[447] Fix | Delete
[448] Fix | Delete
for s in syms.get_symbols():
[449] Fix | Delete
if s.is_namespace():
[450] Fix | Delete
l = [sc for sc in scopes
[451] Fix | Delete
if sc.name == s.get_name()]
[452] Fix | Delete
if len(l) > 1:
[453] Fix | Delete
print "skipping", s.get_name()
[454] Fix | Delete
else:
[455] Fix | Delete
if not list_eq(get_names(s.get_namespace()),
[456] Fix | Delete
l[0].get_names()):
[457] Fix | Delete
print s.get_name()
[458] Fix | Delete
print sorted(get_names(s.get_namespace()))
[459] Fix | Delete
print sorted(l[0].get_names())
[460] Fix | Delete
sys.exit(-1)
[461] Fix | Delete
[462] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function