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: [C API] Introduce a new slot in PyModuleDef to hold the classes
Type: enhancement Stage:
Components: C API, Extension Modules Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, erlendaasland, petr.viktorin, shreyanavigyan, vstinner
Priority: normal Keywords:

Created on 2021-05-10 09:49 by shreyanavigyan, last changed 2022-04-11 14:59 by admin.

Messages (11)
msg393374 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-10 09:49
It's tedious to add classes one by one using PyModule_AddObject in PyInit_module function. Wouldn't it be much easier if there's a slot that has an array of all the classes in their PyObject form and it'll automatically add those classes. Since this is a backwards compatibility change it'll be best if a macro (like PY_AUTO_OBJECT maybe?) is defined then it'll add those classes automatically. If the slot is NULL or 0 then don't add anything. But yeah they may add more classes if they want but it doesn't matter much.

Thanking you,

With Regards,
Shreyan Avigyan
msg393890 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-18 17:13
A friendly ping
msg393956 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2021-05-19 15:17
First off, note that you can use PyModule_AddType rather than PyModule_AddObject to avoid repeating the name.

Adding this *correctly* will be somewhat involved: slots take function pointers, not data pointers which are incompatible according to standard C. The changes will also need to keep backwards compatibility.
msg393957 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-19 15:27
Yes and it wouldn't be a problem. If struct member is 0 or NULL then use default behavior. If not then apply PyModule_AddType or PyModule_AddObject on all of those. Then after finishing set slot to NULL again since we don't want to add a type more than once. And yes after this also anyone can add more types. This will just introduce a handy way to add types.
msg394014 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-05-20 11:41
See also bpo-42376
msg394022 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-20 12:30
I'm not sure if bpo-42376 is same as this one. Yes the basic idea to provide abstraction and make it easy for the users to add type is same but the approaches suggested are completely different. I propose a new structure member in PyModuleDef be added where we can just specify the address of the types and then in PyModule_Create add those automatically and set the member back to NULL. This will not be a performance improvement just an enhancement to allow adding types more easily. The only thing I'm concerned about is that will it break the Stable ABI? It'll not break C extensions though.
msg394023 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-05-20 12:42
How will you differentiate which types should be added to the module dict and which not to add. How will you map the instantiated type objects to the respective module state members?
msg394024 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-20 12:51
In PyModule_Create -> PyModule_Create2 -> SomeFunc... it would loop through the array of object addresses and call PyModule_AddType on each and every one of them. This will not change behavior or control flow only implement a handy way. This I've found very distrubing,

PyModule_AddType(&Example1);
PyModule_AddType(&Example2);
PyModule_AddType(&Example3);


The proposed way will be

PyModuleDef examplemodule = {
    ...
    .types = {Example1, Example2, Example3};
    ...
}

Now just calling PyModule_Create (not PyModule_Init or any other initialization function) would add those types automatically.
msg394025 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2021-05-20 13:06
I suggest you try to implement your proposal first before you post it. (spoilers: you will notice that it doesn't work for non-trivial heap types.)
msg394030 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-20 14:57
Yes. It's becoming hard to implement both static and heap types using one struct member. Since this is all about implementing a helping hand when it comes to types I think two different members one for static and another one for heap would be fine. And if someone then comes up with even a better idea, then I'll implement that. I'm now working on implementing them.
msg394032 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-05-20 15:22
See also https://discuss.python.org/t/define-module-constants-in-pymoduledef/5749
History
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88265
2021-05-20 15:22:46vstinnersetnosy: + vstinner
messages: + msg394032
2021-05-20 14:57:02shreyanavigyansetmessages: + msg394030
2021-05-20 13:06:47christian.heimessetnosy: + christian.heimes
messages: + msg394025
2021-05-20 12:51:35shreyanavigyansetmessages: + msg394024
2021-05-20 12:42:29erlendaaslandsetmessages: + msg394023
2021-05-20 12:30:15shreyanavigyansetmessages: + msg394022
2021-05-20 11:41:04erlendaaslandsetnosy: + erlendaasland
messages: + msg394014
2021-05-19 15:27:56shreyanavigyansetmessages: + msg393957
2021-05-19 15:17:29petr.viktorinsetnosy: + petr.viktorin
messages: + msg393956
2021-05-19 07:21:21shreyanavigyansettitle: [c-api] Introduce a new slot in PyModuleDef to hold the classes -> [C API] Introduce a new slot in PyModuleDef to hold the classes
2021-05-18 17:13:26shreyanavigyansetmessages: + msg393890
2021-05-11 07:34:37shreyanavigyansettitle: Introduce a new slot in PyModuleDef to hold the classes -> [c-api] Introduce a new slot in PyModuleDef to hold the classes
2021-05-10 09:49:59shreyanavigyancreate