Skip to content

Commit 66c0613

Browse files
pythongh-90763: Modernise xx template module initialisation
Use C APIs such as PyModule_AddType instead of PyModule_AddObject. Also remove incorrect module decrefs if module fails to initialise.
1 parent e5d8dbd commit 66c0613

File tree

2 files changed

+60
-53
lines changed

2 files changed

+60
-53
lines changed

Modules/xxlimited_35.c

+40-34
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static PyType_Slot Xxo_Type_slots[] = {
124124
};
125125

126126
static PyType_Spec Xxo_Type_spec = {
127-
"xxlimited.Xxo",
127+
"xxlimited_35.Xxo",
128128
sizeof(XxoObject),
129129
0,
130130
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
@@ -189,7 +189,7 @@ static PyType_Slot Str_Type_slots[] = {
189189
};
190190

191191
static PyType_Spec Str_Type_spec = {
192-
"xxlimited.Str",
192+
"xxlimited_35.Str",
193193
0,
194194
0,
195195
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
@@ -212,7 +212,7 @@ static PyType_Slot Null_Type_slots[] = {
212212
};
213213

214214
static PyType_Spec Null_Type_spec = {
215-
"xxlimited.Null",
215+
"xxlimited_35.Null",
216216
0, /* basicsize */
217217
0, /* itemsize */
218218
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
@@ -236,52 +236,58 @@ static PyMethodDef xx_methods[] = {
236236
PyDoc_STRVAR(module_doc,
237237
"This is a module for testing limited API from Python 3.5.");
238238

239+
static PyObject *
240+
create_and_add_type(PyObject *module, const char *name, PyType_Spec *spec)
241+
{
242+
PyObject *type = PyType_FromSpec(spec);
243+
if (type == NULL) {
244+
return NULL;
245+
}
246+
int rc = PyModule_AddObject(module, name, type);
247+
if (rc < 0) {
248+
Py_DECREF(type);
249+
return NULL;
250+
}
251+
return type;
252+
}
253+
239254
static int
240255
xx_modexec(PyObject *m)
241256
{
242-
PyObject *o;
243-
244257
/* Due to cross platform compiler issues the slots must be filled
245258
* here. It's required for portability to Windows without requiring
246259
* C++. */
247260
Null_Type_slots[0].pfunc = &PyBaseObject_Type;
248261
Null_Type_slots[1].pfunc = PyType_GenericNew;
249262
Str_Type_slots[0].pfunc = &PyUnicode_Type;
250263

251-
Xxo_Type = PyType_FromSpec(&Xxo_Type_spec);
252-
if (Xxo_Type == NULL)
253-
goto fail;
254-
255264
/* Add some symbolic constants to the module */
256265
if (ErrorObject == NULL) {
257-
ErrorObject = PyErr_NewException("xxlimited.error", NULL, NULL);
258-
if (ErrorObject == NULL)
259-
goto fail;
266+
ErrorObject = PyErr_NewException("xxlimited_35.error", NULL, NULL);
267+
if (ErrorObject == NULL) {
268+
return -1;
269+
}
260270
}
261271
Py_INCREF(ErrorObject);
262-
PyModule_AddObject(m, "error", ErrorObject);
263-
264-
/* Add Xxo */
265-
o = PyType_FromSpec(&Xxo_Type_spec);
266-
if (o == NULL)
267-
goto fail;
268-
PyModule_AddObject(m, "Xxo", o);
269-
270-
/* Add Str */
271-
o = PyType_FromSpec(&Str_Type_spec);
272-
if (o == NULL)
273-
goto fail;
274-
PyModule_AddObject(m, "Str", o);
275-
276-
/* Add Null */
277-
o = PyType_FromSpec(&Null_Type_spec);
278-
if (o == NULL)
279-
goto fail;
280-
PyModule_AddObject(m, "Null", o);
272+
int rc = PyModule_AddObject(m, "error", ErrorObject);
273+
if (rc < 0) {
274+
Py_DECREF(ErrorObject);
275+
return -1;
276+
}
277+
278+
/* Add Xxo, Str, and Null types */
279+
Xxo_Type = create_and_add_type(m, "Xxo", &Xxo_Type_spec);
280+
if (Xxo_Type == NULL) {
281+
return -1;
282+
}
283+
if (create_and_add_type(m, "Str", &Str_Type_spec) == NULL) {
284+
return -1;
285+
}
286+
if (create_and_add_type(m, "Null", &Null_Type_spec) == NULL) {
287+
return -1;
288+
}
289+
281290
return 0;
282-
fail:
283-
Py_XDECREF(m);
284-
return -1;
285291
}
286292

287293

Modules/xxmodule.c

+20-19
Original file line numberDiff line numberDiff line change
@@ -358,31 +358,32 @@ xx_exec(PyObject *m)
358358

359359
/* Finalize the type object including setting type of the new type
360360
* object; doing it here is required for portability, too. */
361-
if (PyType_Ready(&Xxo_Type) < 0)
362-
goto fail;
361+
if (PyType_Ready(&Xxo_Type) < 0) {
362+
return -1;
363+
}
363364

364365
/* Add some symbolic constants to the module */
365366
if (ErrorObject == NULL) {
366367
ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
367-
if (ErrorObject == NULL)
368-
goto fail;
368+
if (ErrorObject == NULL) {
369+
return -1;
370+
}
371+
}
372+
int rc = PyModule_AddType(m, (PyTypeObject *)ErrorObject);
373+
Py_DECREF(ErrorObject);
374+
if (rc < 0) {
375+
return -1;
369376
}
370-
Py_INCREF(ErrorObject);
371-
PyModule_AddObject(m, "error", ErrorObject);
372-
373-
/* Add Str */
374-
if (PyType_Ready(&Str_Type) < 0)
375-
goto fail;
376-
PyModule_AddObject(m, "Str", (PyObject *)&Str_Type);
377-
378-
/* Add Null */
379-
if (PyType_Ready(&Null_Type) < 0)
380-
goto fail;
381-
PyModule_AddObject(m, "Null", (PyObject *)&Null_Type);
377+
378+
/* Add Str and Null types */
379+
if (PyModule_AddType(m, &Str_Type) < 0) {
380+
return -1;
381+
}
382+
if (PyModule_AddType(m, &Null_Type) < 0) {
383+
return -1;
384+
}
385+
382386
return 0;
383-
fail:
384-
Py_XDECREF(m);
385-
return -1;
386387
}
387388

388389
static struct PyModuleDef_Slot xx_slots[] = {

0 commit comments

Comments
 (0)