Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'present,': 0.07; 'returned.': 0.07; 'attribute.': 0.09; 'generators': 0.09; 'iterate': 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; 'exception': 0.13; 'def': 0.13; 'subsequent': 0.15; 'called,': 0.16; 'inactive.': 0.16; 'instance:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'something.': 0.16; 'traceback.': 0.16; 'tup': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'instance,': 0.18; "aren't": 0.22; 'function,': 0.22; 'am,': 0.23; 'header:In-Reply- To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'chris': 0.26; 'figure': 0.27; 'error': 0.27; 'room': 0.27; 'yield': 0.27; 'function': 0.28; 'idea': 0.28; 'strings,': 0.29; 'sure,': 0.29; 'subject:/': 0.30; 'that.': 0.30; 'point': 0.33; 'traceback': 0.33; 'case,': 0.34; 'that,': 0.34; 'list': 0.34; 'next': 0.35; 'could': 0.35; 'instance': 0.35; 'something': 0.35; 'but': 0.36; 'list,': 0.36; 'there': 0.36; 'possible.': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'received:org': 0.37; 'thought': 0.37; 'list.': 0.37; 'things': 0.38; 'itself': 0.38; 'stuff': 0.38; "didn't": 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'still': 0.40; 'some': 0.40; 'yes': 0.62; 'above,': 0.63; 'more': 0.63; 'state,': 0.66; 'gen': 0.84; 'imagine,': 0.84; "it'd": 0.84; 'pardon': 0.84; 'received:fios.verizon.net': 0.91; 'suspended': 0.91; 'from.': 0.93; 'sitting': 0.93 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: Fri, 17 Jul 2015 16:27:49 -0400 References: <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> <55A7B80B.6090905@rece.vub.ac.be> <55A7C094.7060604@rece.vub.ac.be> <55A8DD75.5000403@rece.vub.ac.be> <55A8EA7D.2040005@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: <55A8EA7D.2040005@rece.vub.ac.be> 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437164908 news.xs4all.nl 2953 [2001:888:2000:d::a6]:36547 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94030 On 7/17/2015 7:43 AM, Antoon Pardon wrote: > On 07/17/2015 01:05 PM, Chris Angelico wrote: >> def gen(): >> yield stuff >> yield more stuff >> >> for stuff in gen(): >> bomb with exception >> >> The error didn't happen in the generator, so I wouldn't expect to see >> it in the traceback. > > Yes something like that. And I wouldn't expect it either but if it > is not present, is it because nobody thought about it or because it > is a bad idea or an idea difficult to implement? > >> There's still room for the cause of an error to >> not be in the traceback; imagine, for instance, a function that >> populates a concrete list, and then you iterate over the list. If that >> function sticks a None into the list and the subsequent processing is >> expecting all strings, that's going to bomb, but then you have to >> figure out where the None came from. If the traceback could include >> that, it'd be great, but some things aren't possible. > > Sure, but in this case, the generator is still active. No more than any other object sitting around inactive. Calling a generator function like gen above returns a generator with the generator function, in a suspended inactive state, attached as an attribute. When the generator.__next__ function is called, it activates its instance of the generator function, which suspends itself again after yielding something. At the point of the exception above, the generator next function has returned. There could be multiple generators with suspended generator functions sitting around. For instance: def f(): for tup in zip(gf0, gf1, gf2, gf3, gf4, gf5): a = tup[6] # whoops, IndexError -- Terry Jan Reedy