mirror of
https://github.com/python/cpython.git
synced 2025-11-26 13:22:51 +00:00
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:
parent
d8395eb38d
commit
a87c9b538f
2 changed files with 52 additions and 41 deletions
|
|
@ -124,7 +124,7 @@ static PyType_Slot Xxo_Type_slots[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
static PyType_Spec Xxo_Type_spec = {
|
static PyType_Spec Xxo_Type_spec = {
|
||||||
"xxlimited.Xxo",
|
"xxlimited_35.Xxo",
|
||||||
sizeof(XxoObject),
|
sizeof(XxoObject),
|
||||||
0,
|
0,
|
||||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
|
||||||
|
|
@ -189,7 +189,7 @@ static PyType_Slot Str_Type_slots[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
static PyType_Spec Str_Type_spec = {
|
static PyType_Spec Str_Type_spec = {
|
||||||
"xxlimited.Str",
|
"xxlimited_35.Str",
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
||||||
|
|
@ -212,7 +212,7 @@ static PyType_Slot Null_Type_slots[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
static PyType_Spec Null_Type_spec = {
|
static PyType_Spec Null_Type_spec = {
|
||||||
"xxlimited.Null",
|
"xxlimited_35.Null",
|
||||||
0, /* basicsize */
|
0, /* basicsize */
|
||||||
0, /* itemsize */
|
0, /* itemsize */
|
||||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
||||||
|
|
@ -248,40 +248,50 @@ xx_modexec(PyObject *m)
|
||||||
Null_Type_slots[1].pfunc = PyType_GenericNew;
|
Null_Type_slots[1].pfunc = PyType_GenericNew;
|
||||||
Str_Type_slots[0].pfunc = &PyUnicode_Type;
|
Str_Type_slots[0].pfunc = &PyUnicode_Type;
|
||||||
|
|
||||||
Xxo_Type = PyType_FromSpec(&Xxo_Type_spec);
|
|
||||||
if (Xxo_Type == NULL)
|
|
||||||
goto fail;
|
|
||||||
|
|
||||||
/* Add some symbolic constants to the module */
|
/* Add some symbolic constants to the module */
|
||||||
if (ErrorObject == NULL) {
|
if (ErrorObject == NULL) {
|
||||||
ErrorObject = PyErr_NewException("xxlimited.error", NULL, NULL);
|
ErrorObject = PyErr_NewException("xxlimited_35.error", NULL, NULL);
|
||||||
if (ErrorObject == NULL)
|
if (ErrorObject == NULL) {
|
||||||
goto fail;
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Py_INCREF(ErrorObject);
|
Py_INCREF(ErrorObject);
|
||||||
PyModule_AddObject(m, "error", ErrorObject);
|
if (PyModule_AddObject(m, "error", ErrorObject) < 0) {
|
||||||
|
Py_DECREF(ErrorObject);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* Add Xxo */
|
/* Add Xxo */
|
||||||
o = PyType_FromSpec(&Xxo_Type_spec);
|
Xxo_Type = PyType_FromSpec(&Xxo_Type_spec);
|
||||||
if (o == NULL)
|
if (Xxo_Type == NULL) {
|
||||||
goto fail;
|
return -1;
|
||||||
PyModule_AddObject(m, "Xxo", o);
|
}
|
||||||
|
if (PyModule_AddObject(m, "Xxo", Xxo_Type) < 0) {
|
||||||
|
Py_DECREF(Xxo_Type);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* Add Str */
|
/* Add Str */
|
||||||
o = PyType_FromSpec(&Str_Type_spec);
|
o = PyType_FromSpec(&Str_Type_spec);
|
||||||
if (o == NULL)
|
if (o == NULL) {
|
||||||
goto fail;
|
return -1;
|
||||||
PyModule_AddObject(m, "Str", o);
|
}
|
||||||
|
if (PyModule_AddObject(m, "Str", o) < 0) {
|
||||||
|
Py_DECREF(o);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* Add Null */
|
/* Add Null */
|
||||||
o = PyType_FromSpec(&Null_Type_spec);
|
o = PyType_FromSpec(&Null_Type_spec);
|
||||||
if (o == NULL)
|
if (o == NULL) {
|
||||||
goto fail;
|
return -1;
|
||||||
PyModule_AddObject(m, "Null", o);
|
}
|
||||||
|
if (PyModule_AddObject(m, "Null", o) < 0) {
|
||||||
|
Py_DECREF(o);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
fail:
|
|
||||||
Py_XDECREF(m);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -358,31 +358,32 @@ xx_exec(PyObject *m)
|
||||||
|
|
||||||
/* Finalize the type object including setting type of the new type
|
/* Finalize the type object including setting type of the new type
|
||||||
* object; doing it here is required for portability, too. */
|
* object; doing it here is required for portability, too. */
|
||||||
if (PyType_Ready(&Xxo_Type) < 0)
|
if (PyType_Ready(&Xxo_Type) < 0) {
|
||||||
goto fail;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* Add some symbolic constants to the module */
|
/* Add some symbolic constants to the module */
|
||||||
if (ErrorObject == NULL) {
|
if (ErrorObject == NULL) {
|
||||||
ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
|
ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
|
||||||
if (ErrorObject == NULL)
|
if (ErrorObject == NULL) {
|
||||||
goto fail;
|
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 */
|
/* Add Str and Null types */
|
||||||
if (PyType_Ready(&Str_Type) < 0)
|
if (PyModule_AddType(m, &Str_Type) < 0) {
|
||||||
goto fail;
|
return -1;
|
||||||
PyModule_AddObject(m, "Str", (PyObject *)&Str_Type);
|
}
|
||||||
|
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;
|
return 0;
|
||||||
fail:
|
|
||||||
Py_XDECREF(m);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct PyModuleDef_Slot xx_slots[] = {
|
static struct PyModuleDef_Slot xx_slots[] = {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue