bpo-23722: Raise a RuntimeError for absent __classcell__. (GH-6931)

A DeprecationWarning was emitted in Python 3.6-3.7.
This commit is contained in:
Serhiy Storchaka 2018-05-20 08:48:12 +03:00 committed by GitHub
parent 8ae8e6af37
commit f5e7b1999f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 51 deletions

View file

@ -254,30 +254,19 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
if (cls != NULL && PyType_Check(cls) && PyCell_Check(cell)) {
PyObject *cell_cls = PyCell_GET(cell);
if (cell_cls != cls) {
/* TODO: In 3.7, DeprecationWarning will become RuntimeError.
* At that point, cell_error won't be needed.
*/
int cell_error;
if (cell_cls == NULL) {
const char *msg =
"__class__ not set defining %.200R as %.200R. "
"Was __classcell__ propagated to type.__new__?";
cell_error = PyErr_WarnFormat(
PyExc_DeprecationWarning, 1, msg, name, cls);
PyErr_Format(PyExc_RuntimeError, msg, name, cls);
} else {
const char *msg =
"__class__ set to %.200R defining %.200R as %.200R";
PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
cell_error = 1;
}
if (cell_error) {
Py_DECREF(cls);
cls = NULL;
goto error;
} else {
/* Fill in the cell, since type.__new__ didn't do it */
PyCell_Set(cell, cls);
}
Py_DECREF(cls);
cls = NULL;
goto error;
}
}
}