]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39573: Add Py_SET_TYPE() function (GH-18394)
authorVictor Stinner <vstinner@python.org>
Fri, 7 Feb 2020 08:17:07 +0000 (09:17 +0100)
committerGitHub <noreply@github.com>
Fri, 7 Feb 2020 08:17:07 +0000 (09:17 +0100)
Add Py_SET_TYPE() function to set the type of an object.

24 files changed:
Doc/c-api/structures.rst
Include/cpython/objimpl.h
Include/object.h
Misc/NEWS.d/next/C API/2020-02-07-03-39-03.bpo-39573.Oa8cL1.rst [new file with mode: 0644]
Modules/_blake2/blake2module.c
Modules/_ctypes/_ctypes.c
Modules/_ctypes/ctypes.h
Modules/_sha3/sha3module.c
Modules/_sqlite/prepare_protocol.c
Modules/_testbuffer.c
Modules/_testcapimodule.c
Modules/arraymodule.c
Modules/itertoolsmodule.c
Modules/md5module.c
Modules/sha1module.c
Modules/sha256module.c
Modules/sha512module.c
Modules/socketmodule.c
Modules/unicodedata.c
Objects/floatobject.c
Objects/moduleobject.c
Objects/object.c
Objects/typeobject.c
Objects/weakrefobject.c

index 100573c5693fcdd4b073e138e30ae460de8bebde..8a1431c2de7faff0798a01d7af50f325f378ceb5 100644 (file)
@@ -70,6 +70,13 @@ the definition of all other Python objects.
       (((PyObject*)(o))->ob_type)
 
 
+.. c:function:: void Py_SET_TYPE(PyObject *o, PyTypeObject *type)
+
+   Set the object *o* type to *type*.
+
+   .. versionadded:: 3.9
+
+
 .. c:macro:: Py_REFCNT(o)
 
    This macro is used to access the :attr:`ob_refcnt` member of a Python
index 3f148146f67a40dde6280ef1bb07872d7360f1a3..ebb3e234e36fef528588da8c63c815ddc0015025 100644 (file)
@@ -15,7 +15,7 @@ static inline PyObject*
 _PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
 {
     assert(op != NULL);
-    Py_TYPE(op) = typeobj;
+    Py_SET_TYPE(op, typeobj);
     if (PyType_GetFlags(typeobj) & Py_TPFLAGS_HEAPTYPE) {
         Py_INCREF(typeobj);
     }
index 0b630513375d37ce0ed3f0490043773513c112bc..eb887f4c6eb50d258d1cd2dc5de8db5d958fd8a6 100644 (file)
@@ -128,6 +128,12 @@ static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
 }
 #define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt)
 
+static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
+    ob->ob_type = type;
+}
+#define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type)
+
+
 /*
 Type objects contain a string containing the type name (to help somewhat
 in debugging), the allocation parameters (see PyObject_New() and
diff --git a/Misc/NEWS.d/next/C API/2020-02-07-03-39-03.bpo-39573.Oa8cL1.rst b/Misc/NEWS.d/next/C API/2020-02-07-03-39-03.bpo-39573.Oa8cL1.rst
new file mode 100644 (file)
index 0000000..22d3d69
--- /dev/null
@@ -0,0 +1 @@
+Add :c:func:`Py_SET_TYPE` function to set the type of an object.
index e2a3d420d4eb8ef845df04943f43ba85e18b61db..9d280a9ccd5f3266dc97b9a126156b06c01c98a5 100644 (file)
@@ -62,7 +62,7 @@ PyInit__blake2(void)
         return NULL;
 
     /* BLAKE2b */
-    Py_TYPE(&PyBlake2_BLAKE2bType) = &PyType_Type;
+    Py_SET_TYPE(&PyBlake2_BLAKE2bType, &PyType_Type);
     if (PyType_Ready(&PyBlake2_BLAKE2bType) < 0) {
         return NULL;
     }
@@ -82,7 +82,7 @@ PyInit__blake2(void)
     PyModule_AddIntConstant(m, "BLAKE2B_MAX_DIGEST_SIZE", BLAKE2B_OUTBYTES);
 
     /* BLAKE2s */
-    Py_TYPE(&PyBlake2_BLAKE2sType) = &PyType_Type;
+    Py_SET_TYPE(&PyBlake2_BLAKE2sType, &PyType_Type);
     if (PyType_Ready(&PyBlake2_BLAKE2sType) < 0) {
         return NULL;
     }
index cb6e03f2ca14e4741bb78f44a637f64fe8ef007e..4747195c2e6b2841638c609afedc957e69587d37 100644 (file)
@@ -5758,42 +5758,42 @@ PyInit__ctypes(void)
     if (PyType_Ready(&PyCData_Type) < 0)
         return NULL;
 
-    Py_TYPE(&Struct_Type) = &PyCStructType_Type;
+    Py_SET_TYPE(&Struct_Type, &PyCStructType_Type);
     Struct_Type.tp_base = &PyCData_Type;
     if (PyType_Ready(&Struct_Type) < 0)
         return NULL;
     Py_INCREF(&Struct_Type);
     PyModule_AddObject(m, "Structure", (PyObject *)&Struct_Type);
 
-    Py_TYPE(&Union_Type) = &UnionType_Type;
+    Py_SET_TYPE(&Union_Type, &UnionType_Type);
     Union_Type.tp_base = &PyCData_Type;
     if (PyType_Ready(&Union_Type) < 0)
         return NULL;
     Py_INCREF(&Union_Type);
     PyModule_AddObject(m, "Union", (PyObject *)&Union_Type);
 
-    Py_TYPE(&PyCPointer_Type) = &PyCPointerType_Type;
+    Py_SET_TYPE(&PyCPointer_Type, &PyCPointerType_Type);
     PyCPointer_Type.tp_base = &PyCData_Type;
     if (PyType_Ready(&PyCPointer_Type) < 0)
         return NULL;
     Py_INCREF(&PyCPointer_Type);
     PyModule_AddObject(m, "_Pointer", (PyObject *)&PyCPointer_Type);
 
-    Py_TYPE(&PyCArray_Type) = &PyCArrayType_Type;
+    Py_SET_TYPE(&PyCArray_Type, &PyCArrayType_Type);
     PyCArray_Type.tp_base = &PyCData_Type;
     if (PyType_Ready(&PyCArray_Type) < 0)
         return NULL;
     Py_INCREF(&PyCArray_Type);
     PyModule_AddObject(m, "Array", (PyObject *)&PyCArray_Type);
 
-    Py_TYPE(&Simple_Type) = &PyCSimpleType_Type;
+    Py_SET_TYPE(&Simple_Type, &PyCSimpleType_Type);
     Simple_Type.tp_base = &PyCData_Type;
     if (PyType_Ready(&Simple_Type) < 0)
         return NULL;
     Py_INCREF(&Simple_Type);
     PyModule_AddObject(m, "_SimpleCData", (PyObject *)&Simple_Type);
 
-    Py_TYPE(&PyCFuncPtr_Type) = &PyCFuncPtrType_Type;
+    Py_SET_TYPE(&PyCFuncPtr_Type, &PyCFuncPtrType_Type);
     PyCFuncPtr_Type.tp_base = &PyCData_Type;
     if (PyType_Ready(&PyCFuncPtr_Type) < 0)
         return NULL;
index 351f996be05c65bb8cbd682e72538650ad5584dc..a232a4bc832067503ab91662b5bc3d97d7b1ce2f 100644 (file)
@@ -68,7 +68,7 @@ typedef struct {
     ffi_type *atypes[1];
 } CThunkObject;
 extern PyTypeObject PyCThunk_Type;
-#define CThunk_CheckExact(v)        ((v)->ob_type == &PyCThunk_Type)
+#define CThunk_CheckExact(v)        (Py_TYPE(v) == &PyCThunk_Type)
 
 typedef struct {
     /* First part identical to tagCDataObject */
index d4ca9a111da69bfd5169d5f1656df25b59b40a25..9cdee5869a2716cf399b0c23660c2687bddc0fd8 100644 (file)
@@ -713,7 +713,7 @@ PyInit__sha3(void)
 
 #define init_sha3type(name, type)     \
     do {                              \
-        Py_TYPE(type) = &PyType_Type; \
+        Py_SET_TYPE(type, &PyType_Type); \
         if (PyType_Ready(type) < 0) { \
             goto error;               \
         }                             \
index 181c7edf96b45c386010da0fcdbdad6007071261..05a2ca5a652f5e3b74ef7852772e46c8964d4bd7 100644 (file)
@@ -78,6 +78,6 @@ PyTypeObject pysqlite_PrepareProtocolType= {
 extern int pysqlite_prepare_protocol_setup_types(void)
 {
     pysqlite_PrepareProtocolType.tp_new = PyType_GenericNew;
-    Py_TYPE(&pysqlite_PrepareProtocolType)= &PyType_Type;
+    Py_SET_TYPE(&pysqlite_PrepareProtocolType, &PyType_Type);
     return PyType_Ready(&pysqlite_PrepareProtocolType);
 }
index 047e3d3974cf1ca30a961f020b33c4e3d1ef570e..600a52aa872f83a7d2e9ea9cc44ca4bf8558e1fb 100644 (file)
@@ -2835,11 +2835,11 @@ PyInit__testbuffer(void)
     if (m == NULL)
         return NULL;
 
-    Py_TYPE(&NDArray_Type) = &PyType_Type;
+    Py_SET_TYPE(&NDArray_Type, &PyType_Type);
     Py_INCREF(&NDArray_Type);
     PyModule_AddObject(m, "ndarray", (PyObject *)&NDArray_Type);
 
-    Py_TYPE(&StaticArray_Type) = &PyType_Type;
+    Py_SET_TYPE(&StaticArray_Type, &PyType_Type);
     Py_INCREF(&StaticArray_Type);
     PyModule_AddObject(m, "staticarray", (PyObject *)&StaticArray_Type);
 
index 3bb1bf1e274ffe23680b19087934ec24aa700395..e6d30341cdfb17148a05c5d5905066e0e9eae405 100644 (file)
@@ -6605,9 +6605,9 @@ PyInit__testcapi(void)
     if (m == NULL)
         return NULL;
 
-    Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type;
+    Py_SET_TYPE(&_HashInheritanceTester_Type, &PyType_Type);
 
-    Py_TYPE(&test_structmembersType)=&PyType_Type;
+    Py_SET_TYPE(&test_structmembersType, &PyType_Type);
     Py_INCREF(&test_structmembersType);
     /* don't use a name starting with "test", since we don't want
        test_capi to automatically call this */
index edb56ab6e18736932c418a8d3ed2ba542b4b1155..5065d28dafbc2fde58c4dc0c96ea7d86cf20b749 100644 (file)
@@ -2996,7 +2996,7 @@ array_modexec(PyObject *m)
 
     if (PyType_Ready(&Arraytype) < 0)
         return -1;
-    Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
+    Py_SET_TYPE(&PyArrayIter_Type, &PyType_Type);
 
     Py_INCREF((PyObject *)&Arraytype);
     if (PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype) < 0) {
index 0cb472966d1f950bcf67065fffce98bfe810886b..0dafb65c288e48ba357efa8f0725d13d88620a8c 100644 (file)
@@ -4750,14 +4750,16 @@ PyInit_itertools(void)
         NULL
     };
 
-    Py_TYPE(&teedataobject_type) = &PyType_Type;
+    Py_SET_TYPE(&teedataobject_type, &PyType_Type);
     m = PyModule_Create(&itertoolsmodule);
-    if (m == NULL)
+    if (m == NULL) {
         return NULL;
+    }
 
     for (i=0 ; typelist[i] != NULL ; i++) {
-        if (PyType_Ready(typelist[i]) < 0)
+        if (PyType_Ready(typelist[i]) < 0) {
             return NULL;
+        }
         name = _PyType_Name(typelist[i]);
         Py_INCREF(typelist[i]);
         PyModule_AddObject(m, name, (PyObject *)typelist[i]);
index f2c2d32cbe7df33535b7fbe9ca4fd3628cc1a6a9..d783ae5a765fa2925ea3d907274ae933091df1ae 100644 (file)
@@ -572,13 +572,15 @@ PyInit__md5(void)
 {
     PyObject *m;
 
-    Py_TYPE(&MD5type) = &PyType_Type;
-    if (PyType_Ready(&MD5type) < 0)
+    Py_SET_TYPE(&MD5type, &PyType_Type);
+    if (PyType_Ready(&MD5type) < 0) {
         return NULL;
+    }
 
     m = PyModule_Create(&_md5module);
-    if (m == NULL)
+    if (m == NULL) {
         return NULL;
+    }
 
     Py_INCREF((PyObject *)&MD5type);
     PyModule_AddObject(m, "MD5Type", (PyObject *)&MD5type);
index 4d191c3c48885ace4ac8bf9596125cc9a9f1cd36..e066b8802294165055a8acaa39d671bcfc775e82 100644 (file)
@@ -549,13 +549,15 @@ PyInit__sha1(void)
 {
     PyObject *m;
 
-    Py_TYPE(&SHA1type) = &PyType_Type;
-    if (PyType_Ready(&SHA1type) < 0)
+    Py_SET_TYPE(&SHA1type, &PyType_Type);
+    if (PyType_Ready(&SHA1type) < 0) {
         return NULL;
+    }
 
     m = PyModule_Create(&_sha1module);
-    if (m == NULL)
+    if (m == NULL) {
         return NULL;
+    }
 
     Py_INCREF((PyObject *)&SHA1type);
     PyModule_AddObject(m, "SHA1Type", (PyObject *)&SHA1type);
index 245f4c04542a77dada6b357e9f018f7703afad25..0e0c4461880f0528fe3bd65df24a50e3f2b3e2ab 100644 (file)
@@ -713,12 +713,14 @@ PyInit__sha256(void)
 {
     PyObject *m;
 
-    Py_TYPE(&SHA224type) = &PyType_Type;
-    if (PyType_Ready(&SHA224type) < 0)
+    Py_SET_TYPE(&SHA224type, &PyType_Type);
+    if (PyType_Ready(&SHA224type) < 0) {
         return NULL;
-    Py_TYPE(&SHA256type) = &PyType_Type;
-    if (PyType_Ready(&SHA256type) < 0)
+    }
+    Py_SET_TYPE(&SHA256type, &PyType_Type);
+    if (PyType_Ready(&SHA256type) < 0) {
         return NULL;
+    }
 
     m = PyModule_Create(&_sha256module);
     if (m == NULL)
index 4045698387faa4d5ae577e6f5c262829d193b9e5..07bf28351888bddd1c7d22662ac90dc219816747 100644 (file)
@@ -778,16 +778,19 @@ PyInit__sha512(void)
 {
     PyObject *m;
 
-    Py_TYPE(&SHA384type) = &PyType_Type;
-    if (PyType_Ready(&SHA384type) < 0)
+    Py_SET_TYPE(&SHA384type, &PyType_Type);
+    if (PyType_Ready(&SHA384type) < 0) {
         return NULL;
-    Py_TYPE(&SHA512type) = &PyType_Type;
-    if (PyType_Ready(&SHA512type) < 0)
+    }
+    Py_SET_TYPE(&SHA512type, &PyType_Type);
+    if (PyType_Ready(&SHA512type) < 0) {
         return NULL;
+    }
 
     m = PyModule_Create(&_sha512module);
-    if (m == NULL)
+    if (m == NULL) {
         return NULL;
+    }
 
     Py_INCREF((PyObject *)&SHA384type);
     PyModule_AddObject(m, "SHA384Type", (PyObject *)&SHA384type);
index e54f7a9b1c2bb68399a94852b3fcf0e58ba49929..37b312396f9440bc2af4bb3cc9408a8b271989aa 100644 (file)
@@ -7100,7 +7100,7 @@ PyInit__socket(void)
     }
 #endif
 
-    Py_TYPE(&sock_type) = &PyType_Type;
+    Py_SET_TYPE(&sock_type, &PyType_Type);
     m = PyModule_Create(&socketmodule);
     if (m == NULL)
         return NULL;
index e99d914b797bf7aea9b6a4cd85257528daca32a8..58b1bc2d0a105769d8100244e176f8816317ad5b 100644 (file)
@@ -1455,7 +1455,7 @@ PyInit_unicodedata(void)
 {
     PyObject *m, *v;
 
-    Py_TYPE(&UCD_Type) = &PyType_Type;
+    Py_SET_TYPE(&UCD_Type, &PyType_Type);
 
     m = PyModule_Create(&unicodedatamodule);
     if (!m)
index 26e238cf05ad3874aaa8bc39aec22223fe4004f8..dfc5b196f18e4cebe8874bb46638ac42b7a08bf6 100644 (file)
@@ -221,7 +221,7 @@ float_dealloc(PyFloatObject *op)
             return;
         }
         numfree++;
-        Py_TYPE(op) = (struct _typeobject *)free_list;
+        Py_SET_TYPE(op, (PyTypeObject *)free_list);
         free_list = op;
     }
     else
index da329b4fbac8b5830b4fd2cbc6f4ba8605a24574..0a593261c41349d964b1e661cd7a66573292cd89 100644 (file)
@@ -52,7 +52,7 @@ PyModuleDef_Init(struct PyModuleDef* def)
     if (def->m_base.m_index == 0) {
         max_module_number++;
         Py_SET_REFCNT(def, 1);
-        Py_TYPE(def) = &PyModuleDef_Type;
+        Py_SET_TYPE(def, &PyModuleDef_Type);
         def->m_base.m_index = max_module_number;
     }
     return (PyObject*)def;
index 58065424881159b4f36b516a06a220b77688416d..503fb867802c1e0052657da8b437c8158d16ccfe 100644 (file)
@@ -144,7 +144,7 @@ PyObject_Init(PyObject *op, PyTypeObject *tp)
         return PyErr_NoMemory();
     }
 
-    Py_TYPE(op) = tp;
+    Py_SET_TYPE(op, tp);
     if (PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE) {
         Py_INCREF(tp);
     }
index 5b8d5a228e5af45dcce3c55778ce19db785f4733..e6a84b017aa6753f0100f815dc908bdf53cc5504 100644 (file)
@@ -4097,9 +4097,10 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
     }
 
     if (compatible_for_assignment(oldto, newto, "__class__")) {
-        if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE)
+        if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
             Py_INCREF(newto);
-        Py_TYPE(self) = newto;
+        }
+        Py_SET_TYPE(self, newto);
         if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)
             Py_DECREF(oldto);
         return 0;
@@ -5334,8 +5335,9 @@ PyType_Ready(PyTypeObject *type)
        NULL when type is &PyBaseObject_Type, and we know its ob_type is
        not NULL (it's initialized to &PyType_Type).      But coverity doesn't
        know that. */
-    if (Py_TYPE(type) == NULL && base != NULL)
-        Py_TYPE(type) = Py_TYPE(base);
+    if (Py_TYPE(type) == NULL && base != NULL) {
+        Py_SET_TYPE(type, Py_TYPE(base));
+    }
 
     /* Initialize tp_bases */
     bases = type->tp_bases;
index d104b646f01815db406aef9166cd836a0e5301c3..18c737e7e4097d4a545503ba3f8c448ce620de92 100644 (file)
@@ -882,10 +882,12 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
         if (result != NULL) {
             PyWeakReference *prev;
 
-            if (PyCallable_Check(ob))
-                Py_TYPE(result) = &_PyWeakref_CallableProxyType;
-            else
-                Py_TYPE(result) = &_PyWeakref_ProxyType;
+            if (PyCallable_Check(ob)) {
+                Py_SET_TYPE(result, &_PyWeakref_CallableProxyType);
+            }
+            else {
+                Py_SET_TYPE(result, &_PyWeakref_ProxyType);
+            }
             get_basic_refs(*list, &ref, &proxy);
             if (callback == NULL) {
                 if (proxy != NULL) {