Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #22257

Re: python segfault

References <20120327222757.GA22125@terra.cms.at>
From Chris Kaynor <ckaynor@zindagigames.com>
Date 2012-03-27 15:40 -0700
Subject Re: python segfault
Newsgroups comp.lang.python
Message-ID <mailman.1054.1332888026.3037.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Mar 27, 2012 at 3:27 PM, Michael Poeltl
<michael.poeltl@univie.ac.at> wrote:
> hi,
>
> can anybody tell why this 'little stupid *thing* of code' let's python-3.2.2, 2.6.X or python 2.7.2 segfault?
>
>>> def get_steps2(pos=0, steps=0):
> ...     if steps == 0:
> ...         pos = random.randint(-1,1)
> ...     if pos == 0:
> ...         return steps
> ...     steps += 2
> ...     pos += random.randint(-1,1)
> ...     return get_steps2(pos,steps)
> ...
>>>> import random, sys
>>>> sys.setrecursionlimit(100000)

If you remove this setrecursionlimit, it will throw an exception. The
issue is that your code is overflowing the C stack by trying to make
too many calls. The Python recursion limit prevents this by turning
such cases into Python exceptions prior to the stack overflow.

As your recursion is based on a random number generator, all you need
is a sequence of random numbers that is unbalanced between -1 and 1
results for 1000-3000 calls (the exact number will vary from os,
compiler, maybe machine, etc), which is not too unlikely to occur.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: python segfault Chris Kaynor <ckaynor@zindagigames.com> - 2012-03-27 15:40 -0700

csiph-web