pyo3/
marker.rs

1//! Fundamental properties of objects tied to the Python interpreter.
2//!
3//! The Python interpreter is not thread-safe. To protect the Python interpreter in multithreaded
4//! scenarios there is a global lock, the *global interpreter lock* (hereafter referred to as *GIL*)
5//! that must be held to safely interact with Python objects. This is why in PyO3 when you acquire
6//! the GIL you get a [`Python`] marker token that carries the *lifetime* of holding the GIL and all
7//! borrowed references to Python objects carry this lifetime as well. This will statically ensure
8//! that you can never use Python objects after dropping the lock - if you mess this up it will be
9//! caught at compile time and your program will fail to compile.
10//!
11//! It also supports this pattern that many extension modules employ:
12//! - Drop the GIL, so that other Python threads can acquire it and make progress themselves
13//! - Do something independently of the Python interpreter, like IO, a long running calculation or
14//!   awaiting a future
15//! - Once that is done, reacquire the GIL
16//!
17//! That API is provided by [`Python::allow_threads`] and enforced via the [`Ungil`] bound on the
18//! closure and the return type. This is done by relying on the [`Send`] auto trait. `Ungil` is
19//! defined as the following:
20//!
21//! ```rust,no_run
22//! # #![allow(dead_code)]
23//! pub unsafe trait Ungil {}
24//!
25//! unsafe impl<T: Send> Ungil for T {}
26//! ```
27//!
28//! We piggy-back off the `Send` auto trait because it is not possible to implement custom auto
29//! traits on stable Rust. This is the solution which enables it for as many types as possible while
30//! making the API usable.
31//!
32//! In practice this API works quite well, but it comes with some drawbacks:
33//!
34//! ## Drawbacks
35//!
36//! There is no reason to prevent `!Send` types like [`Rc`] from crossing the closure. After all,
37//! [`Python::allow_threads`] just lets other Python threads run - it does not itself launch a new
38//! thread.
39//!
40//! ```rust, compile_fail
41//! # #[cfg(feature = "nightly")]
42//! # compile_error!("this actually works on nightly")
43//! use pyo3::prelude::*;
44//! use std::rc::Rc;
45//!
46//! fn main() {
47//!     Python::with_gil(|py| {
48//!         let rc = Rc::new(5);
49//!
50//!         py.allow_threads(|| {
51//!             // This would actually be fine...
52//!             println!("{:?}", *rc);
53//!         });
54//!     });
55//! }
56//! ```
57//!
58//! Because we are using `Send` for something it's not quite meant for, other code that
59//! (correctly) upholds the invariants of [`Send`] can cause problems.
60//!
61//! [`SendWrapper`] is one of those. Per its documentation:
62//!
63//! > A wrapper which allows you to move around non-Send-types between threads, as long as you
64//! > access the contained value only from within the original thread and make sure that it is
65//! > dropped from within the original thread.
66//!
67//! This will "work" to smuggle Python references across the closure, because we're not actually
68//! doing anything with threads:
69//!
70//! ```rust, no_run
71//! use pyo3::prelude::*;
72//! use pyo3::types::PyString;
73//! use send_wrapper::SendWrapper;
74//!
75//! Python::with_gil(|py| {
76//!     let string = PyString::new(py, "foo");
77//!
78//!     let wrapped = SendWrapper::new(string);
79//!
80//!     py.allow_threads(|| {
81//! # #[cfg(not(feature = "nightly"))]
82//! # {
83//!         // 💥 Unsound! 💥
84//!         let smuggled: &Bound<'_, PyString> = &*wrapped;
85//!         println!("{:?}", smuggled);
86//! # }
87//!     });
88//! });
89//! ```
90//!
91//! For now the answer to that is "don't do that".
92//!
93//! # A proper implementation using an auto trait
94//!
95//! However on nightly Rust and when PyO3's `nightly` feature is
96//! enabled, `Ungil` is defined as the following:
97//!
98//! ```rust,no_run
99//! # #[cfg(any())]
100//! # {
101//! #![feature(auto_traits, negative_impls)]
102//!
103//! pub unsafe auto trait Ungil {}
104//!
105//! // It is unimplemented for the `Python` struct and Python objects.
106//! impl !Ungil for Python<'_> {}
107//! impl !Ungil for ffi::PyObject {}
108//!
109//! // `Py` wraps it in  a safe api, so this is OK
110//! unsafe impl<T> Ungil for Py<T> {}
111//! # }
112//! ```
113//!
114//! With this feature enabled, the above two examples will start working and not working, respectively.
115//!
116//! [`SendWrapper`]: https://docs.rs/send_wrapper/latest/send_wrapper/struct.SendWrapper.html
117//! [`Rc`]: std::rc::Rc
118//! [`Py`]: crate::Py
119use crate::conversion::IntoPyObject;
120use crate::err::PyErr;
121use crate::err::{self, PyResult};
122use crate::ffi_ptr_ext::FfiPtrExt;
123use crate::gil::{GILGuard, SuspendGIL};
124use crate::impl_::not_send::NotSend;
125use crate::py_result_ext::PyResultExt;
126use crate::types::any::PyAnyMethods;
127use crate::types::{
128    PyAny, PyDict, PyEllipsis, PyModule, PyNone, PyNotImplemented, PyString, PyType,
129};
130use crate::version::PythonVersionInfo;
131use crate::{ffi, Bound, PyObject, PyTypeInfo};
132use std::ffi::CStr;
133use std::marker::PhantomData;
134use std::os::raw::c_int;
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 std::rc::Rc;
153///
154/// Python::with_gil(|py| {
155///     let rc = Rc::new(42);
156///
157///     py.allow_threads(|| {
158///         println!("{:?}", rc);
159///     });
160/// });
161/// ```
162///
163/// This also implies that the interplay between `with_gil` and `allow_threads` 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::with_gil(|py| {
172///     let string = PyString::new(py, "foo");
173///
174///     let wrapped = SendWrapper::new(string);
175///
176///     py.allow_threads(|| {
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::with_gil(|py| {
218        ///     let string = PyString::new(py, "foo");
219        ///
220        ///     py.allow_threads(|| {
221        ///         println!("{:?}", string);
222        ///     });
223        /// });
224        /// ```
225        ///
226        /// This applies to the GIL token `Python` itself as well, e.g.
227        ///
228        /// ```compile_fail
229        /// # use pyo3::prelude::*;
230        /// Python::with_gil(|py| {
231        ///     py.allow_threads(|| {
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::with_gil(|py| {
246        ///     let string = PyString::new(py, "foo");
247        ///
248        ///     let wrapped = SendWrapper::new(string);
249        ///
250        ///     py.allow_threads(|| {
251        ///         let sneaky: &PyString = *wrapped;
252        ///
253        ///         println!("{:?}", sneaky);
254        ///     });
255        /// });
256        /// ```
257        ///
258        /// This also enables using non-[`Send`] types in `allow_threads`,
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::with_gil(|py| {
266        ///     let rc = Rc::new(42);
267        ///
268        ///     py.allow_threads(|| {
269        ///         println!("{:?}", rc);
270        ///     });
271        /// });
272        /// ```
273        pub unsafe auto trait Ungil {}
274    }
275
276    impl !Ungil for crate::Python<'_> {}
277
278    // This means that PyString, PyList, etc all inherit !Ungil from  this.
279    impl !Ungil for crate::PyAny {}
280
281    impl<T> !Ungil for crate::PyRef<'_, T> {}
282    impl<T> !Ungil for crate::PyRefMut<'_, T> {}
283
284    // FFI pointees
285    impl !Ungil for crate::ffi::PyObject {}
286    impl !Ungil for crate::ffi::PyLongObject {}
287
288    impl !Ungil for crate::ffi::PyThreadState {}
289    impl !Ungil for crate::ffi::PyInterpreterState {}
290    impl !Ungil for crate::ffi::PyWeakReference {}
291    impl !Ungil for crate::ffi::PyFrameObject {}
292    impl !Ungil for crate::ffi::PyCodeObject {}
293    #[cfg(not(Py_LIMITED_API))]
294    impl !Ungil for crate::ffi::PyDictKeysObject {}
295    #[cfg(not(any(Py_LIMITED_API, Py_3_10)))]
296    impl !Ungil for crate::ffi::PyArena {}
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::with_gil`] to do that and pass your code as a closure to it.
324///
325/// The first two options are zero-cost; [`Python::with_gil`] 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::allow_threads`].
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 GILGuard, NotSend)>);
358
359impl Python<'_> {
360    /// Acquires the global interpreter lock, allowing access to the Python interpreter. The
361    /// provided closure `F` will be executed with the acquired `Python` marker token.
362    ///
363    /// If implementing [`#[pymethods]`](crate::pymethods) or [`#[pyfunction]`](crate::pyfunction),
364    /// declare `py: Python` as an argument. PyO3 will pass in the token to grant access to the GIL
365    /// context in which the function is running, avoiding the need to call `with_gil`.
366    ///
367    /// If the [`auto-initialize`] feature is enabled and the Python runtime is not already
368    /// initialized, this function will initialize it. See
369    #[cfg_attr(
370        not(any(PyPy, GraalPy)),
371        doc = "[`prepare_freethreaded_python`](crate::prepare_freethreaded_python)"
372    )]
373    #[cfg_attr(PyPy, doc = "`prepare_freethreaded_python`")]
374    /// for details.
375    ///
376    /// If the current thread does not yet have a Python "thread state" associated with it,
377    /// a new one will be automatically created before `F` is executed and destroyed after `F`
378    /// completes.
379    ///
380    /// # Panics
381    ///
382    /// - If the [`auto-initialize`] feature is not enabled and the Python interpreter is not
383    ///   initialized.
384    ///
385    /// # Examples
386    ///
387    /// ```
388    /// use pyo3::prelude::*;
389    /// use pyo3::ffi::c_str;
390    ///
391    /// # fn main() -> PyResult<()> {
392    /// Python::with_gil(|py| -> PyResult<()> {
393    ///     let x: i32 = py.eval(c_str!("5"), None, None)?.extract()?;
394    ///     assert_eq!(x, 5);
395    ///     Ok(())
396    /// })
397    /// # }
398    /// ```
399    ///
400    /// [`auto-initialize`]: https://pyo3.rs/main/features.html#auto-initialize
401    #[inline]
402    #[track_caller]
403    pub fn with_gil<F, R>(f: F) -> R
404    where
405        F: for<'py> FnOnce(Python<'py>) -> R,
406    {
407        let guard = GILGuard::acquire();
408
409        // SAFETY: Either the GIL was already acquired or we just created a new `GILGuard`.
410        f(guard.python())
411    }
412
413    /// Like [`Python::with_gil`] except Python interpreter state checking is skipped.
414    ///
415    /// Normally when the GIL is acquired, we check that the Python interpreter is an
416    /// appropriate state (e.g. it is fully initialized). This function skips those
417    /// checks.
418    ///
419    /// # Safety
420    ///
421    /// If [`Python::with_gil`] would succeed, it is safe to call this function.
422    ///
423    /// In most cases, you should use [`Python::with_gil`].
424    ///
425    /// A justified scenario for calling this function is during multi-phase interpreter
426    /// initialization when [`Python::with_gil`] would fail before
427    // this link is only valid on 3.8+not pypy and up.
428    #[cfg_attr(
429        all(Py_3_8, not(PyPy)),
430        doc = "[`_Py_InitializeMain`](crate::ffi::_Py_InitializeMain)"
431    )]
432    #[cfg_attr(any(not(Py_3_8), PyPy), doc = "`_Py_InitializeMain`")]
433    /// is called because the interpreter is only partially initialized.
434    ///
435    /// Behavior in other scenarios is not documented.
436    #[inline]
437    #[track_caller]
438    pub unsafe fn with_gil_unchecked<F, R>(f: F) -> R
439    where
440        F: for<'py> FnOnce(Python<'py>) -> R,
441    {
442        let guard = unsafe { GILGuard::acquire_unchecked() };
443
444        f(guard.python())
445    }
446}
447
448impl<'py> Python<'py> {
449    /// Temporarily releases the GIL, thus allowing other Python threads to run. The GIL will be
450    /// reacquired when `F`'s scope ends.
451    ///
452    /// If you don't need to touch the Python
453    /// interpreter for some time and have other Python threads around, this will let you run
454    /// Rust-only code while letting those other Python threads make progress.
455    ///
456    /// Only types that implement [`Ungil`] can cross the closure. See the
457    /// [module level documentation](self) for more information.
458    ///
459    /// If you need to pass Python objects into the closure you can use [`Py`]`<T>`to create a
460    /// reference independent of the GIL lifetime. However, you cannot do much with those without a
461    /// [`Python`] token, for which you'd need to reacquire the GIL.
462    ///
463    /// # Example: Releasing the GIL while running a computation in Rust-only code
464    ///
465    /// ```
466    /// use pyo3::prelude::*;
467    ///
468    /// #[pyfunction]
469    /// fn sum_numbers(py: Python<'_>, numbers: Vec<u32>) -> PyResult<u32> {
470    ///     // We release the GIL here so any other Python threads get a chance to run.
471    ///     py.allow_threads(move || {
472    ///         // An example of an "expensive" Rust calculation
473    ///         let sum = numbers.iter().sum();
474    ///
475    ///         Ok(sum)
476    ///     })
477    /// }
478    /// #
479    /// # fn main() -> PyResult<()> {
480    /// #     Python::with_gil(|py| -> PyResult<()> {
481    /// #         let fun = pyo3::wrap_pyfunction!(sum_numbers, py)?;
482    /// #         let res = fun.call1((vec![1_u32, 2, 3],))?;
483    /// #         assert_eq!(res.extract::<u32>()?, 6_u32);
484    /// #         Ok(())
485    /// #     })
486    /// # }
487    /// ```
488    ///
489    /// Please see the [Parallelism] chapter of the guide for a thorough discussion of using
490    /// [`Python::allow_threads`] in this manner.
491    ///
492    /// # Example: Passing borrowed Python references into the closure is not allowed
493    ///
494    /// ```compile_fail
495    /// use pyo3::prelude::*;
496    /// use pyo3::types::PyString;
497    ///
498    /// fn parallel_print(py: Python<'_>) {
499    ///     let s = PyString::new(py, "This object cannot be accessed without holding the GIL >_<");
500    ///     py.allow_threads(move || {
501    ///         println!("{:?}", s); // This causes a compile error.
502    ///     });
503    /// }
504    /// ```
505    ///
506    /// [`Py`]: crate::Py
507    /// [`PyString`]: crate::types::PyString
508    /// [auto-traits]: https://doc.rust-lang.org/nightly/unstable-book/language-features/auto-traits.html
509    /// [Parallelism]: https://pyo3.rs/main/parallelism.html
510    pub fn allow_threads<T, F>(self, f: F) -> T
511    where
512        F: Ungil + FnOnce() -> T,
513        T: Ungil,
514    {
515        // Use a guard pattern to handle reacquiring the GIL,
516        // so that the GIL will be reacquired even if `f` panics.
517        // The `Send` bound on the closure prevents the user from
518        // transferring the `Python` token into the closure.
519        let _guard = unsafe { SuspendGIL::new() };
520        f()
521    }
522
523    /// Evaluates a Python expression in the given context and returns the result.
524    ///
525    /// If `globals` is `None`, it defaults to Python module `__main__`.
526    /// If `locals` is `None`, it defaults to the value of `globals`.
527    ///
528    /// If `globals` doesn't contain `__builtins__`, default `__builtins__`
529    /// will be added automatically.
530    ///
531    /// # Examples
532    ///
533    /// ```
534    /// # use pyo3::prelude::*;
535    /// # use pyo3::ffi::c_str;
536    /// # Python::with_gil(|py| {
537    /// let result = py.eval(c_str!("[i * 10 for i in range(5)]"), None, None).unwrap();
538    /// let res: Vec<i64> = result.extract().unwrap();
539    /// assert_eq!(res, vec![0, 10, 20, 30, 40])
540    /// # });
541    /// ```
542    pub fn eval(
543        self,
544        code: &CStr,
545        globals: Option<&Bound<'py, PyDict>>,
546        locals: Option<&Bound<'py, PyDict>>,
547    ) -> PyResult<Bound<'py, PyAny>> {
548        self.run_code(code, ffi::Py_eval_input, globals, locals)
549    }
550
551    /// Executes one or more Python statements in the given context.
552    ///
553    /// If `globals` is `None`, it defaults to Python module `__main__`.
554    /// If `locals` is `None`, it defaults to the value of `globals`.
555    ///
556    /// If `globals` doesn't contain `__builtins__`, default `__builtins__`
557    /// will be added automatically.
558    ///
559    /// # Examples
560    /// ```
561    /// use pyo3::{
562    ///     prelude::*,
563    ///     types::{PyBytes, PyDict},
564    ///     ffi::c_str,
565    /// };
566    /// Python::with_gil(|py| {
567    ///     let locals = PyDict::new(py);
568    ///     py.run(c_str!(
569    ///         r#"
570    /// import base64
571    /// s = 'Hello Rust!'
572    /// ret = base64.b64encode(s.encode('utf-8'))
573    /// "#),
574    ///         None,
575    ///         Some(&locals),
576    ///     )
577    ///     .unwrap();
578    ///     let ret = locals.get_item("ret").unwrap().unwrap();
579    ///     let b64 = ret.downcast::<PyBytes>().unwrap();
580    ///     assert_eq!(b64.as_bytes(), b"SGVsbG8gUnVzdCE=");
581    /// });
582    /// ```
583    ///
584    /// You can use [`py_run!`](macro.py_run.html) for a handy alternative of `run`
585    /// if you don't need `globals` and unwrapping is OK.
586    pub fn run(
587        self,
588        code: &CStr,
589        globals: Option<&Bound<'py, PyDict>>,
590        locals: Option<&Bound<'py, PyDict>>,
591    ) -> PyResult<()> {
592        let res = self.run_code(code, ffi::Py_file_input, globals, locals);
593        res.map(|obj| {
594            debug_assert!(obj.is_none());
595        })
596    }
597
598    /// Runs code in the given context.
599    ///
600    /// `start` indicates the type of input expected: one of `Py_single_input`,
601    /// `Py_file_input`, or `Py_eval_input`.
602    ///
603    /// If `globals` is `None`, it defaults to Python module `__main__`.
604    /// If `locals` is `None`, it defaults to the value of `globals`.
605    fn run_code(
606        self,
607        code: &CStr,
608        start: c_int,
609        globals: Option<&Bound<'py, PyDict>>,
610        locals: Option<&Bound<'py, PyDict>>,
611    ) -> PyResult<Bound<'py, PyAny>> {
612        let mptr = unsafe {
613            ffi::compat::PyImport_AddModuleRef(ffi::c_str!("__main__").as_ptr())
614                .assume_owned_or_err(self)?
615        };
616        let attr = mptr.getattr(crate::intern!(self, "__dict__"))?;
617        let globals = match globals {
618            Some(globals) => globals,
619            None => attr.downcast::<PyDict>()?,
620        };
621        let locals = locals.unwrap_or(globals);
622
623        // If `globals` don't provide `__builtins__`, most of the code will fail if Python
624        // version is <3.10. That's probably not what user intended, so insert `__builtins__`
625        // for them.
626        //
627        // See also:
628        // - https://github.com/python/cpython/pull/24564 (the same fix in CPython 3.10)
629        // - https://github.com/PyO3/pyo3/issues/3370
630        let builtins_s = crate::intern!(self, "__builtins__");
631        let has_builtins = globals.contains(builtins_s)?;
632        if !has_builtins {
633            crate::sync::with_critical_section(globals, || {
634                // check if another thread set __builtins__ while this thread was blocked on the critical section
635                let has_builtins = globals.contains(builtins_s)?;
636                if !has_builtins {
637                    // Inherit current builtins.
638                    let builtins = unsafe { ffi::PyEval_GetBuiltins() };
639
640                    // `PyDict_SetItem` doesn't take ownership of `builtins`, but `PyEval_GetBuiltins`
641                    // seems to return a borrowed reference, so no leak here.
642                    if unsafe {
643                        ffi::PyDict_SetItem(globals.as_ptr(), builtins_s.as_ptr(), builtins)
644                    } == -1
645                    {
646                        return Err(PyErr::fetch(self));
647                    }
648                }
649                Ok(())
650            })?;
651        }
652
653        let code_obj = unsafe {
654            ffi::Py_CompileString(code.as_ptr(), ffi::c_str!("<string>").as_ptr(), start)
655                .assume_owned_or_err(self)?
656        };
657
658        unsafe {
659            ffi::PyEval_EvalCode(code_obj.as_ptr(), globals.as_ptr(), locals.as_ptr())
660                .assume_owned_or_err(self)
661                .downcast_into_unchecked()
662        }
663    }
664
665    /// Gets the Python type object for type `T`.
666    #[inline]
667    pub fn get_type<T>(self) -> Bound<'py, PyType>
668    where
669        T: PyTypeInfo,
670    {
671        T::type_object(self)
672    }
673
674    /// Imports the Python module with the specified name.
675    pub fn import<N>(self, name: N) -> PyResult<Bound<'py, PyModule>>
676    where
677        N: IntoPyObject<'py, Target = PyString>,
678    {
679        PyModule::import(self, name)
680    }
681
682    /// Gets the Python builtin value `None`.
683    #[allow(non_snake_case)] // the Python keyword starts with uppercase
684    #[inline]
685    pub fn None(self) -> PyObject {
686        PyNone::get(self).to_owned().into_any().unbind()
687    }
688
689    /// Gets the Python builtin value `Ellipsis`, or `...`.
690    #[allow(non_snake_case)] // the Python keyword starts with uppercase
691    #[inline]
692    pub fn Ellipsis(self) -> PyObject {
693        PyEllipsis::get(self).to_owned().into_any().unbind()
694    }
695
696    /// Gets the Python builtin value `NotImplemented`.
697    #[allow(non_snake_case)] // the Python keyword starts with uppercase
698    #[inline]
699    pub fn NotImplemented(self) -> PyObject {
700        PyNotImplemented::get(self).to_owned().into_any().unbind()
701    }
702
703    /// Gets the running Python interpreter version as a string.
704    ///
705    /// # Examples
706    /// ```rust
707    /// # use pyo3::Python;
708    /// Python::with_gil(|py| {
709    ///     // The full string could be, for example:
710    ///     // "3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)]"
711    ///     assert!(py.version().starts_with("3."));
712    /// });
713    /// ```
714    pub fn version(self) -> &'py str {
715        unsafe {
716            CStr::from_ptr(ffi::Py_GetVersion())
717                .to_str()
718                .expect("Python version string not UTF-8")
719        }
720    }
721
722    /// Gets the running Python interpreter version as a struct similar to
723    /// `sys.version_info`.
724    ///
725    /// # Examples
726    /// ```rust
727    /// # use pyo3::Python;
728    /// Python::with_gil(|py| {
729    ///     // PyO3 supports Python 3.7 and up.
730    ///     assert!(py.version_info() >= (3, 7));
731    ///     assert!(py.version_info() >= (3, 7, 0));
732    /// });
733    /// ```
734    pub fn version_info(self) -> PythonVersionInfo<'py> {
735        let version_str = self.version();
736
737        // Portion of the version string returned by Py_GetVersion up to the first space is the
738        // version number.
739        let version_number_str = version_str.split(' ').next().unwrap_or(version_str);
740
741        PythonVersionInfo::from_str(version_number_str).unwrap()
742    }
743
744    /// Lets the Python interpreter check and handle any pending signals. This will invoke the
745    /// corresponding signal handlers registered in Python (if any).
746    ///
747    /// Returns `Err(`[`PyErr`]`)` if any signal handler raises an exception.
748    ///
749    /// These signals include `SIGINT` (normally raised by CTRL + C), which by default raises
750    /// `KeyboardInterrupt`. For this reason it is good practice to call this function regularly
751    /// as part of long-running Rust functions so that users can cancel it.
752    ///
753    /// # Example
754    ///
755    /// ```rust,no_run
756    /// # #![allow(dead_code)] // this example is quite impractical to test
757    /// use pyo3::prelude::*;
758    ///
759    /// # fn main() {
760    /// #[pyfunction]
761    /// fn loop_forever(py: Python<'_>) -> PyResult<()> {
762    ///     loop {
763    ///         // As this loop is infinite it should check for signals every once in a while.
764    ///         // Using `?` causes any `PyErr` (potentially containing `KeyboardInterrupt`)
765    ///         // to break out of the loop.
766    ///         py.check_signals()?;
767    ///
768    ///         // do work here
769    ///         # break Ok(()) // don't actually loop forever
770    ///     }
771    /// }
772    /// # }
773    /// ```
774    ///
775    /// # Note
776    ///
777    /// This function calls [`PyErr_CheckSignals()`][1] which in turn may call signal handlers.
778    /// As Python's [`signal`][2] API allows users to define custom signal handlers, calling this
779    /// function allows arbitrary Python code inside signal handlers to run.
780    ///
781    /// If the function is called from a non-main thread, or under a non-main Python interpreter,
782    /// it does nothing yet still returns `Ok(())`.
783    ///
784    /// [1]: https://docs.python.org/3/c-api/exceptions.html?highlight=pyerr_checksignals#c.PyErr_CheckSignals
785    /// [2]: https://docs.python.org/3/library/signal.html
786    pub fn check_signals(self) -> PyResult<()> {
787        err::error_on_minusone(self, unsafe { ffi::PyErr_CheckSignals() })
788    }
789}
790
791impl<'unbound> Python<'unbound> {
792    /// Unsafely creates a Python token with an unbounded lifetime.
793    ///
794    /// Many of PyO3 APIs use `Python<'_>` as proof that the GIL is held, but this function can be
795    /// used to call them unsafely.
796    ///
797    /// # Safety
798    ///
799    /// - This token and any borrowed Python references derived from it can only be safely used
800    ///   whilst the currently executing thread is actually holding the GIL.
801    /// - This function creates a token with an *unbounded* lifetime. Safe code can assume that
802    ///   holding a `Python<'py>` token means the GIL is and stays acquired for the lifetime `'py`.
803    ///   If you let it or borrowed Python references escape to safe code you are
804    ///   responsible for bounding the lifetime `'unbound` appropriately. For more on unbounded
805    ///   lifetimes, see the [nomicon].
806    ///
807    /// [nomicon]: https://doc.rust-lang.org/nomicon/unbounded-lifetimes.html
808    #[inline]
809    pub unsafe fn assume_gil_acquired() -> Python<'unbound> {
810        Python(PhantomData)
811    }
812}
813
814#[cfg(test)]
815mod tests {
816    use super::*;
817    use crate::types::{IntoPyDict, PyList};
818
819    #[test]
820    fn test_eval() {
821        Python::with_gil(|py| {
822            // Make sure builtin names are accessible
823            let v: i32 = py
824                .eval(ffi::c_str!("min(1, 2)"), None, None)
825                .map_err(|e| e.display(py))
826                .unwrap()
827                .extract()
828                .unwrap();
829            assert_eq!(v, 1);
830
831            let d = [("foo", 13)].into_py_dict(py).unwrap();
832
833            // Inject our own global namespace
834            let v: i32 = py
835                .eval(ffi::c_str!("foo + 29"), Some(&d), None)
836                .unwrap()
837                .extract()
838                .unwrap();
839            assert_eq!(v, 42);
840
841            // Inject our own local namespace
842            let v: i32 = py
843                .eval(ffi::c_str!("foo + 29"), None, Some(&d))
844                .unwrap()
845                .extract()
846                .unwrap();
847            assert_eq!(v, 42);
848
849            // Make sure builtin names are still accessible when using a local namespace
850            let v: i32 = py
851                .eval(ffi::c_str!("min(foo, 2)"), None, Some(&d))
852                .unwrap()
853                .extract()
854                .unwrap();
855            assert_eq!(v, 2);
856        });
857    }
858
859    #[test]
860    #[cfg(not(target_arch = "wasm32"))] // We are building wasm Python with pthreads disabled
861    fn test_allow_threads_releases_and_acquires_gil() {
862        Python::with_gil(|py| {
863            let b = std::sync::Arc::new(std::sync::Barrier::new(2));
864
865            let b2 = b.clone();
866            std::thread::spawn(move || Python::with_gil(|_| b2.wait()));
867
868            py.allow_threads(|| {
869                // If allow_threads does not release the GIL, this will deadlock because
870                // the thread spawned above will never be able to acquire the GIL.
871                b.wait();
872            });
873
874            unsafe {
875                // If the GIL is not reacquired at the end of allow_threads, this call
876                // will crash the Python interpreter.
877                let tstate = ffi::PyEval_SaveThread();
878                ffi::PyEval_RestoreThread(tstate);
879            }
880        });
881    }
882
883    #[test]
884    fn test_allow_threads_panics_safely() {
885        Python::with_gil(|py| {
886            let result = std::panic::catch_unwind(|| unsafe {
887                let py = Python::assume_gil_acquired();
888                py.allow_threads(|| {
889                    panic!("There was a panic!");
890                });
891            });
892
893            // Check panic was caught
894            assert!(result.is_err());
895
896            // If allow_threads is implemented correctly, this thread still owns the GIL here
897            // so the following Python calls should not cause crashes.
898            let list = PyList::new(py, [1, 2, 3, 4]).unwrap();
899            assert_eq!(list.extract::<Vec<i32>>().unwrap(), vec![1, 2, 3, 4]);
900        });
901    }
902
903    #[cfg(not(pyo3_disable_reference_pool))]
904    #[test]
905    fn test_allow_threads_pass_stuff_in() {
906        let list = Python::with_gil(|py| PyList::new(py, vec!["foo", "bar"]).unwrap().unbind());
907        let mut v = vec![1, 2, 3];
908        let a = std::sync::Arc::new(String::from("foo"));
909
910        Python::with_gil(|py| {
911            py.allow_threads(|| {
912                drop((list, &mut v, a));
913            });
914        });
915    }
916
917    #[test]
918    #[cfg(not(Py_LIMITED_API))]
919    fn test_acquire_gil() {
920        const GIL_NOT_HELD: c_int = 0;
921        const GIL_HELD: c_int = 1;
922
923        // Before starting the interpreter the state of calling `PyGILState_Check`
924        // seems to be undefined, so let's ensure that Python is up.
925        #[cfg(not(any(PyPy, GraalPy)))]
926        crate::prepare_freethreaded_python();
927
928        let state = unsafe { crate::ffi::PyGILState_Check() };
929        assert_eq!(state, GIL_NOT_HELD);
930
931        Python::with_gil(|_| {
932            let state = unsafe { crate::ffi::PyGILState_Check() };
933            assert_eq!(state, GIL_HELD);
934        });
935
936        let state = unsafe { crate::ffi::PyGILState_Check() };
937        assert_eq!(state, GIL_NOT_HELD);
938    }
939
940    #[test]
941    fn test_ellipsis() {
942        Python::with_gil(|py| {
943            assert_eq!(py.Ellipsis().to_string(), "Ellipsis");
944
945            let v = py
946                .eval(ffi::c_str!("..."), None, None)
947                .map_err(|e| e.display(py))
948                .unwrap();
949
950            assert!(v.eq(py.Ellipsis()).unwrap());
951        });
952    }
953
954    #[test]
955    fn test_py_run_inserts_globals() {
956        use crate::types::dict::PyDictMethods;
957
958        Python::with_gil(|py| {
959            let namespace = PyDict::new(py);
960            py.run(
961                ffi::c_str!("class Foo: pass\na = int(3)"),
962                Some(&namespace),
963                Some(&namespace),
964            )
965            .unwrap();
966            assert!(matches!(namespace.get_item("Foo"), Ok(Some(..))));
967            assert!(matches!(namespace.get_item("a"), Ok(Some(..))));
968            // 3.9 and older did not automatically insert __builtins__ if it wasn't inserted "by hand"
969            #[cfg(not(Py_3_10))]
970            assert!(matches!(namespace.get_item("__builtins__"), Ok(Some(..))));
971        })
972    }
973
974    #[cfg(feature = "macros")]
975    #[test]
976    fn test_py_run_inserts_globals_2() {
977        use std::ffi::CString;
978
979        #[crate::pyclass(crate = "crate")]
980        #[derive(Clone)]
981        struct CodeRunner {
982            code: CString,
983        }
984
985        impl CodeRunner {
986            fn reproducer(&mut self, py: Python<'_>) -> PyResult<()> {
987                let variables = PyDict::new(py);
988                variables.set_item("cls", crate::Py::new(py, self.clone())?)?;
989
990                py.run(self.code.as_c_str(), Some(&variables), None)?;
991                Ok(())
992            }
993        }
994
995        #[crate::pymethods(crate = "crate")]
996        impl CodeRunner {
997            fn func(&mut self, py: Python<'_>) -> PyResult<()> {
998                py.import("math")?;
999                Ok(())
1000            }
1001        }
1002
1003        let mut runner = CodeRunner {
1004            code: CString::new(
1005                r#"
1006cls.func()
1007"#
1008                .to_string(),
1009            )
1010            .unwrap(),
1011        };
1012
1013        Python::with_gil(|py| {
1014            runner.reproducer(py).unwrap();
1015        });
1016    }
1017}