Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Too many elifs cause MemoryError or RecursionError #105771

Open
pbzweihander opened this issue Jun 14, 2023 · 0 comments
Open

Too many elifs cause MemoryError or RecursionError #105771

pbzweihander opened this issue Jun 14, 2023 · 0 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@pbzweihander
Copy link

Bug report

Save the above code as main.py:

code = """a = 1
if a == 0:
    print(0)
"""
for i in range(1, 10000):
    code += f"""elif a == {i}:
    print({i})
"""

print(code)

... and run python main.py | python causes:

$ python main.py | python
MemoryError
Traceback (most recent call last):
  File "{redacted}/main.py", line 10, in <module>
    print(code)
BrokenPipeError: [Errno 32] Broken pipe

(BrokenPipeError is not relevant here)

Or, run the following code:

code = """a = 1
if a == 0:
    print(0)
"""
for i in range(1, 10000):
    code += f"""elif a == {i}:
    print({i})
"""

exec(code)

... causes the following error:

Traceback (most recent call last):
  File "/home/pbzweihander/projects/python/playground/main.py", line 10, in <module>
    exec(code)
MemoryError

In Python 3.8.10, the error message is changed as follows:

Traceback (most recent call last):
  File "main.py", line 10, in <module>
    exec(code)
RecursionError: maximum recursion depth exceeded during compilation

I think this is a bug, and the bug is due to parsing the Python code to AST causing RecursionError.

Your environment

  • CPython versions tested on: Python 3.11.3
  • Operating system and architecture: Arch Linux 6.3.5-arch1-1
@pbzweihander pbzweihander added the type-bug An unexpected behavior, bug, or error label Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant