Skip to content

Recursion limitation in Python 3.13 #114413

Open
@serhiy-storchaka

Description

@serhiy-storchaka

Bug report

Every recursive call of Python function increases the recursion_depth counter. Some recursive calls of C functions also increase it. A RecursionError is raised when it exceed the limit (sys.getrecursionlimit()).

The following code expectedly prints a value close to 1/2 of sys.getrecursionlimit() in all Python versions before 3.7 (including 2.7):

def test(n):
    if n > 0:
        a = []
        a.extend(test(n-1) for _ in [1])

for i in range(1000):
    try:
        test(i)
    except RecursionError:
        print(i)
        break

One for every test() function call and one for every generator expression. Both are shown in a traceback.

In Python 3.7 to 3.11 it prints 1/3 of the limit. Perhaps it adds one level for every list.extend() call?

In Python 3.12 it returns to 1/2 of the limit.

But in the main branch it only prints 1/6 of the limit. How can it increase the recursion_depth counter 6 times per the recursion level? It is a regression, because now the code that was successfully passed in previous versions starts to raise a RecursionError. See #111803 for example.

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions