Skip to content

Commit 7d79568

Browse files
authored
bpo-1635741: Port _contextvars module to multiphase initialization (PEP 489) (GH-18374)
1 parent 3d235f5 commit 7d79568

File tree

2 files changed

+30
-26
lines changed

2 files changed

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

Modules/_contextvarsmodule.c

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,52 +27,55 @@ static PyMethodDef _contextvars_methods[] = {
2727
{NULL, NULL}
2828
};
2929

30-
static struct PyModuleDef _contextvarsmodule = {
31-
PyModuleDef_HEAD_INIT, /* m_base */
32-
"_contextvars", /* m_name */
33-
module_doc, /* m_doc */
34-
-1, /* m_size */
35-
_contextvars_methods, /* m_methods */
36-
NULL, /* m_slots */
37-
NULL, /* m_traverse */
38-
NULL, /* m_clear */
39-
NULL, /* m_free */
40-
};
41-
42-
PyMODINIT_FUNC
43-
PyInit__contextvars(void)
30+
static int
31+
_contextvars_exec(PyObject *m)
4432
{
45-
PyObject *m = PyModule_Create(&_contextvarsmodule);
46-
if (m == NULL) {
47-
return NULL;
48-
}
49-
5033
Py_INCREF(&PyContext_Type);
5134
if (PyModule_AddObject(m, "Context",
5235
(PyObject *)&PyContext_Type) < 0)
5336
{
5437
Py_DECREF(&PyContext_Type);
55-
Py_DECREF(m);
56-
return NULL;
38+
return -1;
5739
}
5840

5941
Py_INCREF(&PyContextVar_Type);
6042
if (PyModule_AddObject(m, "ContextVar",
6143
(PyObject *)&PyContextVar_Type) < 0)
6244
{
6345
Py_DECREF(&PyContextVar_Type);
64-
Py_DECREF(m);
65-
return NULL;
46+
return -1;
6647
}
6748

6849
Py_INCREF(&PyContextToken_Type);
6950
if (PyModule_AddObject(m, "Token",
7051
(PyObject *)&PyContextToken_Type) < 0)
7152
{
7253
Py_DECREF(&PyContextToken_Type);
73-
Py_DECREF(m);
74-
return NULL;
54+
return -1;
7555
}
7656

77-
return m;
57+
return 0;
58+
}
59+
60+
static struct PyModuleDef_Slot _contextvars_slots[] = {
61+
{Py_mod_exec, _contextvars_exec},
62+
{0, NULL}
63+
};
64+
65+
static struct PyModuleDef _contextvarsmodule = {
66+
PyModuleDef_HEAD_INIT, /* m_base */
67+
"_contextvars", /* m_name */
68+
module_doc, /* m_doc */
69+
0, /* m_size */
70+
_contextvars_methods, /* m_methods */
71+
_contextvars_slots, /* m_slots */
72+
NULL, /* m_traverse */
73+
NULL, /* m_clear */
74+
NULL, /* m_free */
75+
};
76+
77+
PyMODINIT_FUNC
78+
PyInit__contextvars(void)
79+
{
80+
return PyModuleDef_Init(&_contextvarsmodule);
7881
}

0 commit comments

Comments
 (0)