Skip to content

Commit 329e4a1

Browse files
gh-86493: Modernize modules initialization code (GH-106858)
Use PyModule_Add() or PyModule_AddObjectRef() instead of soft deprecated PyModule_AddObject().
1 parent f443b54 commit 329e4a1

29 files changed

+84
-292
lines changed

Doc/extending/extending.rst

+2-5
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ with an exception object::
221221
return NULL;
222222

223223
SpamError = PyErr_NewException("spam.error", NULL, NULL);
224-
Py_XINCREF(SpamError);
225-
if (PyModule_AddObject(m, "error", SpamError) < 0) {
226-
Py_XDECREF(SpamError);
224+
if (PyModule_AddObjectRef(m, "error", SpamError) < 0) {
227225
Py_CLEAR(SpamError);
228226
Py_DECREF(m);
229227
return NULL;
@@ -1281,8 +1279,7 @@ function must take care of initializing the C API pointer array::
12811279
/* Create a Capsule containing the API pointer array's address */
12821280
c_api_object = PyCapsule_New((void *)PySpam_API, "spam._C_API", NULL);
12831281

1284-
if (PyModule_AddObject(m, "_C_API", c_api_object) < 0) {
1285-
Py_XDECREF(c_api_object);
1282+
if (PyModule_Add(m, "_C_API", c_api_object) < 0) {
12861283
Py_DECREF(m);
12871284
return NULL;
12881285
}

Doc/extending/newtypes_tutorial.rst

+2-6
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,7 @@ This initializes the :class:`Custom` type, filling in a number of members
180180
to the appropriate default values, including :attr:`ob_type` that we initially
181181
set to ``NULL``. ::
182182

183-
Py_INCREF(&CustomType);
184-
if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType) < 0) {
185-
Py_DECREF(&CustomType);
183+
if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
186184
Py_DECREF(m);
187185
return NULL;
188186
}
@@ -862,9 +860,7 @@ function::
862860
if (m == NULL)
863861
return NULL;
864862

865-
Py_INCREF(&SubListType);
866-
if (PyModule_AddObject(m, "SubList", (PyObject *) &SubListType) < 0) {
867-
Py_DECREF(&SubListType);
863+
if (PyModule_AddObjectRef(m, "SubList", (PyObject *) &SubListType) < 0) {
868864
Py_DECREF(m);
869865
return NULL;
870866
}

Doc/includes/custom.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ PyInit_custom(void)
3434
if (m == NULL)
3535
return NULL;
3636

37-
Py_INCREF(&CustomType);
38-
if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType) < 0) {
39-
Py_DECREF(&CustomType);
37+
if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
4038
Py_DECREF(m);
4139
return NULL;
4240
}

Doc/includes/sublist.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ PyInit_sublist(void)
5858
if (m == NULL)
5959
return NULL;
6060

61-
Py_INCREF(&SubListType);
62-
if (PyModule_AddObject(m, "SubList", (PyObject *) &SubListType) < 0) {
63-
Py_DECREF(&SubListType);
61+
if (PyModule_AddObjectRef(m, "SubList", (PyObject *) &SubListType) < 0) {
6462
Py_DECREF(m);
6563
return NULL;
6664
}

Modules/_datetimemodule.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -6834,8 +6834,7 @@ _datetime_exec(PyObject *module)
68346834
return -1;
68356835
}
68366836

6837-
if (PyModule_AddObject(module, "datetime_CAPI", x) < 0) {
6838-
Py_DECREF(x);
6837+
if (PyModule_Add(module, "datetime_CAPI", x) < 0) {
68396838
return -1;
68406839
}
68416840

Modules/_decimal/_decimal.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -6053,9 +6053,8 @@ PyInit__decimal(void)
60536053

60546054
/* Init mpd_ssize_t constants */
60556055
for (ssize_cm = ssize_constants; ssize_cm->name != NULL; ssize_cm++) {
6056-
ASSIGN_PTR(obj, PyLong_FromSsize_t(ssize_cm->val));
6057-
CHECK_INT(PyModule_AddObject(m, ssize_cm->name, obj));
6058-
obj = NULL;
6056+
CHECK_INT(PyModule_Add(m, ssize_cm->name,
6057+
PyLong_FromSsize_t(ssize_cm->val)));
60596058
}
60606059

60616060
/* Init int constants */

Modules/_gdbmmodule.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -787,11 +787,7 @@ _gdbm_exec(PyObject *module)
787787
defined(GDBM_VERSION_PATCH)
788788
PyObject *obj = Py_BuildValue("iii", GDBM_VERSION_MAJOR,
789789
GDBM_VERSION_MINOR, GDBM_VERSION_PATCH);
790-
if (obj == NULL) {
791-
return -1;
792-
}
793-
if (PyModule_AddObject(module, "_GDBM_VERSION", obj) < 0) {
794-
Py_DECREF(obj);
790+
if (PyModule_Add(module, "_GDBM_VERSION", obj) < 0) {
795791
return -1;
796792
}
797793
#endif

Modules/_hashopenssl.c

+1-6
Original file line numberDiff line numberDiff line change
@@ -1889,12 +1889,7 @@ hashlib_md_meth_names(PyObject *module)
18891889
return -1;
18901890
}
18911891

1892-
if (PyModule_AddObject(module, "openssl_md_meth_names", state.set) < 0) {
1893-
Py_DECREF(state.set);
1894-
return -1;
1895-
}
1896-
1897-
return 0;
1892+
return PyModule_Add(module, "openssl_md_meth_names", state.set);
18981893
}
18991894

19001895
/*[clinic input]

Modules/_heapqmodule.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,7 @@ From all times, sorting has always been a Great Art! :-)\n");
672672
static int
673673
heapq_exec(PyObject *m)
674674
{
675-
PyObject *about = PyUnicode_FromString(__about__);
676-
if (PyModule_AddObject(m, "__about__", about) < 0) {
677-
Py_DECREF(about);
675+
if (PyModule_Add(m, "__about__", PyUnicode_FromString(__about__)) < 0) {
678676
return -1;
679677
}
680678
return 0;

Modules/_localemodule.c

+1-6
Original file line numberDiff line numberDiff line change
@@ -844,12 +844,7 @@ _locale_exec(PyObject *module)
844844

845845
_locale_state *state = get_locale_state(module);
846846
state->Error = PyErr_NewException("locale.Error", NULL, NULL);
847-
if (state->Error == NULL) {
848-
return -1;
849-
}
850-
Py_INCREF(get_locale_state(module)->Error);
851-
if (PyModule_AddObject(module, "Error", get_locale_state(module)->Error) < 0) {
852-
Py_DECREF(get_locale_state(module)->Error);
847+
if (PyModule_AddObjectRef(module, "Error", state->Error) < 0) {
853848
return -1;
854849
}
855850

Modules/_lzmamodule.c

+1-9
Original file line numberDiff line numberDiff line change
@@ -1498,15 +1498,7 @@ _lzma__decode_filter_properties_impl(PyObject *module, lzma_vli filter_id,
14981498
static int
14991499
module_add_int_constant(PyObject *m, const char *name, long long value)
15001500
{
1501-
PyObject *o = PyLong_FromLongLong(value);
1502-
if (o == NULL) {
1503-
return -1;
1504-
}
1505-
if (PyModule_AddObject(m, name, o) == 0) {
1506-
return 0;
1507-
}
1508-
Py_DECREF(o);
1509-
return -1;
1501+
return PyModule_Add(m, name, PyLong_FromLongLong(value));
15101502
}
15111503

15121504
static int

Modules/_multiprocessing/multiprocessing.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,7 @@ multiprocessing_exec(PyObject *module)
266266
ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
267267
#endif
268268

269-
if (PyModule_AddObject(module, "flags", flags) < 0) {
270-
Py_DECREF(flags);
269+
if (PyModule_Add(module, "flags", flags) < 0) {
271270
return -1;
272271
}
273272

Modules/_ssl.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -5986,7 +5986,7 @@ sslmodule_init_constants(PyObject *m)
59865986
#define addbool(m, key, value) \
59875987
do { \
59885988
PyObject *bool_obj = (value) ? Py_True : Py_False; \
5989-
PyModule_AddObject((m), (key), Py_NewRef(bool_obj)); \
5989+
PyModule_AddObjectRef((m), (key), bool_obj); \
59905990
} while (0)
59915991

59925992
addbool(m, "HAS_SNI", 1);

Modules/_testcapi/heaptype.c

+24-68
Original file line numberDiff line numberDiff line change
@@ -1122,127 +1122,83 @@ _PyTestCapi_Init_Heaptype(PyObject *m) {
11221122
return -1;
11231123
}
11241124

1125+
#define ADD(name, value) do { \
1126+
if (PyModule_Add(m, name, value) < 0) { \
1127+
return -1; \
1128+
} \
1129+
} while (0)
1130+
11251131
PyObject *HeapDocCType = PyType_FromSpec(&HeapDocCType_spec);
1126-
if (HeapDocCType == NULL) {
1127-
return -1;
1128-
}
1129-
PyModule_AddObject(m, "HeapDocCType", HeapDocCType);
1132+
ADD("HeapDocCType", HeapDocCType);
11301133

11311134
/* bpo-41832: Add a new type to test PyType_FromSpec()
11321135
now can accept a NULL tp_doc slot. */
11331136
PyObject *NullTpDocType = PyType_FromSpec(&NullTpDocType_spec);
1134-
if (NullTpDocType == NULL) {
1135-
return -1;
1136-
}
1137-
PyModule_AddObject(m, "NullTpDocType", NullTpDocType);
1137+
ADD("NullTpDocType", NullTpDocType);
11381138

11391139
PyObject *HeapGcCType = PyType_FromSpec(&HeapGcCType_spec);
1140-
if (HeapGcCType == NULL) {
1141-
return -1;
1142-
}
1143-
PyModule_AddObject(m, "HeapGcCType", HeapGcCType);
1140+
ADD("HeapGcCType", HeapGcCType);
11441141

11451142
PyObject *HeapCType = PyType_FromSpec(&HeapCType_spec);
11461143
if (HeapCType == NULL) {
11471144
return -1;
11481145
}
11491146
PyObject *subclass_bases = PyTuple_Pack(1, HeapCType);
1147+
Py_DECREF(HeapCType);
11501148
if (subclass_bases == NULL) {
11511149
return -1;
11521150
}
11531151
PyObject *HeapCTypeSubclass = PyType_FromSpecWithBases(&HeapCTypeSubclass_spec, subclass_bases);
1154-
if (HeapCTypeSubclass == NULL) {
1155-
return -1;
1156-
}
11571152
Py_DECREF(subclass_bases);
1158-
PyModule_AddObject(m, "HeapCTypeSubclass", HeapCTypeSubclass);
1153+
ADD("HeapCTypeSubclass", HeapCTypeSubclass);
11591154

11601155
PyObject *HeapCTypeWithDict = PyType_FromSpec(&HeapCTypeWithDict_spec);
1161-
if (HeapCTypeWithDict == NULL) {
1162-
return -1;
1163-
}
1164-
PyModule_AddObject(m, "HeapCTypeWithDict", HeapCTypeWithDict);
1156+
ADD("HeapCTypeWithDict", HeapCTypeWithDict);
11651157

11661158
PyObject *HeapCTypeWithDict2 = PyType_FromSpec(&HeapCTypeWithDict2_spec);
1167-
if (HeapCTypeWithDict2 == NULL) {
1168-
return -1;
1169-
}
1170-
PyModule_AddObject(m, "HeapCTypeWithDict2", HeapCTypeWithDict2);
1159+
ADD("HeapCTypeWithDict2", HeapCTypeWithDict2);
11711160

11721161
PyObject *HeapCTypeWithNegativeDict = PyType_FromSpec(&HeapCTypeWithNegativeDict_spec);
1173-
if (HeapCTypeWithNegativeDict == NULL) {
1174-
return -1;
1175-
}
1176-
PyModule_AddObject(m, "HeapCTypeWithNegativeDict", HeapCTypeWithNegativeDict);
1162+
ADD("HeapCTypeWithNegativeDict", HeapCTypeWithNegativeDict);
11771163

11781164
PyObject *HeapCTypeWithManagedDict = PyType_FromSpec(&HeapCTypeWithManagedDict_spec);
1179-
if (HeapCTypeWithManagedDict == NULL) {
1180-
return -1;
1181-
}
1182-
PyModule_AddObject(m, "HeapCTypeWithManagedDict", HeapCTypeWithManagedDict);
1165+
ADD("HeapCTypeWithManagedDict", HeapCTypeWithManagedDict);
11831166

11841167
PyObject *HeapCTypeWithManagedWeakref = PyType_FromSpec(&HeapCTypeWithManagedWeakref_spec);
1185-
if (HeapCTypeWithManagedWeakref == NULL) {
1186-
return -1;
1187-
}
1188-
PyModule_AddObject(m, "HeapCTypeWithManagedWeakref", HeapCTypeWithManagedWeakref);
1168+
ADD("HeapCTypeWithManagedWeakref", HeapCTypeWithManagedWeakref);
11891169

11901170
PyObject *HeapCTypeWithWeakref = PyType_FromSpec(&HeapCTypeWithWeakref_spec);
1191-
if (HeapCTypeWithWeakref == NULL) {
1192-
return -1;
1193-
}
1194-
PyModule_AddObject(m, "HeapCTypeWithWeakref", HeapCTypeWithWeakref);
1171+
ADD("HeapCTypeWithWeakref", HeapCTypeWithWeakref);
11951172

11961173
PyObject *HeapCTypeWithWeakref2 = PyType_FromSpec(&HeapCTypeWithWeakref2_spec);
1197-
if (HeapCTypeWithWeakref2 == NULL) {
1198-
return -1;
1199-
}
1200-
PyModule_AddObject(m, "HeapCTypeWithWeakref2", HeapCTypeWithWeakref2);
1174+
ADD("HeapCTypeWithWeakref2", HeapCTypeWithWeakref2);
12011175

12021176
PyObject *HeapCTypeWithBuffer = PyType_FromSpec(&HeapCTypeWithBuffer_spec);
1203-
if (HeapCTypeWithBuffer == NULL) {
1204-
return -1;
1205-
}
1206-
PyModule_AddObject(m, "HeapCTypeWithBuffer", HeapCTypeWithBuffer);
1177+
ADD("HeapCTypeWithBuffer", HeapCTypeWithBuffer);
12071178

12081179
PyObject *HeapCTypeSetattr = PyType_FromSpec(&HeapCTypeSetattr_spec);
1209-
if (HeapCTypeSetattr == NULL) {
1210-
return -1;
1211-
}
1212-
PyModule_AddObject(m, "HeapCTypeSetattr", HeapCTypeSetattr);
1180+
ADD("HeapCTypeSetattr", HeapCTypeSetattr);
12131181

12141182
PyObject *subclass_with_finalizer_bases = PyTuple_Pack(1, HeapCTypeSubclass);
12151183
if (subclass_with_finalizer_bases == NULL) {
12161184
return -1;
12171185
}
12181186
PyObject *HeapCTypeSubclassWithFinalizer = PyType_FromSpecWithBases(
12191187
&HeapCTypeSubclassWithFinalizer_spec, subclass_with_finalizer_bases);
1220-
if (HeapCTypeSubclassWithFinalizer == NULL) {
1221-
return -1;
1222-
}
12231188
Py_DECREF(subclass_with_finalizer_bases);
1224-
PyModule_AddObject(m, "HeapCTypeSubclassWithFinalizer", HeapCTypeSubclassWithFinalizer);
1189+
ADD("HeapCTypeSubclassWithFinalizer", HeapCTypeSubclassWithFinalizer);
12251190

12261191
PyObject *HeapCTypeMetaclass = PyType_FromMetaclass(
12271192
&PyType_Type, m, &HeapCTypeMetaclass_spec, (PyObject *) &PyType_Type);
1228-
if (HeapCTypeMetaclass == NULL) {
1229-
return -1;
1230-
}
1231-
PyModule_AddObject(m, "HeapCTypeMetaclass", HeapCTypeMetaclass);
1193+
ADD("HeapCTypeMetaclass", HeapCTypeMetaclass);
12321194

12331195
PyObject *HeapCTypeMetaclassCustomNew = PyType_FromMetaclass(
12341196
&PyType_Type, m, &HeapCTypeMetaclassCustomNew_spec, (PyObject *) &PyType_Type);
1235-
if (HeapCTypeMetaclassCustomNew == NULL) {
1236-
return -1;
1237-
}
1238-
PyModule_AddObject(m, "HeapCTypeMetaclassCustomNew", HeapCTypeMetaclassCustomNew);
1197+
ADD("HeapCTypeMetaclassCustomNew", HeapCTypeMetaclassCustomNew);
12391198

12401199
PyObject *HeapCTypeMetaclassNullNew = PyType_FromMetaclass(
12411200
&PyType_Type, m, &HeapCTypeMetaclassNullNew_spec, (PyObject *) &PyType_Type);
1242-
if (HeapCTypeMetaclassNullNew == NULL) {
1243-
return -1;
1244-
}
1245-
PyModule_AddObject(m, "HeapCTypeMetaclassNullNew", HeapCTypeMetaclassNullNew);
1201+
ADD("HeapCTypeMetaclassNullNew", HeapCTypeMetaclassNullNew);
12461202

12471203
PyObject *HeapCCollection = PyType_FromMetaclass(
12481204
NULL, m, &HeapCCollection_spec, NULL);

Modules/_testmultiphase.c

+4-20
Original file line numberDiff line numberDiff line change
@@ -383,32 +383,20 @@ static int execfunc(PyObject *m)
383383

384384
/* Add a custom type */
385385
temp = PyType_FromSpec(&Example_Type_spec);
386-
if (temp == NULL) {
387-
goto fail;
388-
}
389-
if (PyModule_AddObject(m, "Example", temp) != 0) {
390-
Py_DECREF(temp);
386+
if (PyModule_Add(m, "Example", temp) != 0) {
391387
goto fail;
392388
}
393389

394390

395391
/* Add an exception type */
396392
temp = PyErr_NewException("_testimportexec.error", NULL, NULL);
397-
if (temp == NULL) {
398-
goto fail;
399-
}
400-
if (PyModule_AddObject(m, "error", temp) != 0) {
401-
Py_DECREF(temp);
393+
if (PyModule_Add(m, "error", temp) != 0) {
402394
goto fail;
403395
}
404396

405397
/* Add Str */
406398
temp = PyType_FromSpec(&Str_Type_spec);
407-
if (temp == NULL) {
408-
goto fail;
409-
}
410-
if (PyModule_AddObject(m, "Str", temp) != 0) {
411-
Py_DECREF(temp);
399+
if (PyModule_Add(m, "Str", temp) != 0) {
412400
goto fail;
413401
}
414402

@@ -857,11 +845,7 @@ meth_state_access_exec(PyObject *m)
857845
}
858846

859847
temp = PyType_FromModuleAndSpec(m, &StateAccessType_spec, NULL);
860-
if (temp == NULL) {
861-
return -1;
862-
}
863-
if (PyModule_AddObject(m, "StateAccessType", temp) != 0) {
864-
Py_DECREF(temp);
848+
if (PyModule_Add(m, "StateAccessType", temp) != 0) {
865849
return -1;
866850
}
867851

0 commit comments

Comments
 (0)