-
-
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
RFC: Basic module state support #328
base: master
Are you sure you want to change the base?
Conversation
Adds support for specifying and using module state including the traverse and destroy slots. What is missing is the ability to query module state from a defining class. For that to be useful, HPy would have to add support for calling convention that passes along the defining class. Module state: add support for destroy slot Module state: more tests and cleanup Module state: embed user data instead of using indirection
One more thing to consider regarding NumPy port including its API. NumPy API exposes several global |
I think this API is used by recordclass |
Update:
|
I would like to take back the call to just polish and merge this PR. I've read more on module state in CPython and I think we should figure out the whole thing first, especially how to access the module state in various other contexts (CPython docs on the topic):
On top of that: in the light of multi-phase module init, which allows to create multiple modules from one extension (in single interpreter), it seems to me that CPython's way of accessing module state from slots may have a fundamental flaw. Since module state is identified by With this understanding I am now also convinced that module state is necessary feature to complement or replace |
Adds support for specifying and using module state including the traverse and destroy slots. Module state can contain any plain C data and
HPyField
s.There is one thing missing: ability to query the module state from a class. In CPython this plays along with calling conventions that pass along defining class. HPy does not provide those calling conventions, but we can add them.
Introducing module state API similar to CPython will ease the migration to HPy for existing extensions that use CPython module state, and it adds one missing feature to complement the
HPyGlobal
: a place for plain C per-interpreter state, e.g., someint
counter cannot be stored inHPyGlobal
(unless wrapped in Python object).Before we introduce module state, we may consider some alternatives:
HPyContext
, see belowWe can also support module state and HPy specific alternative. If we merge this PR that would be the situation: we'd have more generic module state and specialized HPy specific concept of
HPyGlobal
.Module context idea:
Like
HPyContext
it would be like an "argument", i.e., it would containHPy
handles directly and the user would have toHPy_Dup
them if needed. They would be closed implicitly at the end of the "downcall".This would allow to avoid the issue with
HPyGlobal
that to use a global, one has to do:which is extra 2 calls. With something like a context it could be just
HPy_New(ctx, moduleCtx->myType)
, like it would be with some constant fromHPyContext
.Alongside a calling convention that passes the module context as an argument, there would have to be a function to fetch the module context in places where it is not (easily) available and then probably some "close module context" function.
What is not clear is how the module context would be specified. Since it does not contain
HPyField
s, user cannot just assignHPy
handles to it. Logically the module context would be created by the Python engine as an argument for each call (implementation may cache it). It may be specified by usingoffsetof
. Complete example:the implementation would have to remember all offsets and Python objects whose handles are expected to be found at those offsets. Then it would allocate memory of the right size and set the right
HPy
handles to the right offsets.