[Python-checkins] bpo-31506: Improve the error message logic for object.__new__ and object.__init__. (GH-3650)

Nick Coghlan webhook-mailer at python.org
Tue Sep 19 23:44:36 EDT 2017


https://github.com/python/cpython/commit/a6c0c0695614177c8b6e1840465375eefcfee586
commit: a6c0c0695614177c8b6e1840465375eefcfee586
branch: master
author: Serhiy Storchaka <storchaka at gmail.com>
committer: Nick Coghlan <ncoghlan at gmail.com>
date: 2017-09-20T13:44:32+10:00
summary:

bpo-31506: Improve the error message logic for object.__new__ and object.__init__. (GH-3650)

files:
A Misc/NEWS.d/next/Core and Builtins/2017-09-19-10-29-36.bpo-31506.pRVTRB.rst
M Objects/typeobject.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-09-19-10-29-36.bpo-31506.pRVTRB.rst b/Misc/NEWS.d/next/Core and Builtins/2017-09-19-10-29-36.bpo-31506.pRVTRB.rst
new file mode 100644
index 00000000000..3bafd8332f9
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2017-09-19-10-29-36.bpo-31506.pRVTRB.rst	
@@ -0,0 +1 @@
+Improved the error message logic for object.__new__ and object.__init__.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 190a8b2aa48..5e0d81f6859 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -3543,23 +3543,34 @@ excess_args(PyObject *args, PyObject *kwds)
 static int
 object_init(PyObject *self, PyObject *args, PyObject *kwds)
 {
-    int err = 0;
     PyTypeObject *type = Py_TYPE(self);
-    if (excess_args(args, kwds) &&
-        (type->tp_new == object_new || type->tp_init != object_init)) {
-        PyErr_SetString(PyExc_TypeError, "object.__init__() takes no parameters");
-        err = -1;
+    if (excess_args(args, kwds)) {
+        if (type->tp_init != object_init) {
+            PyErr_SetString(PyExc_TypeError, "object() takes no arguments");
+            return -1;
+        }
+        if (type->tp_new == object_new) {
+            PyErr_Format(PyExc_TypeError, "%.200s() takes no arguments",
+                         type->tp_name);
+            return -1;
+        }
     }
-    return err;
+    return 0;
 }
 
 static PyObject *
 object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-    if (excess_args(args, kwds) &&
-        (type->tp_init == object_init || type->tp_new != object_new)) {
-        PyErr_SetString(PyExc_TypeError, "object() takes no parameters");
-        return NULL;
+    if (excess_args(args, kwds)) {
+        if (type->tp_new != object_new) {
+            PyErr_SetString(PyExc_TypeError, "object() takes no arguments");
+            return NULL;
+        }
+        if (type->tp_init == object_init) {
+            PyErr_Format(PyExc_TypeError, "%.200s() takes no arguments",
+                         type->tp_name);
+            return NULL;
+        }
     }
 
     if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {



More information about the Python-checkins mailing list