[Python-checkins] r62232 - in sandbox/trunk/2to3/lib2to3: fixes/fix_dict.py fixes/fix_xrange.py fixes/util.py tests/test_fixers.py
collin.winter
python-checkins at python.org
Wed Apr 9 00:12:38 CEST 2008
Author: collin.winter
Date: Wed Apr 9 00:12:38 2008
New Revision: 62232
Modified:
sandbox/trunk/2to3/lib2to3/fixes/fix_dict.py
sandbox/trunk/2to3/lib2to3/fixes/fix_xrange.py
sandbox/trunk/2to3/lib2to3/fixes/util.py
sandbox/trunk/2to3/lib2to3/tests/test_fixers.py
Log:
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)".
Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_dict.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_dict.py (original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_dict.py Wed Apr 9 00:12:38 2008
@@ -29,11 +29,10 @@
from ..pgen2 import token
from . import basefix
from .util import Name, Call, LParen, RParen, ArgList, Dot, set
+from . import util
-exempt = set(["sorted", "list", "set", "any", "all", "tuple", "sum",
- "min", "max"])
-iter_exempt = exempt | set(["iter"])
+iter_exempt = util.consuming_calls | set(["iter"])
class FixDict(basefix.BaseFix):
@@ -93,7 +92,7 @@
return results["func"].value in iter_exempt
else:
# list(d.keys()) -> list(d.keys()), etc.
- return results["func"].value in exempt
+ return results["func"].value in util.consuming_calls
if not isiter:
return False
# for ... in d.iterkeys() -> for ... in d.keys(), etc.
Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_xrange.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_xrange.py (original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_xrange.py Wed Apr 9 00:12:38 2008
@@ -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
Modified: sandbox/trunk/2to3/lib2to3/fixes/util.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/util.py (original)
+++ sandbox/trunk/2to3/lib2to3/fixes/util.py Wed Apr 9 00:12:38 2008
@@ -182,6 +182,10 @@
### Misc
###########################################################
+
+consuming_calls = set(["sorted", "list", "set", "any", "all", "tuple", "sum",
+ "min", "max"])
+
def attr_chain(obj, attr):
"""Follow an attribute chain.
Modified: sandbox/trunk/2to3/lib2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/tests/test_fixers.py (original)
+++ sandbox/trunk/2to3/lib2to3/tests/test_fixers.py Wed Apr 9 00:12:38 2008
@@ -16,6 +16,8 @@
from .. import pygram
from .. import pytree
from .. import refactor
+from ..fixes import util
+
class Options:
def __init__(self, **kwargs):
@@ -1103,9 +1105,7 @@
self.check(b, a)
def test_unchanged(self):
- wrappers = ["set", "sorted", "any", "all", "tuple", "sum",
- "min", "max"]
- for wrapper in wrappers:
+ for wrapper in util.consuming_calls:
s = "s = %s(d.keys())" % wrapper
self.unchanged(s)
@@ -1253,26 +1253,54 @@
a = """x = range( 0 , 10 , 2 )"""
self.check(b, a)
- def test_1(self):
+ def test_single_arg(self):
b = """x = xrange(10)"""
a = """x = range(10)"""
self.check(b, a)
- def test_2(self):
+ def test_two_args(self):
b = """x = xrange(1, 10)"""
a = """x = range(1, 10)"""
self.check(b, a)
- def test_3(self):
+ def test_three_args(self):
b = """x = xrange(0, 10, 2)"""
a = """x = range(0, 10, 2)"""
self.check(b, a)
- def test_4(self):
+ def test_wrap_in_list(self):
+ b = """x = range(10, 3, 9)"""
+ a = """x = list(range(10, 3, 9))"""
+ self.check(b, a)
+
+ b = """x = foo(range(10, 3, 9))"""
+ a = """x = foo(list(range(10, 3, 9)))"""
+ self.check(b, a)
+
+ b = """x = range(10, 3, 9) + [4]"""
+ a = """x = list(range(10, 3, 9)) + [4]"""
+ self.check(b, a)
+
+ def test_xrange_in_for(self):
b = """for i in xrange(10):\n j=i"""
a = """for i in range(10):\n j=i"""
self.check(b, a)
+ b = """[i for i in xrange(10)]"""
+ a = """[i for i in range(10)]"""
+ self.check(b, a)
+
+ def test_range_in_for(self):
+ self.unchanged("for i in range(10): pass")
+ self.unchanged("[i for i in range(10)]")
+
+ def test_in_contains_test(self):
+ self.unchanged("x in range(10, 3, 9)")
+
+ def test_in_consuming_context(self):
+ for call in util.consuming_calls:
+ self.unchanged("a = %s(range(10))" % call)
+
class Test_raw_input(FixerTestCase):
fixer = "raw_input"
More information about the Python-checkins
mailing list