[Python-checkins] r56390 - in sandbox/trunk/2to3: README fixes/fix_xreadlines.py tests/test_fixers.py
collin.winter
python-checkins at python.org
Sun Jul 15 00:34:48 CEST 2007
Author: collin.winter
Date: Sun Jul 15 00:34:48 2007
New Revision: 56390
Added:
sandbox/trunk/2to3/fixes/fix_xreadlines.py
Modified:
sandbox/trunk/2to3/ (props changed)
sandbox/trunk/2to3/README
sandbox/trunk/2to3/tests/test_fixers.py
Log:
Add a fixer to convert 'for x in f.xreadlines():' into 'for x in f:'.
Modified: sandbox/trunk/2to3/README
==============================================================================
--- sandbox/trunk/2to3/README (original)
+++ sandbox/trunk/2to3/README Sun Jul 15 00:34:48 2007
@@ -87,6 +87,9 @@
* **fix_xrange** - "xrange()" -> "range()".
+* **fix_xreadlines** - "for x in f.xreadlines():" -> "for x in f:". Also,
+ "g(f.xreadlines)" -> "g(f.__iter__)".
+
Limitations
===========
Added: sandbox/trunk/2to3/fixes/fix_xreadlines.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/2to3/fixes/fix_xreadlines.py Sun Jul 15 00:34:48 2007
@@ -0,0 +1,24 @@
+"""Fix "for x in f.xreadlines()" -> "for x in f".
+
+This fixer will also convert g(f.xreadlines) into g(f.__iter__)."""
+# Author: Collin Winter
+
+# Local imports
+from fixes import basefix
+from fixes.util import Name
+
+
+class FixXreadlines(basefix.BaseFix):
+ PATTERN = """
+ power< call=any+ trailer< '.' 'xreadlines' > trailer< '(' ')' > >
+ |
+ power< any+ trailer< '.' no_call='xreadlines' > >
+ """
+
+ def transform(self, node, results):
+ no_call = results.get("no_call")
+
+ if no_call:
+ no_call.replace(Name("__iter__", prefix=no_call.get_prefix()))
+ else:
+ node.replace([x.clone() for x in results["call"]])
Modified: sandbox/trunk/2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_fixers.py (original)
+++ sandbox/trunk/2to3/tests/test_fixers.py Sun Jul 15 00:34:48 2007
@@ -1200,6 +1200,49 @@
self.check(s, s)
+class Test_xreadlines(FixerTestCase):
+ fixer = "xreadlines"
+
+ def test_call(self):
+ b = "for x in f.xreadlines(): pass"
+ a = "for x in f: pass"
+ self.check(b, a)
+
+ b = "for x in foo().xreadlines(): pass"
+ a = "for x in foo(): pass"
+ self.check(b, a)
+
+ b = "for x in (5 + foo()).xreadlines(): pass"
+ a = "for x in (5 + foo()): pass"
+ self.check(b, a)
+
+ def test_attr_ref(self):
+ b = "foo(f.xreadlines + 5)"
+ a = "foo(f.__iter__ + 5)"
+ self.check(b, a)
+
+ b = "foo(f().xreadlines + 5)"
+ a = "foo(f().__iter__ + 5)"
+ self.check(b, a)
+
+ b = "foo((5 + f()).xreadlines + 5)"
+ a = "foo((5 + f()).__iter__ + 5)"
+ self.check(b, a)
+
+ def test_unchanged(self):
+ s = "for x in f.xreadlines(5): pass"
+ self.check(s, s)
+
+ s = "for x in f.xreadlines(k=5): pass"
+ self.check(s, s)
+
+ s = "for x in f.xreadlines(*k, **v): pass"
+ self.check(s, s)
+
+ s = "foo(xreadlines)"
+ self.check(s, s)
+
+
class Test_stringio(FixerTestCase):
fixer = "stringio"
More information about the Python-checkins
mailing list