These 5000 nested generators work fine, this prints 5000:
it = [0]
for _ in range(5000):
it = (x+1 for x in it)
print(next(it))
But if I make just one tiny trivial function call in the middle like this:
def true():
return True
it = [0]
for i in range(5000):
if i != 2500:
it = (x+1 for x in it)
else:
it = (x+1 for x in it if true())
print(next(it))
Then I get a RecursionError:
Traceback (most recent call last):
File "/workspaces/cpython/benchmark.py", line 10, in <module>
print(next(it))
~~~~^^^^
File "/workspaces/cpython/benchmark.py", line 7, in <genexpr>
it = (x+1 for x in it)
^^
File "/workspaces/cpython/benchmark.py", line 7, in <genexpr>
it = (x+1 for x in it)
^^
File "/workspaces/cpython/benchmark.py", line 7, in <genexpr>
it = (x+1 for x in it)
^^
[Previous line repeated 2496 more times]
File "/workspaces/cpython/benchmark.py", line 9, in <genexpr>
it = (x+1 for x in it if true())
^^
RecursionError: maximum recursion depth exceeded
Note the “Previous line repeated 2496 more times”.
So apparently Python does count the recursive iteration towards the recursion limit (1000 by default), but lets me go far beyond the limit, unless I dare to make a call, and then it snaps at me.
This doesn’t seem right. Is that intentional / desirable?
Whenever you find something surprising that’s different between Python versions, a good place to start is the What’s New. You’ve noticed that there’s a difference between 3.14 and 3.10, possibly 3.11, so every change from 3.11 (maybe), 3.12, 3.13, or 3.14 is potentially relevant.
For something like this, searching those pages for “generator” and “recursion” would be a good start (though I would search for “recursi” so that it also catches “recursive”). I did exactly that, and came up with something in the very earliest version:
Now, I can’t say for certain that these generators are getting inlined, but it’s absolutely plausible. You’d have to do further checks to be sure.
Generally speaking, the recursion limit (and RecursionError when you blow past it) is an alternative to outright crashing, rather than being a restriction that you can impose on arbitrary code. If you want to enforce some sort of limit, you may need to look to other tools; it’s possible that audit hooks can do what you want, or maybe spin off a subprocess. But personally, the only reason that I would need to be concerned about this change in behaviour is if I had to support 3.10 and also had something deeply recursive.
(This is with Python 3.15.2)
What I see is that FOR_ITER is replaced by FOR_ITER_GEN in one of the generators. I’m guessing FOR_ITER_GEN is missing a check for if it exceeded the recursion limit? (similar issue to gh-132744 and gh-137883)
Makes me yet again wish that Python would finally get rid of the artificial limit. Back when it was there for preventing crashes due to C stack overflow, it was somewhat acceptable. But now that it can do deep recursion without C stack, it’s between annoying and infuriating. One thing I like about Python is that ints simply grow as large as needed, so I don’t have an artificial limit like 32 bits that I need to care about. I wish Python had the same mindset about recursion. Many things are naturally recursive and I want to do them that way, without an artificial limit screwing it up. Recently for example in more-itertools I saw that natural pretty four lines of recursion got replaced by 22 lines of iterative mess. That’s not good.
Like it currently doesn’t for that particular code. It does for other code, and I worry this will get “fixed” by adding a check that then does it for that code, too.
The recursion limit is there to prevent crashes. The reason it isn’t throwing it here is that the calls are being inlined, meaning that there isn’t a limit, and so, there’s no risk of a crash.
If anything changes along those lines, it’s more likely to be other things optimizing away recursion, and thus fewer RecursionErrors.
What do you mean with “there isn’t a limit”? And are you saying that in the other variations, the calls aren’t inlined? Even the following, where I solely changed the base from [0] to map(lambda x: x, [0]), gives me RecursionError. Does that prevent the inlining of everything else?
it = map(lambda x: x, [0])
for _ in range(5000):
it = (x+1 for x in it)
print(next(it))
That “What’s New” that you linked also says that the inlining means recursive functions can recurse significantly deeper “if the user increases the recursion limit with sys.setrecursionlimit()”. So I really don’t get what you mean with “the calls are being inlined, meaning that there isn’t a limit”.