Skip to content

functools.cache vs sys.setrecursionlimit #127346

Closed as not planned
Closed as not planned

Description

@gsbrodal

Bug report

Bug description:

When running the below code with Python 3.13.0 on Windows

import functools
import sys

sys.setrecursionlimit(1_000_000)

@functools.cache
def double(n):
    return 2 + double(n - 1) if n > 0 else 0

print(f'{double(2_000) = }')

I get the following error

return 2 + double(n - 1) if n > 0 else 0
           ~~~~~~^^^^^^^
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded"

It appears that the sys.setrecursionlimit does not carry over to functools.cache. The error does not occur when using Python 3.12.3.

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Activity

  1. Eclips4 commented on Nov 27, 2024

    @Eclips4
    Member

    Duplicate of #112215

  2. gsbrodal commented on Nov 27, 2024

    @gsbrodal
    Author

    Sorry for the slightly wrong info - the problem also persisted in Python 3.12 on Windows (I was not running the program with Python 3.12 on my Windows machine). I just downloaded the Windows binaries for Python 3.10.10, 3.11.7, and 3.12.7, and all have problems with increasing the recursionlimit and the functools.cache.

    I know increasing the recursion limit comes with a risk, so I ask for trouble. I was just hopping that I always could use functools.cache instead of writing my own @cache decorator, like the below. The below @cache decorator handles significantly higher recursion depths with Python 3.13.0 on Windows (and with 3.12 and 3.11, but not with 3.10).

    import sys
    sys.setrecursionlimit(1_000_000)
    
    def cache(f):
        answers = {}
        def wrapper(x):
            if x not in answers:
                answers[x] = f(x)
            return answers[x]
        return wrapper
    
    @cache
    def double(n):
        return 2 + double(n - 1) if n > 0 else 0
    
    print(f'{double(400_000) = }')  # prints 800_000
    
  3. Eclips4 commented on Nov 28, 2024

    @Eclips4
    Member

    Sorry for the slightly wrong info - the problem also persisted in Python 3.12 on Windows (I was not running the program with Python 3.12 on my Windows machine). I just downloaded the Windows binaries for Python 3.10.10, 3.11.7, and 3.12.7, and all have problems with increasing the recursionlimit and the functools.cache.

    I know increasing the recursion limit comes with a risk, so I ask for trouble. I was just hopping that I always could use functools.cache instead of writing my own @cache decorator, like the below. The below @cache decorator handles significantly higher recursion depths with Python 3.13.0 on Windows (and with 3.12 and 3.11, but not with 3.10).

    import sys
    sys.setrecursionlimit(1_000_000)
    
    def cache(f):
        answers = {}
        def wrapper(x):
            if x not in answers:
                answers[x] = f(x)
            return answers[x]
        return wrapper
    
    @cache
    def double(n):
        return 2 + double(n - 1) if n > 0 else 0
    
    print(f'{double(400_000) = }')  # prints 800_000
    

    Yeah, I understand your concerns, Gerth. Can you please continue discussion on #112215? Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions