summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHai Shi <shihai1992@gmail.com>2020-01-15 16:32:51 (GMT)
committerVictor Stinner <vstinner@python.org>2020-01-15 16:32:51 (GMT)
commited154c387efc5f978ec97900ec9e0ec6631d5498 (patch)
tree9abce6142d09d12f03d17c38781ef90a37b5d7f9
parent3f12ac18a407983a23d43ae785e805e773571477 (diff)
downloadcpython-ed154c387efc5f978ec97900ec9e0ec6631d5498.zip
cpython-ed154c387efc5f978ec97900ec9e0ec6631d5498.tar.gz
cpython-ed154c387efc5f978ec97900ec9e0ec6631d5498.tar.bz2
bpo-1635741: Port _json extension module to multiphase initialization (PEP 489) (GH-17835)
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2020-01-05-13-40-08.bpo-1635741.QRTJVC.rst1
-rw-r--r--Modules/_json.c53
2 files changed, 31 insertions, 23 deletions
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-01-05-13-40-08.bpo-1635741.QRTJVC.rst b/Misc/NEWS.d/next/Core and Builtins/2020-01-05-13-40-08.bpo-1635741.QRTJVC.rst
new file mode 100644
index 0000000..9b856c9
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-01-05-13-40-08.bpo-1635741.QRTJVC.rst
@@ -0,0 +1 @@
+Port _json extension module to multiphase initialization (:pep:`489`).
diff --git a/Modules/_json.c b/Modules/_json.c
index 439414f..3e4fe79 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -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);
}