[][src]Struct pyo3::python::Python

pub struct Python<'p>(_);

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]

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.

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.

Temporarily releases the GIL, thus allowing other Python threads to run.

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.

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.

Gets the Python type object for type T.

Import the Python module with the specified name.

Check whether obj is an instance of type T like Python isinstance function

Check whether type T is subclass of type U like Python issubclass function

Gets the Python builtin value None.

Gets the Python builtin value NotImplemented.

impl<'p> Python<'p>
[src]

Create new instance of T and move it under python management. Returns Py<T>.

Create new instance of T and move it under python management. Created object get registered in release pool. Returns references to T

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]

Register object in release pool, and try to downcast to specific type.

Register object in release pool, and do unchecked downcast to specific type.

Register ffi::PyObject pointer in release pool

Register ffi::PyObject pointer in release pool, and do unchecked downcast to specific type.

Register ffi::PyObject pointer in release pool, Do unchecked downcast to specific type. Returns mutable reference.

Register owned ffi::PyObject pointer in release pool. Returns Err(PyErr) if the pointer is null. do unchecked downcast to specific type.

Register owned ffi::PyObject pointer in release pool. Returns None if the pointer is null. do unchecked downcast to specific type.

Register borrowed ffi::PyObject pointer in release pool. Panics if the pointer is null. do unchecked downcast to specific type.

Register borrowed ffi::PyObject pointer in release pool. Panics if the pointer is null. do unchecked downcast to specific type.

Register borrowed ffi::PyObject pointer in release pool. Returns Err(PyErr) if the pointer is null. do unchecked downcast to specific type.

Register borrowed ffi::PyObject pointer in release pool. Returns None if the pointer is null. do unchecked downcast to specific T.

Release PyObject reference.

Release ffi::PyObject pointer.

Trait Implementations

impl<'p> Clone for Python<'p>
[src]

Performs copy-assignment from source. Read more

impl<'p> Copy for Python<'p>
[src]

Auto Trait Implementations

impl<'p> !Send for Python<'p>

impl<'p> !Sync for Python<'p>

Blanket Implementations

impl<T> From for T
[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> BorrowMut for T where
    T: ?Sized
[src]