Skip to content

Commit d2ec81a

Browse files
authored
bpo-39573: Add Py_SET_TYPE() function (pythonGH-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

+7
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

+1-1
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

+6
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
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

+2-2
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

+6-6
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

+1-1
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

+1-1
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

+1-1
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

+2-2
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

Modules/_testcapimodule.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -6605,9 +6605,9 @@ PyInit__testcapi(void)
66056605
if (m == NULL)
66066606
return NULL;
66076607

6608-
Py_TYPE(&_HashInheritanceTester_Type)=&PyType_Type;
6608+
Py_SET_TYPE(&_HashInheritanceTester_Type, &PyType_Type);
66096609

6610-
Py_TYPE(&test_structmembersType)=&PyType_Type;
6610+
Py_SET_TYPE(&test_structmembersType, &PyType_Type);
66116611
Py_INCREF(&test_structmembersType);
66126612
/* don't use a name starting with "test", since we don't want
66136613
test_capi to automatically call this */

Modules/arraymodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2996,7 +2996,7 @@ array_modexec(PyObject *m)
29962996

29972997
if (PyType_Ready(&Arraytype) < 0)
29982998
return -1;
2999-
Py_TYPE(&PyArrayIter_Type) = &PyType_Type;
2999+
Py_SET_TYPE(&PyArrayIter_Type, &PyType_Type);
30003000

30013001
Py_INCREF((PyObject *)&Arraytype);
30023002
if (PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype) < 0) {

Modules/itertoolsmodule.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -4750,14 +4750,16 @@ PyInit_itertools(void)
47504750
NULL
47514751
};
47524752

4753-
Py_TYPE(&teedataobject_type) = &PyType_Type;
4753+
Py_SET_TYPE(&teedataobject_type, &PyType_Type);
47544754
m = PyModule_Create(&itertoolsmodule);
4755-
if (m == NULL)
4755+
if (m == NULL) {
47564756
return NULL;
4757+
}
47574758

47584759
for (i=0 ; typelist[i] != NULL ; i++) {
4759-
if (PyType_Ready(typelist[i]) < 0)
4760+
if (PyType_Ready(typelist[i]) < 0) {
47604761
return NULL;
4762+
}
47614763
name = _PyType_Name(typelist[i]);
47624764
Py_INCREF(typelist[i]);
47634765
PyModule_AddObject(m, name, (PyObject *)typelist[i]);

Modules/md5module.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -572,13 +572,15 @@ PyInit__md5(void)
572572
{
573573
PyObject *m;
574574

575-
Py_TYPE(&MD5type) = &PyType_Type;
576-
if (PyType_Ready(&MD5type) < 0)
575+
Py_SET_TYPE(&MD5type, &PyType_Type);
576+
if (PyType_Ready(&MD5type) < 0) {
577577
return NULL;
578+
}
578579

579580
m = PyModule_Create(&_md5module);
580-
if (m == NULL)
581+
if (m == NULL) {
581582
return NULL;
583+
}
582584

583585
Py_INCREF((PyObject *)&MD5type);
584586
PyModule_AddObject(m, "MD5Type", (PyObject *)&MD5type);

Modules/sha1module.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,15 @@ PyInit__sha1(void)
549549
{
550550
PyObject *m;
551551

552-
Py_TYPE(&SHA1type) = &PyType_Type;
553-
if (PyType_Ready(&SHA1type) < 0)
552+
Py_SET_TYPE(&SHA1type, &PyType_Type);
553+
if (PyType_Ready(&SHA1type) < 0) {
554554
return NULL;
555+
}
555556

556557
m = PyModule_Create(&_sha1module);
557-
if (m == NULL)
558+
if (m == NULL) {
558559
return NULL;
560+
}
559561

560562
Py_INCREF((PyObject *)&SHA1type);
561563
PyModule_AddObject(m, "SHA1Type", (PyObject *)&SHA1type);

Modules/sha256module.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -713,12 +713,14 @@ PyInit__sha256(void)
713713
{
714714
PyObject *m;
715715

716-
Py_TYPE(&SHA224type) = &PyType_Type;
717-
if (PyType_Ready(&SHA224type) < 0)
716+
Py_SET_TYPE(&SHA224type, &PyType_Type);
717+
if (PyType_Ready(&SHA224type) < 0) {
718718
return NULL;
719-
Py_TYPE(&SHA256type) = &PyType_Type;
720-
if (PyType_Ready(&SHA256type) < 0)
719+
}
720+
Py_SET_TYPE(&SHA256type, &PyType_Type);
721+
if (PyType_Ready(&SHA256type) < 0) {
721722
return NULL;
723+
}
722724

723725
m = PyModule_Create(&_sha256module);
724726
if (m == NULL)

Modules/sha512module.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -778,16 +778,19 @@ PyInit__sha512(void)
778778
{
779779
PyObject *m;
780780

781-
Py_TYPE(&SHA384type) = &PyType_Type;
782-
if (PyType_Ready(&SHA384type) < 0)
781+
Py_SET_TYPE(&SHA384type, &PyType_Type);
782+
if (PyType_Ready(&SHA384type) < 0) {
783783
return NULL;
784-
Py_TYPE(&SHA512type) = &PyType_Type;
785-
if (PyType_Ready(&SHA512type) < 0)
784+
}
785+
Py_SET_TYPE(&SHA512type, &PyType_Type);
786+
if (PyType_Ready(&SHA512type) < 0) {
786787
return NULL;
788+
}
787789

788790
m = PyModule_Create(&_sha512module);
789-
if (m == NULL)
791+
if (m == NULL) {
790792
return NULL;
793+
}
791794

792795
Py_INCREF((PyObject *)&SHA384type);
793796
PyModule_AddObject(m, "SHA384Type", (PyObject *)&SHA384type);

Modules/socketmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7100,7 +7100,7 @@ PyInit__socket(void)
71007100
}
71017101
#endif
71027102

7103-
Py_TYPE(&sock_type) = &PyType_Type;
7103+
Py_SET_TYPE(&sock_type, &PyType_Type);
71047104
m = PyModule_Create(&socketmodule);
71057105
if (m == NULL)
71067106
return NULL;

Modules/unicodedata.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ PyInit_unicodedata(void)
14551455
{
14561456
PyObject *m, *v;
14571457

1458-
Py_TYPE(&UCD_Type) = &PyType_Type;
1458+
Py_SET_TYPE(&UCD_Type, &PyType_Type);
14591459

14601460
m = PyModule_Create(&unicodedatamodule);
14611461
if (!m)

Objects/floatobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ float_dealloc(PyFloatObject *op)
221221
return;
222222
}
223223
numfree++;
224-
Py_TYPE(op) = (struct _typeobject *)free_list;
224+
Py_SET_TYPE(op, (PyTypeObject *)free_list);
225225
free_list = op;
226226
}
227227
else

Objects/moduleobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ PyModuleDef_Init(struct PyModuleDef* def)
5252
if (def->m_base.m_index == 0) {
5353
max_module_number++;
5454
Py_SET_REFCNT(def, 1);
55-
Py_TYPE(def) = &PyModuleDef_Type;
55+
Py_SET_TYPE(def, &PyModuleDef_Type);
5656
def->m_base.m_index = max_module_number;
5757
}
5858
return (PyObject*)def;

Objects/object.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ PyObject_Init(PyObject *op, PyTypeObject *tp)
144144
return PyErr_NoMemory();
145145
}
146146

147-
Py_TYPE(op) = tp;
147+
Py_SET_TYPE(op, tp);
148148
if (PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE) {
149149
Py_INCREF(tp);
150150
}

Objects/typeobject.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -4097,9 +4097,10 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
40974097
}
40984098

40994099
if (compatible_for_assignment(oldto, newto, "__class__")) {
4100-
if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE)
4100+
if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
41014101
Py_INCREF(newto);
4102-
Py_TYPE(self) = newto;
4102+
}
4103+
Py_SET_TYPE(self, newto);
41034104
if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)
41044105
Py_DECREF(oldto);
41054106
return 0;
@@ -5334,8 +5335,9 @@ PyType_Ready(PyTypeObject *type)
53345335
NULL when type is &PyBaseObject_Type, and we know its ob_type is
53355336
not NULL (it's initialized to &PyType_Type). But coverity doesn't
53365337
know that. */
5337-
if (Py_TYPE(type) == NULL && base != NULL)
5338-
Py_TYPE(type) = Py_TYPE(base);
5338+
if (Py_TYPE(type) == NULL && base != NULL) {
5339+
Py_SET_TYPE(type, Py_TYPE(base));
5340+
}
53395341

53405342
/* Initialize tp_bases */
53415343
bases = type->tp_bases;

Objects/weakrefobject.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -882,10 +882,12 @@ PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
882882
if (result != NULL) {
883883
PyWeakReference *prev;
884884

885-
if (PyCallable_Check(ob))
886-
Py_TYPE(result) = &_PyWeakref_CallableProxyType;
887-
else
888-
Py_TYPE(result) = &_PyWeakref_ProxyType;
885+
if (PyCallable_Check(ob)) {
886+
Py_SET_TYPE(result, &_PyWeakref_CallableProxyType);
887+
}
888+
else {
889+
Py_SET_TYPE(result, &_PyWeakref_ProxyType);
890+
}
889891
get_basic_refs(*list, &ref, &proxy);
890892
if (callback == NULL) {
891893
if (proxy != NULL) {

0 commit comments

Comments
 (0)