Skip to content

Commit b0c5ba2

Browse files
committed
Update plugin example to work with recent changes
Path::into_pyobject now returns a pathlib.Path object which doesn't work as expected when added to sys.path so revert to string (see #4925). `impl FromPyObject for PyRefMut` now returns a custom error type (that doesn't impl `std::error::Error`) so map the error back the PyErr to return it (see #5413).
1 parent 5e35cc1 commit b0c5ba2

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

examples/plugin/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
use plugin_api::plugin_api as pylib_module;
22
use pyo3::prelude::*;
33
use pyo3::types::PyList;
4-
use std::path::Path;
54

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

@@ -28,7 +27,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2827
{
2928
//this scope will have mutable access to the gadget instance, which will be dropped on
3029
//scope exit so Python can access it again.
31-
let mut gadget_rs: PyRefMut<'_, plugin_api::Gadget> = gadget.extract()?;
30+
let mut gadget_rs: PyRefMut<'_, plugin_api::Gadget> = gadget.extract().map_err( PyErr::from)?;
3231
// we can now modify it as if it was a native rust struct
3332
gadget_rs.prop = 42;
3433
//which includes access to rust-only fields that are not visible to python

newsfragments/5601.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Updated plugin example to work with recent changes to library

0 commit comments

Comments
 (0)