This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: lib2to3 does not accept "exec" as name
Type: behavior Stage: resolved
Components: 2to3 (2.x to 3.x conversion tool) Versions: Python 3.9
Status: closed Resolution: wont fix
Dependencies: Superseder: Close 2to3 issues and list them here
View: 45544
Assigned To: Nosy List: iritkatriel, mulugruntz, terry.reedy
Priority: normal Keywords:

Created on 2021-05-28 10:19 by mulugruntz, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg394650 - (view) Author: Mulugruntz (mulugruntz) Date: 2021-05-28 10:19
I was trying to use to generate some doc for

It was failing and for a while I thought I was doing something wrong. But when I did dig deeper, I realized that it was failing because I has a method named "exec".

The reason why I want to use "exec" is to make it obvious whether I'm executing the subprocess in shell mode or not (there's also a "shell" method).

As we can see here

"exec" is not reserved.

Moreover, it's pretty counterintuitive that the code parses and runs correctly (cpython 3.9.5) but the lib2to3 parser crashes.

See below a working example:

from lib2to3 import pygram, pytree
from lib2to3.pgen2 import driver
from lib2to3.pgen2.parse import ParseError

grammar = pygram.python_grammar.copy()
driver = driver.Driver(grammar, convert=pytree.convert)

strings = [
    "def fname(): pass",
    "def exec(): pass",
class C:
    def exec(self): pass""",

for s in strings:
        driver.parse_string(s + '\n')
    except ParseError as pe:
        print("It fails:", s)
        print("It works:", s)


It works: def fname(): pass
It fails: def exec(): pass
It fails: 
class C:
    def exec(self): pass
msg394684 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-05-28 20:12
'exec' was a keyword in 2.x, but that should not matter in 3.9.  What OS?  Did you get an actual crash (core dump on *nix), or a python exception and traceback (not a crash)?  If the latter, copy and paste it.
msg394721 - (view) Author: Mulugruntz (mulugruntz) Date: 2021-05-29 07:24
Traceback (most recent call last):
  File "/Users/sgiffard/Library/Application Support/JetBrains/PyCharm2020.3/scratches/", line 9, in <module>
    driver.parse_string("""class C:\n    def exec(self): pass\n""")
  File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib2to3/pgen2/", line 103, in parse_string
    return self.parse_tokens(tokens, debug)
  File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib2to3/pgen2/", line 71, in parse_tokens
    if p.addtoken(type, value, (prefix, start)):
  File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib2to3/pgen2/", line 162, in addtoken
    raise ParseError("bad input", type, value, context)
lib2to3.pgen2.parse.ParseError: bad input: type=1, value='exec', context=(' ', (2, 8))
msg394722 - (view) Author: Mulugruntz (mulugruntz) Date: 2021-05-29 07:27
Sorry, I forgot to mention:
macOS Mojave 10.14.5 (18F132)
msg401338 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-09-07 19:35
Changing type as crash typically refers to segfault and not an exception.
Date User Action Args
2022-04-11 14:59:46adminsetgithub: 88425
2021-10-20 22:52:24iritkatrielsetstatus: open -> closed
superseder: Close 2to3 issues and list them here
resolution: wont fix
stage: resolved
2021-09-07 19:35:35iritkatrielsettype: crash -> behavior

messages: + msg401338
nosy: + iritkatriel
2021-05-29 07:27:11mulugruntzsetmessages: + msg394722
2021-05-29 07:24:54mulugruntzsetmessages: + msg394721
2021-05-28 20:12:29terry.reedysetnosy: + terry.reedy
messages: + msg394684
2021-05-28 10:19:27mulugruntzcreate