Skip to content

Commit 8334f30

Browse files
authored
bpo-1635741: Port _weakref extension module to multiphase initialization (PEP 489) (GH-19084)
1 parent 2de7ac9 commit 8334f30

File tree

2 files changed

+38
-21
lines changed

2 files changed

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

Modules/_weakref.c

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,48 @@ weakref_functions[] = {
136136
{NULL, NULL, 0, NULL}
137137
};
138138

139+
static int
140+
weakref_exec(PyObject *module)
141+
{
142+
Py_INCREF(&_PyWeakref_RefType);
143+
if (PyModule_AddObject(module, "ref", (PyObject *) &_PyWeakref_RefType) < 0) {
144+
Py_DECREF(&_PyWeakref_RefType);
145+
return -1;
146+
}
147+
Py_INCREF(&_PyWeakref_RefType);
148+
if (PyModule_AddObject(module, "ReferenceType",
149+
(PyObject *) &_PyWeakref_RefType) < 0) {
150+
Py_DECREF(&_PyWeakref_RefType);
151+
return -1;
152+
}
153+
Py_INCREF(&_PyWeakref_ProxyType);
154+
if (PyModule_AddObject(module, "ProxyType",
155+
(PyObject *) &_PyWeakref_ProxyType) < 0) {
156+
Py_DECREF(&_PyWeakref_ProxyType);
157+
return -1;
158+
}
159+
Py_INCREF(&_PyWeakref_CallableProxyType);
160+
if (PyModule_AddObject(module, "CallableProxyType",
161+
(PyObject *) &_PyWeakref_CallableProxyType) < 0) {
162+
Py_DECREF(&_PyWeakref_CallableProxyType);
163+
return -1;
164+
}
165+
166+
return 0;
167+
}
168+
169+
static struct PyModuleDef_Slot weakref_slots[] = {
170+
{Py_mod_exec, weakref_exec},
171+
{0, NULL}
172+
};
139173

140174
static struct PyModuleDef weakrefmodule = {
141175
PyModuleDef_HEAD_INIT,
142176
"_weakref",
143177
"Weak-reference support module.",
144-
-1,
178+
0,
145179
weakref_functions,
146-
NULL,
180+
weakref_slots,
147181
NULL,
148182
NULL,
149183
NULL
@@ -152,23 +186,5 @@ static struct PyModuleDef weakrefmodule = {
152186
PyMODINIT_FUNC
153187
PyInit__weakref(void)
154188
{
155-
PyObject *m;
156-
157-
m = PyModule_Create(&weakrefmodule);
158-
159-
if (m != NULL) {
160-
Py_INCREF(&_PyWeakref_RefType);
161-
PyModule_AddObject(m, "ref",
162-
(PyObject *) &_PyWeakref_RefType);
163-
Py_INCREF(&_PyWeakref_RefType);
164-
PyModule_AddObject(m, "ReferenceType",
165-
(PyObject *) &_PyWeakref_RefType);
166-
Py_INCREF(&_PyWeakref_ProxyType);
167-
PyModule_AddObject(m, "ProxyType",
168-
(PyObject *) &_PyWeakref_ProxyType);
169-
Py_INCREF(&_PyWeakref_CallableProxyType);
170-
PyModule_AddObject(m, "CallableProxyType",
171-
(PyObject *) &_PyWeakref_CallableProxyType);
172-
}
173-
return m;
189+
return PyModuleDef_Init(&weakrefmodule);
174190
}

0 commit comments

Comments
 (0)