Skip to content
/ pyo3 Public
forked from PyO3/pyo3

Commit

Permalink
src/impl_: add ff unsafe-allow-subinterpreters
Browse files Browse the repository at this point in the history
Allow the subinterpreter safeguards to be disabled, so that applications
like Ceph's manager can continue to use pyo3 modules without soft
crashing.

Enabling this feature should be done with caution, as any storage of Py
objects in rust statics can lead to undefined behavior.

However, not all consumers of pyo3 use global state, and thus a subset
of them (such as python-bcrypt) are safe to use in subinterpreter
contexts.

References: bazaah/aur-ceph#20
References: PyO3#2523
References: pyca/cryptography#9016
References: PyO3#2346 (comment)
References: PyO3#2346 (comment)
References: PyO3#3451
Signed-off-by: Paul Stemmet <[email protected]>
  • Loading branch information
bazaah authored and zer0def committed Dec 12, 2024
1 parent a967826 commit 8b5ce7c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ py-clone = []
# Optimizes PyObject to Vec conversion and so on.
nightly = []

# Disables the checks for use in subinterpreters.
unsafe-allow-subinterpreters = []

# Activates all additional features
# This is mostly intended for testing purposes - activating *all* of these isn't particularly useful.
full = [
Expand Down
2 changes: 1 addition & 1 deletion src/impl_/pymodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl ModuleDef {
// that static data is not reused across interpreters.
//
// PyPy does not have subinterpreters, so no need to check interpreter ID.
#[cfg(not(any(PyPy, GraalPy)))]
#[cfg(not(any(PyPy, GraalPy, feature = "unsafe-allow-subinterpreters")))]
{
// PyInterpreterState_Get is only available on 3.9 and later, but is missing
// from python3.dll for Windows stable API on 3.9
Expand Down

0 comments on commit 8b5ce7c

Please sign in to comment.