RFC: Basic module state support #328
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.