]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39313: Add an option to RefactoringTool for using exec as a function (GH-17967)
authorBatuhan Taşkaya <47358913+isidentical@users.noreply.github.com>
Sun, 12 Jan 2020 22:13:31 +0000 (01:13 +0300)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 12 Jan 2020 22:13:31 +0000 (14:13 -0800)
https://bugs.python.org/issue39313

Automerge-Triggered-By: @pablogsal
Doc/library/2to3.rst
Lib/lib2to3/main.py
Lib/lib2to3/refactor.py
Lib/lib2to3/tests/test_refactor.py
Misc/NEWS.d/next/Library/2020-01-12-18-17-00.bpo-39313.DCTsnm.rst [new file with mode: 0644]

index c3ff3e607e7978121403ed05e0a910e6c0f0d597..eb4c9185f48bf28642e2157d307e9a62f70b58a7 100644 (file)
@@ -102,7 +102,7 @@ presence of the ``from __future__ import print_function`` compiler directive, it
 modifies its internal grammar to interpret :func:`print` as a function.  This
 change can also be enabled manually with the :option:`!-p` flag.  Use
 :option:`!-p` to run fixers on code that already has had its print statements
-converted.
+converted. Also :option:`!-e` can be used to make :func:`exec` a function.
 
 The :option:`!-o` or :option:`!--output-dir` option allows specification of an
 alternate directory for processed output files to be written to.  The
index c51626babf8ec02ef06ef72e5b3ba4de88b1eb83..f2849fd6be3fd93d0f0c48768f09270aea77a209 100644 (file)
@@ -154,6 +154,8 @@ def main(fixer_pkg, args=None):
                       help="List available transformations")
     parser.add_option("-p", "--print-function", action="store_true",
                       help="Modify the grammar so that print() is a function")
+    parser.add_option("-e", "--exec-function", action="store_true",
+                      help="Modify the grammar so that exec() is a function")
     parser.add_option("-v", "--verbose", action="store_true",
                       help="More verbose logging")
     parser.add_option("--no-diffs", action="store_true",
@@ -211,6 +213,9 @@ def main(fixer_pkg, args=None):
     if options.print_function:
         flags["print_function"] = True
 
+    if options.exec_function:
+        flags["exec_function"] = True
+
     # Set up logging handler
     level = logging.DEBUG if options.verbose else logging.INFO
     logging.basicConfig(format='%(name)s: %(message)s', level=level)
index 55fd60fa27c3da9ba0eed4fff1c472aac62e5d9d..3a5aafffc6df07462dac0829ccfc2eb51c6938e5 100644 (file)
@@ -155,6 +155,7 @@ class FixerError(Exception):
 class RefactoringTool(object):
 
     _default_options = {"print_function" : False,
+                        "exec_function": False,
                         "write_unchanged_files" : False}
 
     CLASS_PREFIX = "Fix" # The prefix for fixer classes
@@ -173,10 +174,13 @@ class RefactoringTool(object):
         self.options = self._default_options.copy()
         if options is not None:
             self.options.update(options)
-        if self.options["print_function"]:
-            self.grammar = pygram.python_grammar_no_print_statement
-        else:
-            self.grammar = pygram.python_grammar
+        self.grammar = pygram.python_grammar.copy()
+
+        if self.options['print_function']:
+            del self.grammar.keywords["print"]
+        elif self.options['exec_function']:
+            del self.grammar.keywords["exec"]
+
         # When this is True, the refactor*() methods will call write_file() for
         # files processed even if they were not changed during refactoring. If
         # and only if the refactor method's write parameter was True.
index 9e3b8fbb90b2f3b0888fe54d0f08589c46290e91..be705679f06dbadefd99edaee240a0a3db02e584 100644 (file)
@@ -44,9 +44,13 @@ class TestRefactoringTool(unittest.TestCase):
 
     def test_print_function_option(self):
         rt = self.rt({"print_function" : True})
-        self.assertIs(rt.grammar, pygram.python_grammar_no_print_statement)
-        self.assertIs(rt.driver.grammar,
-                      pygram.python_grammar_no_print_statement)
+        self.assertNotIn("print", rt.grammar.keywords)
+        self.assertNotIn("print", rt.driver.grammar.keywords)
+
+    def test_exec_function_option(self):
+        rt = self.rt({"exec_function" : True})
+        self.assertNotIn("exec", rt.grammar.keywords)
+        self.assertNotIn("exec", rt.driver.grammar.keywords)
 
     def test_write_unchanged_files_option(self):
         rt = self.rt()
diff --git a/Misc/NEWS.d/next/Library/2020-01-12-18-17-00.bpo-39313.DCTsnm.rst b/Misc/NEWS.d/next/Library/2020-01-12-18-17-00.bpo-39313.DCTsnm.rst
new file mode 100644 (file)
index 0000000..784d73c
--- /dev/null
@@ -0,0 +1,2 @@
+Add a new ``exec_function`` option (*--exec-function* in the CLI) to
+``RefactoringTool`` for making ``exec`` a function. Patch by Batuhan Taskaya.