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 in __init__ causes broken new-style class
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: mwh, rhettinger, stevea_zope
Priority: normal Keywords:

Created on 2005-03-03 15:42 by stevea_zope, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pythonbug.py stevea_zope, 2005-03-03 15:44 script that reproduces the problem
Messages (4)
msg24439 - (view) Author: Steve Alexander (stevea_zope) Date: 2005-03-03 15:42
 class Foo(object):
 
     def __init__(self):
         print "foo"
         raise RuntimeError
         yield 23

 Foo()

With a classic class, this correctly fails saying
"__init__ must return None".  With a new-style class,
the class suite works, and Foo() is called without
reporting an error, but also without printing "foo".
msg24440 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2005-03-03 16:02
Logged In: YES 
user_id=6656

You don't have to be that cute:

>>> class Foo(object):
...  def __init__(self):
...   return "foo"
... 

where did that check go?
msg24441 - (view) Author: Steve Alexander (stevea_zope) Date: 2005-03-03 16:46
Logged In: YES 
user_id=492001

Albertas Agejevas has pointed out that:

1. New style classes don't care what is returned from __init__.

2. If __init__ contains a yield, the code in __init__ won't
be called until the generator that is returned has its
'.next()' method called.

Which all makes sense.  However, someone I work with was
trying for ages to work out what a particular __init__
method was not being called.  The __init__ method was rather
long, and there was a yield, copy-pasted from some other
code, hidden behind a raise.

So, the behaviour may be correct, but I don't think it is
very helpful. 
msg24442 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-03-03 16:56
Logged In: YES 
user_id=80475

Fixed.
See typeobject.c 2.264 and 2.264.2.1.
History
Date User Action Args
2022-04-11 14:56:10adminsetgithub: 41645
2005-03-03 15:42:59stevea_zopecreate