From 329e4a1a3f8c53d9d120d2eed93b95a04b826f2f Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 25 Jul 2023 14:34:49 +0300 Subject: [PATCH] gh-86493: Modernize modules initialization code (GH-106858) Use PyModule_Add() or PyModule_AddObjectRef() instead of soft deprecated PyModule_AddObject(). --- Doc/extending/extending.rst | 7 +- Doc/extending/newtypes_tutorial.rst | 8 +- Doc/includes/custom.c | 4 +- Doc/includes/sublist.c | 4 +- Modules/_datetimemodule.c | 3 +- Modules/_decimal/_decimal.c | 5 +- Modules/_gdbmmodule.c | 6 +- Modules/_hashopenssl.c | 7 +- Modules/_heapqmodule.c | 4 +- Modules/_localemodule.c | 7 +- Modules/_lzmamodule.c | 10 +-- Modules/_multiprocessing/multiprocessing.c | 3 +- Modules/_ssl.c | 2 +- Modules/_testcapi/heaptype.c | 92 ++++++---------------- Modules/_testmultiphase.c | 24 +----- Modules/_tkinter.c | 42 +++------- Modules/_weakref.c | 16 +--- Modules/arraymodule.c | 8 +- Modules/binascii.c | 20 +---- Modules/cjkcodecs/cjkcodecs.h | 6 +- Modules/md5module.c | 8 +- Modules/mmapmodule.c | 4 +- Modules/overlapped.c | 7 +- Modules/pyexpat.c | 14 +--- Modules/resource.c | 11 +-- Modules/sha1module.c | 9 +-- Modules/termios.c | 7 +- Modules/unicodedata.c | 6 +- Modules/zlibmodule.c | 32 ++------ 29 files changed, 84 insertions(+), 292 deletions(-) diff --git a/Doc/extending/extending.rst b/Doc/extending/extending.rst index 0c5da49fe61344..00a9edcf2e4147 100644 --- a/Doc/extending/extending.rst +++ b/Doc/extending/extending.rst @@ -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; @@ -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; } diff --git a/Doc/extending/newtypes_tutorial.rst b/Doc/extending/newtypes_tutorial.rst index ba09fb1b05c0bf..4f9c8899ffee17 100644 --- a/Doc/extending/newtypes_tutorial.rst +++ b/Doc/extending/newtypes_tutorial.rst @@ -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; } @@ -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; } diff --git a/Doc/includes/custom.c b/Doc/includes/custom.c index 9cfba50ace25db..5253f879360210 100644 --- a/Doc/includes/custom.c +++ b/Doc/includes/custom.c @@ -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; } diff --git a/Doc/includes/sublist.c b/Doc/includes/sublist.c index b36dadf07eae87..d8aba463f30ba2 100644 --- a/Doc/includes/sublist.c +++ b/Doc/includes/sublist.c @@ -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; } diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index b8cb0c012fd537..e0e34b78d5d843 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -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; } diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index f531def7b3a333..671976a220a647 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -6053,9 +6053,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 */ diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c index 47f2da8e0e3d04..eff36fd7fb669b 100644 --- a/Modules/_gdbmmodule.c +++ b/Modules/_gdbmmodule.c @@ -787,11 +787,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 diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 6d60037cd2054e..ee6fb8b4b03643 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -1889,12 +1889,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] diff --git a/Modules/_heapqmodule.c b/Modules/_heapqmodule.c index 00285ae01f8574..9d4ec256ee9e3e 100644 --- a/Modules/_heapqmodule.c +++ b/Modules/_heapqmodule.c @@ -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; diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c index 970530facd01b0..34915de7515d63 100644 --- a/Modules/_localemodule.c +++ b/Modules/_localemodule.c @@ -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; } diff --git a/Modules/_lzmamodule.c b/Modules/_lzmamodule.c index 02a32ddccb3ed7..edb2e3673f0b2f 100644 --- a/Modules/_lzmamodule.c +++ b/Modules/_lzmamodule.c @@ -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; + return PyModule_Add(m, name, PyLong_FromLongLong(value)); } static int diff --git a/Modules/_multiprocessing/multiprocessing.c b/Modules/_multiprocessing/multiprocessing.c index 8f9daa5c3de0cc..16b5cb5dd9ec7a 100644 --- a/Modules/_multiprocessing/multiprocessing.c +++ b/Modules/_multiprocessing/multiprocessing.c @@ -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; } diff --git a/Modules/_ssl.c b/Modules/_ssl.c index 699431c248a071..b61d10d9f59fde 100644 --- a/Modules/_ssl.c +++ b/Modules/_ssl.c @@ -5986,7 +5986,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); diff --git a/Modules/_testcapi/heaptype.c b/Modules/_testcapi/heaptype.c index c124871e433431..2da06f447afaa7 100644 --- a/Modules/_testcapi/heaptype.c +++ b/Modules/_testcapi/heaptype.c @@ -1122,94 +1122,62 @@ _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) { @@ -1217,32 +1185,20 @@ _PyTestCapi_Init_Heaptype(PyObject *m) { } 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); diff --git a/Modules/_testmultiphase.c b/Modules/_testmultiphase.c index ca71b6156b005d..fdef06168bfc86 100644 --- a/Modules/_testmultiphase.c +++ b/Modules/_testmultiphase.c @@ -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; } @@ -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; } diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 76af803bd6eefb..1605606e8a470e 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -3197,7 +3197,7 @@ static struct PyModuleDef _tkintermodule = { PyMODINIT_FUNC PyInit__tkinter(void) { - PyObject *m, *uexe, *cexe, *o; + PyObject *m, *uexe, *cexe; tcl_lock = PyThread_allocate_lock(); if (tcl_lock == NULL) @@ -3207,17 +3207,11 @@ PyInit__tkinter(void) if (m == NULL) return NULL; - o = PyErr_NewException("_tkinter.TclError", NULL, NULL); - if (o == NULL) { + Tkinter_TclError = PyErr_NewException("_tkinter.TclError", NULL, NULL); + if (PyModule_AddObjectRef(m, "TclError", Tkinter_TclError)) { Py_DECREF(m); return NULL; } - if (PyModule_AddObject(m, "TclError", Py_NewRef(o))) { - Py_DECREF(o); - Py_DECREF(m); - return NULL; - } - Tkinter_TclError = o; if (PyModule_AddIntConstant(m, "READABLE", TCL_READABLE)) { Py_DECREF(m); @@ -3264,41 +3258,23 @@ PyInit__tkinter(void) return NULL; } - o = PyType_FromSpec(&Tkapp_Type_spec); - if (o == NULL) { - Py_DECREF(m); - return NULL; - } - if (PyModule_AddObject(m, "TkappType", o)) { - Py_DECREF(o); + Tkapp_Type = PyType_FromSpec(&Tkapp_Type_spec); + if (PyModule_AddObjectRef(m, "TkappType", Tkapp_Type)) { Py_DECREF(m); return NULL; } - Tkapp_Type = o; - o = PyType_FromSpec(&Tktt_Type_spec); - if (o == NULL) { + Tktt_Type = PyType_FromSpec(&Tktt_Type_spec); + if (PyModule_AddObjectRef(m, "TkttType", Tktt_Type)) { Py_DECREF(m); return NULL; } - if (PyModule_AddObject(m, "TkttType", o)) { - Py_DECREF(o); - Py_DECREF(m); - return NULL; - } - Tktt_Type = o; - o = PyType_FromSpec(&PyTclObject_Type_spec); - if (o == NULL) { - Py_DECREF(m); - return NULL; - } - if (PyModule_AddObject(m, "Tcl_Obj", o)) { - Py_DECREF(o); + PyTclObject_Type = PyType_FromSpec(&PyTclObject_Type_spec); + if (PyModule_AddObjectRef(m, "Tcl_Obj", PyTclObject_Type)) { Py_DECREF(m); return NULL; } - PyTclObject_Type = o; /* This helps the dynamic loader; in Unicode aware Tcl versions diff --git a/Modules/_weakref.c b/Modules/_weakref.c index dcb2984e3e8177..4e2862e7467c3d 100644 --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -144,27 +144,19 @@ weakref_functions[] = { static int weakref_exec(PyObject *module) { - Py_INCREF(&_PyWeakref_RefType); - if (PyModule_AddObject(module, "ref", (PyObject *) &_PyWeakref_RefType) < 0) { - Py_DECREF(&_PyWeakref_RefType); + if (PyModule_AddObjectRef(module, "ref", (PyObject *) &_PyWeakref_RefType) < 0) { return -1; } - Py_INCREF(&_PyWeakref_RefType); - if (PyModule_AddObject(module, "ReferenceType", + if (PyModule_AddObjectRef(module, "ReferenceType", (PyObject *) &_PyWeakref_RefType) < 0) { - Py_DECREF(&_PyWeakref_RefType); return -1; } - Py_INCREF(&_PyWeakref_ProxyType); - if (PyModule_AddObject(module, "ProxyType", + if (PyModule_AddObjectRef(module, "ProxyType", (PyObject *) &_PyWeakref_ProxyType) < 0) { - Py_DECREF(&_PyWeakref_ProxyType); return -1; } - Py_INCREF(&_PyWeakref_CallableProxyType); - if (PyModule_AddObject(module, "CallableProxyType", + if (PyModule_AddObjectRef(module, "CallableProxyType", (PyObject *) &_PyWeakref_CallableProxyType) < 0) { - Py_DECREF(&_PyWeakref_CallableProxyType); return -1; } diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 0000a8d637eb56..80a95428ebfd07 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -3163,9 +3163,8 @@ array_modexec(PyObject *m) CREATE_TYPE(m, state->ArrayIterType, &arrayiter_spec); Py_SET_TYPE(state->ArrayIterType, &PyType_Type); - if (PyModule_AddObject(m, "ArrayType", - Py_NewRef((PyObject *)state->ArrayType)) < 0) { - Py_DECREF((PyObject *)state->ArrayType); + if (PyModule_AddObjectRef(m, "ArrayType", + (PyObject *)state->ArrayType) < 0) { return -1; } @@ -3193,8 +3192,7 @@ array_modexec(PyObject *m) *p++ = (char)descr->typecode; } typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL); - if (PyModule_AddObject(m, "typecodes", typecodes) < 0) { - Py_XDECREF(typecodes); + if (PyModule_Add(m, "typecodes", typecodes) < 0) { return -1; } diff --git a/Modules/binascii.c b/Modules/binascii.c index cf9328795c2bcc..a87a2ef2e89927 100644 --- a/Modules/binascii.c +++ b/Modules/binascii.c @@ -1253,32 +1253,20 @@ static struct PyMethodDef binascii_module_methods[] = { PyDoc_STRVAR(doc_binascii, "Conversion between binary data and ASCII"); static int -binascii_exec(PyObject *module) { - int result; +binascii_exec(PyObject *module) +{ binascii_state *state = PyModule_GetState(module); if (state == NULL) { return -1; } state->Error = PyErr_NewException("binascii.Error", PyExc_ValueError, NULL); - if (state->Error == NULL) { - return -1; - } - Py_INCREF(state->Error); - result = PyModule_AddObject(module, "Error", state->Error); - if (result == -1) { - Py_DECREF(state->Error); + if (PyModule_AddObjectRef(module, "Error", state->Error) < 0) { return -1; } state->Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL); - if (state->Incomplete == NULL) { - return -1; - } - Py_INCREF(state->Incomplete); - result = PyModule_AddObject(module, "Incomplete", state->Incomplete); - if (result == -1) { - Py_DECREF(state->Incomplete); + if (PyModule_AddObjectRef(module, "Incomplete", state->Incomplete) < 0) { return -1; } diff --git a/Modules/cjkcodecs/cjkcodecs.h b/Modules/cjkcodecs/cjkcodecs.h index ee588785e7403f..766f82983025e4 100644 --- a/Modules/cjkcodecs/cjkcodecs.h +++ b/Modules/cjkcodecs/cjkcodecs.h @@ -398,11 +398,7 @@ register_maps(PyObject *module) strcpy(mhname + sizeof("__map_") - 1, h->charset); PyObject *capsule = PyCapsule_New((void *)h, MAP_CAPSULE, NULL); - if (capsule == NULL) { - return -1; - } - if (PyModule_AddObject(module, mhname, capsule) < 0) { - Py_DECREF(capsule); + if (PyModule_Add(module, mhname, capsule) < 0) { return -1; } } diff --git a/Modules/md5module.c b/Modules/md5module.c index 2122f8b18baf6e..5463effb507de6 100644 --- a/Modules/md5module.c +++ b/Modules/md5module.c @@ -356,13 +356,7 @@ md5_exec(PyObject *m) st->md5_type = (PyTypeObject *)PyType_FromModuleAndSpec( m, &md5_type_spec, NULL); - if (st->md5_type == NULL) { - return -1; - } - - Py_INCREF((PyObject *)st->md5_type); - if (PyModule_AddObject(m, "MD5Type", (PyObject *)st->md5_type) < 0) { - Py_DECREF(st->md5_type); + if (PyModule_AddObjectRef(m, "MD5Type", (PyObject *)st->md5_type) < 0) { return -1; } diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index c1cd5b0efaa3d2..cfbd2f41130ac9 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1579,9 +1579,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) static int mmap_exec(PyObject *module) { - Py_INCREF(PyExc_OSError); - if (PyModule_AddObject(module, "error", PyExc_OSError) < 0) { - Py_DECREF(PyExc_OSError); + if (PyModule_AddObjectRef(module, "error", PyExc_OSError) < 0) { return -1; } diff --git a/Modules/overlapped.c b/Modules/overlapped.c index 18899509c87712..7d3976faef96b2 100644 --- a/Modules/overlapped.c +++ b/Modules/overlapped.c @@ -1996,12 +1996,7 @@ static PyMethodDef overlapped_functions[] = { #define WINAPI_CONSTANT(fmt, con) \ do { \ - PyObject *value = Py_BuildValue(fmt, con); \ - if (value == NULL) { \ - return -1; \ - } \ - if (PyModule_AddObject(module, #con, value) < 0 ) { \ - Py_DECREF(value); \ + if (PyModule_Add(module, #con, Py_BuildValue(fmt, con)) < 0 ) { \ return -1; \ } \ } while (0) diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index 33a56c57a876ec..d8395e9a9aa80d 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -1656,8 +1656,7 @@ add_submodule(PyObject *mod, const char *fullname) Py_DECREF(mod_name); /* gives away the reference to the submodule */ - if (PyModule_AddObject(mod, name, submodule) < 0) { - Py_DECREF(submodule); + if (PyModule_Add(mod, name, submodule) < 0) { return NULL; } @@ -1887,10 +1886,7 @@ add_features(PyObject *mod) goto error; } } - if (PyModule_AddObject(mod, "features", list) < 0) { - goto error; - } - return 0; + return PyModule_Add(mod, "features", list); error: Py_DECREF(list); @@ -1959,8 +1955,7 @@ pyexpat_exec(PyObject *mod) info.major, info.minor, info.micro); - if (PyModule_AddObject(mod, "version_info", versionInfo) < 0) { - Py_DECREF(versionInfo); + if (PyModule_Add(mod, "version_info", versionInfo) < 0) { return -1; } } @@ -2040,8 +2035,7 @@ pyexpat_exec(PyObject *mod) return -1; } - if (PyModule_AddObject(mod, "expat_CAPI", capi_object) < 0) { - Py_DECREF(capi_object); + if (PyModule_Add(mod, "expat_CAPI", capi_object) < 0) { return -1; } diff --git a/Modules/resource.c b/Modules/resource.c index 3c89468c48c56e..4614f5e98cc888 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -372,9 +372,7 @@ resource_exec(PyObject *module) } while (0) /* Add some symbolic constants to the module */ - Py_INCREF(PyExc_OSError); - if (PyModule_AddObject(module, "error", PyExc_OSError) < 0) { - Py_DECREF(PyExc_OSError); + if (PyModule_AddObjectRef(module, "error", PyExc_OSError) < 0) { return -1; } @@ -502,12 +500,7 @@ resource_exec(PyObject *module) { v = PyLong_FromLong((long) RLIM_INFINITY); } - if (!v) { - return -1; - } - - if (PyModule_AddObject(module, "RLIM_INFINITY", v) < 0) { - Py_DECREF(v); + if (PyModule_Add(module, "RLIM_INFINITY", v) < 0) { return -1; } return 0; diff --git a/Modules/sha1module.c b/Modules/sha1module.c index ef8e067dd337b3..3fd53123229ac4 100644 --- a/Modules/sha1module.c +++ b/Modules/sha1module.c @@ -357,16 +357,9 @@ _sha1_exec(PyObject *module) st->sha1_type = (PyTypeObject *)PyType_FromModuleAndSpec( module, &sha1_type_spec, NULL); - - if (st->sha1_type == NULL) { - return -1; - } - - Py_INCREF(st->sha1_type); - if (PyModule_AddObject(module, + if (PyModule_AddObjectRef(module, "SHA1Type", (PyObject *)st->sha1_type) < 0) { - Py_DECREF(st->sha1_type); return -1; } diff --git a/Modules/termios.c b/Modules/termios.c index 6dc8200572bc0c..6b254104ed1e21 100644 --- a/Modules/termios.c +++ b/Modules/termios.c @@ -1232,12 +1232,7 @@ termios_exec(PyObject *mod) struct constant *constant = termios_constants; termiosmodulestate *state = get_termios_state(mod); state->TermiosError = PyErr_NewException("termios.error", NULL, NULL); - if (state->TermiosError == NULL) { - return -1; - } - Py_INCREF(state->TermiosError); - if (PyModule_AddObject(mod, "error", state->TermiosError) < 0) { - Py_DECREF(state->TermiosError); + if (PyModule_AddObjectRef(mod, "error", state->TermiosError) < 0) { return -1; } diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c index 966123f4624c08..6267c5ae29ab38 100644 --- a/Modules/unicodedata.c +++ b/Modules/unicodedata.c @@ -1487,11 +1487,7 @@ unicodedata_exec(PyObject *module) v = new_previous_version(ucd_type, "3.2.0", get_change_3_2_0, normalization_3_2_0); Py_DECREF(ucd_type); - if (v == NULL) { - return -1; - } - if (PyModule_AddObject(module, "ucd_3_2_0", v) < 0) { - Py_DECREF(v); + if (PyModule_Add(module, "ucd_3_2_0", v) < 0) { return -1; } diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c index c0f6b96f51baba..9970bc9e5ad97c 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -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; }