[Python-Dev] stack check on Unix: any suggestions?

M.-A. Lemburg mal@lemburg.com
Tue, 29 Aug 2000 21:42:08 +0200


Jeremy Hylton wrote:
> 
> >>>>> "MAL" == M -A Lemburg <mal@lemburg.com> writes:
> 
>   >> I don't see an obvious solution.  Is there any way to implement
>   >> PyOS_CheckStack on Unix?  I imagine that each platform would have
>   >> its own variant and that there is no hope of getting them
>   >> debugged before 2.0b1.
> 
>   MAL> I've looked around in the include files for Linux but haven't
>   MAL> found any APIs which could be used to check the stack size.
>   MAL> Not even getrusage() returns anything useful for the current
>   MAL> stack size.
> 
> Right.
> 
>   MAL> For the foo() example I found that on my machine the core dump
>   MAL> happens at depth 9821 (counted from 0), so setting the
>   MAL> recursion limit to something around 9000 should fix it at least
>   MAL> for Linux2.
> 
> Right.  I had forgotten about the MAX_RECURSION_LIMIT.  It would
> probably be better to set the limit lower on Linux only, right?  If
> so, what's the cleanest was to make the value depend on the platform.

Perhaps a naive test in the configure script might help. I used
the following script to determine the limit:

import resource
i = 0    
def foo(x):
    global i
    print i,resource.getrusage(resource.RUSAGE_SELF)   
    i = i + 1
    foo(x)
foo(None)

Perhaps a configure script could emulate the stack requirements
of eval_code2() by using declaring a buffer of a certain size.
The script would then run in a similar way as the one
above printing the current stack depth and then dump core at
some point. The configure script would then have to remove the
core file and use the last written  depth number as basis
for setting the MAX_RECURSION_LIMIT.

E.g. for the above Python script I get:

9818 (4.2199999999999998, 0.48999999999999999, 0, 0, 0, 0, 1432, 627, 0, 0, 0, 0, 0, 0, 0, 0)
9819 (4.2199999999999998, 0.48999999999999999, 0, 0, 0, 0, 1432, 627, 0, 0, 0, 0, 0, 0, 0, 0)
9820 (4.2199999999999998, 0.48999999999999999, 0, 0, 0, 0, 1432, 627, 0, 0, 0, 0, 0, 0, 0, 0)
9821 (4.2199999999999998, 0.48999999999999999, 0, 0, 0, 0, 1432, 627, 0, 0, 0, 0, 0, 0, 0, 0)
Segmentation fault (core dumped)

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/