##// END OF EJS Templates
rust-pyo3: getting rust-cpython GIL handle from various PyO3 objects...
Georges Racinet -
r53425:1dd673c1 default
parent child Browse files
Show More
@@ -11,6 +11,7
11 //! the arguments side of function signatures when they are not simply elided.
11 //! the arguments side of function signatures when they are not simply elided.
12 use pyo3::exceptions::PyTypeError;
12 use pyo3::exceptions::PyTypeError;
13 use pyo3::prelude::*;
13 use pyo3::prelude::*;
14 use pyo3::{pyclass::boolean_struct::False, PyClass};
14
15
15 use cpython::ObjectProtocol;
16 use cpython::ObjectProtocol;
16 use cpython::PythonObject;
17 use cpython::PythonObject;
@@ -19,6 +20,23 use lazy_static::lazy_static;
19 use hg::revlog::index::Index as CoreIndex;
20 use hg::revlog::index::Index as CoreIndex;
20 use rusthg::revlog::{InnerRevlog, PySharedIndex};
21 use rusthg::revlog::{InnerRevlog, PySharedIndex};
21
22
23 /// Marker trait for PyO3 objects with a lifetime representing the acquired GIL
24 ///
25 /// # Safety
26 ///
27 /// This trait must not be implemented for objects with lifetimes that
28 /// do not imply in PyO3 that the GIL is acquired during the whole lifetime.
29 pub unsafe trait WithGIL<'py> {}
30
31 // Safety: the lifetime on these PyO3 objects all represent the acquired GIL
32 unsafe impl<'py> WithGIL<'py> for Python<'py> {}
33 unsafe impl<'py, T> WithGIL<'py> for Bound<'py, T> {}
34 unsafe impl<'py, T: PyClass> WithGIL<'py> for PyRef<'py, T> {}
35 unsafe impl<'py, T: PyClass<Frozen = False>> WithGIL<'py>
36 for PyRefMut<'py, T>
37 {
38 }
39
22 /// Force cpython's GIL handle with the appropriate lifetime
40 /// Force cpython's GIL handle with the appropriate lifetime
23 ///
41 ///
24 /// In `pyo3`, the fact that we have the GIL is expressed by the lifetime of
42 /// In `pyo3`, the fact that we have the GIL is expressed by the lifetime of
@@ -31,10 +49,11 use rusthg::revlog::{InnerRevlog, PyShar
31 /// already has it works) *as long as it is properly released*
49 /// already has it works) *as long as it is properly released*
32 /// reference:
50 /// reference:
33 /// <https://docs.python.org/3.8/c-api/init.html#c.PyGILState_Ensure>
51 /// <https://docs.python.org/3.8/c-api/init.html#c.PyGILState_Ensure>
34 pub(crate) fn cpython_handle<'py, T>(
52 pub(crate) fn cpython_handle<'py, T: WithGIL<'py>>(
35 _bound: &Bound<'py, T>,
53 _with_gil: &T,
36 ) -> cpython::Python<'py> {
54 ) -> cpython::Python<'py> {
37 // safety: this is safe because the returned object has the 'py lifetime
55 // safety: this is safe because the returned object has the same lifetime
56 // as the incoming object.
38 unsafe { cpython::Python::assume_gil_acquired() }
57 unsafe { cpython::Python::assume_gil_acquired() }
39 }
58 }
40
59
General Comments 0
You need to be logged in to leave comments. Login now