.. versionadded:: 3.10
+ .. data:: Py_TPFLAGS_IMMUTABLETYPE
+
+ This bit is set for type objects that are immutable: type attributes cannot be set nor deleted.
+
+ :c:func:`PyType_Ready` automatically applies this flag to static types.
+
+ **Inheritance:**
+
+ This flag is not inherited.
+
+ .. versionadded:: 3.10
+
.. c:member:: const char* PyTypeObject.tp_doc
given type object has a specified feature.
*/
+/* Set if the type object is immutable: type attributes cannot be set nor deleted */
+#define Py_TPFLAGS_IMMUTABLETYPE (1UL << 8)
+
/* Set if the type object is dynamically allocated */
#define Py_TPFLAGS_HEAPTYPE (1UL << 9)
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'",
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;