Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-86493: Modernize modules initialization code #106858

Merged
merged 3 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions Doc/extending/extending.rst
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,7 @@ with an exception object::
return NULL;

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

if (PyModule_AddObject(m, "_C_API", c_api_object) < 0) {
Py_XDECREF(c_api_object);
if (PyModule_Add(m, "_C_API", c_api_object) < 0) {
Py_DECREF(m);
return NULL;
}
Expand Down
8 changes: 2 additions & 6 deletions Doc/extending/newtypes_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@ This initializes the :class:`Custom` type, filling in a number of members
to the appropriate default values, including :attr:`ob_type` that we initially
set to ``NULL``. ::

Py_INCREF(&CustomType);
if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType) < 0) {
Py_DECREF(&CustomType);
if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
Py_DECREF(m);
return NULL;
}
Expand Down Expand Up @@ -862,9 +860,7 @@ function::
if (m == NULL)
return NULL;

Py_INCREF(&SubListType);
if (PyModule_AddObject(m, "SubList", (PyObject *) &SubListType) < 0) {
Py_DECREF(&SubListType);
if (PyModule_AddObjectRef(m, "SubList", (PyObject *) &SubListType) < 0) {
Py_DECREF(m);
return NULL;
}
Expand Down
4 changes: 1 addition & 3 deletions Doc/includes/custom.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ PyInit_custom(void)
if (m == NULL)
return NULL;

Py_INCREF(&CustomType);
if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType) < 0) {
Py_DECREF(&CustomType);
if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
Py_DECREF(m);
return NULL;
}
Expand Down
4 changes: 1 addition & 3 deletions Doc/includes/sublist.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ PyInit_sublist(void)
if (m == NULL)
return NULL;

Py_INCREF(&SubListType);
if (PyModule_AddObject(m, "SubList", (PyObject *) &SubListType) < 0) {
Py_DECREF(&SubListType);
if (PyModule_AddObjectRef(m, "SubList", (PyObject *) &SubListType) < 0) {
Py_DECREF(m);
return NULL;
}
Expand Down
3 changes: 1 addition & 2 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6834,8 +6834,7 @@ _datetime_exec(PyObject *module)
return -1;
}

if (PyModule_AddObject(module, "datetime_CAPI", x) < 0) {
Py_DECREF(x);
if (PyModule_Add(module, "datetime_CAPI", x) < 0) {
return -1;
}

Expand Down
5 changes: 2 additions & 3 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -6023,9 +6023,8 @@ PyInit__decimal(void)

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

/* Init int constants */
Expand Down
6 changes: 1 addition & 5 deletions Modules/_gdbmmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -755,11 +755,7 @@ _gdbm_exec(PyObject *module)
defined(GDBM_VERSION_PATCH)
PyObject *obj = Py_BuildValue("iii", GDBM_VERSION_MAJOR,
GDBM_VERSION_MINOR, GDBM_VERSION_PATCH);
if (obj == NULL) {
return -1;
}
if (PyModule_AddObject(module, "_GDBM_VERSION", obj) < 0) {
Py_DECREF(obj);
if (PyModule_Add(module, "_GDBM_VERSION", obj) < 0) {
return -1;
}
#endif
Expand Down
7 changes: 1 addition & 6 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1888,12 +1888,7 @@ hashlib_md_meth_names(PyObject *module)
return -1;
}

if (PyModule_AddObject(module, "openssl_md_meth_names", state.set) < 0) {
Py_DECREF(state.set);
return -1;
}

return 0;
return PyModule_Add(module, "openssl_md_meth_names", state.set);
}

/*[clinic input]
Expand Down
4 changes: 1 addition & 3 deletions Modules/_heapqmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -672,9 +672,7 @@ From all times, sorting has always been a Great Art! :-)\n");
static int
heapq_exec(PyObject *m)
{
PyObject *about = PyUnicode_FromString(__about__);
if (PyModule_AddObject(m, "__about__", about) < 0) {
Py_DECREF(about);
if (PyModule_Add(m, "__about__", PyUnicode_FromString(__about__)) < 0) {
return -1;
}
return 0;
Expand Down
7 changes: 1 addition & 6 deletions Modules/_localemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -844,12 +844,7 @@ _locale_exec(PyObject *module)

_locale_state *state = get_locale_state(module);
state->Error = PyErr_NewException("locale.Error", NULL, NULL);
if (state->Error == NULL) {
return -1;
}
Py_INCREF(get_locale_state(module)->Error);
if (PyModule_AddObject(module, "Error", get_locale_state(module)->Error) < 0) {
Py_DECREF(get_locale_state(module)->Error);
if (PyModule_AddObjectRef(module, "Error", state->Error) < 0) {
return -1;
}

Expand Down
10 changes: 1 addition & 9 deletions Modules/_lzmamodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1498,15 +1498,7 @@ _lzma__decode_filter_properties_impl(PyObject *module, lzma_vli filter_id,
static int
module_add_int_constant(PyObject *m, const char *name, long long value)
{
PyObject *o = PyLong_FromLongLong(value);
if (o == NULL) {
return -1;
}
if (PyModule_AddObject(m, name, o) == 0) {
return 0;
}
Py_DECREF(o);
return -1;
Comment on lines -1505 to -1509
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is very confusing code with inverse logic. I thought there was a bug.

return PyModule_Add(m, name, PyLong_FromLongLong(value));
}

static int
Expand Down
3 changes: 1 addition & 2 deletions Modules/_multiprocessing/multiprocessing.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ multiprocessing_exec(PyObject *module)
ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
#endif

if (PyModule_AddObject(module, "flags", flags) < 0) {
Py_DECREF(flags);
if (PyModule_Add(module, "flags", flags) < 0) {
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -5991,7 +5991,7 @@ sslmodule_init_constants(PyObject *m)
#define addbool(m, key, value) \
do { \
PyObject *bool_obj = (value) ? Py_True : Py_False; \
PyModule_AddObject((m), (key), Py_NewRef(bool_obj)); \
PyModule_AddObjectRef((m), (key), bool_obj); \
} while (0)

addbool(m, "HAS_SNI", 1);
Expand Down
92 changes: 24 additions & 68 deletions Modules/_testcapi/heaptype.c
Original file line number Diff line number Diff line change
Expand Up @@ -1122,127 +1122,83 @@ _PyTestCapi_Init_Heaptype(PyObject *m) {
return -1;
}

#define ADD(name, value) do { \
if (PyModule_Add(m, name, value) < 0) { \
return -1; \
} \
} while (0)

PyObject *HeapDocCType = PyType_FromSpec(&HeapDocCType_spec);
if (HeapDocCType == NULL) {
return -1;
}
PyModule_AddObject(m, "HeapDocCType", HeapDocCType);
ADD("HeapDocCType", HeapDocCType);

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

PyObject *HeapGcCType = PyType_FromSpec(&HeapGcCType_spec);
if (HeapGcCType == NULL) {
return -1;
}
PyModule_AddObject(m, "HeapGcCType", HeapGcCType);
ADD("HeapGcCType", HeapGcCType);

PyObject *HeapCType = PyType_FromSpec(&HeapCType_spec);
if (HeapCType == NULL) {
return -1;
}
PyObject *subclass_bases = PyTuple_Pack(1, HeapCType);
Py_DECREF(HeapCType);
if (subclass_bases == NULL) {
return -1;
}
PyObject *HeapCTypeSubclass = PyType_FromSpecWithBases(&HeapCTypeSubclass_spec, subclass_bases);
if (HeapCTypeSubclass == NULL) {
return -1;
}
Py_DECREF(subclass_bases);
PyModule_AddObject(m, "HeapCTypeSubclass", HeapCTypeSubclass);
ADD("HeapCTypeSubclass", HeapCTypeSubclass);

PyObject *HeapCTypeWithDict = PyType_FromSpec(&HeapCTypeWithDict_spec);
if (HeapCTypeWithDict == NULL) {
return -1;
}
PyModule_AddObject(m, "HeapCTypeWithDict", HeapCTypeWithDict);
ADD("HeapCTypeWithDict", HeapCTypeWithDict);

PyObject *HeapCTypeWithDict2 = PyType_FromSpec(&HeapCTypeWithDict2_spec);
if (HeapCTypeWithDict2 == NULL) {
return -1;
}
PyModule_AddObject(m, "HeapCTypeWithDict2", HeapCTypeWithDict2);
ADD("HeapCTypeWithDict2", HeapCTypeWithDict2);

PyObject *HeapCTypeWithNegativeDict = PyType_FromSpec(&HeapCTypeWithNegativeDict_spec);
if (HeapCTypeWithNegativeDict == NULL) {
return -1;
}
PyModule_AddObject(m, "HeapCTypeWithNegativeDict", HeapCTypeWithNegativeDict);
ADD("HeapCTypeWithNegativeDict", HeapCTypeWithNegativeDict);

PyObject *HeapCTypeWithManagedDict = PyType_FromSpec(&HeapCTypeWithManagedDict_spec);
if (HeapCTypeWithManagedDict == NULL) {
return -1;
}
PyModule_AddObject(m, "HeapCTypeWithManagedDict", HeapCTypeWithManagedDict);
ADD("HeapCTypeWithManagedDict", HeapCTypeWithManagedDict);

PyObject *HeapCTypeWithManagedWeakref = PyType_FromSpec(&HeapCTypeWithManagedWeakref_spec);
if (HeapCTypeWithManagedWeakref == NULL) {
return -1;
}
PyModule_AddObject(m, "HeapCTypeWithManagedWeakref", HeapCTypeWithManagedWeakref);
ADD("HeapCTypeWithManagedWeakref", HeapCTypeWithManagedWeakref);

PyObject *HeapCTypeWithWeakref = PyType_FromSpec(&HeapCTypeWithWeakref_spec);
if (HeapCTypeWithWeakref == NULL) {
return -1;
}
PyModule_AddObject(m, "HeapCTypeWithWeakref", HeapCTypeWithWeakref);
ADD("HeapCTypeWithWeakref", HeapCTypeWithWeakref);

PyObject *HeapCTypeWithWeakref2 = PyType_FromSpec(&HeapCTypeWithWeakref2_spec);
if (HeapCTypeWithWeakref2 == NULL) {
return -1;
}
PyModule_AddObject(m, "HeapCTypeWithWeakref2", HeapCTypeWithWeakref2);
ADD("HeapCTypeWithWeakref2", HeapCTypeWithWeakref2);

PyObject *HeapCTypeWithBuffer = PyType_FromSpec(&HeapCTypeWithBuffer_spec);
if (HeapCTypeWithBuffer == NULL) {
return -1;
}
PyModule_AddObject(m, "HeapCTypeWithBuffer", HeapCTypeWithBuffer);
ADD("HeapCTypeWithBuffer", HeapCTypeWithBuffer);

PyObject *HeapCTypeSetattr = PyType_FromSpec(&HeapCTypeSetattr_spec);
if (HeapCTypeSetattr == NULL) {
return -1;
}
PyModule_AddObject(m, "HeapCTypeSetattr", HeapCTypeSetattr);
ADD("HeapCTypeSetattr", HeapCTypeSetattr);

PyObject *subclass_with_finalizer_bases = PyTuple_Pack(1, HeapCTypeSubclass);
if (subclass_with_finalizer_bases == NULL) {
return -1;
}
PyObject *HeapCTypeSubclassWithFinalizer = PyType_FromSpecWithBases(
&HeapCTypeSubclassWithFinalizer_spec, subclass_with_finalizer_bases);
if (HeapCTypeSubclassWithFinalizer == NULL) {
return -1;
}
Py_DECREF(subclass_with_finalizer_bases);
PyModule_AddObject(m, "HeapCTypeSubclassWithFinalizer", HeapCTypeSubclassWithFinalizer);
ADD("HeapCTypeSubclassWithFinalizer", HeapCTypeSubclassWithFinalizer);

PyObject *HeapCTypeMetaclass = PyType_FromMetaclass(
&PyType_Type, m, &HeapCTypeMetaclass_spec, (PyObject *) &PyType_Type);
if (HeapCTypeMetaclass == NULL) {
return -1;
}
PyModule_AddObject(m, "HeapCTypeMetaclass", HeapCTypeMetaclass);
ADD("HeapCTypeMetaclass", HeapCTypeMetaclass);

PyObject *HeapCTypeMetaclassCustomNew = PyType_FromMetaclass(
&PyType_Type, m, &HeapCTypeMetaclassCustomNew_spec, (PyObject *) &PyType_Type);
if (HeapCTypeMetaclassCustomNew == NULL) {
return -1;
}
PyModule_AddObject(m, "HeapCTypeMetaclassCustomNew", HeapCTypeMetaclassCustomNew);
ADD("HeapCTypeMetaclassCustomNew", HeapCTypeMetaclassCustomNew);

PyObject *HeapCTypeMetaclassNullNew = PyType_FromMetaclass(
&PyType_Type, m, &HeapCTypeMetaclassNullNew_spec, (PyObject *) &PyType_Type);
if (HeapCTypeMetaclassNullNew == NULL) {
return -1;
}
PyModule_AddObject(m, "HeapCTypeMetaclassNullNew", HeapCTypeMetaclassNullNew);
ADD("HeapCTypeMetaclassNullNew", HeapCTypeMetaclassNullNew);

PyObject *HeapCCollection = PyType_FromMetaclass(
NULL, m, &HeapCCollection_spec, NULL);
Expand Down
2 changes: 1 addition & 1 deletion Modules/_testcapi/structmember.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ _PyTestCapi_Init_Structmember(PyObject *m)
if (res < 0) {
return -1;
}
res = PyModule_AddObject(
res = PyModule_AddObjectRef(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there was a bug.

m,
"_test_structmembersType_OldAPI",
(PyObject *)&test_structmembersType_OldAPI);
Expand Down
24 changes: 4 additions & 20 deletions Modules/_testmultiphase.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,32 +383,20 @@ static int execfunc(PyObject *m)

/* Add a custom type */
temp = PyType_FromSpec(&Example_Type_spec);
if (temp == NULL) {
goto fail;
}
if (PyModule_AddObject(m, "Example", temp) != 0) {
Py_DECREF(temp);
if (PyModule_Add(m, "Example", temp) != 0) {
goto fail;
}


/* Add an exception type */
temp = PyErr_NewException("_testimportexec.error", NULL, NULL);
if (temp == NULL) {
goto fail;
}
if (PyModule_AddObject(m, "error", temp) != 0) {
Py_DECREF(temp);
if (PyModule_Add(m, "error", temp) != 0) {
goto fail;
}

/* Add Str */
temp = PyType_FromSpec(&Str_Type_spec);
if (temp == NULL) {
goto fail;
}
if (PyModule_AddObject(m, "Str", temp) != 0) {
Py_DECREF(temp);
if (PyModule_Add(m, "Str", temp) != 0) {
goto fail;
}

Expand Down Expand Up @@ -857,11 +845,7 @@ meth_state_access_exec(PyObject *m)
}

temp = PyType_FromModuleAndSpec(m, &StateAccessType_spec, NULL);
if (temp == NULL) {
return -1;
}
if (PyModule_AddObject(m, "StateAccessType", temp) != 0) {
Py_DECREF(temp);
if (PyModule_Add(m, "StateAccessType", temp) != 0) {
return -1;
}

Expand Down
Loading