Skip to content

Commit a6c0c06

Browse files
serhiy-storchakancoghlan
authored andcommitted
bpo-31506: Improve the error message logic for object.__new__ and object.__init__. (GH-3650)
1 parent d6e2f26 commit a6c0c06

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved the error message logic for object.__new__ and object.__init__.

Objects/typeobject.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3543,23 +3543,34 @@ excess_args(PyObject *args, PyObject *kwds)
35433543
static int
35443544
object_init(PyObject *self, PyObject *args, PyObject *kwds)
35453545
{
3546-
int err = 0;
35473546
PyTypeObject *type = Py_TYPE(self);
3548-
if (excess_args(args, kwds) &&
3549-
(type->tp_new == object_new || type->tp_init != object_init)) {
3550-
PyErr_SetString(PyExc_TypeError, "object.__init__() takes no parameters");
3551-
err = -1;
3547+
if (excess_args(args, kwds)) {
3548+
if (type->tp_init != object_init) {
3549+
PyErr_SetString(PyExc_TypeError, "object() takes no arguments");
3550+
return -1;
3551+
}
3552+
if (type->tp_new == object_new) {
3553+
PyErr_Format(PyExc_TypeError, "%.200s() takes no arguments",
3554+
type->tp_name);
3555+
return -1;
3556+
}
35523557
}
3553-
return err;
3558+
return 0;
35543559
}
35553560

35563561
static PyObject *
35573562
object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
35583563
{
3559-
if (excess_args(args, kwds) &&
3560-
(type->tp_init == object_init || type->tp_new != object_new)) {
3561-
PyErr_SetString(PyExc_TypeError, "object() takes no parameters");
3562-
return NULL;
3564+
if (excess_args(args, kwds)) {
3565+
if (type->tp_new != object_new) {
3566+
PyErr_SetString(PyExc_TypeError, "object() takes no arguments");
3567+
return NULL;
3568+
}
3569+
if (type->tp_init == object_init) {
3570+
PyErr_Format(PyExc_TypeError, "%.200s() takes no arguments",
3571+
type->tp_name);
3572+
return NULL;
3573+
}
35633574
}
35643575

35653576
if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {

0 commit comments

Comments
 (0)