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()? |
Sorry, something went wrong.
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. |
Sorry, something went wrong.
|
Oh, |
Sorry, something went wrong.
|
|
Sorry, something went wrong.
|
Also, |
Sorry, something went wrong.
|
PyImport_AddModule() history, oldest to newest:
|
Sorry, something went wrong.
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 |
Sorry, something went wrong.
|
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 |
Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
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.
Sorry, something went wrong.
|
I have great doubts that it was worth to add support of non-dict as |
Sorry, something went wrong.
The problem is that modules have no |
Sorry, something went wrong.
|
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 |
Sorry, something went wrong.
|
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 |
Sorry, something went wrong.
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/