[Python-checkins] r56429 - in sandbox/trunk/2to3: fixes/fix_print.py tests/test_fixers.py
collin.winter
python-checkins at python.org
Tue Jul 17 23:12:07 CEST 2007
Author: collin.winter
Date: Tue Jul 17 23:12:07 2007
New Revision: 56429
Modified:
sandbox/trunk/2to3/ (props changed)
sandbox/trunk/2to3/fixes/fix_print.py
sandbox/trunk/2to3/tests/test_fixers.py
Log:
Simplify print fixer, make it idempotent.
Modified: sandbox/trunk/2to3/fixes/fix_print.py
==============================================================================
--- sandbox/trunk/2to3/fixes/fix_print.py (original)
+++ sandbox/trunk/2to3/fixes/fix_print.py Tue Jul 17 23:12:07 2007
@@ -14,32 +14,31 @@
import pytree
from pgen2 import token
from fixes import basefix
-from fixes.util import Name, Call, Comma, String
+from fixes.util import Name, Call, Comma, String, is_tuple
class FixPrint(basefix.BaseFix):
PATTERN = """
- 'print' | print_stmt
+ simple_stmt< bare='print' any > | print_stmt
"""
- def match(self, node):
- # Override
- if node.parent is not None and node.parent.type == self.syms.print_stmt:
- # Avoid matching 'print' as part of a print_stmt
- return None
- return self.pattern.match(node)
-
def transform(self, node, results):
assert results
- syms = self.syms
+ bare_print = results.get("bare")
- if node == Name("print"):
+ if bare_print:
# Special-case print all by itself
- return Call(Name("print"), [], prefix=node.get_prefix())
+ bare_print.replace(Call(Name("print"), [],
+ prefix=bare_print.get_prefix()))
+ return
assert node.children[0] == Name("print")
args = node.children[1:]
sep = end = file = None
+ if is_tuple(args[0]):
+ # We don't want to keep sticking parens around an
+ # already-parenthesised expression.
+ return
if args and args[-1] == Comma():
args = args[:-1]
end = " "
Modified: sandbox/trunk/2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_fixers.py (original)
+++ sandbox/trunk/2to3/tests/test_fixers.py Tue Jul 17 23:12:07 2007
@@ -13,6 +13,7 @@
import unittest
# Local imports
+import pygram
import pytree
import refactor
@@ -353,6 +354,24 @@
a = """print(1, 1+1, 1+1+1)"""
self.check(b, a)
+ def test_idempotency(self):
+ s = """print(1, 1+1, 1+1+1)"""
+ self.unchanged(s)
+
+ s = """print()"""
+ self.unchanged(s)
+
+ def test_idempotency_print_as_function(self):
+ print_stmt = pygram.python_grammar.keywords.pop("print")
+ try:
+ s = """print(1, 1+1, 1+1+1)"""
+ self.unchanged(s)
+
+ s = """print()"""
+ self.unchanged(s)
+ finally:
+ pygram.python_grammar.keywords["print"] = print_stmt
+
def test_1(self):
b = """print 1, 1+1, 1+1+1"""
a = """print(1, 1+1, 1+1+1)"""
More information about the Python-checkins
mailing list