gh-78465: Fix error message for cls.__new__(cls, ...) where cls is not instantiable (GH-135981)

Previous error message suggested to use cls.__new__(), which
obviously does not work. Now the error message is the same as for
cls(...).
This commit is contained in:
Serhiy Storchaka 2025-06-27 14:35:55 +03:00 committed by GitHub
parent f3aec60d7a
commit c45f4f3ebe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 11 additions and 9 deletions

View file

@ -10020,6 +10020,11 @@ tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
/* If staticbase is NULL now, it is a really weird type.
In the spirit of backwards compatibility (?), just shut up. */
if (staticbase && staticbase->tp_new != type->tp_new) {
if (staticbase->tp_new == NULL) {
PyErr_Format(PyExc_TypeError,
"cannot create '%s' instances", subtype->tp_name);
return NULL;
}
PyErr_Format(PyExc_TypeError,
"%s.__new__(%s) is not safe, use %s.__new__()",
type->tp_name,