Struct cpython::Python

source ·
pub struct Python<'p>(/* private fields */);
Expand description

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.

You can imagine the GIL to be a giant Mutex<PythonInterpreterState>. The type Python<'p> then acts like a reference &'p PythonInterpreterState.

Implementations§

source§

impl<'p> Python<'p>

source

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.

source

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.

source

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.

source

pub fn eval( self, code: &str, globals: Option<&PyDict>, locals: Option<&PyDict> ) -> PyResult<PyObject>

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.

source

pub fn run( self, code: &str, globals: Option<&PyDict>, locals: Option<&PyDict> ) -> PyResult<()>

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.

source

pub fn None(self) -> PyObject

Gets the Python builtin value None.

source

pub fn True(self) -> PyBool

Gets the Python builtin value True.

source

pub fn False(self) -> PyBool

Gets the Python builtin value False.

source

pub fn NotImplemented(self) -> PyObject

Gets the Python builtin value NotImplemented.

source

pub fn get_type<T>(self) -> PyType

Gets the Python type object for type T.

source

pub fn import(self, name: &str) -> PyResult<PyModule>

Import the Python module with the specified name.

Trait Implementations§

source§

impl<'p> Clone for Python<'p>

source§

fn clone(&self) -> Python<'p>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'p> Copy for Python<'p>

Auto Trait Implementations§

§

impl<'p> Freeze for Python<'p>

§

impl<'p> RefUnwindSafe for Python<'p>

§

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

§

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

§

impl<'p> Unpin for Python<'p>

§

impl<'p> UnwindSafe for Python<'p>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.