bpo-1635741: Port _json extension module to multiphase initialization (PEP 489) (GH-17835)

This commit is contained in:
Hai Shi 2020-01-16 00:32:51 +08:00 committed by Victor Stinner
parent 3f12ac18a4
commit ed154c387e
2 changed files with 31 additions and 23 deletions

View File

@ -0,0 +1 @@
Port _json extension module to multiphase initialization (:pep:`489`).

View File

@ -1863,13 +1863,40 @@ static PyMethodDef speedups_methods[] = {
PyDoc_STRVAR(module_doc,
"json speedups\n");
static int
_json_exec(PyObject *module)
{
if (PyType_Ready(&PyScannerType) < 0) {
return -1;
}
if (PyType_Ready(&PyEncoderType) < 0) {
return -1;
}
Py_INCREF((PyObject*)&PyScannerType);
if (PyModule_AddObject(module, "make_scanner", (PyObject*)&PyScannerType) < 0) {
Py_DECREF((PyObject*)&PyScannerType);
return -1;
}
Py_INCREF((PyObject*)&PyEncoderType);
if (PyModule_AddObject(module, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
Py_DECREF((PyObject*)&PyEncoderType);
return -1;
}
return 0;
}
static PyModuleDef_Slot _json_slots[] = {
{Py_mod_exec, _json_exec},
{0, NULL}
};
static struct PyModuleDef jsonmodule = {
PyModuleDef_HEAD_INIT,
"_json",
module_doc,
-1,
0,
speedups_methods,
NULL,
_json_slots,
NULL,
NULL,
NULL
@ -1878,25 +1905,5 @@ static struct PyModuleDef jsonmodule = {
PyMODINIT_FUNC
PyInit__json(void)
{
PyObject *m = PyModule_Create(&jsonmodule);
if (!m)
return NULL;
if (PyType_Ready(&PyScannerType) < 0)
goto fail;
if (PyType_Ready(&PyEncoderType) < 0)
goto fail;
Py_INCREF((PyObject*)&PyScannerType);
if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
Py_DECREF((PyObject*)&PyScannerType);
goto fail;
}
Py_INCREF((PyObject*)&PyEncoderType);
if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
Py_DECREF((PyObject*)&PyEncoderType);
goto fail;
}
return m;
fail:
Py_DECREF(m);
return NULL;
return PyModuleDef_Init(&jsonmodule);
}