bpo-1635741: Port _weakref extension module to multiphase initialization (PEP 489) (GH-19140)

Co-authored-by: Hai Shi <shihai1992@gmail.com>
This commit is contained in:
Victor Stinner 2020-03-24 18:31:19 +01:00 committed by GitHub
parent f5c7cabb2b
commit 93460d097f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 21 deletions

View File

@ -0,0 +1 @@
Port _weakref extension module to multiphase initialization (:pep:`489`).

View File

@ -136,14 +136,48 @@ weakref_functions[] = {
{NULL, NULL, 0, NULL}
};
static int
weakref_exec(PyObject *module)
{
Py_INCREF(&_PyWeakref_RefType);
if (PyModule_AddObject(module, "ref", (PyObject *) &_PyWeakref_RefType) < 0) {
Py_DECREF(&_PyWeakref_RefType);
return -1;
}
Py_INCREF(&_PyWeakref_RefType);
if (PyModule_AddObject(module, "ReferenceType",
(PyObject *) &_PyWeakref_RefType) < 0) {
Py_DECREF(&_PyWeakref_RefType);
return -1;
}
Py_INCREF(&_PyWeakref_ProxyType);
if (PyModule_AddObject(module, "ProxyType",
(PyObject *) &_PyWeakref_ProxyType) < 0) {
Py_DECREF(&_PyWeakref_ProxyType);
return -1;
}
Py_INCREF(&_PyWeakref_CallableProxyType);
if (PyModule_AddObject(module, "CallableProxyType",
(PyObject *) &_PyWeakref_CallableProxyType) < 0) {
Py_DECREF(&_PyWeakref_CallableProxyType);
return -1;
}
return 0;
}
static struct PyModuleDef_Slot weakref_slots[] = {
{Py_mod_exec, weakref_exec},
{0, NULL}
};
static struct PyModuleDef weakrefmodule = {
PyModuleDef_HEAD_INIT,
"_weakref",
"Weak-reference support module.",
-1,
0,
weakref_functions,
NULL,
weakref_slots,
NULL,
NULL,
NULL
@ -152,23 +186,5 @@ static struct PyModuleDef weakrefmodule = {
PyMODINIT_FUNC
PyInit__weakref(void)
{
PyObject *m;
m = PyModule_Create(&weakrefmodule);
if (m != NULL) {
Py_INCREF(&_PyWeakref_RefType);
PyModule_AddObject(m, "ref",
(PyObject *) &_PyWeakref_RefType);
Py_INCREF(&_PyWeakref_RefType);
PyModule_AddObject(m, "ReferenceType",
(PyObject *) &_PyWeakref_RefType);
Py_INCREF(&_PyWeakref_ProxyType);
PyModule_AddObject(m, "ProxyType",
(PyObject *) &_PyWeakref_ProxyType);
Py_INCREF(&_PyWeakref_CallableProxyType);
PyModule_AddObject(m, "CallableProxyType",
(PyObject *) &_PyWeakref_CallableProxyType);
}
return m;
return PyModuleDef_Init(&weakrefmodule);
}