This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Don't access the module dictionary directly
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: erlendaasland, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2020-11-02 08:20 by erlendaasland, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 23101 closed erlendaasland, 2020-11-02 08:33
Messages (4)
msg380197 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2020-11-02 08:20
Quoting from https://docs.python.org/3.10/c-api/module.html:
"It is recommended extensions use other PyModule_* and PyObject_* functions rather than directly manipulate a module’s __dict__."

A number of modules still access the module dictionary directly:
zsh % grep -r PyModule_GetDict Modules
Modules/errnomodule.c:    PyObject *module_dict = PyModule_GetDict(module);
Modules/_sre.c:    d = PyModule_GetDict(m);
Modules/_cursesmodule.c:    d = PyModule_GetDict(m);
Modules/_threadmodule.c:    d = PyModule_GetDict(m);
Modules/signalmodule.c:    PyObject *d = PyModule_GetDict(m);
Modules/_xxsubinterpretersmodule.c:    PyObject *ns = PyModule_GetDict(main_mod);  // borrowed
Modules/_xxsubinterpretersmodule.c:    PyObject *ns = PyModule_GetDict(module);  // borrowed
Modules/socketmodule.c:    dict = PyModule_GetDict(m);
Modules/_ssl.c:    d = PyModule_GetDict(m);
Modules/_curses_panel.c:    PyObject *d = PyModule_GetDict(mod);
Modules/_sqlite/connection.c:    module_dict = PyModule_GetDict(module);
Modules/_winapi.c:    PyObject *d = PyModule_GetDict(m);
Modules/pyexpat.c:    d = PyModule_GetDict(m);
msg380198 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-11-02 09:02
Are all these occurrences in the module initialization code?

I think there is nothing wrong with this. Yes, the code can be a little clearer if use PyObject_GetArrt or PyModule_Add*, but rewriting can introduce new bugs.

The remark in the documentation is rather about reading the module attributes and writing a new code. It does not require rewriting existing code.

You can do this, but it will be treated as pure cosmetic change. It can be accepted if the new code is much clearer.
msg380200 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2020-11-02 09:15
> Are all these occurrences in the module initialization code?

Most of them, but not all.

> but rewriting can introduce new bugs.

Of course.


> You can do this, but it will be treated as pure cosmetic change. It can be accepted if the new code is much clearer.

It mostly results in more compact code, which, in my opinion, would improve both readability and maintainability.
msg380247 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-11-02 19:42
I would leave it as-is.
History
Date User Action Args
2022-04-11 14:59:37adminsetgithub: 86409
2020-12-28 22:36:54erlendaaslandsetstatus: open -> closed
stage: patch review -> resolved
2020-12-28 22:36:09erlendaaslandsetresolution: wont fix
2020-11-02 19:42:24rhettingersetnosy: + rhettinger
messages: + msg380247
2020-11-02 09:15:28erlendaaslandsetmessages: + msg380200
2020-11-02 09:02:42serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg380198
2020-11-02 08:33:51erlendaaslandsetkeywords: + patch
stage: patch review
pull_requests: + pull_request22018
2020-11-02 08:20:25erlendaaslandcreate