[Python-checkins] r61635 - in sandbox/trunk/2to3/lib2to3: fixes/fix_future.py fixes/fix_print.py tests/test_fixers.py
david.wolever
python-checkins at python.org
Wed Mar 19 20:16:04 CET 2008
Author: david.wolever
Date: Wed Mar 19 20:16:03 2008
New Revision: 61635
Modified:
sandbox/trunk/2to3/lib2to3/fixes/fix_future.py
sandbox/trunk/2to3/lib2to3/fixes/fix_print.py
sandbox/trunk/2to3/lib2to3/tests/test_fixers.py
Log:
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_future.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_future.py (original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_future.py Wed Mar 19 20:16:03 2008
@@ -11,5 +11,11 @@
class FixFuture(basefix.BaseFix):
PATTERN = """import_from< 'from' module_name="__future__" 'import' any >"""
+ # While the order that the tree is traversed does not matter,
+ # the postorder traversal fixers are run after the preorder fixers.
+ # Forcing this fixer to run in postorder ensures that fixers which check
+ # for __future__ can run first.
+ order='post'
+
def transform(self, node, results):
return BlankLine()
Modified: sandbox/trunk/2to3/lib2to3/fixes/fix_print.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/fixes/fix_print.py (original)
+++ sandbox/trunk/2to3/lib2to3/fixes/fix_print.py Wed Mar 19 20:16:03 2008
@@ -8,6 +8,9 @@
'print ...' into 'print(...)'
'print ... ,' into 'print(..., end=" ")'
'print >>x, ...' into 'print(..., file=x)'
+
+No changes are applied if print_function is imported from __future__
+
"""
# Local imports
@@ -23,14 +26,25 @@
)
-class FixPrint(basefix.BaseFix):
+class FixPrint(basefix.ConditionalFix):
PATTERN = """
simple_stmt< bare='print' any > | print_stmt
"""
+ # The traversal order does not matter, but the preorder fixers are run
+ # before the post-order fixers, and fix_future (which is defined to be
+ # postorder will remove the __future__ import (so we've got to be run
+ # first)
+ order = 'pre'
+ skip_on = '__future__.print_function'
+
def transform(self, node, results):
assert results
+
+ if self.should_skip(node):
+ return
+
bare_print = results.get("bare")
if bare_print:
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 Mar 19 20:16:03 2008
@@ -413,6 +413,30 @@
a = """print(file=sys.stderr)"""
self.check(b, a)
+ # With from __future__ import print_function
+ def test_with_future_print_function(self):
+ # XXX: These tests won't actually do anything until the parser
+ # is fixed so it won't crash when it sees print(x=y).
+ # When #2412 is fixed, the try/except block can be taken
+ # out and the tests can be run like normal.
+ try:
+ s = "from __future__ import print_function\n"\
+ "print('Hai!', end=' ')"
+ self.unchanged(s)
+
+ b = "print 'Hello, world!'"
+ a = "print('Hello, world!')"
+ self.check(b, a)
+
+ s = "from __future__ import *\n"\
+ "print('Hai!', end=' ')"
+ self.unchanged(s)
+ except:
+ return
+ else:
+ self.assertFalse(True, "#2421 has been fixed -- printing tests "\
+ "need to be updated!")
+
class Test_exec(FixerTestCase):
fixer = "exec"
More information about the Python-checkins
mailing list