Skip to content

Commit 2ef64d2

Browse files
Add import support
1 parent 94cc39b commit 2ef64d2

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

crates/capi/src/import.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
use core::ptr;
2-
31
use crate::PyObject;
2+
use crate::pystate::with_vm;
3+
use rustpython_vm::builtins::PyStr;
44

55
#[unsafe(no_mangle)]
6-
pub extern "C" fn PyImport_Import(_name: *mut PyObject) -> *mut PyObject {
7-
crate::log_stub("PyImport_Import");
8-
ptr::null_mut()
6+
pub extern "C" fn PyImport_Import(name: *mut PyObject) -> *mut PyObject {
7+
with_vm(|vm| {
8+
let name = unsafe { (&*name).downcast_unchecked_ref::<PyStr>() };
9+
vm.import(name, 0).map_or_else(
10+
|err| {
11+
vm.push_exception(Some(err));
12+
std::ptr::null_mut()
13+
},
14+
|module| module.into_raw().as_ptr(),
15+
)
16+
})
917
}

example_projects/pyo3_embed/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ fn main() {
3939
PyTypeError::new_err("This is a type error").restore(py);
4040
assert!(PyErr::take(py).is_some());
4141

42+
py.import("sys")?;
43+
4244
PyResult::Ok(())
4345
})
4446
.unwrap();

0 commit comments

Comments
 (0)