Alexis Daboville added the comment:

I don't think it can be "fixed" with sys.setrecursionlimit for a few reasons:

* I think the issue arises when the AST is built. Otherwise if we put code 
before the if it would execute. But that's not the case (try putting a 
print('hello') before the if and it won't print anything).
  - This also means that you cannot directly call sys.setrecursionlimit in the 
file with the elifs.
  - Though we can set recursion limit using a second file which will then 
import the elifs file: I tried with different limits and CPython still crash in 
the same way (and always with the same number of elifs, roughly, because I 
didn't binary search for the exact amount of elifs).
  - sys.setrecursionlimit controls the stack size of the running Python 
program, while here we break C stack directly before running Python bytecode.
* When recursion limit is hit, an exception is raised, there's no segfault:
>>> def f():
...     f()
>>> f()
# plenty of omitted lines
RuntimeError: maximum recursion depth exceeded
* Having a RuntimeError raised would be nice, though 'maximum recursion depth 
exceeded' may not be the best possible error message as from a 'Python user' 
POV there's no recursion here.


A possible solution would be, I guess, to store elifs as excepts are stored. 
Instead of storing elifs recursively, the else part would just contain a list 
of if nodes (and if there is a else, well just store an if True node).

Though I don't know how difficult it would be to implement that, or if it's 
likely to break a lot of things which relies on ifs/elifs to be stored that way.


Python tracker <>
Python-bugs-list mailing list

Reply via email to