summaryrefslogtreecommitdiffstats
path: root/Lib/lib2to3/fixes/fix_xrange.py
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2008-04-10 02:48:01 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2008-04-10 02:48:01 (GMT)
commit60a819d681d1004a9703b050501b70912f40b1ed (patch)
treec7f8b66797ba0df84f074c71e437d574137dc775 /Lib/lib2to3/fixes/fix_xrange.py
parentc00eb73a309d5e9a4e89c3114b32eda88bd83e98 (diff)
downloadcpython-60a819d681d1004a9703b050501b70912f40b1ed.zip
cpython-60a819d681d1004a9703b050501b70912f40b1ed.tar.gz
cpython-60a819d681d1004a9703b050501b70912f40b1ed.tar.bz2
Merged revisions 62080-62262 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3 ........ r62092 | collin.winter | 2008-04-01 18:27:10 +0200 (Di, 01 Apr 2008) | 1 line Add get_prev_sibling() to complement pytree's get_next_sibling(). ........ r62226 | collin.winter | 2008-04-08 21:07:56 +0200 (Di, 08 Apr 2008) | 1 line Add min() and max() to the list of special contexts that don't require adding list() calls around dict methods. ........ r62232 | collin.winter | 2008-04-09 00:12:38 +0200 (Mi, 09 Apr 2008) | 4 lines Fix for http://bugs.python.org/issue2596 This extends fix_xrange to know about the (mostly) same special contexts as fix_dict (where a special context is something that is guaranteed to fully consume the iterable), adding list() calls where appropriate. It also special-cases "x in range(y)". ........
Diffstat (limited to 'Lib/lib2to3/fixes/fix_xrange.py')
-rw-r--r--Lib/lib2to3/fixes/fix_xrange.py45
1 files changed, 43 insertions, 2 deletions
diff --git a/Lib/lib2to3/fixes/fix_xrange.py b/Lib/lib2to3/fixes/fix_xrange.py
index 410e601..2e4040e 100644
--- a/Lib/lib2to3/fixes/fix_xrange.py
+++ b/Lib/lib2to3/fixes/fix_xrange.py
@@ -5,14 +5,55 @@
# Local imports
from .import basefix
-from .util import Name
+from .util import Name, Call, consuming_calls
+from .. import patcomp
+
class FixXrange(basefix.BaseFix):
PATTERN = """
- power< name='xrange' trailer< '(' [any] ')' > >
+ power< (name='range'|name='xrange') trailer< '(' [any] ')' > any* >
"""
def transform(self, node, results):
name = results["name"]
+ if name.value == "xrange":
+ return self.transform_xrange(node, results)
+ elif name.value == "range":
+ return self.transform_range(node, results)
+ else:
+ raise ValueError(repr(name))
+
+ def transform_xrange(self, node, results):
+ name = results["name"]
name.replace(Name("range", prefix=name.get_prefix()))
+
+ def transform_range(self, node, results):
+ if not self.in_special_context(node):
+ arg = node.clone()
+ arg.set_prefix("")
+ call = Call(Name("list"), [arg])
+ call.set_prefix(node.get_prefix())
+ return call
+ return node
+
+ P1 = "power< func=NAME trailer< '(' node=any ')' > any* >"
+ p1 = patcomp.compile_pattern(P1)
+
+ P2 = """for_stmt< 'for' any 'in' node=any ':' any* >
+ | comp_for< 'for' any 'in' node=any any* >
+ | comparison< any 'in' node=any any*>
+ """
+ p2 = patcomp.compile_pattern(P2)
+
+ def in_special_context(self, node):
+ if node.parent is None:
+ return False
+ results = {}
+ if (node.parent.parent is not None and
+ self.p1.match(node.parent.parent, results) and
+ results["node"] is node):
+ # list(d.keys()) -> list(d.keys()), etc.
+ return results["func"].value in consuming_calls
+ # for ... in d.iterkeys() -> for ... in d.keys(), etc.
+ return self.p2.match(node.parent, results) and results["node"] is node