[][src]Struct inline_python::Context

pub struct Context { /* fields omitted */ }

An execution context for Python code.

If you pass a manually created context to the python!{} macro, you can share it across invocations. This will keep all global variables and imports intact between macro invocations.

let c = inline_python::Context::new();
python! {
  #![context = &c]
  foo = 5
}
python! {
  #![context = &c]
  assert foo == 5
}

You may also use it to inspect global variables after the execution of the Python code. Note that you need to acquire the GIL in order to access those globals:

use inline_python::python;
let context = inline_python::Context::new();
python! {
  #![context = &context]
  foo = 5
}

let foo: Option<i32> = context.get_global("foo").unwrap();
assert_eq!(foo, Some(5));

Methods

impl Context[src]

pub fn new() -> Self[src]

Create a new context for running python code.

This function temporarily acquires the GIL. If you already have the GIL, use Context::new_with_gil instead.

This function panics if it fails to create the context. See Context::new_checked for a version that returns a result.

pub fn new_checked() -> PyResult<Self>[src]

Create a new context for running python code.

This function temporarily acquires the GIL. If you already have the GIL, use Context::new_with_gil instead.

pub fn new_with_gil(py: Python) -> PyResult<Self>[src]

Create a new context for running Python code.

You must acquire the GIL to call this function.

pub fn globals<'p>(&self, py: Python<'p>) -> &'p PyDict[src]

Get the globals as dictionary.

pub fn get_global<T: for<'p> FromPyObject<'p>>(
    &self,
    name: &str
) -> PyResult<Option<T>>
[src]

Retrieve a global variable from the context.

This function temporarily acquires the GIL. If you already have the GIL, use Context::get_global_with_gil instead.

pub fn get_global_with_gil<'p, T: FromPyObject<'p>>(
    &self,
    py: Python<'p>,
    name: &str
) -> PyResult<Option<T>>
[src]

Retrieve a global variable from the context.

Auto Trait Implementations

impl Send for Context

impl Sync for Context

impl Unpin for Context

impl UnwindSafe for Context

impl RefUnwindSafe for Context

Blanket Implementations

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

impl<T> From<T> for T[src]

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.

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

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

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

impl<T, U> IntoPy<U> for T where
    U: FromPy<T>, 
[src]

impl<T> FromPy<T> for T[src]