Skip to content

Commit c5008bb

Browse files
Add import support to c-api
1 parent 0871bc8 commit c5008bb

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎crates/capi/src/import.rs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::{PyObject, pystate::with_vm};
2+
use rustpython_vm::builtins::PyStr;
3+
4+
#[unsafe(no_mangle)]
5+
pub unsafe extern "C" fn PyImport_Import(name: *mut PyObject) -> *mut PyObject {
6+
with_vm(|vm| {
7+
let name = unsafe { (&*name).try_downcast_ref::<PyStr>(vm)? };
8+
vm.import(name, 0)
9+
})
10+
}
11+
12+
#[cfg(false)]
13+
mod tests {
14+
use pyo3::prelude::*;
15+
16+
#[test]
17+
fn test_import() {
18+
Python::attach(|py| {
19+
let _module = py.import("sys").unwrap();
20+
})
21+
}
22+
}

‎crates/capi/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::sync::MutexGuard;
88

99
extern crate alloc;
1010

11+
pub mod import;
1112
pub mod object;
1213
pub mod pyerrors;
1314
pub mod pylifecycle;

0 commit comments

Comments
 (0)