-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
C API: Add PyImport_AddModuleRef() function #105922
Comments
Add tests on PyImport_AddModuleRef(), PyImport_AddModule() and PyImport_AddModuleObject().
* Add tests on PyImport_AddModuleRef(), PyImport_AddModule() and PyImport_AddModuleObject(). * PyRun_InteractiveOneObjectEx(), _PyRun_SimpleFileObject() and PyRun_SimpleStringFlags() now use PyImport_AddModuleRef() and keep the module strong reference alive longer than before.
* Add tests on PyImport_AddModuleRef(), PyImport_AddModule() and PyImport_AddModuleObject(). * PyRun_InteractiveOneObjectEx(), _PyRun_SimpleFileObject() and PyRun_SimpleStringFlags() now use PyImport_AddModuleRef() and keep the module strong reference alive longer than before.
Refactor PyRun_InteractiveOneObjectEx(), _PyRun_SimpleFileObject() and PyRun_SimpleStringFlags(): * Declare variables closer to where they are defined * Rename variables to use name longer than 1 character * Use PyImport_AddModule() with Py_XNewRef()
Refactor PyRun_InteractiveOneObjectEx(), _PyRun_SimpleFileObject() and PyRun_SimpleStringFlags(): * Keep a strong reference to the __main__ module while using its dictionary (PyModule_GetDict()). * Declare variables closer to where they are defined * Rename variables to use name longer than 1 character * Use PyImport_AddModule() with Py_XNewRef() * Add pyrun_one_parse_ast() sub-function.
Refactor PyRun_InteractiveOneObjectEx(), _PyRun_SimpleFileObject() and PyRun_SimpleStringFlags(): * Keep a strong reference to the __main__ module while using its dictionary (PyModule_GetDict()). * Declare variables closer to where they are defined * Rename variables to use name longer than 1 character * Use PyImport_AddModule() with Py_XNewRef() * Add pyrun_one_parse_ast() sub-function.
Refactor PyRun_InteractiveOneObjectEx(), _PyRun_SimpleFileObject() and PyRun_SimpleStringFlags(): * Keep a strong reference to the __main__ module while using its dictionary (PyModule_GetDict()). Use PyImport_AddModule() with Py_XNewRef(). * Declare variables closer to where they are defined. * Rename variables to use name longer than 1 character. * Add pyrun_one_parse_ast() sub-function.
Refactor PyRun_InteractiveOneObjectEx(), _PyRun_SimpleFileObject() and PyRun_SimpleStringFlags(): * Keep a strong reference to the __main__ module while using its dictionary (PyModule_GetDict()). Use PyImport_AddModule() with Py_XNewRef(). * Declare variables closer to where they are defined. * Rename variables to use name longer than 1 character. * Add pyrun_one_parse_ast() sub-function.
Refactor PyRun_InteractiveOneObjectEx(), _PyRun_SimpleFileObject() and PyRun_SimpleStringFlags(): * Keep a strong reference to the __main__ module while using its dictionary (PyModule_GetDict()). Use PyImport_AddModule() with Py_XNewRef(). * Declare variables closer to where they are defined. * Rename variables to use name longer than 1 character. * Add pyrun_one_parse_ast() sub-function.
* Add tests on PyImport_AddModuleRef(), PyImport_AddModule() and PyImport_AddModuleObject(). * pythonrun.c: Replace Py_XNewRef(PyImport_AddModule(name)) with PyImport_AddModuleRef(name).
* Add tests on PyImport_AddModuleRef(), PyImport_AddModule() and PyImport_AddModuleObject(). * pythonrun.c: Replace Py_XNewRef(PyImport_AddModule(name)) with PyImport_AddModuleRef(name).
* Add tests on PyImport_AddModuleRef(), PyImport_AddModule() and PyImport_AddModuleObject(). * pythonrun.c: Replace Py_XNewRef(PyImport_AddModule(name)) with PyImport_AddModuleRef(name).
The PRs are merged; looks like this can be closed. |
Well, this part is not done yet:
|
I added PyImport_AddModuleRef() to pythoncapi-compat: commit python/pythoncapi-compat@8ba8db3 |
Is this function actually useful for anything except the Would it be better to deprecate the old functions without direct replacement, and add Or, should we change the function to indicate whether the returned module is new or not? |
I'm not sure. Here is a code search on PyPI top 5,000 projcts: 31 projects use it.
|
Replace PyImport_AddModuleObject() + Py_XNewRef() with PyImport_AddModuleRef() to get directly a strong reference.
Rewrite PyImport_AddModule() to simply call Py_DECREF(), rather than creating a weak reference, to get a borrowed reference to the module. In the documentation, add a link to sys.modules to explicit which "modules" are checked.
Rewrite PyImport_AddModule() to simply call Py_DECREF(), rather than creating a weak reference, to get a borrowed reference to the module.
Replace PyImport_AddModuleObject() + Py_XNewRef() with PyImport_AddModuleRef() to get directly a strong reference.
The initial issue, adding PyImport_AddModuleRef() function, was done, so I close the issue.
IMO PyWeakref_GetObject() API is unsafe since it returns a borrowed reference from a weak reference, so I chose to deprecate it as soon as I added PyWeakref_GetRef(). For PyImport_AddModuleObject(), it's less obvious since the module must be stored in See PR #105998 and PR #106001 for technical details. Moreover, the Steering Council is still discussing the topic: python/steering-council#201 For now, I prefer to not deprecate the old API. We can still deprecate it later once the situation will be more clear. |
Let me bring the discussion from python/steering-council#201 (comment) here:
No, there is no such request. Neither is there a user request for a
I opened an issue here: #106915 |
The C API PyImport_AddModule() returns a borrowed reference using a special dance added by commit 4db8988 of issue #86160:
Borrowed references are bad:
I proposed to:
PyImport_AddModuleRef(const char *name)
function, similar toPyImport_AddModule()
but return a strong referencePyImport_AddModule()
andPyImport_AddModuleObject()
Linked PRs
The text was updated successfully, but these errors were encountered: