Skip to content

bpo-1635741: Port _weakref extension module to multiphase initialization(PEP 489) #19084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Port _weakref extension module to multiphase initialization (:pep:`489`).
58 changes: 37 additions & 21 deletions Modules/_weakref.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@corona10: Ah, now I see that we would benefit of a helper function calling PyType_Ready() + _PyType_Name() + PyModule_AddObject() ;-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will submit the PR for this :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am looking forword this feature;)
In this case, _PyWeakref_RefType have been added much times(it is means this type have much objects name). it can use this feature too?

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
Expand All @@ -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);
}