Skip to content

Commit 600db5f

Browse files
committed
embedding: new feature to enable embedding Python
1 parent fb9ad1e commit 600db5f

File tree

6 files changed

+125
-47
lines changed

6 files changed

+125
-47
lines changed

Cargo.toml

+20-7
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,39 @@ assert_approx_eq = "1.1.0"
3333
trybuild = "1.0.23"
3434
rustversion = "1.0"
3535
proptest = { version = "0.10.1", default-features = false, features = ["std"] }
36+
# features needed to run the PyO3 test suite
37+
pyo3 = { path = ".", default-features = false, features = ["macros", "embedding", "auto-initialize"] }
3638

3739
[features]
38-
default = ["macros"]
39-
macros = ["ctor", "indoc", "inventory", "paste", "pyo3-macros", "unindent"]
40+
default = ["macros", "embedding", "auto-initialize"]
41+
42+
# Enables macros: #[pyclass], #[pymodule], #[pyfunction] etc.
43+
macros = ["pyo3-macros", "ctor", "indoc", "inventory", "paste", "unindent"]
44+
45+
# Use this feature when building an extension module.
46+
# It tells the linker to keep the python symbols unresolved,
47+
# so that the module can also be used with statically linked python interpreters.
48+
extension-module = []
49+
4050
# Use the Python limited API. See https://www.python.org/dev/peps/pep-0384/ for more.
4151
abi3 = []
52+
4253
# With abi3, we can manually set the minimum Python version.
4354
abi3-py36 = ["abi3-py37"]
4455
abi3-py37 = ["abi3-py38"]
4556
abi3-py38 = ["abi3-py39"]
4657
abi3-py39 = ["abi3"]
4758

59+
# Enables embedding a Python interpreter inside Rust programs.
60+
embedding = []
61+
62+
# Changes `Python::with_gil` and `Python::acquire_gil` to automatically initialize the
63+
# Python interpreter if needed.
64+
auto-initialize = ["embedding"]
65+
4866
# Optimizes PyObject to Vec conversion and so on.
4967
nightly = []
5068

51-
# Use this feature when building an extension module.
52-
# It tells the linker to keep the python symbols unresolved,
53-
# so that the module can also be used with statically linked python interpreters.
54-
extension-module = []
55-
5669
[workspace]
5770
members = [
5871
"pyo3-macros",

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ If you want your Rust application to create a Python interpreter internally and
104104
use it to run Python code, add `pyo3` to your `Cargo.toml` like this:
105105

106106
```toml
107-
[dependencies]
108-
pyo3 = "0.13.0"
107+
[dependencies.pyo3]
108+
version = "0.13.0"
109+
features = ["embedding", "auto-initialize"]
109110
```
110111

111112
Example program displaying the value of `sys.version` and the current user name:

build.rs

+37
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,11 @@ fn configure(interpreter_config: &InterpreterConfig) -> Result<String> {
764764
}
765765

766766
check_target_architecture(interpreter_config)?;
767+
// TODO just let this be an error on PyO3 0.14
768+
if let Err(e) = check_embedding_feature(interpreter_config) {
769+
println!("cargo:warning={}", e)
770+
}
771+
767772
let target_os = env::var_os("CARGO_CFG_TARGET_OS").unwrap();
768773

769774
let is_extension_module = env::var_os("CARGO_FEATURE_EXTENSION_MODULE").is_some();
@@ -854,6 +859,38 @@ fn check_target_architecture(interpreter_config: &InterpreterConfig) -> Result<(
854859
Ok(())
855860
}
856861

862+
fn check_embedding_feature(interpreter_config: &InterpreterConfig) -> Result<()> {
863+
if env::var_os("CARGO_FEATURE_EMBEDDING").is_some() {
864+
if !interpreter_config.shared {
865+
bail!(concat!(
866+
"The `embedding` feature is not supported when linking Python statically instead ",
867+
"of with a shared library.\n",
868+
"\n",
869+
"Please disable the `embedding` feature, for example by entering the following ",
870+
"in your cargo.toml:\n",
871+
"\n",
872+
" pyo3 = { version = \"0.13.0\", default-features = false }\n",
873+
"\n",
874+
"Alternatively, compile PyO3 using a Python distribution which contains a shared ",
875+
"libary."
876+
));
877+
}
878+
879+
if interpreter_config.version.implementation == PythonInterpreterKind::PyPy {
880+
bail!(concat!(
881+
"The `embedding` feature is not supported by PyPy.\n",
882+
"\n",
883+
"Please disable the `embedding` feature, for example by entering the following ",
884+
"in your cargo.toml:\n",
885+
"\n",
886+
" pyo3 = { version = \"0.13.0\", default-features = false }\n",
887+
));
888+
}
889+
}
890+
891+
Ok(())
892+
}
893+
857894
fn abi3_without_interpreter() -> Result<()> {
858895
println!("cargo:rustc-cfg=Py_LIMITED_API");
859896
let mut flags = "FLAG_WITH_THREAD=1".to_string();

src/gil.rs

+45-28
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
//! Interaction with python's global interpreter lock
44
55
use crate::{ffi, internal_tricks::Unsendable, Python};
6-
use parking_lot::{const_mutex, Mutex};
6+
use parking_lot::{const_mutex, Mutex, Once};
77
use std::cell::{Cell, RefCell};
8-
use std::{mem::ManuallyDrop, ptr::NonNull, sync};
8+
use std::{mem::ManuallyDrop, ptr::NonNull};
99

10-
static START: sync::Once = sync::Once::new();
10+
static START: Once = Once::new();
1111

1212
thread_local! {
1313
/// This is a internal counter in pyo3 monitoring whether this thread has the GIL.
@@ -45,16 +45,17 @@ pub(crate) fn gil_is_acquired() -> bool {
4545
/// If both the Python interpreter and Python threading are already initialized,
4646
/// this function has no effect.
4747
///
48+
/// # Features
49+
///
50+
/// This function is only available with the `embedding` feature.
51+
///
4852
/// # Panic
4953
/// If the Python interpreter is initialized but Python threading is not,
5054
/// a panic occurs.
5155
/// It is not possible to safely access the Python runtime unless the main
5256
/// thread (the thread which originally initialized Python) also initializes
5357
/// threading.
54-
///
55-
/// When writing an extension module, the `#[pymodule]` macro
56-
/// will ensure that Python threading is initialized.
57-
///
58+
#[cfg(feature = "embedding")]
5859
pub fn prepare_freethreaded_python() {
5960
// Protect against race conditions when Python is not yet initialized
6061
// and multiple threads concurrently call 'prepare_freethreaded_python()'.
@@ -66,15 +67,15 @@ pub fn prepare_freethreaded_python() {
6667
// as we can't make the existing Python main thread acquire the GIL.
6768
assert_ne!(ffi::PyEval_ThreadsInitialized(), 0);
6869
} else {
69-
// Initialize Python.
70-
// We use Py_InitializeEx() with initsigs=0 to disable Python signal handling.
71-
// Signal handling depends on the notion of a 'main thread', which doesn't exist in this case.
72-
// Note that the 'main thread' notion in Python isn't documented properly;
73-
// and running Python without one is not officially supported.
74-
75-
// PyPy does not support the embedding API
70+
// TODO remove this cfg once build.rs rejects embedding feature misuse.
7671
#[cfg(not(PyPy))]
7772
{
73+
// Initialize Python.
74+
// We use Py_InitializeEx() with initsigs=0 to disable Python signal handling.
75+
// Signal handling depends on the notion of a 'main thread', which doesn't exist in this case.
76+
// Note that the 'main thread' notion in Python isn't documented properly;
77+
// and running Python without one is not officially supported.
78+
7879
ffi::Py_InitializeEx(0);
7980

8081
// Make sure Py_Finalize will be called before exiting.
@@ -87,22 +88,21 @@ pub fn prepare_freethreaded_python() {
8788
}
8889
}
8990
libc::atexit(finalize);
90-
}
9191

92-
// > Changed in version 3.7: This function is now called by Py_Initialize(), so you don’t have
93-
// > to call it yourself anymore.
94-
#[cfg(not(Py_3_7))]
95-
if ffi::PyEval_ThreadsInitialized() == 0 {
96-
ffi::PyEval_InitThreads();
92+
// > Changed in version 3.7: This function is now called by Py_Initialize(), so you don’t have
93+
// > to call it yourself anymore.
94+
#[cfg(not(Py_3_7))]
95+
if ffi::PyEval_ThreadsInitialized() == 0 {
96+
ffi::PyEval_InitThreads();
97+
}
98+
99+
// Py_InitializeEx() will acquire the GIL, but we don't want to hold it at this point
100+
// (it's not acquired in the other code paths)
101+
// So immediately release the GIL:
102+
let _thread_state = ffi::PyEval_SaveThread();
103+
// Note that the PyThreadState returned by PyEval_SaveThread is also held in TLS by the Python runtime,
104+
// and will be restored by PyGILState_Ensure.
97105
}
98-
// PyEval_InitThreads() will acquire the GIL,
99-
// but we don't want to hold it at this point
100-
// (it's not acquired in the other code paths)
101-
// So immediately release the GIL:
102-
#[cfg(not(PyPy))]
103-
let _thread_state = ffi::PyEval_SaveThread();
104-
// Note that the PyThreadState returned by PyEval_SaveThread is also held in TLS by the Python runtime,
105-
// and will be restored by PyGILState_Ensure.
106106
}
107107
});
108108
}
@@ -137,8 +137,25 @@ impl GILGuard {
137137
/// If PyO3 does not yet have a `GILPool` for tracking owned PyObject references, then this
138138
/// new `GILGuard` will also contain a `GILPool`.
139139
pub(crate) fn acquire() -> GILGuard {
140+
#[cfg(feature = "auto-initialize")]
140141
prepare_freethreaded_python();
141142

143+
#[cfg(not(feature = "auto-initialize"))]
144+
START.call_once_force(|_| unsafe {
145+
// Use call_once_force because if there is a panic because the interpreter is not
146+
// initialized, it's fine for the user to initialize the interpreter and retry.
147+
assert_ne!(
148+
ffi::Py_IsInitialized(),
149+
0,
150+
"The Python interpreter is not initalized and the `auto-initialize` feature is not enabled."
151+
);
152+
assert_ne!(
153+
ffi::PyEval_ThreadsInitialized(),
154+
0,
155+
"Python threading is not initalized and the `auto-initialize` feature is not enabled."
156+
);
157+
});
158+
142159
let gstate = unsafe { ffi::PyGILState_Ensure() }; // acquire GIL
143160

144161
// If there's already a GILPool, we should not create another or this could lead to

src/lib.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@
114114
//! Add `pyo3` to your `Cargo.toml`:
115115
//!
116116
//! ```toml
117-
//! [dependencies]
118-
//! pyo3 = "0.13.0"
117+
//! [dependencies.pyo3]
118+
//! version = "0.13.0"
119+
//! features = ["embedding", "auto-initialize"]
119120
//! ```
120121
//!
121122
//! Example program displaying the value of `sys.version`:
@@ -145,12 +146,14 @@ pub use crate::conversion::{
145146
ToBorrowedObject, ToPyObject,
146147
};
147148
pub use crate::err::{PyDowncastError, PyErr, PyErrArguments, PyResult};
149+
#[cfg(feature = "embedding")]
150+
pub use crate::gil::prepare_freethreaded_python;
148151
pub use crate::gil::{GILGuard, GILPool};
149152
pub use crate::instance::{Py, PyNativeType, PyObject};
150153
pub use crate::pycell::{PyCell, PyRef, PyRefMut};
151154
pub use crate::pyclass::PyClass;
152155
pub use crate::pyclass_init::PyClassInitializer;
153-
pub use crate::python::{prepare_freethreaded_python, Python, PythonVersionInfo};
156+
pub use crate::python::{Python, PythonVersionInfo};
154157
pub use crate::type_object::{type_flags, PyTypeInfo};
155158
// Since PyAny is as important as PyObject, we expose it to the top level.
156159
pub use crate::types::PyAny;

src/python.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ use std::ffi::{CStr, CString};
1111
use std::marker::PhantomData;
1212
use std::os::raw::{c_char, c_int};
1313

14-
pub use gil::prepare_freethreaded_python;
15-
1614
/// Represents the major, minor, and patch (if any) versions of this interpreter.
1715
///
1816
/// See [Python::version].
@@ -134,8 +132,13 @@ impl Python<'_> {
134132
/// Acquires the global interpreter lock, which allows access to the Python runtime. The
135133
/// provided closure F will be executed with the acquired `Python` marker token.
136134
///
137-
/// If the Python runtime is not already initialized, this function will initialize it.
138-
/// See [prepare_freethreaded_python()](fn.prepare_freethreaded_python.html) for details.
135+
/// If the `auto-initialize` feature is enabled and the Python runtime is not already
136+
/// initialized, this function will initialize it. See
137+
/// [prepare_freethreaded_python()](fn.prepare_freethreaded_python.html) for details.
138+
///
139+
/// # Panics
140+
/// - If the `auto-initialize` feature is not enabled and the Python interpreter is not
141+
/// initialized.
139142
///
140143
/// # Example
141144
/// ```
@@ -158,20 +161,24 @@ impl Python<'_> {
158161
impl<'p> Python<'p> {
159162
/// Acquires the global interpreter lock, which allows access to the Python runtime.
160163
///
161-
/// If the Python runtime is not already initialized, this function will initialize it.
162-
/// See [prepare_freethreaded_python()](fn.prepare_freethreaded_python.html) for details.
164+
/// If the `auto-initialize` feature is enabled and the Python runtime is not already
165+
/// initialized, this function will initialize it. See
166+
/// [prepare_freethreaded_python()](fn.prepare_freethreaded_python.html) for details.
163167
///
164168
/// Most users should not need to use this API directly, and should prefer one of two options:
165169
/// 1. When implementing `#[pymethods]` or `#[pyfunction]` add a function argument
166170
/// `py: Python` to receive access to the GIL context in which the function is running.
167171
/// 2. Use [`Python::with_gil`](#method.with_gil) to run a closure with the GIL, acquiring
168172
/// only if needed.
169-
///
170173
/// **Note:** This return type from this function, `GILGuard`, is implemented as a RAII guard
171174
/// around the C-API Python_EnsureGIL. This means that multiple `acquire_gil()` calls are
172175
/// allowed, and will not deadlock. However, `GILGuard`s must be dropped in the reverse order
173176
/// to acquisition. If PyO3 detects this order is not maintained, it may be forced to begin
174177
/// an irrecoverable panic.
178+
///
179+
/// # Panics
180+
/// - If the `auto-initialize` feature is not enabled and the Python interpreter is not
181+
/// initialized.
175182
#[inline]
176183
pub fn acquire_gil() -> GILGuard {
177184
GILGuard::acquire()

0 commit comments

Comments
 (0)