gh-86493: Modernize modules initialization code (GH-106858)

Use PyModule_Add() or PyModule_AddObjectRef() instead of soft deprecated
PyModule_AddObject().
This commit is contained in:
Serhiy Storchaka 2023-07-25 14:34:49 +03:00 committed by GitHub
parent f443b54a2f
commit 329e4a1a3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 84 additions and 292 deletions

View file

@ -2030,17 +2030,11 @@ zlib_exec(PyObject *mod)
}
state->ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
if (state->ZlibError == NULL) {
if (PyModule_AddObjectRef(mod, "error", state->ZlibError) < 0) {
return -1;
}
if (PyModule_AddObject(mod, "error", Py_NewRef(state->ZlibError)) < 0) {
Py_DECREF(state->ZlibError);
return -1;
}
if (PyModule_AddObject(mod, "_ZlibDecompressor",
Py_NewRef(state->ZlibDecompressorType)) < 0) {
Py_DECREF(state->ZlibDecompressorType);
if (PyModule_AddObjectRef(mod, "_ZlibDecompressor",
(PyObject *)state->ZlibDecompressorType) < 0) {
return -1;
}
@ -2082,26 +2076,14 @@ zlib_exec(PyObject *mod)
#ifdef Z_TREES // 1.2.3.4, only for inflate
ZLIB_ADD_INT_MACRO(Z_TREES);
#endif
PyObject *ver = PyUnicode_FromString(ZLIB_VERSION);
if (ver == NULL) {
if (PyModule_Add(mod, "ZLIB_VERSION",
PyUnicode_FromString(ZLIB_VERSION)) < 0) {
return -1;
}
if (PyModule_AddObject(mod, "ZLIB_VERSION", ver) < 0) {
Py_DECREF(ver);
if (PyModule_Add(mod, "ZLIB_RUNTIME_VERSION",
PyUnicode_FromString(zlibVersion())) < 0) {
return -1;
}
ver = PyUnicode_FromString(zlibVersion());
if (ver == NULL) {
return -1;
}
if (PyModule_AddObject(mod, "ZLIB_RUNTIME_VERSION", ver) < 0) {
Py_DECREF(ver);
return -1;
}
if (PyModule_AddStringConstant(mod, "__version__", "1.0") < 0) {
return -1;
}