Skip to content

Commit 14cf886

Browse files
author
rhettinger
committed
SF bug #1155938: Missing None check for __init__().
git-svn-id: http://svn.python.org/projects/python/trunk@38551 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent dd7c3f3 commit 14cf886

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

Lib/test/test_descr.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3965,6 +3965,18 @@ class C(object):
39653965
import gc; gc.collect()
39663966
vereq(hasattr(c, 'attr'), False)
39673967

3968+
def test_init():
3969+
# SF 1155938
3970+
class Foo(object):
3971+
def __init__(self):
3972+
return 10
3973+
try:
3974+
Foo()
3975+
except TypeError:
3976+
pass
3977+
else:
3978+
raise TestFailed, "did not test __init__() for None return"
3979+
39683980

39693981
def test_main():
39703982
weakref_segfault() # Must be first, somehow
@@ -4058,6 +4070,7 @@ def test_main():
40584070
carloverre()
40594071
filefault()
40604072
vicious_descriptor_nonsense()
4073+
test_init()
40614074

40624075
if verbose: print "All OK"
40634076

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 2.5 alpha 1?
1010
Core and builtins
1111
-----------------
1212

13+
- Bug #1155938: new style classes did not check that __init__() was
14+
returning None.
15+
1316
- Patch #802188: Report characters after line continuation character
1417
('\') with a specific error message.
1518

Objects/typeobject.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4753,6 +4753,12 @@ slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
47534753
Py_DECREF(meth);
47544754
if (res == NULL)
47554755
return -1;
4756+
if (res != Py_None) {
4757+
PyErr_SetString(PyExc_TypeError,
4758+
"__init__() should return None");
4759+
Py_DECREF(res);
4760+
return -1;
4761+
}
47564762
Py_DECREF(res);
47574763
return 0;
47584764
}

0 commit comments

Comments
 (0)