Skip to content

Commit dc61706

Browse files
serhiy-storchakajtcave
authored andcommitted
pythongh-86493: Modernize modules initialization code (pythonGH-106858)
Use PyModule_Add() or PyModule_AddObjectRef() instead of soft deprecated PyModule_AddObject().
1 parent edad6da commit dc61706

29 files changed

+84
-292
lines changed

Doc/extending/extending.rst

Lines changed: 2 additions & 5 deletions
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 1 addition & 6 deletions
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 1 addition & 6 deletions
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

0 commit comments

Comments
 (0)