mirror of
https://github.com/python/cpython.git
synced 2025-11-03 11:23:31 +00:00
bpo-31506: Improve the error message logic for object.__new__ and object.__init__. (GH-3650)
This commit is contained in:
parent
d6e2f26f3f
commit
a6c0c06956
2 changed files with 22 additions and 10 deletions
|
|
@ -0,0 +1 @@
|
||||||
|
Improved the error message logic for object.__new__ and object.__init__.
|
||||||
|
|
@ -3543,24 +3543,35 @@ excess_args(PyObject *args, PyObject *kwds)
|
||||||
static int
|
static int
|
||||||
object_init(PyObject *self, PyObject *args, PyObject *kwds)
|
object_init(PyObject *self, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
int err = 0;
|
|
||||||
PyTypeObject *type = Py_TYPE(self);
|
PyTypeObject *type = Py_TYPE(self);
|
||||||
if (excess_args(args, kwds) &&
|
if (excess_args(args, kwds)) {
|
||||||
(type->tp_new == object_new || type->tp_init != object_init)) {
|
if (type->tp_init != object_init) {
|
||||||
PyErr_SetString(PyExc_TypeError, "object.__init__() takes no parameters");
|
PyErr_SetString(PyExc_TypeError, "object() takes no arguments");
|
||||||
err = -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return err;
|
if (type->tp_new == object_new) {
|
||||||
|
PyErr_Format(PyExc_TypeError, "%.200s() takes no arguments",
|
||||||
|
type->tp_name);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
{
|
{
|
||||||
if (excess_args(args, kwds) &&
|
if (excess_args(args, kwds)) {
|
||||||
(type->tp_init == object_init || type->tp_new != object_new)) {
|
if (type->tp_new != object_new) {
|
||||||
PyErr_SetString(PyExc_TypeError, "object() takes no parameters");
|
PyErr_SetString(PyExc_TypeError, "object() takes no arguments");
|
||||||
return NULL;
|
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) {
|
if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
|
||||||
PyObject *abstract_methods = NULL;
|
PyObject *abstract_methods = NULL;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue