Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/lib64/python2..../lib2to3/fixes
File: fix_xrange.py
# Copyright 2007 Google, Inc. All Rights Reserved.
[0] Fix | Delete
# Licensed to PSF under a Contributor Agreement.
[1] Fix | Delete
[2] Fix | Delete
"""Fixer that changes xrange(...) into range(...)."""
[3] Fix | Delete
[4] Fix | Delete
# Local imports
[5] Fix | Delete
from .. import fixer_base
[6] Fix | Delete
from ..fixer_util import Name, Call, consuming_calls
[7] Fix | Delete
from .. import patcomp
[8] Fix | Delete
[9] Fix | Delete
[10] Fix | Delete
class FixXrange(fixer_base.BaseFix):
[11] Fix | Delete
BM_compatible = True
[12] Fix | Delete
PATTERN = """
[13] Fix | Delete
power<
[14] Fix | Delete
(name='range'|name='xrange') trailer< '(' args=any ')' >
[15] Fix | Delete
rest=any* >
[16] Fix | Delete
"""
[17] Fix | Delete
[18] Fix | Delete
def start_tree(self, tree, filename):
[19] Fix | Delete
super(FixXrange, self).start_tree(tree, filename)
[20] Fix | Delete
self.transformed_xranges = set()
[21] Fix | Delete
[22] Fix | Delete
def finish_tree(self, tree, filename):
[23] Fix | Delete
self.transformed_xranges = None
[24] Fix | Delete
[25] Fix | Delete
def transform(self, node, results):
[26] Fix | Delete
name = results["name"]
[27] Fix | Delete
if name.value == u"xrange":
[28] Fix | Delete
return self.transform_xrange(node, results)
[29] Fix | Delete
elif name.value == u"range":
[30] Fix | Delete
return self.transform_range(node, results)
[31] Fix | Delete
else:
[32] Fix | Delete
raise ValueError(repr(name))
[33] Fix | Delete
[34] Fix | Delete
def transform_xrange(self, node, results):
[35] Fix | Delete
name = results["name"]
[36] Fix | Delete
name.replace(Name(u"range", prefix=name.prefix))
[37] Fix | Delete
# This prevents the new range call from being wrapped in a list later.
[38] Fix | Delete
self.transformed_xranges.add(id(node))
[39] Fix | Delete
[40] Fix | Delete
def transform_range(self, node, results):
[41] Fix | Delete
if (id(node) not in self.transformed_xranges and
[42] Fix | Delete
not self.in_special_context(node)):
[43] Fix | Delete
range_call = Call(Name(u"range"), [results["args"].clone()])
[44] Fix | Delete
# Encase the range call in list().
[45] Fix | Delete
list_call = Call(Name(u"list"), [range_call],
[46] Fix | Delete
prefix=node.prefix)
[47] Fix | Delete
# Put things that were after the range() call after the list call.
[48] Fix | Delete
for n in results["rest"]:
[49] Fix | Delete
list_call.append_child(n)
[50] Fix | Delete
return list_call
[51] Fix | Delete
[52] Fix | Delete
P1 = "power< func=NAME trailer< '(' node=any ')' > any* >"
[53] Fix | Delete
p1 = patcomp.compile_pattern(P1)
[54] Fix | Delete
[55] Fix | Delete
P2 = """for_stmt< 'for' any 'in' node=any ':' any* >
[56] Fix | Delete
| comp_for< 'for' any 'in' node=any any* >
[57] Fix | Delete
| comparison< any 'in' node=any any*>
[58] Fix | Delete
"""
[59] Fix | Delete
p2 = patcomp.compile_pattern(P2)
[60] Fix | Delete
[61] Fix | Delete
def in_special_context(self, node):
[62] Fix | Delete
if node.parent is None:
[63] Fix | Delete
return False
[64] Fix | Delete
results = {}
[65] Fix | Delete
if (node.parent.parent is not None and
[66] Fix | Delete
self.p1.match(node.parent.parent, results) and
[67] Fix | Delete
results["node"] is node):
[68] Fix | Delete
# list(d.keys()) -> list(d.keys()), etc.
[69] Fix | Delete
return results["func"].value in consuming_calls
[70] Fix | Delete
# for ... in d.iterkeys() -> for ... in d.keys(), etc.
[71] Fix | Delete
return self.p2.match(node.parent, results) and results["node"] is node
[72] Fix | Delete
[73] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function