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


Groups > comp.lang.python > #108777

Re: setrecursionlimit

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Kaynor <ckaynor@zindagigames.com>
Newsgroups comp.lang.python
Subject Re: setrecursionlimit
Date Wed, 18 May 2016 09:56:48 -0700
Lines 56
Message-ID <mailman.13.1463590630.27390.python-list@python.org> (permalink)
References <573c941f$0$22141$c3e8da3$5496439d@news.astraweb.com> <d75fdf2c-fa98-4d6d-93ba-47a63c330e16@googlegroups.com> <CALvWhxv=BL+dNQVwrT=o6n=a2P31zfwOyW-xEs35RYwD6OMv3g@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de ndxba9eedpTbOAz5Bv3yjAyra2JHhuRfCYn5g6ftp77w==
Return-Path <ckaynor@zindagigames.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'root': 0.04; 'compiler': 0.05; 'cpython': 0.05; 'subject:skip:s 10': 0.05; 'used.': 0.05; 'apis': 0.07; 'executable': 0.07; 'mentioned,': 0.07; 'stack.': 0.07; 'url:msdn': 0.07; 'grow,': 0.09; 'positional': 0.09; 'stack,': 0.09; 'used).': 0.09; 'python': 0.10; 'stack': 0.13; 'wed,': 0.15; '(but': 0.15; 'variables': 0.15; '2016': 0.16; '[1].': 0.16; '[2].': 0.16; 'allocates': 0.16; 'call?': 0.16; "compiler's": 0.16; 'compiler,': 0.16; 'compilers': 0.16; 'fit,': 0.16; 'overflow.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'threads': 0.16; 'wrote:': 0.16; 'obviously': 0.16; 'memory': 0.17; 'basically': 0.18; 'bytes': 0.18; 'library,': 0.18; 'pointer': 0.18; 'typical': 0.18; 'creates': 0.18; 'math': 0.20; 'windows': 0.20; 'to:name:python-list@python.org': 0.20; 'fairly': 0.22; '64-bit': 0.22; 'are.': 0.22; 'arguments': 0.22; 'level,': 0.22; 'made.': 0.22; 'space.': 0.22; 'am,': 0.23; 'fit': 0.23; '(like': 0.23; 'header:In-Reply-To:1': 0.24; 'linux': 0.26; 'rest': 0.26; 'chris': 0.26; 'figure': 0.27; 'right.': 0.27; 'message-id:@mail.gmail.com': 0.27; '[2]': 0.27; 'allocated': 0.27; 'said,': 0.27; 'function': 0.28; '32-bit': 0.29; 'arguments,': 0.29; 'crash': 0.29; 'depth': 0.29; 'operators': 0.29; 'convert': 0.29; "i'm": 0.30; 'creating': 0.30; 'convention': 0.30; 'skip:[ 10': 0.31; 'guess': 0.31; 'rules': 0.31; 'certain': 0.31; 'especially': 0.32; 'knows': 0.32; '[1]': 0.32; 'compiled': 0.32; 'generally': 0.32; 'problem': 0.33; 'conventions': 0.33; 'int': 0.33; '(for': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'exist': 0.35; 'sometimes': 0.35; 'there': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'basic': 0.36; 'keyword': 0.36; 'others.': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'skip:& 10': 0.37; 'method': 0.37; 'received:209': 0.38; 'received:209.85.220': 0.38; 'means': 0.39; 'data': 0.39; 'sure': 0.39; 'application': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'determine': 0.61; 'more': 0.63; 'within': 0.64; 'deeply': 0.64; 'believe': 0.66; 'here': 0.66; 'results': 0.66; 'fact,': 0.67; '8bit%:27': 0.72; 'exceed': 0.72; 'url:en-us': 0.72; 'grow': 0.75; 'sounds': 0.76; '9:19': 0.84; 'batchelder': 0.84; 'calls,': 0.84; 'ease.': 0.84; 'growing.': 0.84; 'valid,': 0.84; 'good,': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=zindagigames-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=9ksvKi9YLxrpVSucyIHc0R/G09fUL/tKh8SP1Ya3SK0=; b=ibY5t6oN74NTR6f3lkE7zwM1uFJ8CwQeFBCSxlWB6vU9sJzf+VyFzNhSkr8n927El+ xFCWpLGZWqgoB5D6eX1fq2Yyh0CY+IuE9u88PH/es2OslbTUb8a6RDJ9d66YaR4oG8Ph XlWFFot9BxZPto5tcn5H4YVhg/uAjF/eHhxSINhynpTUUABw0b4zItGtycOdKgH+n45v s5tgvvGq2Iz1H4onFoQQDTz3bOYb9y7rNirKAvBfOWFmLbca/o8LZsdx2ck651tTEeaQ nDeNShfCiOICVMTNahFVZYXAq5pUyj3ENWM13JMT3LlJFKOa7JXJEV0XBR4Yo0v33Qbw 2UzQ==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=9ksvKi9YLxrpVSucyIHc0R/G09fUL/tKh8SP1Ya3SK0=; b=BZGb79KmmSUVnPPpSUoWRBceYjc0xcoJb2GbGG9kr1meUuXYQDbpDs7Yc2DOIEU+ny q8fhBVyNePX6J95wLw5HN6NVko9gbhW4xUGT/92MVcClZiTX8sdXCgZc4TXrdIU3NSBe 54/EEkIOpPFwyW0/ZkobTyEesmC2a0i8JxtktYJQgQ68/BxwIhjRI7V4MqHu5ZdB0sx1 c0lAXNeYEgEwwKR2LdRgDFEJXFVkWgP2CLPZFvagfTptt9qYg/XjuN4fhXYV2kjC1WG4 tZKmnVUy1ZQN9okyQg9Rhk2soNEjRZLnuj6s3kcGU26rPQ6WKNBivhgGDfDsSK/wMsrk tj6g==
X-Gm-Message-State AOPr4FUsgyFFC3K3xzhlwsrR5QYS1+slz3/Br7yMAT6nhOjYMhsWJyQluuowGdVgretxc5Z9Tw/54Sse290WXA==
X-Received by 10.55.140.69 with SMTP id o66mr9267999qkd.188.1463590628336; Wed, 18 May 2016 09:57:08 -0700 (PDT)
In-Reply-To <d75fdf2c-fa98-4d6d-93ba-47a63c330e16@googlegroups.com>
X-Content-Filtered-By Mailman/MimeDel 2.1.22
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <CALvWhxv=BL+dNQVwrT=o6n=a2P31zfwOyW-xEs35RYwD6OMv3g@mail.gmail.com>
X-Mailman-Original-References <573c941f$0$22141$c3e8da3$5496439d@news.astraweb.com> <d75fdf2c-fa98-4d6d-93ba-47a63c330e16@googlegroups.com>
Xref csiph.com comp.lang.python:108777

Show key headers only | View raw


On Wed, May 18, 2016 at 9:19 AM, Ned Batchelder <ned@nedbatchelder.com>
wrote:

> I believe the issue here is that Python recursion results in the C stack
> growing.  Each Python function call creates a number of C function calls,
> which grows the C stack.  The crash you see is the C stack overflowing.
>
> Is there a way to know how large the C stack can grow, and how much it
> will grow for each Python function call? That sounds complicated to get
> right.
>

I'm fairly sure that it is, in fact, basically impossible to get right.

Some Python calls will use more memory on the C stack than others. They may
call more C functions internally, or C functions that require more stack
space. In the most extreme example, you could have a single Python call
crash (for example, having a super deeply recursive call as a single Python
call), or you could theoretically have extremely deep Python recursion
without a problem (presuming optimizations that do not exist in CPython for
a number of, generally good, reasons).

Even in more typical cases, I believe a Python call with keyword arguments
requires more stack than one with only positional arguments, which may
require more than one with no arguments (depending on which CPython APIs
were used). Additionally, even within the standard library, some of the
native calls will require more stack (think OS calls) than most of the
basic math functions and simple operators (like int add).

The root of the issue is that, much of the time, C function arguments are
allocated on the stack, if they exceed certain limits based on the calling
convention used. 32-bit Windows only allocates the "this" pointer as a
register, all other arguments are stack [1]. 64-bit Windows allocates up-to
4 word arguments as registers, and the rest on the stack [2]. I do not know
what the Linux conventions are. Naturally, these rules may vary based on
the compiler, for any functions the compiler knows is being compiled by the
compiler - the rules listed for the OS are only required for OS calls,
however most compilers will follow them for ease. Additionally, any local
variables that do not fit in registers will be offloaded to the stack, and
sometimes they will be offloaded even if they do fit, at the compiler's
decision, especially if function calls are made.

All of this means that, as Ned mentioned, it is very complicated to figure
out a *recursion* depth that will cause a stack overflow. Generally, there
will be an OS method to determine the stack size in *bytes*, however (and
often, the application can control this when creating threads and in
executable meta data for the main thread). There is basically no way to
convert that bytes to a recursion level, however - the best you can do is
as Rob said, see that a depth is obviously higher than valid, as it is a
reasonable (but not guaranteed) guess that each recusion will use some
number of bytes of stack.

[1] https://msdn.microsoft.com/en-us/library/984x0h58.aspx
[2] https://msdn.microsoft.com/en-us/library/zthk2dkh.aspx

Chris

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


Thread

setrecursionlimit Steven D'Aprano <steve@pearwood.info> - 2016-05-19 02:11 +1000
  Re: setrecursionlimit Ned Batchelder <ned@nedbatchelder.com> - 2016-05-18 09:19 -0700
    Re: setrecursionlimit Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-05-18 16:29 +0000
      Re: setrecursionlimit Steven D'Aprano <steve@pearwood.info> - 2016-05-19 03:15 +1000
        Re: setrecursionlimit Ian Kelly <ian.g.kelly@gmail.com> - 2016-05-18 11:38 -0600
        Re: setrecursionlimit Chris Kaynor <ckaynor@zindagigames.com> - 2016-05-18 10:47 -0700
          Re: setrecursionlimit breamoreboy@gmail.com - 2016-05-18 14:43 -0700
            Re: setrecursionlimit Rustom Mody <rustompmody@gmail.com> - 2016-05-18 20:19 -0700
              Re: setrecursionlimit Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-05-19 21:04 +1200
        Re: setrecursionlimit Christian Gollwitzer <auriocus@gmx.de> - 2016-05-18 20:11 +0200
        Re: setrecursionlimit Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-05-19 21:00 +1200
    Re: setrecursionlimit Chris Kaynor <ckaynor@zindagigames.com> - 2016-05-18 09:56 -0700
    Re: setrecursionlimit Nobody <nobody@nowhere.invalid> - 2016-05-19 11:28 +0100
  Re: setrecursionlimit Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-05-19 10:27 +0100

csiph-web