[−][src]Struct pyo3::python::Python
Marker type that indicates that the GIL is currently held.
The 'Python' struct is a zero-size marker struct that is required for most Python operations.
This is used to indicate that the operation accesses/modifies the Python interpreter state,
and thus can only be called if the Python interpreter is initialized and the
Python global interpreter lock (GIL) is acquired. The lifetime 'p
represents the lifetime of
the Python interpreter.
Note that the GIL can be temporarily released by the python interpreter during a function call (e.g. importing a module), even when you're holding a GILGuard. In general, you don't need to worry about this becauseas the GIL is reaquired before returning to the rust code:
GILGuard |=====================================|
GIL actually held |==========| |================|
Rust code running |=======| |==| |======|
This behaviour can cause deadlocks when trying to lock while holding a GILGuard:
- Thread 1 acquires the GIL
- Thread 1 locks a mutex
- Thread 1 makes a call into the python interpreter, which releases the GIL
- Thread 2 acquires the GIL
- Thraed 2 tries to locks the mutex, blocks
- Thread 1's python interpreter call blocks trying to reacquire the GIL held by thread 2
To avoid deadlocking, you should release the GIL before trying to lock a mutex, e.g. with Python::allow_threads.
Methods
impl<'p> Python<'p>
[src]
impl<'p> Python<'p>
pub unsafe fn assume_gil_acquired() -> Python<'p>
[src]
pub unsafe fn assume_gil_acquired() -> Python<'p>
Retrieve Python instance under the assumption that the GIL is already acquired at this point,
and stays acquired for the lifetime 'p
.
Because the output lifetime 'p
is not connected to any input parameter,
care must be taken that the compiler infers an appropriate lifetime for 'p
when calling this function.
pub fn acquire_gil() -> GILGuard
[src]
pub fn acquire_gil() -> GILGuard
Acquires the global interpreter lock, which allows access to the Python runtime.
If the Python runtime is not already initialized, this function will initialize it. See prepare_freethreaded_python() for details.
pub fn allow_threads<T, F>(self, f: F) -> T where
F: Send + FnOnce() -> T,
[src]
pub fn allow_threads<T, F>(self, f: F) -> T where
F: Send + FnOnce() -> T,
Temporarily releases the GIL
, thus allowing other Python threads to run.
pub fn eval(
self,
code: &str,
globals: Option<&PyDict>,
locals: Option<&PyDict>
) -> PyResult<&'p PyObjectRef>
[src]
pub fn eval(
self,
code: &str,
globals: Option<&PyDict>,
locals: Option<&PyDict>
) -> PyResult<&'p PyObjectRef>
Evaluates a Python expression in the given context and returns the result.
If globals
is None
, it defaults to Python module __main__
.
If locals
is None
, it defaults to the value of globals
.
pub fn run(
self,
code: &str,
globals: Option<&PyDict>,
locals: Option<&PyDict>
) -> PyResult<&'p PyObjectRef>
[src]
pub fn run(
self,
code: &str,
globals: Option<&PyDict>,
locals: Option<&PyDict>
) -> PyResult<&'p PyObjectRef>
Executes one or more Python statements in the given context.
If globals
is None
, it defaults to Python module __main__
.
If locals
is None
, it defaults to the value of globals
.
pub fn get_type<T>(self) -> &'p PyType where
T: PyTypeObject,
[src]
pub fn get_type<T>(self) -> &'p PyType where
T: PyTypeObject,
Gets the Python type object for type T
.
pub fn import(self, name: &str) -> PyResult<&'p PyModule>
[src]
pub fn import(self, name: &str) -> PyResult<&'p PyModule>
Import the Python module with the specified name.
pub fn is_instance<T: PyTypeObject, V: ToPyPointer>(
self,
obj: &V
) -> PyResult<bool>
[src]
pub fn is_instance<T: PyTypeObject, V: ToPyPointer>(
self,
obj: &V
) -> PyResult<bool>
Check whether obj
is an instance of type T
like Python isinstance
function
pub fn is_subclass<T, U>(self) -> PyResult<bool> where
T: PyTypeObject,
U: PyTypeObject,
[src]
pub fn is_subclass<T, U>(self) -> PyResult<bool> where
T: PyTypeObject,
U: PyTypeObject,
Check whether type T
is subclass of type U
like Python issubclass
function
pub fn None(self) -> PyObject
[src]
pub fn None(self) -> PyObject
Gets the Python builtin value None
.
pub fn NotImplemented(self) -> PyObject
[src]
pub fn NotImplemented(self) -> PyObject
Gets the Python builtin value NotImplemented
.
impl<'p> Python<'p>
[src]
impl<'p> Python<'p>
pub fn init<T, F>(self, f: F) -> PyResult<Py<T>> where
F: FnOnce() -> T,
T: PyTypeCreate,
[src]
pub fn init<T, F>(self, f: F) -> PyResult<Py<T>> where
F: FnOnce() -> T,
T: PyTypeCreate,
Create new instance of T
and move it under python management.
Returns Py<T>
.
pub fn init_ref<T, F>(self, f: F) -> PyResult<&'p T> where
F: FnOnce() -> T,
T: PyTypeCreate,
[src]
pub fn init_ref<T, F>(self, f: F) -> PyResult<&'p T> where
F: FnOnce() -> T,
T: PyTypeCreate,
Create new instance of T
and move it under python management.
Created object get registered in release pool. Returns references to T
pub fn init_mut<T, F>(self, f: F) -> PyResult<&'p mut T> where
F: FnOnce() -> T,
T: PyTypeCreate,
[src]
pub fn init_mut<T, F>(self, f: F) -> PyResult<&'p mut T> where
F: FnOnce() -> T,
T: PyTypeCreate,
Create new instance of T
and move it under python management.
Created object get registered in release pool. Returns mutable references to T
impl<'p> Python<'p>
[src]
impl<'p> Python<'p>
pub fn checked_cast_as<T>(self, obj: PyObject) -> Result<&'p T, PyDowncastError> where
T: PyTypeInfo,
[src]
pub fn checked_cast_as<T>(self, obj: PyObject) -> Result<&'p T, PyDowncastError> where
T: PyTypeInfo,
Register object in release pool, and try to downcast to specific type.
pub unsafe fn cast_as<T>(self, obj: PyObject) -> &'p T where
T: PyTypeInfo,
[src]
pub unsafe fn cast_as<T>(self, obj: PyObject) -> &'p T where
T: PyTypeInfo,
Register object in release pool, and do unchecked downcast to specific type.
pub unsafe fn from_borrowed_ptr_to_obj(
self,
ptr: *mut PyObject
) -> &'p PyObjectRef
[src]
pub unsafe fn from_borrowed_ptr_to_obj(
self,
ptr: *mut PyObject
) -> &'p PyObjectRef
Register ffi::PyObject
pointer in release pool
pub unsafe fn from_owned_ptr<T>(self, ptr: *mut PyObject) -> &'p T where
T: PyTypeInfo,
[src]
pub unsafe fn from_owned_ptr<T>(self, ptr: *mut PyObject) -> &'p T where
T: PyTypeInfo,
Register ffi::PyObject
pointer in release pool,
and do unchecked downcast to specific type.
pub unsafe fn mut_from_owned_ptr<T>(self, ptr: *mut PyObject) -> &'p mut T where
T: PyTypeInfo,
[src]
pub unsafe fn mut_from_owned_ptr<T>(self, ptr: *mut PyObject) -> &'p mut T where
T: PyTypeInfo,
Register ffi::PyObject
pointer in release pool,
Do unchecked downcast to specific type. Returns mutable reference.
pub unsafe fn from_owned_ptr_or_err<T>(
self,
ptr: *mut PyObject
) -> PyResult<&'p T> where
T: PyTypeInfo,
[src]
pub unsafe fn from_owned_ptr_or_err<T>(
self,
ptr: *mut PyObject
) -> PyResult<&'p T> where
T: PyTypeInfo,
Register owned ffi::PyObject
pointer in release pool.
Returns Err(PyErr)
if the pointer is null
.
do unchecked downcast to specific type.
pub unsafe fn from_owned_ptr_or_opt<T>(
self,
ptr: *mut PyObject
) -> Option<&'p T> where
T: PyTypeInfo,
[src]
pub unsafe fn from_owned_ptr_or_opt<T>(
self,
ptr: *mut PyObject
) -> Option<&'p T> where
T: PyTypeInfo,
Register owned ffi::PyObject
pointer in release pool.
Returns None
if the pointer is null
.
do unchecked downcast to specific type.
pub unsafe fn from_borrowed_ptr<T>(self, ptr: *mut PyObject) -> &'p T where
T: PyTypeInfo,
[src]
pub unsafe fn from_borrowed_ptr<T>(self, ptr: *mut PyObject) -> &'p T where
T: PyTypeInfo,
Register borrowed ffi::PyObject
pointer in release pool.
Panics if the pointer is null
.
do unchecked downcast to specific type.
pub unsafe fn mut_from_borrowed_ptr<T>(self, ptr: *mut PyObject) -> &'p mut T where
T: PyTypeInfo,
[src]
pub unsafe fn mut_from_borrowed_ptr<T>(self, ptr: *mut PyObject) -> &'p mut T where
T: PyTypeInfo,
Register borrowed ffi::PyObject
pointer in release pool.
Panics if the pointer is null
.
do unchecked downcast to specific type.
pub unsafe fn from_borrowed_ptr_or_err<T>(
self,
ptr: *mut PyObject
) -> PyResult<&'p T> where
T: PyTypeInfo,
[src]
pub unsafe fn from_borrowed_ptr_or_err<T>(
self,
ptr: *mut PyObject
) -> PyResult<&'p T> where
T: PyTypeInfo,
Register borrowed ffi::PyObject
pointer in release pool.
Returns Err(PyErr)
if the pointer is null
.
do unchecked downcast to specific type.
pub unsafe fn from_borrowed_ptr_or_opt<T>(
self,
ptr: *mut PyObject
) -> Option<&'p T> where
T: PyTypeInfo,
[src]
pub unsafe fn from_borrowed_ptr_or_opt<T>(
self,
ptr: *mut PyObject
) -> Option<&'p T> where
T: PyTypeInfo,
Register borrowed ffi::PyObject
pointer in release pool.
Returns None
if the pointer is null
.
do unchecked downcast to specific T
.
pub fn release<T>(self, ob: T) where
T: IntoPyPointer,
[src]
pub fn release<T>(self, ob: T) where
T: IntoPyPointer,
Release PyObject reference.
pub fn xdecref<T: IntoPyPointer>(self, ptr: T)
[src]
pub fn xdecref<T: IntoPyPointer>(self, ptr: T)
Release ffi::PyObject
pointer.
Trait Implementations
Auto Trait Implementations
Blanket Implementations
impl<T> From for T
[src]
impl<T> From for T
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
impl<T, U> Into for T where
U: From<T>,
[src]
impl<T, U> Into for T where
U: From<T>,
impl<T, U> TryFrom for T where
T: From<U>,
[src]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T> Borrow for T where
T: ?Sized,
[src]
impl<T> Borrow for T where
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
impl<T> BorrowMut for T where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
fn borrow_mut(&mut self) -> &mut T
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T> Any for T where
T: 'static + ?Sized,
[src]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
fn get_type_id(&self) -> TypeId