bpo-39313: Add an option to RefactoringTool for using exec as a function (GH-17967)
https://bugs.python.org/issue39313
Automerge-Triggered-By: @pablogsal
diff --git a/Lib/lib2to3/refactor.py b/Lib/lib2to3/refactor.py
index 55fd60f..3a5aaff 100644
--- a/Lib/lib2to3/refactor.py
+++ b/Lib/lib2to3/refactor.py
@@ -155,6 +155,7 @@
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 @@
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.