Skip to content

Commit 8e8129f

Browse files
author
Erlend E. Aasland
committed
Add Py_TPFLAGS_IMMUTABLETYPE and set it for all static types
1 parent 1b1f985 commit 8e8129f

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

Doc/c-api/typeobj.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,17 @@ and :c:type:`PyType_Type` effectively act as defaults.)
11771177

11781178
.. versionadded:: 3.10
11791179

1180+
.. data:: Py_TPFLAGS_IMMUTABLETYPE
1181+
1182+
This bit is set for type objects that are immutable. :c:func:`PyType_Ready`
1183+
automatically applies this flag to static types.
1184+
1185+
**Inheritance:**
1186+
1187+
This flag is never inherited.
1188+
1189+
.. versionadded:: 3.10
1190+
11801191

11811192
.. c:member:: const char* PyTypeObject.tp_doc
11821193

Include/object.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
320320
given type object has a specified feature.
321321
*/
322322

323+
/* Set if the type object is immutable */
324+
#define Py_TPFLAGS_IMMUTABLETYPE (1UL << 8)
325+
323326
/* Set if the type object is dynamically allocated */
324327
#define Py_TPFLAGS_HEAPTYPE (1UL << 9)
325328

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Introduce :const:`Py_TPFLAGS_IMMUTABLETYPE` for immutable type objects, and
2+
modify :c:func:`PyType_Ready` to set it for all static types. Patch by
3+
Erlend E. Aasland.

Objects/typeobject.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3875,7 +3875,7 @@ static int
38753875
type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
38763876
{
38773877
int res;
3878-
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
3878+
if (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) {
38793879
PyErr_Format(
38803880
PyExc_TypeError,
38813881
"can't set attributes of built-in/extension type '%s'",
@@ -6229,6 +6229,10 @@ PyType_Ready(PyTypeObject *type)
62296229

62306230
type->tp_flags |= Py_TPFLAGS_READYING;
62316231

6232+
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
6233+
type->tp_flags |= Py_TPFLAGS_IMMUTABLETYPE;
6234+
}
6235+
62326236
if (type_ready(type) < 0) {
62336237
type->tp_flags &= ~Py_TPFLAGS_READYING;
62346238
return -1;

0 commit comments

Comments
 (0)