summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-03-24 17:31:19 (GMT)
committerGitHub <noreply@github.com>2020-03-24 17:31:19 (GMT)
commit93460d097f50db0870161a63911d61ce3c5f4583 (patch)
tree4b4efef92baf24ba89c470aa57ca9c66a59f49df
parentf5c7cabb2be4e42a5975ba8aac8bb458c8d9d6d7 (diff)
downloadcpython-93460d097f50db0870161a63911d61ce3c5f4583.zip
cpython-93460d097f50db0870161a63911d61ce3c5f4583.tar.gz
cpython-93460d097f50db0870161a63911d61ce3c5f4583.tar.bz2
bpo-1635741: Port _weakref extension module to multiphase initialization (PEP 489) (GH-19140)
Co-authored-by: Hai Shi <shihai1992@gmail.com>
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2020-03-20-13-42-35.bpo-1635741.bhIu5M.rst1
-rw-r--r--Modules/_weakref.c58
2 files changed, 38 insertions, 21 deletions
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 0000000..ab5d0ae
--- /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`).
diff --git a/Modules/_weakref.c b/Modules/_weakref.c
index c1238e0..cd7c4c1 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);
}