Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved the error message logic for object.__new__ and object.__init__.
31 changes: 21 additions & 10 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down