Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'stack.': 0.07; 'cc:addr:python-list': 0.09; '%r"': 0.09; 'runtime': 0.09; '(at': 0.13; 'stack': 0.13; 'def': 0.13; 'thu,': 0.15; 'variables': 0.15; 'detects': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'gregory': 0.16; 'himself.': 0.16; 'py3': 0.16; 'stack:': 0.16; 'useless.': 0.16; 'wrote:': 0.16; 'debugging': 0.18; 'say,': 0.18; 'try:': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'trace': 0.22; 'am,': 0.23; 'code.': 0.23; "haven't": 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'example': 0.26; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'helpful.': 0.29; 'tail': 0.29; 'there.': 0.30; 'subject:/': 0.30; 'point': 0.33; 'useful': 0.33; 'traceback': 0.33; 'except': 0.34; 'received:google.com': 0.35; 'next': 0.35; 'nothing.': 0.35; 'but': 0.36; 'should': 0.36; 'tool': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'skip:z 10': 0.38; 'someone': 0.38; 'sure': 0.39; 'whatever': 0.39; 'choose': 0.68; 'records': 0.70; 'jul': 0.72; 'actually,': 0.84; 'calls,': 0.84; 'chrisa': 0.84; 'locals': 0.84; 'pardon': 0.84; 'to:none': 0.91; 'hair': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=/Ozglr9UVQIsZ8yFGFETxdxiBq59BUkC+tJ719E7G1M=; b=BPt21cKBrjmNyfSUF/p1REWh/SGsL6VxcvL4KQwSQKXwloPskBQWzj5hRCOSlViQOz nAEYgQEY7EwyRdJsRs898Xb2PyDFxs9O0yIMUOT8qEaSILhvhhTt/6p+krCz3X2JTcA2 G29dYRD7uRrKRNlXRzD72lbEin0I1QheM9Gnz5ZNvPWhOeATV+Ul0C8E+7Dq0D8DW0FM U6e+PQ89e9dGPHfjf4QYb7z0btH0UBogUgsZuly0FHfkmyiQPf7kGWiVTNCu5BMwTspl VWzTXfDAbntVVNQ7F5ftpRRlEyJkNsoaOEicd2YUfbydYVzweGWT156c+dou2Ns0FUIJ XLLQ== MIME-Version: 1.0 X-Received: by 10.50.134.196 with SMTP id pm4mr2755165igb.6.1437047128922; Thu, 16 Jul 2015 04:45:28 -0700 (PDT) In-Reply-To: <55A75DE0.1070101@rece.vub.ac.be> References: <55A3A853.4040006@rece.vub.ac.be> <55A3C366.6060602@rece.vub.ac.be> <87fv4r1fre.fsf@jester.gateway.sonic.net> <87bnff1eks.fsf@jester.gateway.sonic.net> <87d1zunctp.fsf@elektro.pacujo.net> <87k2u2eu67.fsf@elektro.pacujo.net> <55A51662.4090007@rece.vub.ac.be> <55A75DE0.1070101@rece.vub.ac.be> Date: Thu, 16 Jul 2015 21:45:28 +1000 Subject: Re: Possibly Pythonic Tail Call Optimization (TCO/TRE) From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437047137 news.xs4all.nl 2886 [2001:888:2000:d::a6]:52321 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.stben.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:93925 On Thu, Jul 16, 2015 at 5:31 PM, Antoon Pardon wrote: > On 07/16/2015 12:43 AM, Gregory Ewing wrote: > >> Antoon Pardon wrote: >>> But it doesn't need to be all or nothing. How about the following possibility. >>> When the runtime detects a serie of tail calls, it will keep the bottom three >>> and the top three backtrace records of the serie. >> Whatever value you choose for N, keeping only the >> first/last N traceback frames will lead to someone >> tearing their hair out. > > I would say, that someone should get over himself. > Traceback are not the only or even the most useful > tool for debugging code. The current stack trace > doesn't even contain the value's of the variables > on the stack. So in case of Terry Reedy's example > that stack trace would IMO have been next to useless. Actually, they do contain all of that (at least, they do in Py3 - not sure about Py2 as I haven't checked). You can poke around with the locals at every point on the stack: def f(x): if x < 10: g(x+10) return 5 def g(x): if x % 3: h(x + 2) return 7 def h(x): return 1/x try: x = -12 print(f(x)) except ZeroDivisionError as e: tb = e.__traceback__ while tb: fr = tb.tb_frame print("In function %s (%s:%d), x = %r" % ( fr.f_code.co_name, fr.f_code.co_filename, fr.f_lineno, fr.f_locals["x"], )) tb = tb.tb_next It's all there. And it's immensely helpful. ChrisA