Skip to content

Commit 175ed6e

Browse files
authoredJun 10, 2022
[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 a87c9b5) Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@protonmail.com>
1 parent 927b5af commit 175ed6e

File tree

2 files changed

+54
-43
lines changed

2 files changed

+54
-43
lines changed
 

‎Modules/xxlimited_35.c

+34-24
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,
@@ -248,40 +248,50 @@ xx_modexec(PyObject *m)
248248
Null_Type_slots[1].pfunc = PyType_GenericNew;
249249
Str_Type_slots[0].pfunc = &PyUnicode_Type;
250250

251-
Xxo_Type = PyType_FromSpec(&Xxo_Type_spec);
252-
if (Xxo_Type == NULL)
253-
goto fail;
254-
255251
/* Add some symbolic constants to the module */
256252
if (ErrorObject == NULL) {
257-
ErrorObject = PyErr_NewException("xxlimited.error", NULL, NULL);
258-
if (ErrorObject == NULL)
259-
goto fail;
253+
ErrorObject = PyErr_NewException("xxlimited_35.error", NULL, NULL);
254+
if (ErrorObject == NULL) {
255+
return -1;
256+
}
260257
}
261258
Py_INCREF(ErrorObject);
262-
PyModule_AddObject(m, "error", ErrorObject);
259+
if (PyModule_AddObject(m, "error", ErrorObject) < 0) {
260+
Py_DECREF(ErrorObject);
261+
return -1;
262+
}
263263

264264
/* Add Xxo */
265-
o = PyType_FromSpec(&Xxo_Type_spec);
266-
if (o == NULL)
267-
goto fail;
268-
PyModule_AddObject(m, "Xxo", o);
265+
Xxo_Type = PyType_FromSpec(&Xxo_Type_spec);
266+
if (Xxo_Type == NULL) {
267+
return -1;
268+
}
269+
if (PyModule_AddObject(m, "Xxo", Xxo_Type) < 0) {
270+
Py_DECREF(Xxo_Type);
271+
return -1;
272+
}
269273

270274
/* Add Str */
271275
o = PyType_FromSpec(&Str_Type_spec);
272-
if (o == NULL)
273-
goto fail;
274-
PyModule_AddObject(m, "Str", o);
276+
if (o == NULL) {
277+
return -1;
278+
}
279+
if (PyModule_AddObject(m, "Str", o) < 0) {
280+
Py_DECREF(o);
281+
return -1;
282+
}
275283

276284
/* Add Null */
277285
o = PyType_FromSpec(&Null_Type_spec);
278-
if (o == NULL)
279-
goto fail;
280-
PyModule_AddObject(m, "Null", o);
286+
if (o == NULL) {
287+
return -1;
288+
}
289+
if (PyModule_AddObject(m, "Null", o) < 0) {
290+
Py_DECREF(o);
291+
return -1;
292+
}
293+
281294
return 0;
282-
fail:
283-
Py_XDECREF(m);
284-
return -1;
285295
}
286296

287297

‎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)
Please sign in to comment.