mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Load stdlib when calling Py_InitializeEx
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -3157,6 +3157,7 @@ name = "rustpython-capi"
|
||||
version = "0.5.0"
|
||||
dependencies = [
|
||||
"pyo3",
|
||||
"rustpython-pylib",
|
||||
"rustpython-stdlib",
|
||||
"rustpython-vm",
|
||||
]
|
||||
|
||||
@@ -17,10 +17,7 @@ rustpython-stdlib = {workspace = true, features = ["threading"] }
|
||||
|
||||
[dev-dependencies]
|
||||
pyo3 = { version = "0.28", features = ["auto-initialize", "abi3"] }
|
||||
rustpython-pylib = { workspace = true, features = ["freeze-stdlib"] }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[package.metadata.cargo-shear]
|
||||
# Not a direct dependency (yet), but we need to enable threading support in the stdlib.
|
||||
ignored = ["rustpython-stdlib"]
|
||||
@@ -3,7 +3,7 @@ use crate::pyerrors::init_exception_statics;
|
||||
use crate::pystate::ensure_thread_has_vm_attached;
|
||||
use core::ffi::c_int;
|
||||
use rustpython_vm::vm::thread::ThreadedVirtualMachine;
|
||||
use rustpython_vm::{Context, Interpreter};
|
||||
use rustpython_vm::{Context, Interpreter, Settings};
|
||||
use std::sync::Mutex;
|
||||
|
||||
pub(crate) static MAIN_INTERP: Mutex<Option<Interpreter>> = Mutex::new(None);
|
||||
@@ -32,7 +32,25 @@ pub extern "C" fn Py_InitializeEx(_initsigs: c_int) {
|
||||
if interp.is_none() {
|
||||
// Safety: Interpreter was not initialized before, so we can safely assume the statics are not used
|
||||
unsafe { init_exception_statics(&Context::genesis().exceptions) };
|
||||
*interp = Interpreter::with_init(Default::default(), |_vm| {}).into();
|
||||
|
||||
let settings = Settings::default();
|
||||
let mut builder = Interpreter::builder(settings);
|
||||
|
||||
let defs = rustpython_stdlib::stdlib_module_defs(&builder.ctx);
|
||||
builder = builder.add_native_modules(&defs);
|
||||
|
||||
#[cfg(test)]
|
||||
{
|
||||
use rustpython_vm::common::rc::PyRc;
|
||||
builder = builder
|
||||
.add_frozen_modules(rustpython_pylib::FROZEN_STDLIB)
|
||||
.init_hook(|vm| {
|
||||
let state = PyRc::get_mut(&mut vm.state).unwrap();
|
||||
state.config.paths.stdlib_dir = Some(rustpython_pylib::LIB_PATH.to_owned());
|
||||
});
|
||||
}
|
||||
|
||||
*interp = Some(builder.build());
|
||||
drop(interp);
|
||||
ensure_thread_has_vm_attached();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user