From 34ba2fc0e831829dc9ca53e2643218cabac2bfeb Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Fri, 20 Mar 2020 13:25:01 +0800 Subject: [PATCH 1/2] Port _weakref extension module to multiphase initialization(PEP 489) --- Modules/_weakref.c | 58 +++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/Modules/_weakref.c b/Modules/_weakref.c index c1238e00d35f4a..cd7c4c159ac1b6 100644 --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -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); } From 1af81e4d2a59080ac548e1637cd2a935d9af6a97 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Fri, 20 Mar 2020 13:44:17 +0800 Subject: [PATCH 2/2] add news --- .../Core and Builtins/2020-03-20-13-42-35.bpo-1635741.bhIu5M.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-03-20-13-42-35.bpo-1635741.bhIu5M.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-03-20-13-42-35.bpo-1635741.bhIu5M.rst b/Misc/NEWS.d/next/Core and Builtins/2020-03-20-13-42-35.bpo-1635741.bhIu5M.rst new file mode 100644 index 00000000000000..ab5d0ae428d7d5 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-03-20-13-42-35.bpo-1635741.bhIu5M.rst @@ -0,0 +1 @@ +Port _weakref extension module to multiphase initialization (:pep:`489`).