pyo3/marker.rs
1// TODO https://github.com/PyO3/pyo3/issues/5487
2#![allow(clippy::undocumented_unsafe_blocks)]
3
4//! Fundamental properties of objects tied to the Python interpreter.
5//!
6//! The Python interpreter is not thread-safe. To protect the Python interpreter in multithreaded
7//! scenarios there is a global lock, the *global interpreter lock* (hereafter referred to as *GIL*)
8//! that must be held to safely interact with Python objects. This is why in PyO3 when you acquire
9//! the GIL you get a [`Python`] marker token that carries the *lifetime* of holding the GIL and all
10//! borrowed references to Python objects carry this lifetime as well. This will statically ensure
11//! that you can never use Python objects after dropping the lock - if you mess this up it will be
12//! caught at compile time and your program will fail to compile.
13//!
14//! It also supports this pattern that many extension modules employ:
15//! - Drop the GIL, so that other Python threads can acquire it and make progress themselves
16//! - Do something independently of the Python interpreter, like IO, a long running calculation or
17//! awaiting a future
18//! - Once that is done, reacquire the GIL
19//!
20//! That API is provided by [`Python::detach`] and enforced via the [`Ungil`] bound on the
21//! closure and the return type. This is done by relying on the [`Send`] auto trait. `Ungil` is
22//! defined as the following:
23//!
24//! ```rust,no_run
25//! # #![allow(dead_code)]
26//! pub unsafe trait Ungil {}
27//!
28//! unsafe impl<T: Send> Ungil for T {}
29//! ```
30//!
31//! We piggy-back off the `Send` auto trait because it is not possible to implement custom auto
32//! traits on stable Rust. This is the solution which enables it for as many types as possible while
33//! making the API usable.
34//!
35//! In practice this API works quite well, but it comes with some drawbacks:
36//!
37//! ## Drawbacks
38//!
39//! There is no reason to prevent `!Send` types like [`Rc`] from crossing the closure. After all,
40//! [`Python::detach`] just lets other Python threads run - it does not itself launch a new
41//! thread.
42//!
43//! ```rust, compile_fail
44//! # #[cfg(feature = "nightly")]
45//! # compile_error!("this actually works on nightly")
46//! use pyo3::prelude::*;
47//! use alloc::rc::Rc;
48//!
49//! fn main() {
50//! Python::attach(|py| {
51//! let rc = Rc::new(5);
52//!
53//! py.detach(|| {
54//! // This would actually be fine...
55//! println!("{:?}", *rc);
56//! });
57//! });
58//! }
59//! ```
60//!
61//! Because we are using `Send` for something it's not quite meant for, other code that
62//! (correctly) upholds the invariants of [`Send`] can cause problems.
63//!
64//! [`SendWrapper`] is one of those. Per its documentation:
65//!
66//! > A wrapper which allows you to move around non-Send-types between threads, as long as you
67//! > access the contained value only from within the original thread and make sure that it is
68//! > dropped from within the original thread.
69//!
70//! This will "work" to smuggle Python references across the closure, because we're not actually
71//! doing anything with threads:
72//!
73//! ```rust, no_run
74//! use pyo3::prelude::*;
75//! use pyo3::types::PyString;
76//! use send_wrapper::SendWrapper;
77//!
78//! Python::attach(|py| {
79//! let string = PyString::new(py, "foo");
80//!
81//! let wrapped = SendWrapper::new(string);
82//!
83//! py.detach(|| {
84//! # #[cfg(not(feature = "nightly"))]
85//! # {
86//! // 💥 Unsound! 💥
87//! let smuggled: &Bound<'_, PyString> = &*wrapped;
88//! println!("{:?}", smuggled);
89//! # }
90//! });
91//! });
92//! ```
93//!
94//! For now the answer to that is "don't do that".
95//!
96//! # A proper implementation using an auto trait
97//!
98//! However on nightly Rust and when PyO3's `nightly` feature is
99//! enabled, `Ungil` is defined as the following:
100//!
101//! ```rust,no_run
102//! # #[cfg(any())]
103//! # {
104//! #![feature(auto_traits, negative_impls)]
105//!
106//! pub unsafe auto trait Ungil {}
107//!
108//! // It is unimplemented for the `Python` struct and Python objects.
109//! impl !Ungil for Python<'_> {}
110//! impl !Ungil for ffi::PyObject {}
111//!
112//! // `Py` wraps it in a safe api, so this is OK
113//! unsafe impl<T> Ungil for Py<T> {}
114//! # }
115//! ```
116//!
117//! With this feature enabled, the above two examples will start working and not working, respectively.
118//!
119//! [`SendWrapper`]: https://docs.rs/send_wrapper/latest/send_wrapper/struct.SendWrapper.html
120//! [`Rc`]: alloc::rc::Rc
121//! [`Py`]: crate::Py
122use crate::conversion::IntoPyObject;
123use crate::err::{self, PyResult};
124use crate::internal::state::{AttachGuard, SuspendAttach};
125use crate::types::any::PyAnyMethods;
126use crate::types::{
127 PyAny, PyCode, PyCodeMethods, PyDict, PyEllipsis, PyModule, PyNone, PyNotImplemented, PyString,
128 PyType,
129};
130use crate::version::PythonVersionInfo;
131use crate::{ffi, Bound, Py, PyTypeInfo};
132use core::ffi::CStr;
133use core::marker::PhantomData;
134use std::sync::LazyLock;
135
136/// Types that are safe to access while the GIL is not held.
137///
138/// # Safety
139///
140/// The type must not carry borrowed Python references or, if it does, not allow access to them if
141/// the GIL is not held.
142///
143/// See the [module-level documentation](self) for more information.
144///
145/// # Examples
146///
147/// This tracking is currently imprecise as it relies on the [`Send`] auto trait on stable Rust.
148/// For example, an `Rc` smart pointer should be usable without the GIL, but we currently prevent that:
149///
150/// ```compile_fail
151/// # use pyo3::prelude::*;
152/// use alloc::rc::Rc;
153///
154/// Python::attach(|py| {
155/// let rc = Rc::new(42);
156///
157/// py.detach(|| {
158/// println!("{:?}", rc);
159/// });
160/// });
161/// ```
162///
163/// This also implies that the interplay between `attach` and `detach` is unsound, for example
164/// one can circumvent this protection using the [`send_wrapper`](https://docs.rs/send_wrapper/) crate:
165///
166/// ```no_run
167/// # use pyo3::prelude::*;
168/// # use pyo3::types::PyString;
169/// use send_wrapper::SendWrapper;
170///
171/// Python::attach(|py| {
172/// let string = PyString::new(py, "foo");
173///
174/// let wrapped = SendWrapper::new(string);
175///
176/// py.detach(|| {
177/// let sneaky: &Bound<'_, PyString> = &*wrapped;
178///
179/// println!("{:?}", sneaky);
180/// });
181/// });
182/// ```
183///
184/// Fixing this loophole on stable Rust has significant ergonomic issues, but it is fixed when using
185/// nightly Rust and the `nightly` feature, c.f. [#2141](https://github.com/PyO3/pyo3/issues/2141).
186#[cfg_attr(docsrs, doc(cfg(all())))] // Hide the cfg flag
187#[cfg(not(feature = "nightly"))]
188pub unsafe trait Ungil {}
189
190#[cfg_attr(docsrs, doc(cfg(all())))] // Hide the cfg flag
191#[cfg(not(feature = "nightly"))]
192unsafe impl<T: Send> Ungil for T {}
193
194#[cfg(feature = "nightly")]
195mod nightly {
196 macro_rules! define {
197 ($($tt:tt)*) => { $($tt)* }
198 }
199
200 define! {
201 /// Types that are safe to access while the GIL is not held.
202 ///
203 /// # Safety
204 ///
205 /// The type must not carry borrowed Python references or, if it does, not allow access to them if
206 /// the GIL is not held.
207 ///
208 /// See the [module-level documentation](self) for more information.
209 ///
210 /// # Examples
211 ///
212 /// Types which are `Ungil` cannot be used in contexts where the GIL was released, e.g.
213 ///
214 /// ```compile_fail
215 /// # use pyo3::prelude::*;
216 /// # use pyo3::types::PyString;
217 /// Python::attach(|py| {
218 /// let string = PyString::new(py, "foo");
219 ///
220 /// py.detach(|| {
221 /// println!("{:?}", string);
222 /// });
223 /// });
224 /// ```
225 ///
226 /// This applies to the [`Python`] token itself as well, e.g.
227 ///
228 /// ```compile_fail
229 /// # use pyo3::prelude::*;
230 /// Python::attach(|py| {
231 /// py.detach(|| {
232 /// drop(py);
233 /// });
234 /// });
235 /// ```
236 ///
237 /// On nightly Rust, this is not based on the [`Send`] auto trait and hence we are able
238 /// to prevent incorrectly circumventing it using e.g. the [`send_wrapper`](https://docs.rs/send_wrapper/) crate:
239 ///
240 /// ```compile_fail
241 /// # use pyo3::prelude::*;
242 /// # use pyo3::types::PyString;
243 /// use send_wrapper::SendWrapper;
244 ///
245 /// Python::attach(|py| {
246 /// let string = PyString::new(py, "foo");
247 ///
248 /// let wrapped = SendWrapper::new(string);
249 ///
250 /// py.detach(|| {
251 /// let sneaky: &PyString = *wrapped;
252 ///
253 /// println!("{:?}", sneaky);
254 /// });
255 /// });
256 /// ```
257 ///
258 /// This also enables using non-[`Send`] types in `detach`,
259 /// at least if they are not also bound to the GIL:
260 ///
261 /// ```rust
262 /// # use pyo3::prelude::*;
263 /// use std::rc::Rc;
264 ///
265 /// Python::attach(|py| {
266 /// let rc = Rc::new(42);
267 ///
268 /// py.detach(|| {
269 /// println!("{:?}", rc);
270 /// });
271 /// });
272 /// ```
273 pub unsafe auto trait Ungil {}
274
275 impl !Ungil for crate::Python<'_> {}
276
277 // This means that PyString, PyList, etc all inherit !Ungil from this.
278 impl !Ungil for crate::PyAny {}
279
280 impl<T> !Ungil for crate::PyRef<'_, T> {}
281 impl<T> !Ungil for crate::PyRefMut<'_, T> {}
282
283 // FFI pointees
284 impl !Ungil for crate::ffi::PyObject {}
285 impl !Ungil for crate::ffi::PyLongObject {}
286
287 impl !Ungil for crate::ffi::PyThreadState {}
288 impl !Ungil for crate::ffi::PyInterpreterState {}
289 impl !Ungil for crate::ffi::PyWeakReference {}
290 impl !Ungil for crate::ffi::PyFrameObject {}
291 impl !Ungil for crate::ffi::PyCodeObject {}
292 #[cfg(not(Py_LIMITED_API))]
293 impl !Ungil for crate::ffi::PyDictKeysObject {}
294 #[cfg(not(any(Py_LIMITED_API, Py_3_10)))]
295 impl !Ungil for crate::ffi::PyArena {}
296 }
297}
298
299#[cfg(feature = "nightly")]
300pub use nightly::Ungil;
301
302/// A marker token that represents holding the GIL.
303///
304/// It serves three main purposes:
305/// - It provides a global API for the Python interpreter, such as [`Python::eval`].
306/// - It can be passed to functions that require a proof of holding the GIL, such as
307/// [`Py::clone_ref`](crate::Py::clone_ref).
308/// - Its lifetime represents the scope of holding the GIL which can be used to create Rust
309/// references that are bound to it, such as [`Bound<'py, PyAny>`].
310///
311/// Note that there are some caveats to using it that you might need to be aware of. See the
312/// [Deadlocks](#deadlocks) and [Releasing and freeing memory](#releasing-and-freeing-memory)
313/// paragraphs for more information about that.
314///
315/// # Obtaining a Python token
316///
317/// The following are the recommended ways to obtain a [`Python<'py>`] token, in order of preference:
318/// - If you already have something with a lifetime bound to the GIL, such as [`Bound<'py, PyAny>`], you can
319/// use its `.py()` method to get a token.
320/// - In a function or method annotated with [`#[pyfunction]`](crate::pyfunction) or [`#[pymethods]`](crate::pymethods) you can declare it
321/// as a parameter, and PyO3 will pass in the token when Python code calls it.
322/// - When you need to acquire the GIL yourself, such as when calling Python code from Rust, you
323/// should call [`Python::attach`] to do that and pass your code as a closure to it.
324///
325/// The first two options are zero-cost; [`Python::attach`] requires runtime checking and may need to block
326/// to acquire the GIL.
327///
328/// # Deadlocks
329///
330/// Note that the GIL can be temporarily released by the Python interpreter during a function call
331/// (e.g. importing a module). In general, you don't need to worry about this because the GIL is
332/// reacquired before returning to the Rust code:
333///
334/// ```text
335/// `Python` exists |=====================================|
336/// GIL actually held |==========| |================|
337/// Rust code running |=======| |==| |======|
338/// ```
339///
340/// This behaviour can cause deadlocks when trying to lock a Rust mutex while holding the GIL:
341///
342/// * Thread 1 acquires the GIL
343/// * Thread 1 locks a mutex
344/// * Thread 1 makes a call into the Python interpreter which releases the GIL
345/// * Thread 2 acquires the GIL
346/// * Thread 2 tries to locks the mutex, blocks
347/// * Thread 1's Python interpreter call blocks trying to reacquire the GIL held by thread 2
348///
349/// To avoid deadlocking, you should release the GIL before trying to lock a mutex or `await`ing in
350/// asynchronous code, e.g. with [`Python::detach`].
351///
352/// # Releasing and freeing memory
353///
354/// The [`Python<'py>`] type can be used to create references to variables owned by the Python
355/// interpreter, using functions such as [`Python::eval`] and [`PyModule::import`].
356#[derive(Copy, Clone)]
357pub struct Python<'py>(PhantomData<&'py AttachGuard>, PhantomData<NotSend>);
358
359/// A marker type that makes the type !Send.
360/// Workaround for lack of !Send on stable (<https://github.com/rust-lang/rust/issues/68318>).
361struct NotSend(PhantomData<*mut Python<'static>>);
362
363impl Python<'_> {
364 /// Acquires the global interpreter lock, allowing access to the Python interpreter. The
365 /// provided closure `F` will be executed with the acquired `Python` marker token.
366 ///
367 /// If implementing [`#[pymethods]`](crate::pymethods) or [`#[pyfunction]`](crate::pyfunction),
368 /// declare `py: Python` as an argument. PyO3 will pass in the token to grant access to the GIL
369 /// context in which the function is running, avoiding the need to call `attach`.
370 ///
371 /// If the [`auto-initialize`] feature is enabled and the Python runtime is not already
372 /// initialized, this function will initialize it. See
373 #[cfg_attr(
374 not(any(PyPy, GraalPy)),
375 doc = "[`Python::initialize`](crate::marker::Python::initialize)"
376 )]
377 #[cfg_attr(PyPy, doc = "`Python::initialize")]
378 /// for details.
379 ///
380 /// If the current thread does not yet have a Python "thread state" associated with it,
381 /// a new one will be automatically created before `F` is executed and destroyed after `F`
382 /// completes.
383 ///
384 /// # Panics
385 ///
386 /// - If the [`auto-initialize`] feature is not enabled and the Python interpreter is not
387 /// initialized.
388 /// - If the Python interpreter is in the process of [shutting down].
389 /// - If the current thread is currently in the middle of a GC traversal (i.e. called from
390 /// within a `__traverse__` method).
391 ///
392 /// To avoid possible initialization or panics if calling in a context where the Python
393 /// interpreter might be unavailable, consider using [`Python::try_attach`].
394 ///
395 /// # Examples
396 ///
397 /// ```
398 /// use pyo3::prelude::*;
399 ///
400 /// # fn main() -> PyResult<()> {
401 /// Python::attach(|py| -> PyResult<()> {
402 /// let x: i32 = py.eval(c"5", None, None)?.extract()?;
403 /// assert_eq!(x, 5);
404 /// Ok(())
405 /// })
406 /// # }
407 /// ```
408 ///
409 /// [`auto-initialize`]: https://pyo3.rs/main/features.html#auto-initialize
410 /// [shutting down]: https://docs.python.org/3/glossary.html#term-interpreter-shutdown
411 #[inline]
412 #[track_caller]
413 pub fn attach<F, R>(f: F) -> R
414 where
415 F: for<'py> FnOnce(Python<'py>) -> R,
416 {
417 let guard = AttachGuard::attach();
418 f(guard.python())
419 }
420
421 /// Variant of [`Python::attach`] which will return without attaching to the Python
422 /// interpreter if the interpreter is in a state where it cannot be attached to:
423 ///
424 /// - If the Python interpreter is not initialized.
425 /// - If the Python interpreter is in the process of [shutting down].
426 /// - If the current thread is currently in the middle of a GC traversal (i.e. called from
427 /// within a `__traverse__` method).
428 ///
429 /// Unlike `Python::attach`, this function will not initialize the Python interpreter,
430 /// even if the [`auto-initialize`] feature is enabled.
431 ///
432 /// Note that due to the nature of the underlying Python APIs used to implement this,
433 /// the behavior is currently provided on a best-effort basis; it is expected that a
434 /// future CPython version will introduce APIs which guarantee this behaviour. This
435 /// function is still recommended for use in the meanwhile as it provides the best
436 /// possible behaviour and should transparently change to an optimal implementation
437 /// once such APIs are available.
438 ///
439 /// [`auto-initialize`]: https://pyo3.rs/main/features.html#auto-initialize
440 /// [shutting down]: https://docs.python.org/3/glossary.html#term-interpreter-shutdown
441 #[inline]
442 #[track_caller]
443 pub fn try_attach<F, R>(f: F) -> Option<R>
444 where
445 F: for<'py> FnOnce(Python<'py>) -> R,
446 {
447 let guard = AttachGuard::try_attach().ok()?;
448 Some(f(guard.python()))
449 }
450
451 /// Prepares the use of Python.
452 ///
453 /// If the Python interpreter is not already initialized, this function will initialize it with
454 /// signal handling disabled (Python will not raise the `KeyboardInterrupt` exception). Python
455 /// signal handling depends on the notion of a 'main thread', which must be the thread that
456 /// initializes the Python interpreter.
457 ///
458 /// If the Python interpreter is already initialized, this function has no effect.
459 ///
460 /// This function is unavailable under PyPy because PyPy cannot be embedded in Rust (or any other
461 /// software). Support for this is tracked on the
462 /// [PyPy issue tracker](https://github.com/pypy/pypy/issues/3836).
463 ///
464 /// # Examples
465 /// ```rust
466 /// use pyo3::prelude::*;
467 ///
468 /// # fn main() -> PyResult<()> {
469 /// Python::initialize();
470 /// Python::attach(|py| py.run(c"print('Hello World')", None, None))
471 /// # }
472 /// ```
473 #[cfg(not(any(PyPy, GraalPy)))]
474 pub fn initialize() {
475 crate::interpreter_lifecycle::initialize();
476 }
477
478 /// Like [`Python::attach`] except Python interpreter state checking is skipped.
479 ///
480 /// Normally when attaching to the Python interpreter, PyO3 checks that it is in
481 /// an appropriate state (e.g. it is fully initialized). This function skips
482 /// those checks.
483 ///
484 /// # Safety
485 ///
486 /// If [`Python::attach`] would succeed, it is safe to call this function.
487 #[inline]
488 #[track_caller]
489 pub unsafe fn attach_unchecked<F, R>(f: F) -> R
490 where
491 F: for<'py> FnOnce(Python<'py>) -> R,
492 {
493 let guard = unsafe { AttachGuard::attach_unchecked() };
494
495 f(guard.python())
496 }
497}
498
499impl<'py> Python<'py> {
500 /// Temporarily releases the GIL, thus allowing other Python threads to run. The GIL will be
501 /// reacquired when `F`'s scope ends.
502 ///
503 /// If you don't need to touch the Python
504 /// interpreter for some time and have other Python threads around, this will let you run
505 /// Rust-only code while letting those other Python threads make progress.
506 ///
507 /// Only types that implement [`Ungil`] can cross the closure. See the
508 /// [module level documentation](self) for more information.
509 ///
510 /// If you need to pass Python objects into the closure you can use [`Py`]`<T>`to create a
511 /// reference independent of the GIL lifetime. However, you cannot do much with those without a
512 /// [`Python`] token, for which you'd need to reacquire the GIL.
513 ///
514 /// # Example: Releasing the GIL while running a computation in Rust-only code
515 ///
516 /// ```
517 /// use pyo3::prelude::*;
518 ///
519 /// #[pyfunction]
520 /// fn sum_numbers(py: Python<'_>, numbers: Vec<u32>) -> PyResult<u32> {
521 /// // We release the GIL here so any other Python threads get a chance to run.
522 /// py.detach(move || {
523 /// // An example of an "expensive" Rust calculation
524 /// let sum = numbers.iter().sum();
525 ///
526 /// Ok(sum)
527 /// })
528 /// }
529 /// #
530 /// # fn main() -> PyResult<()> {
531 /// # Python::attach(|py| -> PyResult<()> {
532 /// # let fun = pyo3::wrap_pyfunction!(sum_numbers, py)?;
533 /// # let res = fun.call1((vec![1_u32, 2, 3],))?;
534 /// # assert_eq!(res.extract::<u32>()?, 6_u32);
535 /// # Ok(())
536 /// # })
537 /// # }
538 /// ```
539 ///
540 /// Please see the [Parallelism] chapter of the guide for a thorough discussion of using
541 /// [`Python::detach`] in this manner.
542 ///
543 /// # Example: Passing borrowed Python references into the closure is not allowed
544 ///
545 /// ```compile_fail
546 /// use pyo3::prelude::*;
547 /// use pyo3::types::PyString;
548 ///
549 /// fn parallel_print(py: Python<'_>) {
550 /// let s = PyString::new(py, "This object cannot be accessed without holding the GIL >_<");
551 /// py.detach(move || {
552 /// println!("{:?}", s); // This causes a compile error.
553 /// });
554 /// }
555 /// ```
556 ///
557 /// [`Py`]: crate::Py
558 /// [`PyString`]: crate::types::PyString
559 /// [auto-traits]: https://doc.rust-lang.org/nightly/unstable-book/language-features/auto-traits.html
560 /// [Parallelism]: https://pyo3.rs/main/parallelism.html
561 pub fn detach<T, F>(self, f: F) -> T
562 where
563 F: Ungil + FnOnce() -> T,
564 T: Ungil,
565 {
566 // Use a guard pattern to handle reacquiring the GIL,
567 // so that the GIL will be reacquired even if `f` panics.
568 // The `Send` bound on the closure prevents the user from
569 // transferring the `Python` token into the closure.
570 let _guard = unsafe { SuspendAttach::new() };
571 f()
572 }
573
574 /// Evaluates a Python expression in the given context and returns the result.
575 ///
576 /// If `globals` is `None`, it defaults to Python module `__main__`.
577 /// If `locals` is `None`, it defaults to the value of `globals`.
578 ///
579 /// If `globals` doesn't contain `__builtins__`, default `__builtins__`
580 /// will be added automatically.
581 ///
582 /// # Examples
583 ///
584 /// ```
585 /// # use pyo3::prelude::*;
586 /// # Python::attach(|py| {
587 /// let result = py.eval(c"[i * 10 for i in range(5)]", None, None).unwrap();
588 /// let res: Vec<i64> = result.extract().unwrap();
589 /// assert_eq!(res, vec![0, 10, 20, 30, 40])
590 /// # });
591 /// ```
592 pub fn eval(
593 self,
594 code: &CStr,
595 globals: Option<&Bound<'py, PyDict>>,
596 locals: Option<&Bound<'py, PyDict>>,
597 ) -> PyResult<Bound<'py, PyAny>> {
598 let code = PyCode::compile(self, code, c"<string>", crate::types::PyCodeInput::Eval)?;
599 code.run(globals, locals)
600 }
601
602 /// Executes one or more Python statements in the given context.
603 ///
604 /// If `globals` is `None`, it defaults to Python module `__main__`.
605 /// If `locals` is `None`, it defaults to the value of `globals`.
606 ///
607 /// If `globals` doesn't contain `__builtins__`, default `__builtins__`
608 /// will be added automatically.
609 ///
610 /// # Examples
611 /// ```
612 /// use pyo3::{
613 /// prelude::*,
614 /// types::{PyBytes, PyDict},
615 /// };
616 /// Python::attach(|py| {
617 /// let locals = PyDict::new(py);
618 /// py.run(cr#"
619 /// import base64
620 /// s = 'Hello Rust!'
621 /// ret = base64.b64encode(s.encode('utf-8'))
622 /// "#,
623 /// None,
624 /// Some(&locals),
625 /// )
626 /// .unwrap();
627 /// let ret = locals.get_item("ret").unwrap().unwrap();
628 /// let b64 = ret.cast::<PyBytes>().unwrap();
629 /// assert_eq!(b64.as_bytes(), b"SGVsbG8gUnVzdCE=");
630 /// });
631 /// ```
632 ///
633 /// You can use [`py_run!`](macro.py_run.html) for a handy alternative of `run`
634 /// if you don't need `globals` and unwrapping is OK.
635 pub fn run(
636 self,
637 code: &CStr,
638 globals: Option<&Bound<'py, PyDict>>,
639 locals: Option<&Bound<'py, PyDict>>,
640 ) -> PyResult<()> {
641 let code = PyCode::compile(self, code, c"<string>", crate::types::PyCodeInput::File)?;
642 code.run(globals, locals).map(|obj| {
643 debug_assert!(obj.is_none());
644 })
645 }
646
647 /// Gets the Python type object for type `T`.
648 #[inline]
649 pub fn get_type<T>(self) -> Bound<'py, PyType>
650 where
651 T: PyTypeInfo,
652 {
653 T::type_object(self)
654 }
655
656 /// Imports the Python module with the specified name.
657 pub fn import<N>(self, name: N) -> PyResult<Bound<'py, PyModule>>
658 where
659 N: IntoPyObject<'py, Target = PyString>,
660 {
661 PyModule::import(self, name)
662 }
663
664 /// Gets the Python builtin value `None`.
665 #[allow(non_snake_case)] // the Python keyword starts with uppercase
666 #[inline]
667 pub fn None(self) -> Py<PyAny> {
668 PyNone::get(self).to_owned().into_any().unbind()
669 }
670
671 /// Gets the Python builtin value `Ellipsis`, or `...`.
672 #[allow(non_snake_case)] // the Python keyword starts with uppercase
673 #[inline]
674 pub fn Ellipsis(self) -> Py<PyAny> {
675 PyEllipsis::get(self).to_owned().into_any().unbind()
676 }
677
678 /// Gets the Python builtin value `NotImplemented`.
679 #[allow(non_snake_case)] // the Python keyword starts with uppercase
680 #[inline]
681 pub fn NotImplemented(self) -> Py<PyAny> {
682 PyNotImplemented::get(self).to_owned().into_any().unbind()
683 }
684
685 /// Deprecated version of [Python::version_str].
686 #[deprecated(since = "0.29.0", note = "use Python::version_str instead")]
687 pub fn version(self) -> &'static str {
688 Python::version_str()
689 }
690
691 /// Gets the running Python interpreter version as a string.
692 ///
693 /// # Examples
694 /// ```rust
695 /// # use pyo3::Python;
696 /// assert!(Python::version_str().starts_with("3."));
697 /// ```
698 pub fn version_str() -> &'static str {
699 static VERSION: LazyLock<&'static str> = LazyLock::new(|| unsafe {
700 CStr::from_ptr(ffi::Py_GetVersion())
701 .to_str()
702 .expect("Python version string not UTF-8")
703 });
704
705 &VERSION
706 }
707
708 /// Gets the running Python interpreter version as a struct similar to
709 /// `sys.version_info`.
710 ///
711 /// # Examples
712 /// ```rust
713 /// # use pyo3::Python;
714 /// Python::attach(|py| {
715 /// // PyO3 supports Python 3.8 and up.
716 /// assert!(py.version_info() >= (3, 8));
717 /// assert!(py.version_info() >= (3, 8, 0));
718 /// });
719 /// ```
720 pub fn version_info(self) -> PythonVersionInfo {
721 let version_str = Python::version_str();
722
723 // Portion of the version string returned by Py_GetVersion up to the first space is the
724 // version number.
725 let version_number_str = version_str.split(' ').next().unwrap_or(version_str);
726
727 PythonVersionInfo::from_str(version_number_str).unwrap()
728 }
729
730 /// Lets the Python interpreter check and handle any pending signals. This will invoke the
731 /// corresponding signal handlers registered in Python (if any).
732 ///
733 /// Returns `Err(`[`PyErr`](crate::PyErr)`)` if any signal handler raises an exception.
734 ///
735 /// These signals include `SIGINT` (normally raised by CTRL + C), which by default raises
736 /// `KeyboardInterrupt`. For this reason it is good practice to call this function regularly
737 /// as part of long-running Rust functions so that users can cancel it.
738 ///
739 /// # Example
740 ///
741 /// ```rust,no_run
742 /// # #![allow(dead_code)] // this example is quite impractical to test
743 /// use pyo3::prelude::*;
744 ///
745 /// # fn main() {
746 /// #[pyfunction]
747 /// fn loop_forever(py: Python<'_>) -> PyResult<()> {
748 /// loop {
749 /// // As this loop is infinite it should check for signals every once in a while.
750 /// // Using `?` causes any `PyErr` (potentially containing `KeyboardInterrupt`)
751 /// // to break out of the loop.
752 /// py.check_signals()?;
753 ///
754 /// // do work here
755 /// # break Ok(()) // don't actually loop forever
756 /// }
757 /// }
758 /// # }
759 /// ```
760 ///
761 /// # Note
762 ///
763 /// This function calls [`PyErr_CheckSignals()`][1] which in turn may call signal handlers.
764 /// As Python's [`signal`][2] API allows users to define custom signal handlers, calling this
765 /// function allows arbitrary Python code inside signal handlers to run.
766 ///
767 /// If the function is called from a non-main thread, or under a non-main Python interpreter,
768 /// it does nothing yet still returns `Ok(())`.
769 ///
770 /// [1]: https://docs.python.org/3/c-api/exceptions.html?highlight=pyerr_checksignals#c.PyErr_CheckSignals
771 /// [2]: https://docs.python.org/3/library/signal.html
772 pub fn check_signals(self) -> PyResult<()> {
773 err::error_on_minusone(self, unsafe { ffi::PyErr_CheckSignals() })
774 }
775}
776
777impl<'unbound> Python<'unbound> {
778 /// Unsafely creates a Python token with an unbounded lifetime.
779 ///
780 /// Many of PyO3 APIs use [`Python<'_>`] as proof that the calling thread is attached to the
781 /// interpreter, but this function can be used to call them unsafely.
782 ///
783 /// # Safety
784 ///
785 /// - This token and any borrowed Python references derived from it can only be safely used
786 /// whilst the currently executing thread is actually attached to the interpreter.
787 /// - This function creates a token with an *unbounded* lifetime. Safe code can assume that
788 /// holding a [`Python<'py>`] token means the thread is attached and stays attached for the
789 /// lifetime `'py`. If you let it or borrowed Python references escape to safe code you are
790 /// responsible for bounding the lifetime `'unbound` appropriately. For more on unbounded
791 /// lifetimes, see the [nomicon].
792 ///
793 /// [nomicon]: https://doc.rust-lang.org/nomicon/unbounded-lifetimes.html
794 #[inline]
795 pub unsafe fn assume_attached() -> Python<'unbound> {
796 Python(PhantomData, PhantomData)
797 }
798}
799
800#[cfg(test)]
801mod tests {
802 use super::*;
803 use crate::{
804 internal::state::ForbidAttaching,
805 types::{IntoPyDict, PyList},
806 };
807
808 #[test]
809 fn test_eval() {
810 Python::attach(|py| {
811 // Make sure builtin names are accessible
812 let v: i32 = py
813 .eval(c"min(1, 2)", None, None)
814 .map_err(|e| e.display(py))
815 .unwrap()
816 .extract()
817 .unwrap();
818 assert_eq!(v, 1);
819
820 let d = [("foo", 13)].into_py_dict(py).unwrap();
821
822 // Inject our own global namespace
823 let v: i32 = py
824 .eval(c"foo + 29", Some(&d), None)
825 .unwrap()
826 .extract()
827 .unwrap();
828 assert_eq!(v, 42);
829
830 // Inject our own local namespace
831 let v: i32 = py
832 .eval(c"foo + 29", None, Some(&d))
833 .unwrap()
834 .extract()
835 .unwrap();
836 assert_eq!(v, 42);
837
838 // Make sure builtin names are still accessible when using a local namespace
839 let v: i32 = py
840 .eval(c"min(foo, 2)", None, Some(&d))
841 .unwrap()
842 .extract()
843 .unwrap();
844 assert_eq!(v, 2);
845 });
846 }
847
848 #[test]
849 #[cfg(not(target_arch = "wasm32"))] // We are building wasm Python with pthreads disabled
850 fn test_detach_releases_and_acquires_gil() {
851 Python::attach(|py| {
852 let b = alloc::sync::Arc::new(std::sync::Barrier::new(2));
853
854 let b2 = b.clone();
855 std::thread::spawn(move || Python::attach(|_| b2.wait()));
856
857 py.detach(|| {
858 // If `detach` does not release the GIL, this will deadlock because
859 // the thread spawned above will never be able to acquire the GIL.
860 b.wait();
861 });
862
863 unsafe {
864 // If the GIL is not reacquired at the end of `detach`, this call
865 // will crash the Python interpreter.
866 let tstate = ffi::PyEval_SaveThread();
867 ffi::PyEval_RestoreThread(tstate);
868 }
869 });
870 }
871
872 #[test]
873 #[cfg(panic = "unwind")]
874 fn test_detach_panics_safely() {
875 Python::attach(|py| {
876 let result = std::panic::catch_unwind(|| unsafe {
877 let py = Python::assume_attached();
878 py.detach(|| {
879 panic!("There was a panic!");
880 });
881 });
882
883 // Check panic was caught
884 assert!(result.is_err());
885
886 // If `detach` is implemented correctly, this thread still owns the GIL here
887 // so the following Python calls should not cause crashes.
888 let list = PyList::new(py, [1, 2, 3, 4]).unwrap();
889 assert_eq!(list.extract::<Vec<i32>>().unwrap(), vec![1, 2, 3, 4]);
890 });
891 }
892
893 #[cfg(not(pyo3_disable_reference_pool))]
894 #[test]
895 fn test_detach_pass_stuff_in() {
896 let list = Python::attach(|py| PyList::new(py, vec!["foo", "bar"]).unwrap().unbind());
897 let mut v = vec![1, 2, 3];
898 let a = alloc::sync::Arc::new(String::from("foo"));
899
900 Python::attach(|py| {
901 py.detach(|| {
902 drop((list, &mut v, a));
903 });
904 });
905 }
906
907 #[test]
908 #[cfg(not(Py_LIMITED_API))]
909 fn test_acquire_gil() {
910 use core::ffi::c_int;
911
912 const GIL_NOT_HELD: c_int = 0;
913 const GIL_HELD: c_int = 1;
914
915 // Before starting the interpreter the state of calling `PyGILState_Check`
916 // seems to be undefined, so let's ensure that Python is up.
917 #[cfg(not(any(PyPy, GraalPy)))]
918 Python::initialize();
919
920 let state = unsafe { crate::ffi::PyGILState_Check() };
921 assert_eq!(state, GIL_NOT_HELD);
922
923 Python::attach(|_| {
924 let state = unsafe { crate::ffi::PyGILState_Check() };
925 assert_eq!(state, GIL_HELD);
926 });
927
928 let state = unsafe { crate::ffi::PyGILState_Check() };
929 assert_eq!(state, GIL_NOT_HELD);
930 }
931
932 #[test]
933 fn test_ellipsis() {
934 Python::attach(|py| {
935 assert_eq!(py.Ellipsis().to_string(), "Ellipsis");
936
937 let v = py
938 .eval(c"...", None, None)
939 .map_err(|e| e.display(py))
940 .unwrap();
941
942 assert!(v.eq(py.Ellipsis()).unwrap());
943 });
944 }
945
946 #[test]
947 fn test_py_run_inserts_globals() {
948 use crate::types::dict::PyDictMethods;
949
950 Python::attach(|py| {
951 let namespace = PyDict::new(py);
952 py.run(
953 c"class Foo: pass\na = int(3)",
954 Some(&namespace),
955 Some(&namespace),
956 )
957 .unwrap();
958 assert!(matches!(namespace.get_item("Foo"), Ok(Some(..))));
959 assert!(matches!(namespace.get_item("a"), Ok(Some(..))));
960 // 3.9 and older did not automatically insert __builtins__ if it wasn't inserted "by hand"
961 #[cfg(not(Py_3_10))]
962 assert!(matches!(namespace.get_item("__builtins__"), Ok(Some(..))));
963 })
964 }
965
966 #[cfg(feature = "macros")]
967 #[test]
968 fn test_py_run_inserts_globals_2() {
969 use alloc::ffi::CString;
970
971 #[crate::pyclass(crate = "crate", skip_from_py_object)]
972 #[derive(Clone)]
973 struct CodeRunner {
974 code: CString,
975 }
976
977 impl CodeRunner {
978 fn reproducer(&mut self, py: Python<'_>) -> PyResult<()> {
979 let variables = PyDict::new(py);
980 variables.set_item("cls", crate::Py::new(py, self.clone())?)?;
981
982 py.run(self.code.as_c_str(), Some(&variables), None)?;
983 Ok(())
984 }
985 }
986
987 #[crate::pymethods(crate = "crate")]
988 impl CodeRunner {
989 fn func(&mut self, py: Python<'_>) -> PyResult<()> {
990 py.import("math")?;
991 Ok(())
992 }
993 }
994
995 let mut runner = CodeRunner {
996 code: CString::new(
997 r#"
998cls.func()
999"#
1000 .to_string(),
1001 )
1002 .unwrap(),
1003 };
1004
1005 Python::attach(|py| {
1006 runner.reproducer(py).unwrap();
1007 });
1008 }
1009
1010 #[test]
1011 fn python_is_zst() {
1012 assert_eq!(core::mem::size_of::<Python<'_>>(), 0);
1013 }
1014
1015 #[test]
1016 fn test_try_attach_fail_during_gc() {
1017 Python::attach(|_| {
1018 assert!(Python::try_attach(|_| {}).is_some());
1019
1020 let guard = ForbidAttaching::during_traverse();
1021 assert!(Python::try_attach(|_| {}).is_none());
1022 drop(guard);
1023
1024 assert!(Python::try_attach(|_| {}).is_some());
1025 })
1026 }
1027
1028 #[test]
1029 fn test_try_attach_ok_when_detached() {
1030 Python::attach(|py| {
1031 py.detach(|| {
1032 assert!(Python::try_attach(|_| {}).is_some());
1033 });
1034 });
1035 }
1036}