[Python-checkins] r57883 - in sandbox/trunk/2to3: fixes/fix_print.py tests/test_fixers.py

collin.winter python-checkins at python.org
Sat Sep 1 22:18:29 CEST 2007


Author: collin.winter
Date: Sat Sep  1 22:18:19 2007
New Revision: 57883

Modified:
   sandbox/trunk/2to3/   (props changed)
   sandbox/trunk/2to3/fixes/fix_print.py
   sandbox/trunk/2to3/tests/test_fixers.py
Log:
Fix a long-standing idempotency problem in fix_print.


Modified: sandbox/trunk/2to3/fixes/fix_print.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_print.py	(original)
+++ sandbox/trunk/2to3/fixes/fix_print.py	Sat Sep  1 22:18:19 2007
@@ -11,12 +11,16 @@
 """
 
 # Local imports
+import patcomp
 import pytree
 from pgen2 import token
 from fixes import basefix
 from fixes.util import Name, Call, Comma, String, is_tuple
 
 
+paren_call = patcomp.compile_pattern("""atom< '(' any ')' >""")
+
+
 class FixPrint(basefix.BaseFix):
 
     PATTERN = """
@@ -35,7 +39,7 @@
         assert node.children[0] == Name("print")
         args = node.children[1:]
         sep = end = file = None
-        if is_tuple(args[0]):
+        if len(args) == 1 and (is_tuple(args[0]) or paren_call.match(args[0])):
             # We don't want to keep sticking parens around an
             # already-parenthesised expression.
             return

Modified: sandbox/trunk/2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_fixers.py	(original)
+++ sandbox/trunk/2to3/tests/test_fixers.py	Sat Sep  1 22:18:19 2007
@@ -361,6 +361,9 @@
         s = """print()"""
         self.unchanged(s)
 
+        s = """print('')"""
+        self.unchanged(s)
+
     def test_idempotency_print_as_function(self):
         print_stmt = pygram.python_grammar.keywords.pop("print")
         try:
@@ -369,6 +372,9 @@
 
             s = """print()"""
             self.unchanged(s)
+
+            s = """print('')"""
+            self.unchanged(s)
         finally:
             pygram.python_grammar.keywords["print"] = print_stmt
 


More information about the Python-checkins mailing list