Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../compiler
File: transformer.py
"""Parse tree transformation module.
[0] Fix | Delete
[1] Fix | Delete
Transforms Python source code into an abstract syntax tree (AST)
[2] Fix | Delete
defined in the ast module.
[3] Fix | Delete
[4] Fix | Delete
The simplest ways to invoke this module are via parse and parseFile.
[5] Fix | Delete
parse(buf) -> AST
[6] Fix | Delete
parseFile(path) -> AST
[7] Fix | Delete
"""
[8] Fix | Delete
[9] Fix | Delete
# Original version written by Greg Stein (gstein@lyra.org)
[10] Fix | Delete
# and Bill Tutt (rassilon@lima.mudlib.org)
[11] Fix | Delete
# February 1997.
[12] Fix | Delete
#
[13] Fix | Delete
# Modifications and improvements for Python 2.0 by Jeremy Hylton and
[14] Fix | Delete
# Mark Hammond
[15] Fix | Delete
#
[16] Fix | Delete
# Some fixes to try to have correct line number on almost all nodes
[17] Fix | Delete
# (except Module, Discard and Stmt) added by Sylvain Thenault
[18] Fix | Delete
#
[19] Fix | Delete
# Portions of this file are:
[20] Fix | Delete
# Copyright (C) 1997-1998 Greg Stein. All Rights Reserved.
[21] Fix | Delete
#
[22] Fix | Delete
# This module is provided under a BSD-ish license. See
[23] Fix | Delete
# http://www.opensource.org/licenses/bsd-license.html
[24] Fix | Delete
# and replace OWNER, ORGANIZATION, and YEAR as appropriate.
[25] Fix | Delete
[26] Fix | Delete
from compiler.ast import *
[27] Fix | Delete
import parser
[28] Fix | Delete
import symbol
[29] Fix | Delete
import token
[30] Fix | Delete
[31] Fix | Delete
class WalkerError(StandardError):
[32] Fix | Delete
pass
[33] Fix | Delete
[34] Fix | Delete
from compiler.consts import CO_VARARGS, CO_VARKEYWORDS
[35] Fix | Delete
from compiler.consts import OP_ASSIGN, OP_DELETE, OP_APPLY
[36] Fix | Delete
[37] Fix | Delete
def parseFile(path):
[38] Fix | Delete
f = open(path, "U")
[39] Fix | Delete
# XXX The parser API tolerates files without a trailing newline,
[40] Fix | Delete
# but not strings without a trailing newline. Always add an extra
[41] Fix | Delete
# newline to the file contents, since we're going through the string
[42] Fix | Delete
# version of the API.
[43] Fix | Delete
src = f.read() + "\n"
[44] Fix | Delete
f.close()
[45] Fix | Delete
return parse(src)
[46] Fix | Delete
[47] Fix | Delete
def parse(buf, mode="exec"):
[48] Fix | Delete
if mode == "exec" or mode == "single":
[49] Fix | Delete
return Transformer().parsesuite(buf)
[50] Fix | Delete
elif mode == "eval":
[51] Fix | Delete
return Transformer().parseexpr(buf)
[52] Fix | Delete
else:
[53] Fix | Delete
raise ValueError("compile() arg 3 must be"
[54] Fix | Delete
" 'exec' or 'eval' or 'single'")
[55] Fix | Delete
[56] Fix | Delete
def asList(nodes):
[57] Fix | Delete
l = []
[58] Fix | Delete
for item in nodes:
[59] Fix | Delete
if hasattr(item, "asList"):
[60] Fix | Delete
l.append(item.asList())
[61] Fix | Delete
else:
[62] Fix | Delete
if type(item) is type( (None, None) ):
[63] Fix | Delete
l.append(tuple(asList(item)))
[64] Fix | Delete
elif type(item) is type( [] ):
[65] Fix | Delete
l.append(asList(item))
[66] Fix | Delete
else:
[67] Fix | Delete
l.append(item)
[68] Fix | Delete
return l
[69] Fix | Delete
[70] Fix | Delete
def extractLineNo(ast):
[71] Fix | Delete
if not isinstance(ast[1], tuple):
[72] Fix | Delete
# get a terminal node
[73] Fix | Delete
return ast[2]
[74] Fix | Delete
for child in ast[1:]:
[75] Fix | Delete
if isinstance(child, tuple):
[76] Fix | Delete
lineno = extractLineNo(child)
[77] Fix | Delete
if lineno is not None:
[78] Fix | Delete
return lineno
[79] Fix | Delete
[80] Fix | Delete
def Node(*args):
[81] Fix | Delete
kind = args[0]
[82] Fix | Delete
if kind in nodes:
[83] Fix | Delete
try:
[84] Fix | Delete
return nodes[kind](*args[1:])
[85] Fix | Delete
except TypeError:
[86] Fix | Delete
print nodes[kind], len(args), args
[87] Fix | Delete
raise
[88] Fix | Delete
else:
[89] Fix | Delete
raise WalkerError, "Can't find appropriate Node type: %s" % str(args)
[90] Fix | Delete
#return apply(ast.Node, args)
[91] Fix | Delete
[92] Fix | Delete
class Transformer:
[93] Fix | Delete
"""Utility object for transforming Python parse trees.
[94] Fix | Delete
[95] Fix | Delete
Exposes the following methods:
[96] Fix | Delete
tree = transform(ast_tree)
[97] Fix | Delete
tree = parsesuite(text)
[98] Fix | Delete
tree = parseexpr(text)
[99] Fix | Delete
tree = parsefile(fileob | filename)
[100] Fix | Delete
"""
[101] Fix | Delete
[102] Fix | Delete
def __init__(self):
[103] Fix | Delete
self._dispatch = {}
[104] Fix | Delete
for value, name in symbol.sym_name.items():
[105] Fix | Delete
if hasattr(self, name):
[106] Fix | Delete
self._dispatch[value] = getattr(self, name)
[107] Fix | Delete
self._dispatch[token.NEWLINE] = self.com_NEWLINE
[108] Fix | Delete
self._atom_dispatch = {token.LPAR: self.atom_lpar,
[109] Fix | Delete
token.LSQB: self.atom_lsqb,
[110] Fix | Delete
token.LBRACE: self.atom_lbrace,
[111] Fix | Delete
token.BACKQUOTE: self.atom_backquote,
[112] Fix | Delete
token.NUMBER: self.atom_number,
[113] Fix | Delete
token.STRING: self.atom_string,
[114] Fix | Delete
token.NAME: self.atom_name,
[115] Fix | Delete
}
[116] Fix | Delete
self.encoding = None
[117] Fix | Delete
[118] Fix | Delete
def transform(self, tree):
[119] Fix | Delete
"""Transform an AST into a modified parse tree."""
[120] Fix | Delete
if not (isinstance(tree, tuple) or isinstance(tree, list)):
[121] Fix | Delete
tree = parser.st2tuple(tree, line_info=1)
[122] Fix | Delete
return self.compile_node(tree)
[123] Fix | Delete
[124] Fix | Delete
def parsesuite(self, text):
[125] Fix | Delete
"""Return a modified parse tree for the given suite text."""
[126] Fix | Delete
return self.transform(parser.suite(text))
[127] Fix | Delete
[128] Fix | Delete
def parseexpr(self, text):
[129] Fix | Delete
"""Return a modified parse tree for the given expression text."""
[130] Fix | Delete
return self.transform(parser.expr(text))
[131] Fix | Delete
[132] Fix | Delete
def parsefile(self, file):
[133] Fix | Delete
"""Return a modified parse tree for the contents of the given file."""
[134] Fix | Delete
if type(file) == type(''):
[135] Fix | Delete
file = open(file)
[136] Fix | Delete
return self.parsesuite(file.read())
[137] Fix | Delete
[138] Fix | Delete
# --------------------------------------------------------------
[139] Fix | Delete
#
[140] Fix | Delete
# PRIVATE METHODS
[141] Fix | Delete
#
[142] Fix | Delete
[143] Fix | Delete
def compile_node(self, node):
[144] Fix | Delete
### emit a line-number node?
[145] Fix | Delete
n = node[0]
[146] Fix | Delete
[147] Fix | Delete
if n == symbol.encoding_decl:
[148] Fix | Delete
self.encoding = node[2]
[149] Fix | Delete
node = node[1]
[150] Fix | Delete
n = node[0]
[151] Fix | Delete
[152] Fix | Delete
if n == symbol.single_input:
[153] Fix | Delete
return self.single_input(node[1:])
[154] Fix | Delete
if n == symbol.file_input:
[155] Fix | Delete
return self.file_input(node[1:])
[156] Fix | Delete
if n == symbol.eval_input:
[157] Fix | Delete
return self.eval_input(node[1:])
[158] Fix | Delete
if n == symbol.lambdef:
[159] Fix | Delete
return self.lambdef(node[1:])
[160] Fix | Delete
if n == symbol.funcdef:
[161] Fix | Delete
return self.funcdef(node[1:])
[162] Fix | Delete
if n == symbol.classdef:
[163] Fix | Delete
return self.classdef(node[1:])
[164] Fix | Delete
[165] Fix | Delete
raise WalkerError, ('unexpected node type', n)
[166] Fix | Delete
[167] Fix | Delete
def single_input(self, node):
[168] Fix | Delete
### do we want to do anything about being "interactive" ?
[169] Fix | Delete
[170] Fix | Delete
# NEWLINE | simple_stmt | compound_stmt NEWLINE
[171] Fix | Delete
n = node[0][0]
[172] Fix | Delete
if n != token.NEWLINE:
[173] Fix | Delete
return self.com_stmt(node[0])
[174] Fix | Delete
[175] Fix | Delete
return Pass()
[176] Fix | Delete
[177] Fix | Delete
def file_input(self, nodelist):
[178] Fix | Delete
doc = self.get_docstring(nodelist, symbol.file_input)
[179] Fix | Delete
if doc is not None:
[180] Fix | Delete
i = 1
[181] Fix | Delete
else:
[182] Fix | Delete
i = 0
[183] Fix | Delete
stmts = []
[184] Fix | Delete
for node in nodelist[i:]:
[185] Fix | Delete
if node[0] != token.ENDMARKER and node[0] != token.NEWLINE:
[186] Fix | Delete
self.com_append_stmt(stmts, node)
[187] Fix | Delete
return Module(doc, Stmt(stmts))
[188] Fix | Delete
[189] Fix | Delete
def eval_input(self, nodelist):
[190] Fix | Delete
# from the built-in function input()
[191] Fix | Delete
### is this sufficient?
[192] Fix | Delete
return Expression(self.com_node(nodelist[0]))
[193] Fix | Delete
[194] Fix | Delete
def decorator_name(self, nodelist):
[195] Fix | Delete
listlen = len(nodelist)
[196] Fix | Delete
assert listlen >= 1 and listlen % 2 == 1
[197] Fix | Delete
[198] Fix | Delete
item = self.atom_name(nodelist)
[199] Fix | Delete
i = 1
[200] Fix | Delete
while i < listlen:
[201] Fix | Delete
assert nodelist[i][0] == token.DOT
[202] Fix | Delete
assert nodelist[i + 1][0] == token.NAME
[203] Fix | Delete
item = Getattr(item, nodelist[i + 1][1])
[204] Fix | Delete
i += 2
[205] Fix | Delete
[206] Fix | Delete
return item
[207] Fix | Delete
[208] Fix | Delete
def decorator(self, nodelist):
[209] Fix | Delete
# '@' dotted_name [ '(' [arglist] ')' ]
[210] Fix | Delete
assert len(nodelist) in (3, 5, 6)
[211] Fix | Delete
assert nodelist[0][0] == token.AT
[212] Fix | Delete
assert nodelist[-1][0] == token.NEWLINE
[213] Fix | Delete
[214] Fix | Delete
assert nodelist[1][0] == symbol.dotted_name
[215] Fix | Delete
funcname = self.decorator_name(nodelist[1][1:])
[216] Fix | Delete
[217] Fix | Delete
if len(nodelist) > 3:
[218] Fix | Delete
assert nodelist[2][0] == token.LPAR
[219] Fix | Delete
expr = self.com_call_function(funcname, nodelist[3])
[220] Fix | Delete
else:
[221] Fix | Delete
expr = funcname
[222] Fix | Delete
[223] Fix | Delete
return expr
[224] Fix | Delete
[225] Fix | Delete
def decorators(self, nodelist):
[226] Fix | Delete
# decorators: decorator ([NEWLINE] decorator)* NEWLINE
[227] Fix | Delete
items = []
[228] Fix | Delete
for dec_nodelist in nodelist:
[229] Fix | Delete
assert dec_nodelist[0] == symbol.decorator
[230] Fix | Delete
items.append(self.decorator(dec_nodelist[1:]))
[231] Fix | Delete
return Decorators(items)
[232] Fix | Delete
[233] Fix | Delete
def decorated(self, nodelist):
[234] Fix | Delete
assert nodelist[0][0] == symbol.decorators
[235] Fix | Delete
if nodelist[1][0] == symbol.funcdef:
[236] Fix | Delete
n = [nodelist[0]] + list(nodelist[1][1:])
[237] Fix | Delete
return self.funcdef(n)
[238] Fix | Delete
elif nodelist[1][0] == symbol.classdef:
[239] Fix | Delete
decorators = self.decorators(nodelist[0][1:])
[240] Fix | Delete
cls = self.classdef(nodelist[1][1:])
[241] Fix | Delete
cls.decorators = decorators
[242] Fix | Delete
return cls
[243] Fix | Delete
raise WalkerError()
[244] Fix | Delete
[245] Fix | Delete
def funcdef(self, nodelist):
[246] Fix | Delete
# -6 -5 -4 -3 -2 -1
[247] Fix | Delete
# funcdef: [decorators] 'def' NAME parameters ':' suite
[248] Fix | Delete
# parameters: '(' [varargslist] ')'
[249] Fix | Delete
[250] Fix | Delete
if len(nodelist) == 6:
[251] Fix | Delete
assert nodelist[0][0] == symbol.decorators
[252] Fix | Delete
decorators = self.decorators(nodelist[0][1:])
[253] Fix | Delete
else:
[254] Fix | Delete
assert len(nodelist) == 5
[255] Fix | Delete
decorators = None
[256] Fix | Delete
[257] Fix | Delete
lineno = nodelist[-4][2]
[258] Fix | Delete
name = nodelist[-4][1]
[259] Fix | Delete
args = nodelist[-3][2]
[260] Fix | Delete
[261] Fix | Delete
if args[0] == symbol.varargslist:
[262] Fix | Delete
names, defaults, flags = self.com_arglist(args[1:])
[263] Fix | Delete
else:
[264] Fix | Delete
names = defaults = ()
[265] Fix | Delete
flags = 0
[266] Fix | Delete
doc = self.get_docstring(nodelist[-1])
[267] Fix | Delete
[268] Fix | Delete
# code for function
[269] Fix | Delete
code = self.com_node(nodelist[-1])
[270] Fix | Delete
[271] Fix | Delete
if doc is not None:
[272] Fix | Delete
assert isinstance(code, Stmt)
[273] Fix | Delete
assert isinstance(code.nodes[0], Discard)
[274] Fix | Delete
del code.nodes[0]
[275] Fix | Delete
return Function(decorators, name, names, defaults, flags, doc, code,
[276] Fix | Delete
lineno=lineno)
[277] Fix | Delete
[278] Fix | Delete
def lambdef(self, nodelist):
[279] Fix | Delete
# lambdef: 'lambda' [varargslist] ':' test
[280] Fix | Delete
if nodelist[2][0] == symbol.varargslist:
[281] Fix | Delete
names, defaults, flags = self.com_arglist(nodelist[2][1:])
[282] Fix | Delete
else:
[283] Fix | Delete
names = defaults = ()
[284] Fix | Delete
flags = 0
[285] Fix | Delete
[286] Fix | Delete
# code for lambda
[287] Fix | Delete
code = self.com_node(nodelist[-1])
[288] Fix | Delete
[289] Fix | Delete
return Lambda(names, defaults, flags, code, lineno=nodelist[1][2])
[290] Fix | Delete
old_lambdef = lambdef
[291] Fix | Delete
[292] Fix | Delete
def classdef(self, nodelist):
[293] Fix | Delete
# classdef: 'class' NAME ['(' [testlist] ')'] ':' suite
[294] Fix | Delete
[295] Fix | Delete
name = nodelist[1][1]
[296] Fix | Delete
doc = self.get_docstring(nodelist[-1])
[297] Fix | Delete
if nodelist[2][0] == token.COLON:
[298] Fix | Delete
bases = []
[299] Fix | Delete
elif nodelist[3][0] == token.RPAR:
[300] Fix | Delete
bases = []
[301] Fix | Delete
else:
[302] Fix | Delete
bases = self.com_bases(nodelist[3])
[303] Fix | Delete
[304] Fix | Delete
# code for class
[305] Fix | Delete
code = self.com_node(nodelist[-1])
[306] Fix | Delete
[307] Fix | Delete
if doc is not None:
[308] Fix | Delete
assert isinstance(code, Stmt)
[309] Fix | Delete
assert isinstance(code.nodes[0], Discard)
[310] Fix | Delete
del code.nodes[0]
[311] Fix | Delete
[312] Fix | Delete
return Class(name, bases, doc, code, lineno=nodelist[1][2])
[313] Fix | Delete
[314] Fix | Delete
def stmt(self, nodelist):
[315] Fix | Delete
return self.com_stmt(nodelist[0])
[316] Fix | Delete
[317] Fix | Delete
small_stmt = stmt
[318] Fix | Delete
flow_stmt = stmt
[319] Fix | Delete
compound_stmt = stmt
[320] Fix | Delete
[321] Fix | Delete
def simple_stmt(self, nodelist):
[322] Fix | Delete
# small_stmt (';' small_stmt)* [';'] NEWLINE
[323] Fix | Delete
stmts = []
[324] Fix | Delete
for i in range(0, len(nodelist), 2):
[325] Fix | Delete
self.com_append_stmt(stmts, nodelist[i])
[326] Fix | Delete
return Stmt(stmts)
[327] Fix | Delete
[328] Fix | Delete
def parameters(self, nodelist):
[329] Fix | Delete
raise WalkerError
[330] Fix | Delete
[331] Fix | Delete
def varargslist(self, nodelist):
[332] Fix | Delete
raise WalkerError
[333] Fix | Delete
[334] Fix | Delete
def fpdef(self, nodelist):
[335] Fix | Delete
raise WalkerError
[336] Fix | Delete
[337] Fix | Delete
def fplist(self, nodelist):
[338] Fix | Delete
raise WalkerError
[339] Fix | Delete
[340] Fix | Delete
def dotted_name(self, nodelist):
[341] Fix | Delete
raise WalkerError
[342] Fix | Delete
[343] Fix | Delete
def comp_op(self, nodelist):
[344] Fix | Delete
raise WalkerError
[345] Fix | Delete
[346] Fix | Delete
def trailer(self, nodelist):
[347] Fix | Delete
raise WalkerError
[348] Fix | Delete
[349] Fix | Delete
def sliceop(self, nodelist):
[350] Fix | Delete
raise WalkerError
[351] Fix | Delete
[352] Fix | Delete
def argument(self, nodelist):
[353] Fix | Delete
raise WalkerError
[354] Fix | Delete
[355] Fix | Delete
# --------------------------------------------------------------
[356] Fix | Delete
#
[357] Fix | Delete
# STATEMENT NODES (invoked by com_node())
[358] Fix | Delete
#
[359] Fix | Delete
[360] Fix | Delete
def expr_stmt(self, nodelist):
[361] Fix | Delete
# augassign testlist | testlist ('=' testlist)*
[362] Fix | Delete
en = nodelist[-1]
[363] Fix | Delete
exprNode = self.lookup_node(en)(en[1:])
[364] Fix | Delete
if len(nodelist) == 1:
[365] Fix | Delete
return Discard(exprNode, lineno=exprNode.lineno)
[366] Fix | Delete
if nodelist[1][0] == token.EQUAL:
[367] Fix | Delete
nodesl = []
[368] Fix | Delete
for i in range(0, len(nodelist) - 2, 2):
[369] Fix | Delete
nodesl.append(self.com_assign(nodelist[i], OP_ASSIGN))
[370] Fix | Delete
return Assign(nodesl, exprNode, lineno=nodelist[1][2])
[371] Fix | Delete
else:
[372] Fix | Delete
lval = self.com_augassign(nodelist[0])
[373] Fix | Delete
op = self.com_augassign_op(nodelist[1])
[374] Fix | Delete
return AugAssign(lval, op[1], exprNode, lineno=op[2])
[375] Fix | Delete
raise WalkerError, "can't get here"
[376] Fix | Delete
[377] Fix | Delete
def print_stmt(self, nodelist):
[378] Fix | Delete
# print ([ test (',' test)* [','] ] | '>>' test [ (',' test)+ [','] ])
[379] Fix | Delete
items = []
[380] Fix | Delete
if len(nodelist) == 1:
[381] Fix | Delete
start = 1
[382] Fix | Delete
dest = None
[383] Fix | Delete
elif nodelist[1][0] == token.RIGHTSHIFT:
[384] Fix | Delete
assert len(nodelist) == 3 \
[385] Fix | Delete
or nodelist[3][0] == token.COMMA
[386] Fix | Delete
dest = self.com_node(nodelist[2])
[387] Fix | Delete
start = 4
[388] Fix | Delete
else:
[389] Fix | Delete
dest = None
[390] Fix | Delete
start = 1
[391] Fix | Delete
for i in range(start, len(nodelist), 2):
[392] Fix | Delete
items.append(self.com_node(nodelist[i]))
[393] Fix | Delete
if nodelist[-1][0] == token.COMMA:
[394] Fix | Delete
return Print(items, dest, lineno=nodelist[0][2])
[395] Fix | Delete
return Printnl(items, dest, lineno=nodelist[0][2])
[396] Fix | Delete
[397] Fix | Delete
def del_stmt(self, nodelist):
[398] Fix | Delete
return self.com_assign(nodelist[1], OP_DELETE)
[399] Fix | Delete
[400] Fix | Delete
def pass_stmt(self, nodelist):
[401] Fix | Delete
return Pass(lineno=nodelist[0][2])
[402] Fix | Delete
[403] Fix | Delete
def break_stmt(self, nodelist):
[404] Fix | Delete
return Break(lineno=nodelist[0][2])
[405] Fix | Delete
[406] Fix | Delete
def continue_stmt(self, nodelist):
[407] Fix | Delete
return Continue(lineno=nodelist[0][2])
[408] Fix | Delete
[409] Fix | Delete
def return_stmt(self, nodelist):
[410] Fix | Delete
# return: [testlist]
[411] Fix | Delete
if len(nodelist) < 2:
[412] Fix | Delete
return Return(Const(None), lineno=nodelist[0][2])
[413] Fix | Delete
return Return(self.com_node(nodelist[1]), lineno=nodelist[0][2])
[414] Fix | Delete
[415] Fix | Delete
def yield_stmt(self, nodelist):
[416] Fix | Delete
expr = self.com_node(nodelist[0])
[417] Fix | Delete
return Discard(expr, lineno=expr.lineno)
[418] Fix | Delete
[419] Fix | Delete
def yield_expr(self, nodelist):
[420] Fix | Delete
if len(nodelist) > 1:
[421] Fix | Delete
value = self.com_node(nodelist[1])
[422] Fix | Delete
else:
[423] Fix | Delete
value = Const(None)
[424] Fix | Delete
return Yield(value, lineno=nodelist[0][2])
[425] Fix | Delete
[426] Fix | Delete
def raise_stmt(self, nodelist):
[427] Fix | Delete
# raise: [test [',' test [',' test]]]
[428] Fix | Delete
if len(nodelist) > 5:
[429] Fix | Delete
expr3 = self.com_node(nodelist[5])
[430] Fix | Delete
else:
[431] Fix | Delete
expr3 = None
[432] Fix | Delete
if len(nodelist) > 3:
[433] Fix | Delete
expr2 = self.com_node(nodelist[3])
[434] Fix | Delete
else:
[435] Fix | Delete
expr2 = None
[436] Fix | Delete
if len(nodelist) > 1:
[437] Fix | Delete
expr1 = self.com_node(nodelist[1])
[438] Fix | Delete
else:
[439] Fix | Delete
expr1 = None
[440] Fix | Delete
return Raise(expr1, expr2, expr3, lineno=nodelist[0][2])
[441] Fix | Delete
[442] Fix | Delete
def import_stmt(self, nodelist):
[443] Fix | Delete
# import_stmt: import_name | import_from
[444] Fix | Delete
assert len(nodelist) == 1
[445] Fix | Delete
return self.com_node(nodelist[0])
[446] Fix | Delete
[447] Fix | Delete
def import_name(self, nodelist):
[448] Fix | Delete
# import_name: 'import' dotted_as_names
[449] Fix | Delete
return Import(self.com_dotted_as_names(nodelist[1]),
[450] Fix | Delete
lineno=nodelist[0][2])
[451] Fix | Delete
[452] Fix | Delete
def import_from(self, nodelist):
[453] Fix | Delete
# import_from: 'from' ('.'* dotted_name | '.') 'import' ('*' |
[454] Fix | Delete
# '(' import_as_names ')' | import_as_names)
[455] Fix | Delete
assert nodelist[0][1] == 'from'
[456] Fix | Delete
idx = 1
[457] Fix | Delete
while nodelist[idx][1] == '.':
[458] Fix | Delete
idx += 1
[459] Fix | Delete
level = idx - 1
[460] Fix | Delete
if nodelist[idx][0] == symbol.dotted_name:
[461] Fix | Delete
fromname = self.com_dotted_name(nodelist[idx])
[462] Fix | Delete
idx += 1
[463] Fix | Delete
else:
[464] Fix | Delete
fromname = ""
[465] Fix | Delete
assert nodelist[idx][1] == 'import'
[466] Fix | Delete
if nodelist[idx + 1][0] == token.STAR:
[467] Fix | Delete
return From(fromname, [('*', None)], level,
[468] Fix | Delete
lineno=nodelist[0][2])
[469] Fix | Delete
else:
[470] Fix | Delete
node = nodelist[idx + 1 + (nodelist[idx + 1][0] == token.LPAR)]
[471] Fix | Delete
return From(fromname, self.com_import_as_names(node), level,
[472] Fix | Delete
lineno=nodelist[0][2])
[473] Fix | Delete
[474] Fix | Delete
def global_stmt(self, nodelist):
[475] Fix | Delete
# global: NAME (',' NAME)*
[476] Fix | Delete
names = []
[477] Fix | Delete
for i in range(1, len(nodelist), 2):
[478] Fix | Delete
names.append(nodelist[i][1])
[479] Fix | Delete
return Global(names, lineno=nodelist[0][2])
[480] Fix | Delete
[481] Fix | Delete
def exec_stmt(self, nodelist):
[482] Fix | Delete
# exec_stmt: 'exec' expr ['in' expr [',' expr]]
[483] Fix | Delete
expr1 = self.com_node(nodelist[1])
[484] Fix | Delete
if len(nodelist) >= 4:
[485] Fix | Delete
expr2 = self.com_node(nodelist[3])
[486] Fix | Delete
if len(nodelist) >= 6:
[487] Fix | Delete
expr3 = self.com_node(nodelist[5])
[488] Fix | Delete
else:
[489] Fix | Delete
expr3 = None
[490] Fix | Delete
else:
[491] Fix | Delete
expr2 = expr3 = None
[492] Fix | Delete
[493] Fix | Delete
return Exec(expr1, expr2, expr3, lineno=nodelist[0][2])
[494] Fix | Delete
[495] Fix | Delete
def assert_stmt(self, nodelist):
[496] Fix | Delete
# 'assert': test, [',' test]
[497] Fix | Delete
expr1 = self.com_node(nodelist[1])
[498] Fix | Delete
if (len(nodelist) == 4):
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function