Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-105922: PyImport_AddModule() uses Py_DECREF()#105998
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
vstinner commented Jun 22, 2023 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
vstinner commented Jun 22, 2023
@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.
vstinner commented Jun 22, 2023
I reverted the doc change. |
vstinner commented Jun 22, 2023
Oh, |
serhiy-storchaka commented Jun 22, 2023
|
vstinner commented Jun 22, 2023
Also, |
vstinner commented Jun 22, 2023
PyImport_AddModule() history, oldest to newest:
|
vstinner commented Jun 22, 2023
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 |
vstinner commented Jun 22, 2023
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 |
vstinner commented Jun 22, 2023
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. |
serhiy-storchaka left a comment
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.
serhiy-storchaka commented Jun 23, 2023
I have great doubts that it was worth to add support of non-dict as |
vstinner commented Jun 23, 2023
The problem is that modules have no |
vstinner commented Jun 23, 2023
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 |
| // 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. |
vstinner commented Jun 26, 2023
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/