Skip to content

Commit 85c7e2f

Browse files
Add stdlib to c-api
1 parent fe2a7db commit 85c7e2f

4 files changed

Lines changed: 24 additions & 6 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/capi/Cargo.toml‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ crate-type = ["cdylib", "rlib"]
1515
bitflags = { workspace = true }
1616
itertools = { workspace = true }
1717
num-complex = { workspace = true }
18-
rustpython-vm = { workspace = true, features = ["threading", "compiler"] }
18+
rustpython-vm = { workspace = true, features = ["threading", "compiler", "importlib", "host_env"] }
1919
rustpython-stdlib = {workspace = true, features = ["threading"] }
20+
rustpython-pylib = { workspace = true }
2021

2122
[dev-dependencies]
2223
pyo3 = { workspace = true, features = ["auto-initialize", "abi3"] }
2324

2425
[lints]
2526
workspace = true
26-
27-
[package.metadata.cargo-shear]
28-
# Not a direct dependency (yet), but we need to enable threading support in the stdlib.
29-
ignored = ["rustpython-stdlib"]

‎crates/capi/src/import.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@ mod tests {
1919
let _module = py.import("sys").unwrap();
2020
})
2121
}
22+
23+
#[test]
24+
fn import_stdlib() {
25+
Python::attach(|py| {
26+
let _module = py.import("types").unwrap();
27+
})
28+
}
2229
}

‎crates/capi/src/pylifecycle.rs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::get_main_interpreter;
22
use crate::pyerrors::init_exception_statics;
33
use crate::pystate::ensure_thread_has_vm_attached;
44
use core::ffi::c_int;
5+
use rustpython_vm::common::rc::PyRc;
56
use rustpython_vm::vm::thread::ThreadedVirtualMachine;
67
use rustpython_vm::{Context, Interpreter};
78
use std::sync::Mutex;
@@ -32,7 +33,19 @@ pub extern "C" fn Py_InitializeEx(_initsigs: c_int) {
3233
if interp.is_none() {
3334
// Safety: Interpreter was not initialized before, so we can safely assume the statics are not used
3435
unsafe { init_exception_statics(&Context::genesis().exceptions) };
35-
*interp = Interpreter::with_init(Default::default(), |_vm| {}).into();
36+
let builder = Interpreter::builder(Default::default());
37+
let defs = rustpython_stdlib::stdlib_module_defs(&builder.ctx);
38+
*interp = builder
39+
.add_native_modules(&defs)
40+
.init_hook(|vm| {
41+
let state = PyRc::get_mut(&mut vm.state).unwrap();
42+
let path = rustpython_pylib::LIB_PATH.to_owned();
43+
44+
state.config.paths.stdlib_dir = Some(path.clone());
45+
state.config.paths.module_search_paths.insert(0, path);
46+
})
47+
.build()
48+
.into();
3649
drop(interp);
3750
ensure_thread_has_vm_attached();
3851
}

0 commit comments

Comments
 (0)