Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions examples/plugin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use plugin_api::plugin_api as pylib_module;
use pyo3::prelude::*;
use pyo3::types::PyList;
use std::path::Path;

fn main() -> Result<(), Box<dyn std::error::Error>> {
//"export" our API module to the python runtime
pyo3::append_to_inittab!(pylib_module);
//spawn runtime
Python::initialize();
//import path for python
let path = Path::new("./python_plugin/");
let path = "./python_plugin/";
//do useful work
Python::attach(|py| {
//add the current directory to import path of Python (do not use this in production!)
let syspath: Bound<PyList> = py.import("sys")?.getattr("path")?.extract()?;
let syspath: Bound<PyList> = py.import("sys")?.getattr("path")?.extract().map_err(PyErr::from)?;
syspath.insert(0, &path)?;
println!("Import path is: {:?}", syspath);

Expand All @@ -28,7 +27,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
{
//this scope will have mutable access to the gadget instance, which will be dropped on
//scope exit so Python can access it again.
let mut gadget_rs: PyRefMut<'_, plugin_api::Gadget> = gadget.extract()?;
let mut gadget_rs: PyRefMut<'_, plugin_api::Gadget> = gadget.extract().map_err( PyErr::from)?;
// we can now modify it as if it was a native rust struct
gadget_rs.prop = 42;
//which includes access to rust-only fields that are not visible to python
Expand Down
1 change: 1 addition & 0 deletions newsfragments/5601.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updated plugin example to work with recent changes to library
Loading