]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-1635741: Port _contextvars module to multiphase initialization (PEP 489) (GH...
authorHai Shi <shihai1992@gmail.com>
Mon, 17 Feb 2020 13:49:26 +0000 (21:49 +0800)
committerGitHub <noreply@github.com>
Mon, 17 Feb 2020 13:49:26 +0000 (14:49 +0100)
Misc/NEWS.d/next/Core and Builtins/2020-02-06-09-00-35.bpo-1635741.oaxe1j.rst [new file with mode: 0644]
Modules/_contextvarsmodule.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-02-06-09-00-35.bpo-1635741.oaxe1j.rst b/Misc/NEWS.d/next/Core and Builtins/2020-02-06-09-00-35.bpo-1635741.oaxe1j.rst
new file mode 100644 (file)
index 0000000..49336f0
--- /dev/null
@@ -0,0 +1 @@
+Port _contextvars extension module to multiphase initialization (:pep:`489`).
\ No newline at end of file
index 1abcdbfa921c272bab371f1818dc7fd6ca1779c4..d6d7f375d1230782b36c2f344ac8c704ff56a94f 100644 (file)
@@ -27,33 +27,15 @@ static PyMethodDef _contextvars_methods[] = {
     {NULL, NULL}
 };
 
-static struct PyModuleDef _contextvarsmodule = {
-    PyModuleDef_HEAD_INIT,      /* m_base */
-    "_contextvars",             /* m_name */
-    module_doc,                 /* m_doc */
-    -1,                         /* m_size */
-    _contextvars_methods,       /* m_methods */
-    NULL,                       /* m_slots */
-    NULL,                       /* m_traverse */
-    NULL,                       /* m_clear */
-    NULL,                       /* m_free */
-};
-
-PyMODINIT_FUNC
-PyInit__contextvars(void)
+static int
+_contextvars_exec(PyObject *m)
 {
-    PyObject *m = PyModule_Create(&_contextvarsmodule);
-    if (m == NULL) {
-        return NULL;
-    }
-
     Py_INCREF(&PyContext_Type);
     if (PyModule_AddObject(m, "Context",
                            (PyObject *)&PyContext_Type) < 0)
     {
         Py_DECREF(&PyContext_Type);
-        Py_DECREF(m);
-        return NULL;
+        return -1;
     }
 
     Py_INCREF(&PyContextVar_Type);
@@ -61,8 +43,7 @@ PyInit__contextvars(void)
                            (PyObject *)&PyContextVar_Type) < 0)
     {
         Py_DECREF(&PyContextVar_Type);
-        Py_DECREF(m);
-        return NULL;
+        return -1;
     }
 
     Py_INCREF(&PyContextToken_Type);
@@ -70,9 +51,31 @@ PyInit__contextvars(void)
                            (PyObject *)&PyContextToken_Type) < 0)
     {
         Py_DECREF(&PyContextToken_Type);
-        Py_DECREF(m);
-        return NULL;
+        return -1;
     }
 
-    return m;
+    return 0;
+}
+
+static struct PyModuleDef_Slot _contextvars_slots[] = {
+    {Py_mod_exec, _contextvars_exec},
+    {0, NULL}
+};
+
+static struct PyModuleDef _contextvarsmodule = {
+    PyModuleDef_HEAD_INIT,      /* m_base */
+    "_contextvars",             /* m_name */
+    module_doc,                 /* m_doc */
+    0,                          /* m_size */
+    _contextvars_methods,       /* m_methods */
+    _contextvars_slots,         /* m_slots */
+    NULL,                       /* m_traverse */
+    NULL,                       /* m_clear */
+    NULL,                       /* m_free */
+};
+
+PyMODINIT_FUNC
+PyInit__contextvars(void)
+{
+    return PyModuleDef_Init(&_contextvarsmodule);
 }