-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
Provide API for module state #286
Comments
Idea for new/better approach: Example usage:
sketch of generated code for a method:
sketch of generated code for a slot:
Relevant CPython docs:
Suggestions for better name for the macros? We cannot overload Additionally, we either need to add Open question: I would suggest not exposing any further API to access the state. If you need it, use appropriate convention. I think the more is declarative and carried out the Python interpreter, the better. Like the removal of |
The current state in CPython and its issues:
m_size
inPyModuleDef
, runtime allocates the state for the user when creating the moduleself
is already the module instance ->PyModule_GetState(self)
PyType_GetModuleState(type)
nb_add
PyTypeObject*
pointer for an HPy type coming from another HPy extension and attempt to directly callnb_add
on itPyType_GetModuleByDef(Py_TYPE(self), &module_def);
wheremodule_def
is thePyModuleDef
PyModuleDef
(such type must have been created with PyType_FromModuleAndSpec)The issue with the last API is that one can import one module multiple times in one interpreter:
In such case
PyModuleDef
does not uniquely identify the module instance. An example that shows how this can lead to a bug is here: https://gist.github.com/steve-s/9dd4d7e4810bf4cb61302f049d03ecf5CPython documentation: https://docs.python.org/3/howto/isolating-extensions.html
There is also a Discord conversation about this with CPython core.
Ideal API:
How implementation of such API can look like:
PyType_GetModuleByDef(Py_TYPE(self), &module_def);
walks the MRO -- to slow to be called in every slot trampolineHPyGlobal
(or thread local) to store the module state for the trampolines. Eventually if CPython implements HPy natively, it can pass in the right state.HPyType_FromSpec
to also take the moduleHPyModuleDef
(advantages: declarative, fewer API calls to create and initialize the module, lessens the need for the "exec" slot in multiphase init):would automatically call
HPyType_FromSpec
and put the type into the module dict.The text was updated successfully, but these errors were encountered: