Skip to content

Commit ed154c3

Browse files
shihai1991vstinner
authored andcommitted
bpo-1635741: Port _json extension module to multiphase initialization (PEP 489) (GH-17835)
1 parent 3f12ac1 commit ed154c3

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port _json extension module to multiphase initialization (:pep:`489`).

Modules/_json.c

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,13 +1863,40 @@ static PyMethodDef speedups_methods[] = {
18631863
PyDoc_STRVAR(module_doc,
18641864
"json speedups\n");
18651865

1866+
static int
1867+
_json_exec(PyObject *module)
1868+
{
1869+
if (PyType_Ready(&PyScannerType) < 0) {
1870+
return -1;
1871+
}
1872+
if (PyType_Ready(&PyEncoderType) < 0) {
1873+
return -1;
1874+
}
1875+
Py_INCREF((PyObject*)&PyScannerType);
1876+
if (PyModule_AddObject(module, "make_scanner", (PyObject*)&PyScannerType) < 0) {
1877+
Py_DECREF((PyObject*)&PyScannerType);
1878+
return -1;
1879+
}
1880+
Py_INCREF((PyObject*)&PyEncoderType);
1881+
if (PyModule_AddObject(module, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
1882+
Py_DECREF((PyObject*)&PyEncoderType);
1883+
return -1;
1884+
}
1885+
return 0;
1886+
}
1887+
1888+
static PyModuleDef_Slot _json_slots[] = {
1889+
{Py_mod_exec, _json_exec},
1890+
{0, NULL}
1891+
};
1892+
18661893
static struct PyModuleDef jsonmodule = {
18671894
PyModuleDef_HEAD_INIT,
18681895
"_json",
18691896
module_doc,
1870-
-1,
1897+
0,
18711898
speedups_methods,
1872-
NULL,
1899+
_json_slots,
18731900
NULL,
18741901
NULL,
18751902
NULL
@@ -1878,25 +1905,5 @@ static struct PyModuleDef jsonmodule = {
18781905
PyMODINIT_FUNC
18791906
PyInit__json(void)
18801907
{
1881-
PyObject *m = PyModule_Create(&jsonmodule);
1882-
if (!m)
1883-
return NULL;
1884-
if (PyType_Ready(&PyScannerType) < 0)
1885-
goto fail;
1886-
if (PyType_Ready(&PyEncoderType) < 0)
1887-
goto fail;
1888-
Py_INCREF((PyObject*)&PyScannerType);
1889-
if (PyModule_AddObject(m, "make_scanner", (PyObject*)&PyScannerType) < 0) {
1890-
Py_DECREF((PyObject*)&PyScannerType);
1891-
goto fail;
1892-
}
1893-
Py_INCREF((PyObject*)&PyEncoderType);
1894-
if (PyModule_AddObject(m, "make_encoder", (PyObject*)&PyEncoderType) < 0) {
1895-
Py_DECREF((PyObject*)&PyEncoderType);
1896-
goto fail;
1897-
}
1898-
return m;
1899-
fail:
1900-
Py_DECREF(m);
1901-
return NULL;
1908+
return PyModuleDef_Init(&jsonmodule);
19021909
}

0 commit comments

Comments
 (0)