Skip to content
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
7 changes: 1 addition & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,7 @@ fn parse_arguments<'a>(app: App<'a, '_>) -> ArgMatches<'a> {
fn add_stdlib(vm: &mut VirtualMachine) {
let _ = vm;
#[cfg(feature = "stdlib")]
{
let stdlib = rustpython_stdlib::get_module_inits();
for (name, init) in stdlib.into_iter() {
vm.add_native_module(name, init);
}
}
vm.add_native_modules(rustpython_stdlib::get_module_inits());
}

/// Create settings by examining command line arguments and environment
Expand Down
13 changes: 5 additions & 8 deletions stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,23 @@ mod termios;
use rustpython_common as common;
use rustpython_vm as vm;

use crate::vm::{
builtins,
stdlib::{StdlibInitFunc, StdlibMap},
};
use crate::vm::{builtins, stdlib::StdlibInitFunc};
use std::borrow::Cow;

pub fn get_module_inits() -> StdlibMap {
pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInitFunc)> {
macro_rules! modules {
{
$(
#[cfg($cfg:meta)]
{ $( $key:expr => $val:expr),* $(,)? }
)*
} => {{
let modules = [
[
$(
$(#[cfg($cfg)] (Cow::<'static, str>::from($key), Box::new($val) as StdlibInitFunc),)*
)*
];
modules.into_iter().collect()
]
.into_iter()
}};
}
modules! {
Expand Down
20 changes: 14 additions & 6 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,24 @@ impl VirtualMachine {
self.initialized = true;
}

fn state_mut(&mut self) -> &mut PyGlobalState {
PyRc::get_mut(&mut self.state)
.expect("there should not be multiple threads while a user has a mut ref to a vm")
}

/// Can only be used in the initialization closure passed to [`Interpreter::new_with_init`]
pub fn add_native_module<S>(&mut self, name: S, module: stdlib::StdlibInitFunc)
where
S: Into<Cow<'static, str>>,
{
let state = PyRc::get_mut(&mut self.state)
.expect("can't add_native_module when there are multiple threads");
state.module_inits.insert(name.into(), module);
self.state_mut().module_inits.insert(name.into(), module);
}

pub fn add_native_modules<I>(&mut self, iter: I)
where
I: IntoIterator<Item = (Cow<'static, str>, stdlib::StdlibInitFunc)>,
{
self.state_mut().module_inits.extend(iter);
}

/// Can only be used in the initialization closure passed to [`Interpreter::new_with_init`]
Expand All @@ -402,9 +412,7 @@ impl VirtualMachine {
I: IntoIterator<Item = (String, bytecode::FrozenModule)>,
{
let frozen = frozen::map_frozen(self, frozen).collect::<Vec<_>>();
let state = PyRc::get_mut(&mut self.state)
.expect("can't add_frozen when there are multiple threads");
state.frozen.extend(frozen);
self.state_mut().frozen.extend(frozen);
}

/// Start a new thread with access to the same interpreter.
Expand Down