setrecursionlimit

Steven D'Aprano steve at pearwood.info
Wed May 18 12:11:09 EDT 2016


The documentation for setrecursion limit warns against setting the limit too
high:

    [quote]
    The highest possible limit is platform-dependent. A user may need to
    set the limit higher when they have a program that requires deep
    recursion and a platform that supports a higher limit. This should
    be done with care, because a too-high limit can lead to a crash.
    [end quote]

https://docs.python.org/3/library/sys.html#sys.setrecursionlimit

Indeed, if you set the recursion limit too high, you can smash the memory
heap and get a segfault. How exactly does that work?

Why doesn't setrecursionlimit() raise an exception when you try to set it
too high? For example:

sys.setrecursionlimit(2000000000)

succeeds on my system, even though that's a ludicrously high number. (It is
more than half the number of bytes of memory my computer has.)


So why can't Python tell if I'm setting the limit too high?

(I'm assuming that if it could, it would.)




-- 
Steven




More information about the Python-list mailing list