Skip to content

Commit d2ec81a

Browse files
authored
bpo-39573: Add Py_SET_TYPE() function (GH-18394)
Add Py_SET_TYPE() function to set the type of an object.
1 parent daa9756 commit d2ec81a

24 files changed

+77
-48
lines changed

Doc/c-api/structures.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ the definition of all other Python objects.
7070
(((PyObject*)(o))->ob_type)
7171

7272

73+
.. c:function:: void Py_SET_TYPE(PyObject *o, PyTypeObject *type)
74+
75+
Set the object *o* type to *type*.
76+
77+
.. versionadded:: 3.9
78+
79+
7380
.. c:macro:: Py_REFCNT(o)
7481
7582
This macro is used to access the :attr:`ob_refcnt` member of a Python

Include/cpython/objimpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static inline PyObject*
1515
_PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
1616
{
1717
assert(op != NULL);
18-
Py_TYPE(op) = typeobj;
18+
Py_SET_TYPE(op, typeobj);
1919
if (PyType_GetFlags(typeobj) & Py_TPFLAGS_HEAPTYPE) {
2020
Py_INCREF(typeobj);
2121
}

Include/object.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
128128
}
129129
#define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt)
130130

131+
static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
132+
ob->ob_type = type;
133+
}
134+
#define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type)
135+
136+
131137
/*
132138
Type objects contain a string containing the type name (to help somewhat
133139
in debugging), the allocation parameters (see PyObject_New() and
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :c:func:`Py_SET_TYPE` function to set the type of an object.

Modules/_blake2/blake2module.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ PyInit__blake2(void)
6262
return NULL;
6363

6464
/* BLAKE2b */
65-
Py_TYPE(&PyBlake2_BLAKE2bType) = &PyType_Type;
65+
Py_SET_TYPE(&PyBlake2_BLAKE2bType, &PyType_Type);
6666
if (PyType_Ready(&PyBlake2_BLAKE2bType) < 0) {
6767
return NULL;
6868
}
@@ -82,7 +82,7 @@ PyInit__blake2(void)
8282
PyModule_AddIntConstant(m, "BLAKE2B_MAX_DIGEST_SIZE", BLAKE2B_OUTBYTES);
8383

8484
/* BLAKE2s */
85-
Py_TYPE(&PyBlake2_BLAKE2sType) = &PyType_Type;
85+
Py_SET_TYPE(&PyBlake2_BLAKE2sType, &PyType_Type);
8686
if (PyType_Ready(&PyBlake2_BLAKE2sType) < 0) {
8787
return NULL;
8888
}

Modules/_ctypes/_ctypes.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5758,42 +5758,42 @@ PyInit__ctypes(void)
57585758
if (PyType_Ready(&PyCData_Type) < 0)
57595759
return NULL;
57605760

5761-
Py_TYPE(&Struct_Type) = &PyCStructType_Type;
5761+
Py_SET_TYPE(&Struct_Type, &PyCStructType_Type);
57625762
Struct_Type.tp_base = &PyCData_Type;
57635763
if (PyType_Ready(&Struct_Type) < 0)
57645764
return NULL;
57655765
Py_INCREF(&Struct_Type);
57665766
PyModule_AddObject(m, "Structure", (PyObject *)&Struct_Type);
57675767

5768-
Py_TYPE(&Union_Type) = &UnionType_Type;
5768+
Py_SET_TYPE(&Union_Type, &UnionType_Type);
57695769
Union_Type.tp_base = &PyCData_Type;
57705770
if (PyType_Ready(&Union_Type) < 0)
57715771
return NULL;
57725772
Py_INCREF(&Union_Type);
57735773
PyModule_AddObject(m, "Union", (PyObject *)&Union_Type);
57745774

5775-
Py_TYPE(&PyCPointer_Type) = &PyCPointerType_Type;
5775+
Py_SET_TYPE(&PyCPointer_Type, &PyCPointerType_Type);
57765776
PyCPointer_Type.tp_base = &PyCData_Type;
57775777
if (PyType_Ready(&PyCPointer_Type) < 0)
57785778
return NULL;
57795779
Py_INCREF(&PyCPointer_Type);
57805780
PyModule_AddObject(m, "_Pointer", (PyObject *)&PyCPointer_Type);
57815781

5782-
Py_TYPE(&PyCArray_Type) = &PyCArrayType_Type;
5782+
Py_SET_TYPE(&PyCArray_Type, &PyCArrayType_Type);
57835783
PyCArray_Type.tp_base = &PyCData_Type;
57845784
if (PyType_Ready(&PyCArray_Type) < 0)
57855785
return NULL;
57865786
Py_INCREF(&PyCArray_Type);
57875787
PyModule_AddObject(m, "Array", (PyObject *)&PyCArray_Type);
57885788

5789-
Py_TYPE(&Simple_Type) = &PyCSimpleType_Type;
5789+
Py_SET_TYPE(&Simple_Type, &PyCSimpleType_Type);
57905790
Simple_Type.tp_base = &PyCData_Type;
57915791
if (PyType_Ready(&Simple_Type) < 0)
57925792
return NULL;
57935793
Py_INCREF(&Simple_Type);
57945794
PyModule_AddObject(m, "_SimpleCData", (PyObject *)&Simple_Type);
57955795

5796-
Py_TYPE(&PyCFuncPtr_Type) = &PyCFuncPtrType_Type;
5796+
Py_SET_TYPE(&PyCFuncPtr_Type, &PyCFuncPtrType_Type);
57975797
PyCFuncPtr_Type.tp_base = &PyCData_Type;
57985798
if (PyType_Ready(&PyCFuncPtr_Type) < 0)
57995799
return NULL;

Modules/_ctypes/ctypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ typedef struct {
6868
ffi_type *atypes[1];
6969
} CThunkObject;
7070
extern PyTypeObject PyCThunk_Type;
71-
#define CThunk_CheckExact(v) ((v)->ob_type == &PyCThunk_Type)
71+
#define CThunk_CheckExact(v) (Py_TYPE(v) == &PyCThunk_Type)
7272

7373
typedef struct {
7474
/* First part identical to tagCDataObject */

Modules/_sha3/sha3module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ PyInit__sha3(void)
713713

714714
#define init_sha3type(name, type) \
715715
do { \
716-
Py_TYPE(type) = &PyType_Type; \
716+
Py_SET_TYPE(type, &PyType_Type); \
717717
if (PyType_Ready(type) < 0) { \
718718
goto error; \
719719
} \

Modules/_sqlite/prepare_protocol.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ PyTypeObject pysqlite_PrepareProtocolType= {
7878
extern int pysqlite_prepare_protocol_setup_types(void)
7979
{
8080
pysqlite_PrepareProtocolType.tp_new = PyType_GenericNew;
81-
Py_TYPE(&pysqlite_PrepareProtocolType)= &PyType_Type;
81+
Py_SET_TYPE(&pysqlite_PrepareProtocolType, &PyType_Type);
8282
return PyType_Ready(&pysqlite_PrepareProtocolType);
8383
}

Modules/_testbuffer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,11 +2835,11 @@ PyInit__testbuffer(void)
28352835
if (m == NULL)
28362836
return NULL;
28372837

2838-
Py_TYPE(&NDArray_Type) = &PyType_Type;
2838+
Py_SET_TYPE(&NDArray_Type, &PyType_Type);
28392839
Py_INCREF(&NDArray_Type);
28402840
PyModule_AddObject(m, "ndarray", (PyObject *)&NDArray_Type);
28412841

2842-
Py_TYPE(&StaticArray_Type) = &PyType_Type;
2842+
Py_SET_TYPE(&StaticArray_Type, &PyType_Type);
28432843
Py_INCREF(&StaticArray_Type);
28442844
PyModule_AddObject(m, "staticarray", (PyObject *)&StaticArray_Type);
28452845

0 commit comments

Comments
 (0)