Skip to content

Commit cfc5316

Browse files
Load stdlib when calling Py_InitializeEx
1 parent eb35a01 commit cfc5316

3 files changed

Lines changed: 22 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: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ rustpython-stdlib = {workspace = true, features = ["threading"] }
1717

1818
[dev-dependencies]
1919
pyo3 = { version = "0.28", features = ["auto-initialize", "abi3"] }
20+
rustpython-pylib = { workspace = true, features = ["freeze-stdlib"] }
2021

2122
[lints]
2223
workspace = true
23-
24-
[package.metadata.cargo-shear]
25-
# Not a direct dependency (yet), but we need to enable threading support in the stdlib.
26-
ignored = ["rustpython-stdlib"]

crates/capi/src/pylifecycle.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::pyerrors::init_exception_statics;
33
use crate::pystate::ensure_thread_has_vm_attached;
44
use core::ffi::c_int;
55
use rustpython_vm::vm::thread::ThreadedVirtualMachine;
6-
use rustpython_vm::{Context, Interpreter};
6+
use rustpython_vm::{Context, Interpreter, Settings};
77
use std::sync::Mutex;
88

99
pub(crate) static MAIN_INTERP: Mutex<Option<Interpreter>> = Mutex::new(None);
@@ -32,7 +32,25 @@ pub extern "C" fn Py_InitializeEx(_initsigs: c_int) {
3232
if interp.is_none() {
3333
// Safety: Interpreter was not initialized before, so we can safely assume the statics are not used
3434
unsafe { init_exception_statics(&Context::genesis().exceptions) };
35-
*interp = Interpreter::with_init(Default::default(), |_vm| {}).into();
35+
36+
let settings = Settings::default();
37+
let mut builder = Interpreter::builder(settings);
38+
39+
let defs = rustpython_stdlib::stdlib_module_defs(&builder.ctx);
40+
builder = builder.add_native_modules(&defs);
41+
42+
#[cfg(test)]
43+
{
44+
use rustpython_vm::common::rc::PyRc;
45+
builder = builder
46+
.add_frozen_modules(rustpython_pylib::FROZEN_STDLIB)
47+
.init_hook(|vm| {
48+
let state = PyRc::get_mut(&mut vm.state).unwrap();
49+
state.config.paths.stdlib_dir = Some(rustpython_pylib::LIB_PATH.to_owned());
50+
});
51+
}
52+
53+
*interp = Some(builder.build());
3654
drop(interp);
3755
ensure_thread_has_vm_attached();
3856
}

0 commit comments

Comments
 (0)