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 · 5 comments
Open

Python Inner class raises recursion error #329

koldakov opened this issue Apr 12, 2024 · 5 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

@nifadyev
Copy link
Contributor

Hey @koldakov , could you please write the summarized explanation and make a PR? If it is possible, of course. Or maybe @ngala is interested in making such task?

@koldakov
Copy link
Author

Hi @nifadyev, currently I'm swamped, but I'll take a shot if ngala won't do it first

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