Skip to content

Commit

Permalink
unsafe {Bound::from_owned_ptr(py, py_adders::__pyo3_init())} implemen…
Browse files Browse the repository at this point in the history
…ts ToPyObject
  • Loading branch information
MusicalNinjaDad committed Apr 21, 2024
1 parent a90919c commit 1be772f
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions tests/test_pyo3test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![cfg(all(feature = "testing", not(any(PyPy, GraalPy))))]
use pyo3::prelude::*;
use pyo3::{prelude::*, types::PyDict};

// The example from the Guide ...
fn o3_addone(num: isize) -> isize {
Expand All @@ -23,10 +23,13 @@ fn py_adders(module: &Bound<'_, PyModule>) -> PyResult<()> {
// adders.addone is correctly constructed.
#[test]
fn test_pyo3test_without_macro() {
pyo3::append_to_inittab!(py_adders);
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| {
let adders = py.import_bound("adders").expect("Failed to import adders");
let sys = PyModule::import_bound(py, "sys").unwrap();
let py_modules: Bound<'_, PyDict> = sys.getattr("modules").unwrap().downcast_into().unwrap();
let py_adders_pymodule = unsafe {Bound::from_owned_ptr(py, py_adders::__pyo3_init())};
py_modules.set_item("adders", py_adders_pymodule).expect("Failed to import adders");
let adders = py_modules.get_item("adders").unwrap().unwrap();
let addone = adders
.getattr("addone")
.expect("Failed to get addone function");
Expand Down

0 comments on commit 1be772f

Please sign in to comment.