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

Victor Stinner webhook-mailer at python.org
Wed Jan 15 11:32:56 EST 2020


https://github.com/python/cpython/commit/ed154c387efc5f978ec97900ec9e0ec6631d5498
commit: ed154c387efc5f978ec97900ec9e0ec6631d5498
branch: master
author: Hai Shi <shihai1992 at gmail.com>
committer: Victor Stinner <vstinner at python.org>
date: 2020-01-15T17:32:51+01:00
summary:

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

files:
A Misc/NEWS.d/next/Core and Builtins/2020-01-05-13-40-08.bpo-1635741.QRTJVC.rst
M Modules/_json.c

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 0000000000000..9b856c9e1ba24
--- /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 439414fd59e62..3e4fe795a0573 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);
 }



More information about the Python-checkins mailing list