This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: tail optimization for 'yield from'
Type: enhancement Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Robert Smart, benjamin.peterson, terry.reedy
Priority: normal Keywords:

Created on 2018-01-03 10:47 by Robert Smart, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg309407 - (view) Author: Robert Smart (Robert Smart) Date: 2018-01-03 10:47
When a generator procedure ends with "yield from" it would be nice if this was handled efficiently (just replace the generator with the new source). Because it is natural to push things back into a generator with

def prependGen(hd,tl):
    yield hd
    yield from tl
msg309529 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-01-05 22:10
I think this should have been first floated on python-ideas list.  One objection is the same as for eliminating tail calls in general: it collapses tracebacks.

def g1():
    yield from g2()
def g2():
    yield 1/0
for i in g1(): pass

Traceback (most recent call last):
  File "F:\Python\a\tem.py", line 7, in <module>
    for i in g1(): pass
  File "F:\Python\a\tem.py", line 2, in g1
    yield from g2()
  File "F:\Python\a\tem.py", line 5, in g2
    yield 1/0
ZeroDivisionError: division by zero

There was at least some discussion of making 'yield from' internally more efficient without actual replacement.  I don't know what the current CPython implementation does.
msg309536 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2018-01-06 07:05
The original yield from implementation did something like this, but we dropped it because it caused debuggability problems. See #14230
History
Date User Action Args
2022-04-11 14:58:56adminsetgithub: 76667
2018-01-06 07:05:42benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg309536

resolution: rejected
stage: resolved
2018-01-05 22:10:24terry.reedysetnosy: + terry.reedy
messages: + msg309529
2018-01-05 00:25:09brett.cannonsetversions: + Python 3.7, - Python 3.6
2018-01-05 00:25:00brett.cannonsettype: resource usage -> enhancement
2018-01-03 10:47:38Robert Smartcreate