[−][src]Struct inline_python::Context
An execution context for Python code.
This can be used to keep all global variables and imports intact between macro invocations:
let c = Context::new(); c.run(python! { foo = 5 }); c.run(python! { assert foo == 5 });
You may also use it to inspect global variables after the execution of the Python code, or set global variables before running:
let c = Context::new(); c.set("x", 13); c.run(python! { foo = x + 2 }); assert_eq!(c.get::<i32>("foo"), 15);
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, you can use Context::new_with_gil
instead.
This function panics if it fails to create the context.
pub fn new_with_gil(py: Python) -> Self
[src]
Create a new context for running Python code.
You must acquire the GIL to call this function.
This function panics if it fails to create the context.
pub fn globals<'p>(&'p self, py: Python<'p>) -> &'p PyDict
[src]
Get the globals as dictionary.
pub fn get<T: for<'p> FromPyObject<'p>>(&self, name: &str) -> T
[src]
Retrieve a global variable from the context.
This function temporarily acquires the GIL.
If you already have the GIL, you can use Context::get_with_gil
instead.
This function panics if the variable doesn't exist, or the conversion fails.
pub fn get_with_gil<'p, T: FromPyObject<'p>>(
&'p self,
py: Python<'p>,
name: &str
) -> T
[src]
&'p self,
py: Python<'p>,
name: &str
) -> T
Retrieve a global variable from the context.
This function panics if the variable doesn't exist, or the conversion fails.
pub fn set<T: ToPyObject>(&self, name: &str, value: T)
[src]
Set a global variable in the context.
This function temporarily acquires the GIL.
If you already have the GIL, you can use Context::set_with_gil
instead.
This function panics if the conversion fails.
pub fn set_with_gil<'p, T: ToPyObject>(
&self,
py: Python<'p>,
name: &str,
value: T
)
[src]
&self,
py: Python<'p>,
name: &str,
value: T
)
Set a global variable in the context.
This function panics if the conversion fails.
pub fn add_wrapped(&self, wrapper: &impl Fn(Python) -> PyObject)
[src]
Add a wrapped #[pyfunction] or #[pymodule] using its own __name__
.
Use this with pyo3::wrap_pyfunction
or pyo3::wrap_pymodule
.
use inline_python::pyo3::{prelude::*, wrap_pyfunction}; #[pyfunction] fn get_five() -> i32 { 5 } fn main() { let c = Context::new(); c.add_wrapped(wrap_pyfunction!(get_five)); c.run(python! { assert get_five() == 5 }); }
This function temporarily acquires the GIL.
If you already have the GIL, you can use Context::add_wrapped_with_gil
instead.
pub fn add_wrapped_with_gil<'p>(
&self,
py: Python<'p>,
wrapper: &impl Fn(Python) -> PyObject
)
[src]
&self,
py: Python<'p>,
wrapper: &impl Fn(Python) -> PyObject
)
Add a wrapped #[pyfunction] or #[pymodule] using its own __name__
.
See Context::add_wrapped.
pub fn run<F: FnOnce(&PyDict)>(&self, code: PythonBlock<F>)
[src]
Run Python code using this context.
This function should be called using the python!{}
macro:
let c = Context::new(); c.run(python!{ print("Hello World") });
This function temporarily acquires the GIL.
If you already have the GIL, you can use Context::run_with_gil
instead.
This function panics if the Python code fails.
pub fn run_with_gil<'p, F: FnOnce(&PyDict)>(
&self,
py: Python<'p>,
code: PythonBlock<F>
)
[src]
&self,
py: Python<'p>,
code: PythonBlock<F>
)
Run Python code using this context.
This function should be called using the python!{}
macro, just like
Context::run
.
This function panics if the Python code fails.
Auto Trait Implementations
impl !RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl UnwindSafe for Context
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> FromPy<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> IntoPy<U> for T where
U: FromPy<T>,
[src]
U: FromPy<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,