Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python3': 0.05; 'concurrently': 0.07; 'cc:addr:python-list': 0.09; 'generators': 0.09; 'indeed,': 0.09; 'runtime': 0.09; 'thrown': 0.09; 'underlying': 0.09; 'thread': 0.10; 'python': 0.10; 'exception': 0.13; 'stack': 0.13; 'def': 0.13; 'value.': 0.15; '10:54': 0.16; 'cause.': 0.16; 'crashed': 0.16; 'crashes': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'generator.': 0.16; 'iterator': 0.16; 'thread.': 0.16; 'threading': 0.16; 'threads': 0.16; 'threads:': 0.16; 'trace.': 0.16; 'value"': 0.16; 'zero,': 0.16; 'wrote:': 0.16; 'tree': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'affected': 0.22; "aren't": 0.22; 'suppose': 0.22; 'trace': 0.22; 'second': 0.24; 'import': 0.24; '(most': 0.24; 'header:In- Reply-To:1': 0.24; "doesn't": 0.26; 'chris': 0.26; 'fri,': 0.27; 'handling': 0.27; 'message-id:@mail.gmail.com': 0.27; 'sequence': 0.27; 'yield': 0.27; 'fine': 0.28; 'record': 0.29; 'cat': 0.29; 'exclude': 0.29; 'itself,': 0.29; 'sure,': 0.29; "i'm": 0.30; 'subject:/': 0.30; 'skip:g 30': 0.30; "can't": 0.32; 'jump': 0.33; 'traceback': 0.33; 'case,': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'next': 0.35; 'could': 0.35; "isn't": 0.35; 'but': 0.36; 'list,': 0.36; 'should': 0.36; 'there': 0.36; 'cases': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'expect': 0.37; 'one,': 0.37; 'no,': 0.38; "won't": 0.38; 'skip:p 20': 0.38; 'why': 0.39; 'sure': 0.39; 'enough': 0.39; 'still': 0.40; 'some': 0.40; 'your': 0.60; 'back': 0.62; 'here.': 0.62; 'matter': 0.63; '(that': 0.63; 'different': 0.63; 'information': 0.63; '20,': 0.66; 'fact,': 0.67; 'records': 0.70; 'jul': 0.72; 'special': 0.73; 'chrisa': 0.84; 'divide': 0.84; 'gen': 0.84; 'pardon': 0.84; 'yielded': 0.84; 'to:none': 0.91; 'by.': 0.91 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=76QvFKrsO2LjOQwbHgKZ8cjbuf+C8Nim5NivTCi+CQI=; b=V6UB5qvx/9xdjbJRhHxA4oXNRr4uQ2EXrDbv2j7cHuEEVu2S6M/VvtvZgNEP8oTVur IptW7sDcjBcsmCNc9jAwQenqMByrLsVPUFHp87AP+Emo2X/lQH6O3EskYz6vcKsrqFRD BnrF/zAORuBJ/IS9b7qop40lP5wWvTU01wE2Vqz4J+4SZ6qZlSRQw20SvBs4K5hitNbS 8jWbWSpPzpaIBB44pzfLfbGzOTy4JeLvlHfeMGmTab49DTZKHKff6u9WpDBPeYyH1BcX q3vRX8xJVf4m9IXGtQ4LIJnHy4bi+WLbsZ/QW7SFX04qHTtNZ4jYDklxeEwrc5MlYqaj yFTw== MIME-Version: 1.0 X-Received: by 10.50.142.67 with SMTP id ru3mr9752343igb.16.1437138740387; Fri, 17 Jul 2015 06:12:20 -0700 (PDT) In-Reply-To: <55A8FAF6.5090104@rece.vub.ac.be> References: <87d1zunctp.fsf@elektro.pacujo.net> <87k2u2eu67.fsf@elektro.pacujo.net> <55A51662.4090007@rece.vub.ac.be> <55A75DE0.1070101@rece.vub.ac.be> <55A7B80B.6090905@rece.vub.ac.be> <55A7C094.7060604@rece.vub.ac.be> <55A8DD75.5000403@rece.vub.ac.be> <55A8EA7D.2040005@rece.vub.ac.be> <55A8FAF6.5090104@rece.vub.ac.be> Date: Fri, 17 Jul 2015 23:12:20 +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: 86 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437138743 news.xs4all.nl 2900 [2001:888:2000:d::a6]:36680 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94011 On Fri, Jul 17, 2015 at 10:54 PM, Antoon Pardon wrote: > On 07/17/2015 01:49 PM, Chris Angelico wrote: >> On Fri, Jul 17, 2015 at 9:43 PM, Antoon Pardon >> wrote: >> >> >>> Sure, but in this case, the generator is still active. The Runtime >>> would be able to jump to and somehow activates it's stack record >>> for the next value. So why would we expect it to be impossible to >>> include this trace back record in a stack trace? >> Python could also give you stack traces for any other threads that are >> concurrently running, on the off-chance that one of them affected it. >> But the only influence the generator has on the loop is to yield a >> value or signal termination; if an exception is thrown in the loop >> itself, the local name 'stuff' should have all the information about >> that cause. > > But the local name 'stuff' may only have the information for the immediate > cause. The underlying cause may be available in the generator. Suppose > you have a generator that should only generate positive numbers that you > use to divide some other number by. Your loop crashes because of a DivideByZeroError > Sure the local name shows the dividor to be zero, but you have no > information on why your generator produced a zero, but there may be a > clue in the trace back record of the generator. Indeed, but there's nothing special about generators here. The same sequence could have been a concrete list, or it could have been some other kind of iterator (any object with __iter__ and __next__), which won't have a stack frame. Special cases aren't special enough to warp exception handling around. >> Python isn't a mind-reader, no matter how much it may look >> like one, and it can't know that this function's return value should >> be shown as part of a completely different function's stack trace. > > It is not a matter of mindreading. And it is not a completely different > functions stack trace. It is the trace back record of a generator that > is used by the process/thread that crashed. And AFAIK an active generator > belongs to one specific thread. You can't have it yield a value to a different > thread and you can't send it a value from an other thread. So I really > see no reason to exclude the trace back records of active generators > from a stack trace of a crashed thread. No, generators are fine across threads: rosuav@sikorsky:~$ python3 threadgen.py Starting! First yielded value Continuing! Second yielded value Terminating. Traceback (most recent call last): File "threadgen.py", line 20, in print(next(gen)) StopIteration rosuav@sikorsky:~$ cat threadgen.py import threading import time def thread(): time.sleep(0.5) print(next(gen)) threading.Thread(target=thread).start() def generator():rosuav@sikorsky:~$ print("Starting!") yield "First yielded value" print("Continuing!") yield "Second yielded value" print("Terminating.") gen = generator() print(next(gen)) time.sleep(1) print(next(gen)) rosuav@sikorsky:~$ In fact, a generator doesn't have a stack unless it's currently executing, so all you could get is whatever's actually inside it (that is, if there's a deep tree of 'yield from's, you could dig up that part of the stack). I'm not sure this would really help you very much. ChrisA