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

Python Inner class raises recursion error #329

Open
koldakov opened this issue Apr 12, 2024 · 3 comments
Open

Python Inner class raises recursion error #329

koldakov opened this issue Apr 12, 2024 · 3 comments

Comments

@koldakov
Copy link

Haven't seen anyone raised this before.

import sys

sys.setrecursionlimit(4)


class Foo:
    class Foo:
        foo = 1

Raises

RecursionError: maximum recursion depth exceeded while calling a Python object

@ngala
Copy link

ngala commented Apr 13, 2024

This is interesting!
If we go a step further and set the recursion limit to 2. The function itself will raises the error.
If we set it to 3, it will not allow you to define a class

>>> import sys
>>> sys.setrecursionlimit(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RecursionError: cannot set the recursion limit to 2 at the recursion depth 2: the limit is too low
>>> sys.setrecursionlimit(3)
>>> class Foo:
...     foo = 1
... 
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 62, in apport_excepthook
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 26, in enabled
RecursionError: maximum recursion depth exceeded while calling a Python object

Original exception was:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RecursionError: maximum recursion depth exceeded while calling a Python object

@koldakov
Copy link
Author

Requires further investigation, looks like depends on python version as well.

For example, python 3.12 doesn't raise this error, but python 3.8/3.9/3.10 does.

At the same time python 3.8/3.9/3.10 don't allow to set recursion limit to 2: RecursionError: cannot set the recursion limit to 2 at the recursion depth 2: the limit is too low.

Also, python 2.7 doesn't raise RecursionError: maximum recursion depth exceeded while calling a Python object if limit is 3 or more.

@ngala

@ngala
Copy link

ngala commented Apr 14, 2024

Yes, I had executed the code on Python 3.10.
Python 3.12 gives error if we set the limit to 1 which seems fine, I guess

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants