Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Doc/c-api/typeobj.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,18 @@ and :c:type:`PyType_Type` effectively act as defaults.)

.. versionadded:: 3.10

.. data:: Py_TPFLAGS_IMMUTABLETYPE

This bit is set for type objects that are immutable: type attributes cannot be set or 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

Expand Down
3 changes: 3 additions & 0 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
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)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Introduce :const:`Py_TPFLAGS_IMMUTABLETYPE` for immutable type objects, and
modify :c:func:`PyType_Ready` to set it for all static types. Patch by
Erlend E. Aasland.
6 changes: 5 additions & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand Down Expand Up @@ -6229,6 +6229,10 @@ PyType_Ready(PyTypeObject *type)

type->tp_flags |= Py_TPFLAGS_READYING;

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;
Expand Down