[Python-checkins] r76160 - in sandbox/trunk/2to3/lib2to3: main.py refactor.py tests/test_refactor.py

benjamin.peterson python-checkins at python.org
Mon Nov 9 01:53:48 CET 2009


Author: benjamin.peterson
Date: Mon Nov  9 01:53:48 2009
New Revision: 76160

Log:
undeprecate the -p option; it's useful for converting python3 sources

Modified:
   sandbox/trunk/2to3/lib2to3/main.py
   sandbox/trunk/2to3/lib2to3/refactor.py
   sandbox/trunk/2to3/lib2to3/tests/test_refactor.py

Modified: sandbox/trunk/2to3/lib2to3/main.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/main.py	(original)
+++ sandbox/trunk/2to3/lib2to3/main.py	Mon Nov  9 01:53:48 2009
@@ -91,8 +91,7 @@
     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="DEPRECATED Modify the grammar so that print() is "
-                          "a function")
+                      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("--no-diffs", action="store_true",
@@ -104,12 +103,10 @@
 
     # Parse command line arguments
     refactor_stdin = False
+    flags = {}
     options, args = parser.parse_args(args)
     if not options.write and options.no_diffs:
         warn("not writing files and not printing diffs; that's not very useful")
-    if options.print_function:
-        warn("-p is deprecated; "
-             "detection of from __future__ import print_function is automatic")
     if not options.write and options.nobackups:
         parser.error("Can't use -n without -w")
     if options.list_fixes:
@@ -127,6 +124,8 @@
         if options.write:
             print >> sys.stderr, "Can't write to stdin."
             return 2
+    if options.print_function:
+        flags["print_function"] = True
 
     # Set up logging handler
     level = logging.DEBUG if options.verbose else logging.INFO
@@ -147,7 +146,7 @@
     else:
         requested = avail_fixes.union(explicit)
     fixer_names = requested.difference(unwanted_fixes)
-    rt = StdoutRefactoringTool(sorted(fixer_names), None, sorted(explicit),
+    rt = StdoutRefactoringTool(sorted(fixer_names), flags, sorted(explicit),
                                options.nobackups, not options.no_diffs)
 
     # Refactor all files and directories passed as arguments

Modified: sandbox/trunk/2to3/lib2to3/refactor.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/refactor.py	(original)
+++ sandbox/trunk/2to3/lib2to3/refactor.py	Mon Nov  9 01:53:48 2009
@@ -18,7 +18,6 @@
 import operator
 import collections
 import StringIO
-import warnings
 from itertools import chain
 
 # Local imports
@@ -172,7 +171,7 @@
 
 class RefactoringTool(object):
 
-    _default_options = {}
+    _default_options = {"print_function" : False}
 
     CLASS_PREFIX = "Fix" # The prefix for fixer classes
     FILE_PREFIX = "fix_" # The prefix for modules with a fixer within
@@ -189,15 +188,16 @@
         self.explicit = explicit or []
         self.options = self._default_options.copy()
         if options is not None:
-            if "print_function" in options:
-                warnings.warn("the 'print_function' option is deprecated",
-                              DeprecationWarning)
             self.options.update(options)
+        if self.options["print_function"]:
+            self.grammar = pygram.python_grammar_no_print_statement
+        else:
+            self.grammar = pygram.python_grammar
         self.errors = []
         self.logger = logging.getLogger("RefactoringTool")
         self.fixer_log = []
         self.wrote = False
-        self.driver = driver.Driver(pygram.python_grammar,
+        self.driver = driver.Driver(self.grammar,
                                     convert=pytree.convert,
                                     logger=self.logger)
         self.pre_order, self.post_order = self.get_fixers()
@@ -353,7 +353,7 @@
                            name, err.__class__.__name__, err)
             return
         finally:
-            self.driver.grammar = pygram.python_grammar
+            self.driver.grammar = self.grammar
         self.log_debug("Refactoring %s", name)
         self.refactor_tree(tree, name)
         return tree

Modified: sandbox/trunk/2to3/lib2to3/tests/test_refactor.py
==============================================================================
--- sandbox/trunk/2to3/lib2to3/tests/test_refactor.py	(original)
+++ sandbox/trunk/2to3/lib2to3/tests/test_refactor.py	Mon Nov  9 01:53:48 2009
@@ -45,12 +45,10 @@
         return refactor.RefactoringTool(fixers, options, explicit)
 
     def test_print_function_option(self):
-        with warnings.catch_warnings(record=True) as w:
-            warnings.simplefilter("always", DeprecationWarning)
-            refactor.RefactoringTool(_DEFAULT_FIXERS, {"print_function" : True})
-        self.assertEqual(len(w), 1)
-        msg, = w
-        self.assertTrue(msg.category is DeprecationWarning)
+        rt = self.rt({"print_function" : True})
+        self.assertTrue(rt.grammar is pygram.python_grammar_no_print_statement)
+        self.assertTrue(rt.driver.grammar is
+                        pygram.python_grammar_no_print_statement)
 
     def test_fixer_loading_helpers(self):
         contents = ["explicit", "first", "last", "parrot", "preorder"]


More information about the Python-checkins mailing list