classification
Title: sys.setrecursionlimit: OverflowError still raised when int limited in sys.maxsize
Type: behavior Stage: resolved
Components: C API, Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, skrah, wyz23x2
Priority: normal Keywords:

Created on 2020-07-16 07:47 by wyz23x2, last changed 2020-07-16 21:58 by skrah. This issue is now closed.

Messages (6)
msg373747 - (view) Author: wyz23x2 (wyz23x2) * Date: 2020-07-16 07:47
Consider this code:
import sys
sys.setrecursionlimit(sys.maxsize)
Causes this:
OverflowError: Python int too large to convert to C int
So what is the limit? It should be sys.maxsize.
These 2 also don't work:
sys.setrecursionlimit(sys.maxsize-1)
sys.setrecursionlimit(sys.maxsize//2)
That is a big difference with at least 50%.
msg373748 - (view) Author: wyz23x2 (wyz23x2) * Date: 2020-07-16 07:52
Needs to add 10 zeros (sys.maxsize//10000000000) to get it work. //20000000000 doesn't work.
Python version: 3.8.4
Platform: Windows 10 2004
msg373749 - (view) Author: wyz23x2 (wyz23x2) * Date: 2020-07-16 07:56
Tested. 2**31-31 is the max, which is 2147483617, compared to sys.maxsize's 9223372036854775807!
msg373751 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-07-16 08:17
The recursion depth and recursion limit are stored internally as C ints, so yes, 2**31 - 1 = 2147483647 is the maximum value that you can pass to `sys.setrecursionlimit` that won't fail immediately.

But it's unclear why you'd ever want to set the recursion limit that high. What's your goal here? Are you looking for a way to effectively disable that recursion limit altogether? If so, can you explain the use-case?

Note that the recursion limit is there to prevent your code from exhausting the C stack and segfaulting as a result: simply setting that limit to a huge value won't (by itself) allow you to run arbitrarily deeply nested recursions. On my machine, without the protection of the recursion limit, a simple recursive call segfaults at around a depth of 30800.
msg373761 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-07-16 19:02
Setting to pending; I don't see any bug here, and I suspect the original report was based on a misunderstanding of what sys.setrecursionlimit is for.
msg373774 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2020-07-16 21:58
Mark has already mentioned that setting the recursion limit to a
value higher than 2000-10000 makes no sense.

Apart from that, sys.maxsize is actually documented like this:

   maxsize -- the largest supported length of containers.


So it applies to Py_ssize_t (signed version of size_t), but not to
any C integer in general.
History
Date User Action Args
2020-07-16 21:58:52skrahsetstatus: pending -> closed

nosy: + skrah
messages: + msg373774

stage: resolved
2020-07-16 19:02:04mark.dickinsonsetstatus: open -> pending
resolution: not a bug
messages: + msg373761
2020-07-16 09:04:55eric.smithsettitle: OverflowError still raised when int limited in sys.maxsize -> sys.setrecursionlimit: OverflowError still raised when int limited in sys.maxsize
2020-07-16 08:17:57mark.dickinsonsetnosy: + mark.dickinson
messages: + msg373751
2020-07-16 07:56:14wyz23x2settype: behavior
messages: + msg373749
2020-07-16 07:52:21wyz23x2setmessages: + msg373748
2020-07-16 07:47:19wyz23x2create