Skip to content

Interpreter config #4147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pylib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
// so build.rs sets this env var
pub const LIB_PATH: &str = match option_env!("win_lib_path") {
Some(s) => s,
None => concat!(env!("CARGO_MANIFEST_DIR"), "/../Lib"),
None => concat!(env!("CARGO_MANIFEST_DIR"), "/Lib"),
};

#[cfg(feature = "freeze-stdlib")]
pub fn frozen_stdlib() -> impl Iterator<Item = (String, rustpython_compiler_core::FrozenModule)> {
rustpython_derive::py_freeze!(dir = "../Lib", crate_name = "rustpython_compiler_core")
rustpython_derive::py_freeze!(dir = "./Lib", crate_name = "rustpython_compiler_core")
}
71 changes: 71 additions & 0 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use rustpython_vm::{Interpreter, Settings, VirtualMachine};

pub type InitHook = Box<dyn FnOnce(&mut VirtualMachine)>;

#[derive(Default)]
pub struct InterpreterConfig {
settings: Option<Settings>,
init_hooks: Vec<InitHook>,
}

impl InterpreterConfig {
pub fn new() -> Self {
Self::default()
}
pub fn interpreter(self) -> Interpreter {
let settings = self.settings.unwrap_or_default();
Interpreter::with_init(settings, |vm| {
for hook in self.init_hooks {
hook(vm);
}
})
}

pub fn settings(mut self, settings: Settings) -> Self {
self.settings = Some(settings);
self
}
pub fn init_hook(mut self, hook: InitHook) -> Self {
self.init_hooks.push(hook);
self
}
#[cfg(feature = "stdlib")]
pub fn init_stdlib(self) -> Self {
self.init_hook(Box::new(init_stdlib))
}
}

#[cfg(feature = "stdlib")]
pub fn init_stdlib(vm: &mut VirtualMachine) {
vm.add_native_modules(rustpython_stdlib::get_module_inits());

// if we're on freeze-stdlib, the core stdlib modules will be included anyway
#[cfg(feature = "freeze-stdlib")]
vm.add_frozen(rustpython_pylib::frozen_stdlib());

#[cfg(not(feature = "freeze-stdlib"))]
{
use rustpython_vm::common::rc::PyRc;

let state = PyRc::get_mut(&mut vm.state).unwrap();
let settings = &mut state.settings;

#[allow(clippy::needless_collect)] // false positive
let path_list: Vec<_> = settings.path_list.drain(..).collect();

// BUILDTIME_RUSTPYTHONPATH should be set when distributing
if let Some(paths) = option_env!("BUILDTIME_RUSTPYTHONPATH") {
settings.path_list.extend(
crate::settings::split_paths(paths)
.map(|path| path.into_os_string().into_string().unwrap()),
)
} else {
#[cfg(feature = "rustpython-pylib")]
settings
.path_list
.push(rustpython_pylib::LIB_PATH.to_owned())
}

settings.path_list.extend(path_list.into_iter());
}
}
Loading