Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin2!goblin.stu.neva.ru!cyclone02.ams2.highwinds-media.com!news.highwinds-media.com!voer-me.highwinds-media.com!lightspeed.eweka.nl!lightspeed.eweka.nl!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; '16,': 0.03; 'debug': 0.04; 'except:': 0.07; 'stack.': 0.07; '%r"': 0.09; 'exception,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'jan': 0.11; '(at': 0.13; 'stack': 0.13; 'def': 0.13; 'thu,': 0.15; 'variables': 0.15; '7:45': 0.16; 'py3': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'stack:': 0.16; 'stuff,': 0.16; 'traceback.': 0.16; 'useless.': 0.16; 'which,': 0.16; 'wrote:': 0.16; 'debugging': 0.18; 'tree': 0.18; 'try:': 0.18; 'widget': 0.18; '2015': 0.20; 'trace': 0.22; 'am,': 0.23; 'code.': 0.23; "haven't": 0.24; 'header:In-Reply-To:1': 0.24; 'feature': 0.24; 'header:User- Agent:1': 0.26; "doesn't": 0.26; 'example': 0.26; 'header:X -Complaints-To:1': 0.26; 'chris': 0.26; 'function': 0.28; 'helpful.': 0.29; 'node': 0.29; 'there.': 0.30; 'subject:/': 0.30; 'code': 0.30; 'point': 0.33; 'useful': 0.33; 'idle': 0.33; 'traceback': 0.33; 'except': 0.34; 'running': 0.34; 'next': 0.35; 'displays': 0.35; 'tool': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'skip:z 10': 0.38; 'sure': 0.39; 'easily': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'jul': 0.72; 'actually,': 0.84; 'locals': 0.84; 'pardon': 0.84; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Possibly Pythonic Tail Call Optimization (TCO/TRE) Date: Thu, 16 Jul 2015 16:19:37 -0400 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> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 In-Reply-To: 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: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437078001 news.xs4all.nl 2832 [2001:888:2000:d::a6]:51663 X-Complaints-To: abuse@xs4all.nl X-Received-Body-CRC: 1197983038 X-Received-Bytes: 5805 Xref: csiph.com comp.lang.python:93963 On 7/16/2015 7:45 AM, Chris Angelico wrote: > On Thu, Jul 16, 2015 at 5:31 PM, Antoon Pardon > wrote: >> 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. One of the features of Idle is Debug => Stack Viewer, which, when invoked immediately after an exception, displays a tree widget with a node for each stack frame in the traceback. Running your code without the extra try: except: stuff, so a traceback is displayed, I easily see that x is -12, -2, and 0 in f, g, and h. I suspect that this feature is not well known and is underused. -- Terry Jan Reedy