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: "yield" causes strange behaviour
Type: Stage:
Components: Interpreter Core Versions: Python 2.5
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger, tebeka
Priority: normal Keywords:

Created on 2007-07-15 21:17 by tebeka, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
yield.py tebeka, 2007-07-15 21:17 yield.py
Messages (3)
msg32512 - (view) Author: Miki Tebeka (tebeka) * Date: 2007-07-15 21:17
Running the attached files produces:
ONE PASS

However if you uncomment line 46 and run the file again, you don't get any output.

A pdb session of the file produces:
[mtebeka@lakshmi:python-bug - 14:14] $pdb yield.py 
> /home/mtebeka/work/python-bug/yield.py(3)<module>()
-> from imaplib import IMAP4
(Pdb) b 72
Breakpoint 1 at /home/mtebeka/work/python-bug/yield.py:72
(Pdb) c
> /home/mtebeka/work/python-bug/yield.py(72)main()
-> one_pass()
(Pdb) s
--Call--
/usr/local/lib/python2.5/bdb.py:318: RuntimeWarning: tp_compare didn't return -1 or -2 for exception
  i = max(0, len(stack) - 1)
The program finished and will be restarted
> /home/mtebeka/work/python-bug/yield.py(3)<module>()
-> from imaplib import IMAP4
(Pdb) q
[mtebeka@lakshmi:python-bug - 14:15] $

(Sorry for the length of the example, this is the smaller version that still reproduces the bug)
msg32513 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2007-07-16 07:51
Miki, I don't see a bug here.  The presence of "yield" is significant during compilation not just when the line is executed.  When the yield appears anywhere in a function body, it is not a function anymore, it is a generator.  So, when the "yield" is uncommented, the call to one_pass returns a generator which doesn't start running until its next() method is called.  In contrast, when the "yield" is commented, you have a function that starts running immediately when one_pass() is called.

Here is a simplified comparison of the generator versus function:

>>> def one_pass():
...     print 'running'
...     yield 1
...
>>> g = one_pass()
>>> g.next()
running
1
>>> def one_pass():
...     print 'running'
...     # yield 1
...
>>> one_pass()
running


If you still see a bug, please elaborate.  If not, please close this report.
msg32514 - (view) Author: Miki Tebeka (tebeka) * Date: 2007-07-16 13:05
OUCH, forgot about that sorry.

Closing.
History
Date User Action Args
2022-04-11 14:56:25adminsetgithub: 45203
2007-07-15 21:17:25tebekacreate