From: Raymond Hettinger Date: Fri, 4 Mar 2005 04:47:04 +0000 (+0000) Subject: Convert "__init__ should return None" from an exception to a warning. X-Git-Tag: v2.4.1c1~21 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ace3f61994a7ea5e9284d4297a5a90307be23179;p=thirdparty%2FPython%2Fcpython.git Convert "__init__ should return None" from an exception to a warning. --- diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 26df0f2724e2..4af59b000da8 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -3965,17 +3965,23 @@ def vicious_descriptor_nonsense(): import gc; gc.collect() vereq(hasattr(c, 'attr'), False) +import warnings + def test_init(): # SF 1155938 class Foo(object): def __init__(self): return 10 + + oldfilters = warnings.filters + warnings.filterwarnings("error", category=RuntimeWarning) try: Foo() - except TypeError: + except RuntimeWarning: pass else: raise TestFailed, "did not test __init__() for None return" + warnings.filters = oldfilters def test_main(): diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 6c31c73f706c..26fe2a195347 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4754,10 +4754,11 @@ slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) if (res == NULL) return -1; if (res != Py_None) { - PyErr_SetString(PyExc_TypeError, - "__init__() should return None"); - Py_DECREF(res); - return -1; + if (PyErr_Warn(PyExc_RuntimeWarning, + "__init__() should return None") == -1) { + Py_DECREF(res); + return -1; + } } Py_DECREF(res); return 0;