From: Raymond Hettinger Date: Thu, 3 Mar 2005 16:45:19 +0000 (+0000) Subject: SF bug #1155938: Missing None check for __init__(). X-Git-Tag: v2.5a0~1971 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b67cc80bb915680190eaf1c9feba8fe0799c83f8;p=python SF bug #1155938: Missing None check for __init__(). --- diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index c1bd00d84b..7eea465c82 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -3965,6 +3965,18 @@ def vicious_descriptor_nonsense(): import gc; gc.collect() vereq(hasattr(c, 'attr'), False) +def test_init(): + # SF 1155938 + class Foo(object): + def __init__(self): + return 10 + try: + Foo() + except TypeError: + pass + else: + raise TestFailed, "did not test __init__() for None return" + def test_main(): weakref_segfault() # Must be first, somehow @@ -4058,6 +4070,7 @@ def test_main(): carloverre() filefault() vicious_descriptor_nonsense() + test_init() if verbose: print "All OK" diff --git a/Misc/NEWS b/Misc/NEWS index baf344544c..5bf2e513fa 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 2.5 alpha 1? Core and builtins ----------------- +- Bug #1155938: new style classes did not check that __init__() was + returning None. + - Patch #802188: Report characters after line continuation character ('\') with a specific error message. diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 600dca5c93..6c31c73f70 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4753,6 +4753,12 @@ slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) Py_DECREF(meth); if (res == NULL) return -1; + if (res != Py_None) { + PyErr_SetString(PyExc_TypeError, + "__init__() should return None"); + Py_DECREF(res); + return -1; + } Py_DECREF(res); return 0; }