[Python-checkins] r58002 - in sandbox/trunk/2to3: pygram.py refactor.py tests/test_all_fixers.py tests/test_fixers.py tests/test_refactor.py
collin.winter
python-checkins at python.org
Thu Sep 6 07:21:15 CEST 2007
Author: collin.winter
Date: Thu Sep 6 07:21:14 2007
New Revision: 58002
Modified:
sandbox/trunk/2to3/pygram.py
sandbox/trunk/2to3/refactor.py
sandbox/trunk/2to3/tests/test_all_fixers.py
sandbox/trunk/2to3/tests/test_fixers.py
sandbox/trunk/2to3/tests/test_refactor.py
Log:
Restore the -p option (revert r57916, most of r57914); there turned out to be some corner cases in the print grammar that didn't play nicely with print autodetection/fix_print idempotence. Fixing those edge cases is too much work, given that the -p flag is really only used by Guido and me.
Modified: sandbox/trunk/2to3/pygram.py
==============================================================================
--- sandbox/trunk/2to3/pygram.py (original)
+++ sandbox/trunk/2to3/pygram.py Thu Sep 6 07:21:14 2007
@@ -29,8 +29,6 @@
python_grammar = driver.load_grammar(_GRAMMAR_FILE)
python_symbols = Symbols(python_grammar)
-printless_python_grammar = driver.load_grammar(_GRAMMAR_FILE)
-del printless_python_grammar.keywords["print"]
def parenthesize(node):
Modified: sandbox/trunk/2to3/refactor.py
==============================================================================
--- sandbox/trunk/2to3/refactor.py (original)
+++ sandbox/trunk/2to3/refactor.py Thu Sep 6 07:21:14 2007
@@ -23,8 +23,6 @@
import pytree
import patcomp
from pgen2 import driver
-from pgen2 import parse
-from pgen2 import token
from pgen2 import tokenize
import fixes
import pygram
@@ -54,6 +52,8 @@
help="Each FIX specifies a transformation; default all")
parser.add_option("-l", "--list-fixes", action="store_true",
help="List available transformations (fixes/fix_*.py)")
+ parser.add_option("-p", "--print-function", action="store_true",
+ help="Modify the grammar so that print() is a function")
parser.add_option("-v", "--verbose", action="store_true",
help="More verbose logging")
parser.add_option("-w", "--write", action="store_true",
@@ -106,14 +106,11 @@
self.options = options
self.errors = []
self.logger = logging.getLogger("RefactoringTool")
- # Set up two parser drivers: one that expects print statements and a
- # second that expects print functions.
+ if self.options.print_function:
+ del pygram.python_grammar.keywords["print"]
self.driver = driver.Driver(pygram.python_grammar,
convert=pytree.convert,
logger=self.logger)
- self.printless_driver = driver.Driver(pygram.printless_python_grammar,
- convert=pytree.convert,
- logger=self.logger)
self.pre_order, self.post_order = self.get_fixers()
self.files = [] # List of files that were or should be modified
@@ -243,13 +240,7 @@
there were errors during the parse.
"""
try:
- try:
- tree = self.driver.parse_string(data)
- except parse.ParseError, e:
- if e.type == token.EQUAL:
- tree = self.printless_driver.parse_string(data)
- else:
- raise
+ tree = self.driver.parse_string(data)
except Exception, err:
self.log_error("Can't parse %s: %s: %s",
name, err.__class__.__name__, err)
Modified: sandbox/trunk/2to3/tests/test_all_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_all_fixers.py (original)
+++ sandbox/trunk/2to3/tests/test_all_fixers.py Thu Sep 6 07:21:14 2007
@@ -24,17 +24,17 @@
for k, v in kwargs.items():
setattr(self, k, v)
self.verbose = False
- self.doctests_only = False
class Test_all(support.TestCase):
def setUp(self):
- options = Options(fix=["all", "idioms", "ws_comma"], write=False)
+ options = Options(fix=["all", "idioms", "ws_comma"],
+ print_function=False)
self.refactor = refactor.RefactoringTool(options)
def test_all_project_files(self):
for filepath in support.all_project_files():
print "Fixing %s..." % filepath
- self.refactor.refactor_file(filepath)
+ self.refactor.refactor_string(open(filepath).read(), filepath)
if __name__ == "__main__":
Modified: sandbox/trunk/2to3/tests/test_fixers.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_fixers.py (original)
+++ sandbox/trunk/2to3/tests/test_fixers.py Thu Sep 6 07:21:14 2007
@@ -358,14 +358,18 @@
self.unchanged(s)
def test_idempotency_print_as_function(self):
- s = """print(1, 1+1, 1+1+1)"""
- self.unchanged(s)
+ 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)
+ s = """print()"""
+ self.unchanged(s)
- s = """print('')"""
- 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"""
Modified: sandbox/trunk/2to3/tests/test_refactor.py
==============================================================================
--- sandbox/trunk/2to3/tests/test_refactor.py (original)
+++ sandbox/trunk/2to3/tests/test_refactor.py Thu Sep 6 07:21:14 2007
@@ -1,58 +0,0 @@
-#!/usr/bin/env python2.5
-""" Test suite for refactor.py """
-# Author: Collin Winter
-
-# Testing imports
-try:
- from tests import support
-except ImportError:
- import support
-
-# Python imports
-import unittest
-
-# Local imports
-import refactor
-
-
-class Options:
- def __init__(self, **kwargs):
- for k, v in kwargs.items():
- setattr(self, k, v)
- self.verbose = False
- self.doctests_only = False
-
-
-class TestAutomaticPrintDetection(unittest.TestCase):
-
- def setUp(self):
- self.refactor = refactor.RefactoringTool(Options(fix=["print"]))
-
- def _check(self, before, after):
- before = support.reformat(before)
- after = support.reformat(after)
- tree = self.refactor.refactor_string(before, "<stream>")
- self.failUnlessEqual(str(tree), after)
- return tree
-
- def check(self, before, after):
- tree = self._check(before, after)
- self.failUnless(tree.was_changed)
-
- def unchanged(self, before):
- tree = self._check(before, before)
- self.failIf(tree.was_changed)
-
- def test_print_statement(self):
- b = """print >>b, c"""
- a = """print(c, file=b)"""
- self.check(b, a)
-
- def test_print_function(self):
- s = """print(c, file=b)"""
- self.unchanged(s)
-
-
-if __name__ == "__main__":
- import __main__
- support.run_all_tests(__main__)
More information about the Python-checkins
mailing list