-
Notifications
You must be signed in to change notification settings - Fork 806
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
Safe wrappers for PEP 587 initialization #1474
Comments
I don’t fully grok the role of Also a thing I don’t understand is how initialization interacts with subinterpreters. Should a safe initialization function return a sub interpreter object? A threadstate object? Or is it all necessarily global? |
The While I would welcome safe API wrappers for a lot of that, my guess is that the audience size for some of those APIs will be appropriately 1 :) I think safe PyConfig APIs for managing those object's lifecycle is justifiable, as that is a generically useful API for initializing a Python interpreter. The more questionable features are actually performing mutation operations on those data structures and support for multi-phase initialization. Although wrappers for APIs like Supporting multi-phase initialization is its own beast. I'll need a way to use PyO3's APIs for initializing a Rust-implemented meta path importer between the calls to From my perspective, I need some kind of way to obtain a usable https://github.com/indygreg/PyOxidizer/blob/590e39d747ae5ac98ac0521192f8af483a2a7885/pyembed/src/interpreter.rs#L170 is the specific (hopefully sufficiently documented) code explaining this in more detail. |
There's a concept of a main interpreter. The initialization APIs and the GIL are concerned with its state. Subinterpreters are things that can exist in addition to the main interpreter, after the main interpreter is initialized. Also note that alpha builds of Python 3.10 have conditional support for per-subinterpreter locks ( |
That's true. You also make a good point that multi-phase initialization is complex and not something many users clearly need, so it's probably low priority for us to maintain in PyO3 vs. core optimizations and other features. Thinking on the further, perhaps it's fine to leave this functionality out of PyO3 and allow The original need you had for addition to PyO3 was the default impls for the ffi structs, however now that we note the right way to initialize them is to use ``MaybeUninit |
I'm fine with PyO3 reducing scope here. The things I'll need from PyO3 are defining some missing ffi symbols (seems pretty non-controversial) and maybe a (possibly unsafe) API to coerce PyO3's GIL checks into working during multi-phase initialization. Let me keep hacking on my port. Once I have a better grasp on what's going on in GIL land, I'll make some noise. |
I think for now I'm going to close this as something we're not going to worry about in PyO3, but we can revisit this in the future if it becomes a popular demand. |
This is a follow-on from the discussion in indygreg/PyOxidizer#324.
cc @indygreg @wkschwartz
The issue mentioned there is that the ffi bindings in
initconfig.rs
forPyPreConfig
andPyConfig
are cumbersome to initialize safely, so it would be helpful for us to provide safe wrappers forPyPreConfig_InitPythonConfig
etc.Quoting indygreg/PyOxidizer#324 (comment), a possible design is (credit @wkschwartz)
I have a few notes on this design before we commit to this:
PyConfig_SetBytesString
to further configure the fields contained. Should we also provide safe APIs for these?auto-initialize
feature, asking users to callprepare_freethreaded_python()
directly if they needed. That API doesn't offer a way to tie in these ffi structures. If we are implementing some safe wrappers for the PEP 587 APIs in PyO3, I think it would be nice to go the whole way so that users can have a safe alternative toprepare_freethreaded_python()
.PyConfig_Clear
to clean up the allocations contained. Sounds like a perfect candidate for animpl Drop
.Points 1 and 3 particular make me think that instead of adding the methods provided above, we could consider adding a module
pyo3::init
, which contains safe wrappers roughly along the lines of the following:The text was updated successfully, but these errors were encountered: