From 8e8129ffcbc72b251b0736ef72ffc6c63206804a Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 22 Apr 2021 10:50:05 +0200 Subject: [PATCH 01/10] Add Py_TPFLAGS_IMMUTABLETYPE and set it for all static types --- Doc/c-api/typeobj.rst | 11 +++++++++++ Include/object.h | 3 +++ .../2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst | 3 +++ Objects/typeobject.c | 6 +++++- 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst index 9efe3aac2e1c9f..26678510f91e70 100644 --- a/Doc/c-api/typeobj.rst +++ b/Doc/c-api/typeobj.rst @@ -1177,6 +1177,17 @@ 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. :c:func:`PyType_Ready` + automatically applies this flag to static types. + + **Inheritance:** + + This flag is never inherited. + + .. versionadded:: 3.10 + .. c:member:: const char* PyTypeObject.tp_doc diff --git a/Include/object.h b/Include/object.h index 695f01564282c0..a239063e3965ce 100644 --- a/Include/object.h +++ b/Include/object.h @@ -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 */ +#define Py_TPFLAGS_IMMUTABLETYPE (1UL << 8) + /* Set if the type object is dynamically allocated */ #define Py_TPFLAGS_HEAPTYPE (1UL << 9) diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst b/Misc/NEWS.d/next/Core and Builtins/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst new file mode 100644 index 00000000000000..0ef57f997368ac --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst @@ -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. diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 254d12cc970148..6431b11b85a33f 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -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,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; From 025af5b0d7b7a1049fda9dc9413635965899dd85 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Wed, 28 Apr 2021 17:16:28 +0200 Subject: [PATCH 02/10] Improve docs Co-authored-by: Victor Stinner --- Doc/c-api/typeobj.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst index 26678510f91e70..35613b5cd557e9 100644 --- a/Doc/c-api/typeobj.rst +++ b/Doc/c-api/typeobj.rst @@ -1179,8 +1179,9 @@ and :c:type:`PyType_Type` effectively act as defaults.) .. data:: Py_TPFLAGS_IMMUTABLETYPE - This bit is set for type objects that are immutable. :c:func:`PyType_Ready` - automatically applies this flag to static types. + 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:** From 0e8c709eae289caaaa9240b57881d2df44009ed6 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Wed, 28 Apr 2021 17:16:38 +0200 Subject: [PATCH 03/10] Improve docs Co-authored-by: Victor Stinner --- Doc/c-api/typeobj.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst index 35613b5cd557e9..8a2abeb12fbc48 100644 --- a/Doc/c-api/typeobj.rst +++ b/Doc/c-api/typeobj.rst @@ -1185,7 +1185,7 @@ and :c:type:`PyType_Type` effectively act as defaults.) **Inheritance:** - This flag is never inherited. + This flag is not inherited. .. versionadded:: 3.10 From 8c79eaedab4156226e014d04eb4473a0356af68b Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Wed, 28 Apr 2021 17:16:50 +0200 Subject: [PATCH 04/10] Improve comment Co-authored-by: Victor Stinner --- Include/object.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Include/object.h b/Include/object.h index a239063e3965ce..d8476f9213760d 100644 --- a/Include/object.h +++ b/Include/object.h @@ -320,7 +320,7 @@ 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 */ +/* 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 */ From 068054d0f6a40d94d12c0ac7a68b0d65180733e9 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Wed, 28 Apr 2021 17:27:14 +0200 Subject: [PATCH 05/10] Improve NEWS entry Co-authored-by: Victor Stinner --- .../Core and Builtins/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst b/Misc/NEWS.d/next/Core and Builtins/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst index 0ef57f997368ac..a146d0d71a5b57 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst @@ -1,3 +1,3 @@ -Introduce :const:`Py_TPFLAGS_IMMUTABLETYPE` for immutable type objects, and +Introduce :const:`Py_TPFLAGS_IMMUTABLETYPE` flag for immutable type objects, and modify :c:func:`PyType_Ready` to set it for all static types. Patch by Erlend E. Aasland. From 1987c4c027e41ae294711ca4c7d3ee9c905d5a7f Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 28 Apr 2021 17:29:28 +0200 Subject: [PATCH 06/10] Address review: Move NEWS entry to C API section --- .../2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/{Core and Builtins => C API}/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst (100%) diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst b/Misc/NEWS.d/next/C API/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst similarity index 100% rename from Misc/NEWS.d/next/Core and Builtins/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst rename to Misc/NEWS.d/next/C API/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst From b7560e44ade6312656a3db94f1d8889caeb341c7 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 28 Apr 2021 17:35:17 +0200 Subject: [PATCH 07/10] Address review: Add comment ref. to bpo-43908 --- Objects/typeobject.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 6431b11b85a33f..e1c8be4b815452 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -6229,6 +6229,7 @@ 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; } From 3032d3be9e8f6396966c8b2cda4ab2013e2c8b68 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Wed, 28 Apr 2021 17:37:19 +0200 Subject: [PATCH 08/10] Spelling fix Co-authored-by: Victor Stinner --- Doc/c-api/typeobj.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst index 8a2abeb12fbc48..4486ebe6201fc9 100644 --- a/Doc/c-api/typeobj.rst +++ b/Doc/c-api/typeobj.rst @@ -1179,7 +1179,7 @@ and :c:type:`PyType_Type` effectively act as defaults.) .. data:: Py_TPFLAGS_IMMUTABLETYPE - This bit is set for type objects that are immutable: type attributes cannot be set or deleted. + 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. From edf409723f2f64f9503f7c3987413da19843a52f Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Wed, 28 Apr 2021 17:43:31 +0200 Subject: [PATCH 09/10] Improve NEWS wording --- Misc/NEWS.d/next/C API/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/C API/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst b/Misc/NEWS.d/next/C API/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst index a146d0d71a5b57..0413c20a1b6b22 100644 --- a/Misc/NEWS.d/next/C API/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst +++ b/Misc/NEWS.d/next/C API/2021-04-22-10-46-40.bpo-43908.Co3YhZ.rst @@ -1,3 +1,3 @@ Introduce :const:`Py_TPFLAGS_IMMUTABLETYPE` flag for immutable type objects, and -modify :c:func:`PyType_Ready` to set it for all static types. Patch by +modify :c:func:`PyType_Ready` to set it for static types. Patch by Erlend E. Aasland. From 098ef93b9aa3583b29a7e52888fefb235b956350 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Wed, 28 Apr 2021 17:55:53 +0200 Subject: [PATCH 10/10] Whitespace fix --- Doc/c-api/typeobj.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst index 4486ebe6201fc9..4c75a12194d185 100644 --- a/Doc/c-api/typeobj.rst +++ b/Doc/c-api/typeobj.rst @@ -1180,7 +1180,7 @@ and :c:type:`PyType_Type` effectively act as defaults.) .. 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:**