-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
gh-105922: PyImport_AddModule() uses Py_DECREF() #105998
Conversation
@serhiy-storchaka: In gh-86160, you added this code using PyWeakref_New() + PyWeakref_GET_OBJECT() get a borrowed reference, whereas the module is known to be stored in the sys.modules dictionary. What's the rationale for using a weak reference, rather than just using Py_DECREF()? |
Rewrite PyImport_AddModule() to simply call Py_DECREF(), rather than creating a weak reference, to get a borrowed reference to the module.
I reverted the doc change. |
Oh, |
|
Also, |
PyImport_AddModule() history, oldest to newest:
|
I'm not convinced that GH-72597 was fully implemented. At least, PyImport_AddModule() doesn't support overriding I added a test to my PR to check this special case: custom According to this test, PyImport_AddModule() always uses Calling PyImport_AddModule() writes into the "original" modules dict, even if |
In the main branch, there is a single line which sets It means that all C code using directly Obviously, C code and Python code getting |
Well, my goal here was to get rid of the But maybe this whole code was always useless and so can be removed, since it seems impossible to get a type other than dict in this code path. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is Py_REFCNT any better than PyWeakref_GetObject? I preferred to use later because the former is a macro and more tightly bound to internal representation.
I have great doubts that it was worth to add support of non-dict as |
The problem is that modules have no |
I created issue #106016 "Support defining setattr() and delattr() in a module". It would allow to either disallow setting sys.modules to a type different than dict, or at least to update |
@@ -373,16 +373,20 @@ PyImport_AddModuleObject(PyObject *name) | |||
return NULL; | |||
} | |||
|
|||
// gh-86160: PyImport_AddModuleObject() returns a borrowed reference | |||
PyObject *ref = PyWeakref_NewRef(mod, NULL); | |||
// Convert to a borrowed reference |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Convert to a borrowed reference | |
// Convert to a borrowed reference. |
I fixed the issue differently: commit ee52158 I don't think that PyImport_AddModuleObject() requires this complicated PyWeakref dance, since the code always gets a strong reference from a Python dict: it's not possible to override |
Rewrite PyImport_AddModule() to simply call Py_DECREF(), rather than creating a weak reference, to get a borrowed reference to the module.
📚 Documentation preview 📚: https://cpython-previews--105998.org.readthedocs.build/