Skip to content

Commit f622003

Browse files
committed
add test case for InitConfig::add_module
1 parent 80aca91 commit f622003

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,10 @@ required-features = ["macros"]
352352
name = "test_inheritance"
353353
required-features = ["macros"]
354354

355+
[[test]]
356+
name = "test_init_config_add_module"
357+
required-features = ["macros"]
358+
355359
[[test]]
356360
name = "test_intopyobject"
357361
required-features = ["macros"]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![cfg(all(Py_3_14, not(any(PyPy, GraalPy, RustPython, Py_LIMITED_API))))]
2+
#![cfg(feature = "macros")]
3+
#![allow(clippy::undocumented_unsafe_blocks, reason = "tests")]
4+
5+
use pyo3::add_module_to_init_config;
6+
use pyo3::init_config::InitConfig;
7+
use pyo3::prelude::*;
8+
9+
#[test]
10+
fn test_add_module() {
11+
let mut config = InitConfig::default();
12+
add_module_to_init_config!(config, m).unwrap();
13+
config.initialize().unwrap();
14+
Python::attach(|py| {
15+
let m = py.import("m").unwrap();
16+
let get_42 = m.getattr("get_42").unwrap();
17+
let forty_two = get_42.call0().unwrap().extract::<i32>().unwrap();
18+
assert_eq!(42, forty_two);
19+
});
20+
}
21+
22+
#[pymodule]
23+
mod m {
24+
use pyo3::prelude::*;
25+
26+
#[pyfunction]
27+
fn get_42() -> i32 {
28+
42
29+
}
30+
}

0 commit comments

Comments
 (0)