diff options
author | Erlend Egeberg Aasland <erlend.aasland@protonmail.com> | 2022-06-10 11:28:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-10 11:28:48 (GMT) |
commit | 175ed6e52c7bf6d9805ac8b56ad3f38e46ff7de5 (patch) | |
tree | 5c15d5d342bd7cda0ebdfcf346141cb130fb10d1 /Modules/xxmodule.c | |
parent | 927b5afee73218500a8fa80df86216cfdc24ef5a (diff) | |
download | cpython-175ed6e52c7bf6d9805ac8b56ad3f38e46ff7de5.zip cpython-175ed6e52c7bf6d9805ac8b56ad3f38e46ff7de5.tar.gz cpython-175ed6e52c7bf6d9805ac8b56ad3f38e46ff7de5.tar.bz2 |
[3.11] gh-90763: Modernise xx template module initialisation (GH-93078) (#93681)
Use C APIs such as PyModule_AddType instead of PyModule_AddObject.
Also remove incorrect module decrefs if module fails to initialise.
(cherry picked from commit a87c9b538fbfc42883417c4d5e69f1a5922690e3)
Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@protonmail.com>
Diffstat (limited to 'Modules/xxmodule.c')
-rw-r--r-- | Modules/xxmodule.c | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/Modules/xxmodule.c b/Modules/xxmodule.c index edcd621..a6e5071 100644 --- a/Modules/xxmodule.c +++ b/Modules/xxmodule.c @@ -358,31 +358,32 @@ xx_exec(PyObject *m) /* Finalize the type object including setting type of the new type * object; doing it here is required for portability, too. */ - if (PyType_Ready(&Xxo_Type) < 0) - goto fail; + if (PyType_Ready(&Xxo_Type) < 0) { + return -1; + } /* Add some symbolic constants to the module */ if (ErrorObject == NULL) { ErrorObject = PyErr_NewException("xx.error", NULL, NULL); - if (ErrorObject == NULL) - goto fail; + if (ErrorObject == NULL) { + return -1; + } + } + int rc = PyModule_AddType(m, (PyTypeObject *)ErrorObject); + Py_DECREF(ErrorObject); + if (rc < 0) { + return -1; } - Py_INCREF(ErrorObject); - PyModule_AddObject(m, "error", ErrorObject); - - /* Add Str */ - if (PyType_Ready(&Str_Type) < 0) - goto fail; - PyModule_AddObject(m, "Str", (PyObject *)&Str_Type); - - /* Add Null */ - if (PyType_Ready(&Null_Type) < 0) - goto fail; - PyModule_AddObject(m, "Null", (PyObject *)&Null_Type); + + /* Add Str and Null types */ + if (PyModule_AddType(m, &Str_Type) < 0) { + return -1; + } + if (PyModule_AddType(m, &Null_Type) < 0) { + return -1; + } + return 0; - fail: - Py_XDECREF(m); - return -1; } static struct PyModuleDef_Slot xx_slots[] = { |