bpo-43908: Add Py_TPFLAGS_IMMUTABLETYPE flag (GH-25520)

Introduce Py_TPFLAGS_IMMUTABLETYPE flag for immutable type objects, and
modify PyType_Ready() to set it for static types.

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Erlend Egeberg Aasland 2021-04-28 19:02:42 +02:00 committed by GitHub
parent 3cc481b9de
commit 3b52c8d66b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 1 deletions

View file

@ -3875,7 +3875,7 @@ static int
type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
{
int res;
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
if (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) {
PyErr_Format(
PyExc_TypeError,
"can't set attributes of built-in/extension type '%s'",
@ -6229,6 +6229,11 @@ PyType_Ready(PyTypeObject *type)
type->tp_flags |= Py_TPFLAGS_READYING;
/* Historically, all static types were immutable. See bpo-43908 */
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
type->tp_flags |= Py_TPFLAGS_IMMUTABLETYPE;
}
if (type_ready(type) < 0) {
type->tp_flags &= ~Py_TPFLAGS_READYING;
return -1;