Skip to content

Commit

Permalink
upgrade pyo3
Browse files Browse the repository at this point in the history
  • Loading branch information
mbway committed Nov 17, 2024
1 parent ab97c72 commit 0873398
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 154 deletions.
1 change: 1 addition & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ To update maturin:
test crates and update `uniffi-bindgen` in `requirements.txt` to match.
- check that no crates have been added to `test-crates` that should be excluded from the import hook tests.
If so, add them to `IGNORED_TEST_CRATES` in `common.py`
- upgrade the test packages in `test_import_hook/*_helpers`
- update the version check in the import hook to ensure it allows using the new version
- run the tests to ensure everything still works

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ fn get_num() -> usize { 10 }

#[pymodule]
fn my_script(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(get_num))?;
m.add_function(wrap_pyfunction!(get_num, m)?)?;
Ok(())
}
4 changes: 2 additions & 2 deletions tests/test_import_hook/file_importer_helpers/my_script_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn get_other_num() -> usize { 100 }

#[pymodule]
fn my_script(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(get_num))?;
m.add_wrapped(wrap_pyfunction!(get_other_num))?;
m.add_function(wrap_pyfunction!(get_num, m)?)?;
m.add_function(wrap_pyfunction!(get_other_num, m)?)?;
Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ fn get_num() -> usize {

#[pymodule]
fn my_script(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(get_num))?;
m.add_function(wrap_pyfunction!(get_num, m)?)?;
Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ pub fn do_something(a: usize, b: usize) -> PyResult<usize> {

#[pymodule]
pub fn my_rust_module(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(do_something))?;
m.add_function(wrap_pyfunction!(do_something, m)?)?;
Ok(())
}
20 changes: 10 additions & 10 deletions tests/test_import_hook/file_importer_helpers/reload_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Integer {
}

fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> PyResult<bool> {
let logging = PyModule::import_bound(py, "logging")?;
let logging = PyModule::import(py, "logging")?;
let message = format!(
"comparing Integer instances {} and {}",
self.name, other.name
Expand All @@ -66,7 +66,7 @@ impl PicklableInteger {
}

fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> PyResult<bool> {
let logging = PyModule::import_bound(py, "logging")?;
let logging = PyModule::import(py, "logging")?;
let message = format!(
"comparing PicklableInteger instances {} and {}",
self.name, other.name
Expand All @@ -89,35 +89,35 @@ fn get_str() -> String {
}

fn register_child_module(py: Python<'_>, parent_module: &Bound<'_, PyModule>) -> PyResult<()> {
let child_module = PyModule::new_bound(py, "child")?;
child_module.add_wrapped(wrap_pyfunction!(get_str))?;
let child_module = PyModule::new(py, "child")?;
child_module.add_function(wrap_pyfunction!(get_str, &child_module)?)?;
parent_module.add_submodule(&child_module)?;
Ok(())
}

#[pymodule]
fn my_module(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(get_num))?;
m.add_wrapped(wrap_pyfunction!(get_global_num))?;
m.add_wrapped(wrap_pyfunction!(set_global_num))?;
m.add_function(wrap_pyfunction!(get_num, m)?)?;
m.add_function(wrap_pyfunction!(get_global_num, m)?)?;
m.add_function(wrap_pyfunction!(set_global_num, m)?)?;
m.add_class::<Integer>()?;
m.add_class::<PicklableInteger>()?;

register_child_module(py, m)?;

let data = PyDict::new_bound(py);
let data = PyDict::new(py);
data.set_item("foo", 123)?;
m.add("data", data)?;

if !m.hasattr("data_init_once")? {
let data = PyDict::new_bound(py);
let data = PyDict::new(py);
data.set_item("foo", 123)?;
m.add("data_init_once", data)?;
}

m.add("data_str", "foo")?;

let logging = PyModule::import_bound(py, "logging")?;
let logging = PyModule::new(py, "logging")?;
logging
.getattr("info")?
.call1(("my_module extension module initialised",))?;
Expand Down
159 changes: 21 additions & 138 deletions tests/test_import_hook/project_importer_helpers/blank-project/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
description = ""

[dependencies]
pyo3 = { version = "0.21.2", features = ["extension-module"] }
pyo3 = { version = "0.23.1", features = ["extension-module"] }

[lib]
crate-type = ["cdylib"]

0 comments on commit 0873398

Please sign in to comment.