Skip to content

Commit

Permalink
bpo-1635741: Port _json extension module to multiphase initialization…
Browse files Browse the repository at this point in the history
… (PEP 489) (GH-17835)
  • Loading branch information
shihai1991 authored and vstinner committed Jan 15, 2020
1 parent 3f12ac1 commit ed154c3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Port _json extension module to multiphase initialization (:pep:`489`).
53 changes: 30 additions & 23 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

0 comments on commit ed154c3

Please sign in to comment.