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

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 @@
 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 @@
 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);
 }