gh-90763: Modernise xx template module initialisation (#93078)

Use C APIs such as PyModule_AddType instead of PyModule_AddObject.
Also remove incorrect module decrefs if module fails to initialise.
This commit is contained in:
Erlend Egeberg Aasland 2022-06-10 12:39:02 +02:00 committed by GitHub
parent d8395eb38d
commit a87c9b538f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 41 deletions

View file

@ -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 Str and Null types */
if (PyModule_AddType(m, &Str_Type) < 0) {
return -1;
}
if (PyModule_AddType(m, &Null_Type) < 0) {
return -1;
}
/* Add Null */
if (PyType_Ready(&Null_Type) < 0)
goto fail;
PyModule_AddObject(m, "Null", (PyObject *)&Null_Type);
return 0;
fail:
Py_XDECREF(m);
return -1;
}
static struct PyModuleDef_Slot xx_slots[] = {