Skip to content

Commit 61b1415

Browse files
isidenticalmiss-islington
authored andcommitted
bpo-39313: Add an option to RefactoringTool for using exec as a function (GH-17967)
https://bugs.python.org/issue39313 Automerge-Triggered-By: @pablogsal
1 parent 14dbe4b commit 61b1415

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

Doc/library/2to3.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ presence of the ``from __future__ import print_function`` compiler directive, it
102102
modifies its internal grammar to interpret :func:`print` as a function. This
103103
change can also be enabled manually with the :option:`!-p` flag. Use
104104
:option:`!-p` to run fixers on code that already has had its print statements
105-
converted.
105+
converted. Also :option:`!-e` can be used to make :func:`exec` a function.
106106

107107
The :option:`!-o` or :option:`!--output-dir` option allows specification of an
108108
alternate directory for processed output files to be written to. The

Lib/lib2to3/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ def main(fixer_pkg, args=None):
154154
help="List available transformations")
155155
parser.add_option("-p", "--print-function", action="store_true",
156156
help="Modify the grammar so that print() is a function")
157+
parser.add_option("-e", "--exec-function", action="store_true",
158+
help="Modify the grammar so that exec() is a function")
157159
parser.add_option("-v", "--verbose", action="store_true",
158160
help="More verbose logging")
159161
parser.add_option("--no-diffs", action="store_true",
@@ -211,6 +213,9 @@ def main(fixer_pkg, args=None):
211213
if options.print_function:
212214
flags["print_function"] = True
213215

216+
if options.exec_function:
217+
flags["exec_function"] = True
218+
214219
# Set up logging handler
215220
level = logging.DEBUG if options.verbose else logging.INFO
216221
logging.basicConfig(format='%(name)s: %(message)s', level=level)

Lib/lib2to3/refactor.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ class FixerError(Exception):
155155
class RefactoringTool(object):
156156

157157
_default_options = {"print_function" : False,
158+
"exec_function": False,
158159
"write_unchanged_files" : False}
159160

160161
CLASS_PREFIX = "Fix" # The prefix for fixer classes
@@ -173,10 +174,13 @@ def __init__(self, fixer_names, options=None, explicit=None):
173174
self.options = self._default_options.copy()
174175
if options is not None:
175176
self.options.update(options)
176-
if self.options["print_function"]:
177-
self.grammar = pygram.python_grammar_no_print_statement
178-
else:
179-
self.grammar = pygram.python_grammar
177+
self.grammar = pygram.python_grammar.copy()
178+
179+
if self.options['print_function']:
180+
del self.grammar.keywords["print"]
181+
elif self.options['exec_function']:
182+
del self.grammar.keywords["exec"]
183+
180184
# When this is True, the refactor*() methods will call write_file() for
181185
# files processed even if they were not changed during refactoring. If
182186
# and only if the refactor method's write parameter was True.

Lib/lib2to3/tests/test_refactor.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ def rt(self, options=None, fixers=_DEFAULT_FIXERS, explicit=None):
4444

4545
def test_print_function_option(self):
4646
rt = self.rt({"print_function" : True})
47-
self.assertIs(rt.grammar, pygram.python_grammar_no_print_statement)
48-
self.assertIs(rt.driver.grammar,
49-
pygram.python_grammar_no_print_statement)
47+
self.assertNotIn("print", rt.grammar.keywords)
48+
self.assertNotIn("print", rt.driver.grammar.keywords)
49+
50+
def test_exec_function_option(self):
51+
rt = self.rt({"exec_function" : True})
52+
self.assertNotIn("exec", rt.grammar.keywords)
53+
self.assertNotIn("exec", rt.driver.grammar.keywords)
5054

5155
def test_write_unchanged_files_option(self):
5256
rt = self.rt()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add a new ``exec_function`` option (*--exec-function* in the CLI) to
2+
``RefactoringTool`` for making ``exec`` a function. Patch by Batuhan Taskaya.

0 commit comments

Comments
 (0)