Skip to content

Commit 6fd5094

Browse files
committed
Remove Interpreter::default() not to trap users init without stdlib
1 parent b74a5a6 commit 6fd5094

18 files changed

+104
-95
lines changed

benches/execution.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn bench_cpython_code(b: &mut Bencher, source: &str) {
2323

2424
fn bench_rustpy_code(b: &mut Bencher, name: &str, source: &str) {
2525
// NOTE: Take long time.
26-
Interpreter::default().enter(|vm| {
26+
Interpreter::without_stdlib(Default::default()).enter(|vm| {
2727
// Note: bench_cpython is both compiling and executing the code.
2828
// As such we compile the code in the benchmark loop as well.
2929
b.iter(|| {

benches/microbenchmarks.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use criterion::{
33
Criterion, Throughput,
44
};
55
use rustpython_compiler::Mode;
6-
use rustpython_vm::{common::ascii, InitParameter, Interpreter, PyResult, Settings};
6+
use rustpython_vm::{common::ascii, Interpreter, PyResult, Settings};
77
use std::{
88
ffi, fs, io,
99
path::{Path, PathBuf},
@@ -114,11 +114,10 @@ fn bench_rustpy_code(group: &mut BenchmarkGroup<WallTime>, bench: &MicroBenchmar
114114
settings.dont_write_bytecode = true;
115115
settings.no_user_site = true;
116116

117-
Interpreter::new_with_init(settings, |vm| {
117+
Interpreter::with_init(settings, |vm| {
118118
for (name, init) in rustpython_stdlib::get_module_inits().into_iter() {
119119
vm.add_native_module(name, init);
120120
}
121-
InitParameter::External
122121
})
123122
.enter(|vm| {
124123
let setup_code = vm

examples/freeze/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustpython_vm as vm;
22

33
fn main() -> vm::PyResult<()> {
4-
vm::Interpreter::default().enter(run)
4+
vm::Interpreter::without_stdlib(Default::default()).enter(run)
55
}
66

77
fn run(vm: &vm::VirtualMachine) -> vm::PyResult<()> {

examples/hello_embed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustpython_vm as vm;
22

33
fn main() -> vm::PyResult<()> {
4-
vm::Interpreter::default().enter(|vm| {
4+
vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
55
let scope = vm.new_scope_with_builtins();
66

77
let code_obj = vm

examples/mini_repl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn on(b: bool) {
2929
}
3030

3131
fn main() -> vm::PyResult<()> {
32-
vm::Interpreter::default().enter(run)
32+
vm::Interpreter::without_stdlib(Default::default()).enter(run)
3333
}
3434

3535
fn run(vm: &vm::VirtualMachine) -> vm::PyResult<()> {

src/lib.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use rustpython_vm::{
5151
match_class,
5252
scope::Scope,
5353
stdlib::{atexit, sys},
54-
AsObject, InitParameter, Interpreter, PyResult, Settings, VirtualMachine,
54+
AsObject, Interpreter, PyResult, Settings, VirtualMachine,
5555
};
5656
use std::{env, process, str::FromStr};
5757

@@ -83,10 +83,9 @@ where
8383
}
8484
}
8585

86-
let interp = Interpreter::new_with_init(settings, |vm| {
86+
let interp = Interpreter::with_init(settings, |vm| {
8787
add_stdlib(vm);
8888
init(vm);
89-
InitParameter::External
9089
});
9190

9291
let exitcode = interp.enter(move |vm| {
@@ -573,7 +572,7 @@ __import__("io").TextIOWrapper(
573572
.downcast()
574573
.expect("TextIOWrapper.read() should return str");
575574
eprintln!("running get-pip.py...");
576-
_run_string(vm, scope, getpip_code.as_str(), "get-pip.py".to_owned())
575+
vm.run_code_string(scope, getpip_code.as_str(), "get-pip.py".to_owned())
577576
}
578577

579578
#[cfg(not(feature = "ssl"))]
@@ -599,7 +598,7 @@ fn run_rustpython(vm: &VirtualMachine, matches: &ArgMatches) -> PyResult<()> {
599598
// Figure out if a -c option was given:
600599
if let Some(command) = matches.value_of("c") {
601600
debug!("Running command {}", command);
602-
vm.run_code_string(scope, &command, "<stdin>".to_owned())?;
601+
vm.run_code_string(scope, command, "<stdin>".to_owned())?;
603602
} else if let Some(module) = matches.value_of("m") {
604603
debug!("Running module {}", module);
605604
vm.run_module(module)?;
@@ -627,9 +626,8 @@ mod tests {
627626
use super::*;
628627

629628
fn interpreter() -> Interpreter {
630-
Interpreter::new_with_init(Settings::default(), |vm| {
629+
Interpreter::with_init(Settings::default(), |vm| {
631630
add_stdlib(vm);
632-
InitParameter::External
633631
})
634632
}
635633

vm/src/builtins/str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,7 @@ mod tests {
15591559

15601560
#[test]
15611561
fn str_maketrans_and_translate() {
1562-
Interpreter::default().enter(|vm| {
1562+
Interpreter::without_stdlib(Default::default()).enter(|vm| {
15631563
let table = vm.ctx.new_dict();
15641564
table
15651565
.set_item("a", vm.ctx.new_str("🎅").into(), &vm)

vm/src/dictdatatype.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ mod tests {
872872

873873
#[test]
874874
fn test_insert() {
875-
Interpreter::default().enter(|vm| {
875+
Interpreter::without_stdlib(Default::default()).enter(|vm| {
876876
let dict = Dict::default();
877877
assert_eq!(0, dict.len());
878878

@@ -921,7 +921,7 @@ mod tests {
921921
}
922922

923923
fn check_hash_equivalence(text: &str) {
924-
Interpreter::default().enter(|vm| {
924+
Interpreter::without_stdlib(Default::default()).enter(|vm| {
925925
let value1 = text;
926926
let value2 = vm.new_pyobj(value1.to_owned());
927927

vm/src/eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod tests {
2020

2121
#[test]
2222
fn test_print_42() {
23-
Interpreter::default().enter(|vm| {
23+
Interpreter::without_stdlib(Default::default()).enter(|vm| {
2424
let source = String::from("print('Hello world')");
2525
let vars = vm.new_scope_with_builtins();
2626
let result = eval(&vm, &source, vars, "<unittest>").expect("this should pass");

vm/src/import.rs

+51-41
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@ use crate::{
77
builtins::{code, code::CodeObject, list, traceback::PyTraceback, PyBaseExceptionRef},
88
scope::Scope,
99
version::get_git_revision,
10-
vm::{InitParameter, VirtualMachine},
10+
vm::{thread, VirtualMachine},
1111
AsObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
1212
};
1313
use rand::Rng;
1414

1515
pub(crate) fn init_importlib(
1616
vm: &mut VirtualMachine,
17-
initialize_parameter: InitParameter,
17+
allow_external_library: bool,
1818
) -> PyResult<()> {
19-
use crate::vm::thread::enter_vm;
19+
let importlib = init_importlib_base(vm)?;
20+
if allow_external_library && cfg!(feature = "rustpython-compiler") {
21+
init_importlib_package(vm, importlib)?;
22+
}
23+
Ok(())
24+
}
25+
26+
pub(crate) fn init_importlib_base(vm: &mut VirtualMachine) -> PyResult<PyObjectRef> {
2027
flame_guard!("init importlib");
2128

2229
// importlib_bootstrap needs these and it inlines checks to sys.modules before calling into
@@ -26,53 +33,56 @@ pub(crate) fn init_importlib(
2633
import_builtin(vm, "_warnings")?;
2734
import_builtin(vm, "_weakref")?;
2835

29-
let importlib = enter_vm(vm, || {
36+
let importlib = thread::enter_vm(vm, || {
3037
let importlib = import_frozen(vm, "_frozen_importlib")?;
3138
let impmod = import_builtin(vm, "_imp")?;
3239
let install = importlib.get_attr("_install", vm)?;
3340
vm.invoke(&install, (vm.sys_module.clone(), impmod))?;
3441
Ok(importlib)
3542
})?;
3643
vm.import_func = importlib.get_attr("__import__", vm)?;
44+
Ok(importlib)
45+
}
3746

38-
if initialize_parameter == InitParameter::External && cfg!(feature = "rustpython-compiler") {
39-
enter_vm(vm, || {
40-
flame_guard!("install_external");
41-
42-
// same deal as imports above
43-
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
44-
import_builtin(vm, crate::stdlib::os::MODULE_NAME)?;
45-
#[cfg(windows)]
46-
import_builtin(vm, "winreg")?;
47-
import_builtin(vm, "_io")?;
48-
import_builtin(vm, "marshal")?;
49-
50-
let install_external = importlib.get_attr("_install_external_importers", vm)?;
51-
vm.invoke(&install_external, ())?;
52-
// Set pyc magic number to commit hash. Should be changed when bytecode will be more stable.
53-
let importlib_external = vm.import("_frozen_importlib_external", None, 0)?;
54-
let mut magic = get_git_revision().into_bytes();
55-
magic.truncate(4);
56-
if magic.len() != 4 {
57-
magic = rand::thread_rng().gen::<[u8; 4]>().to_vec();
58-
}
59-
let magic: PyObjectRef = vm.ctx.new_bytes(magic).into();
60-
importlib_external.set_attr("MAGIC_NUMBER", magic, vm)?;
61-
let zipimport_res = (|| -> PyResult<()> {
62-
let zipimport = vm.import("zipimport", None, 0)?;
63-
let zipimporter = zipimport.get_attr("zipimporter", vm)?;
64-
let path_hooks = vm.sys_module.get_attr("path_hooks", vm)?;
65-
let path_hooks = list::PyListRef::try_from_object(vm, path_hooks)?;
66-
path_hooks.insert(0, zipimporter);
67-
Ok(())
68-
})();
69-
if zipimport_res.is_err() {
70-
warn!("couldn't init zipimport")
71-
}
47+
pub(crate) fn init_importlib_package(
48+
vm: &mut VirtualMachine,
49+
importlib: PyObjectRef,
50+
) -> PyResult<()> {
51+
thread::enter_vm(vm, || {
52+
flame_guard!("install_external");
53+
54+
// same deal as imports above
55+
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
56+
import_builtin(vm, crate::stdlib::os::MODULE_NAME)?;
57+
#[cfg(windows)]
58+
import_builtin(vm, "winreg")?;
59+
import_builtin(vm, "_io")?;
60+
import_builtin(vm, "marshal")?;
61+
62+
let install_external = importlib.get_attr("_install_external_importers", vm)?;
63+
vm.invoke(&install_external, ())?;
64+
// Set pyc magic number to commit hash. Should be changed when bytecode will be more stable.
65+
let importlib_external = vm.import("_frozen_importlib_external", None, 0)?;
66+
let mut magic = get_git_revision().into_bytes();
67+
magic.truncate(4);
68+
if magic.len() != 4 {
69+
magic = rand::thread_rng().gen::<[u8; 4]>().to_vec();
70+
}
71+
let magic: PyObjectRef = vm.ctx.new_bytes(magic).into();
72+
importlib_external.set_attr("MAGIC_NUMBER", magic, vm)?;
73+
let zipimport_res = (|| -> PyResult<()> {
74+
let zipimport = vm.import("zipimport", None, 0)?;
75+
let zipimporter = zipimport.get_attr("zipimporter", vm)?;
76+
let path_hooks = vm.sys_module.get_attr("path_hooks", vm)?;
77+
let path_hooks = list::PyListRef::try_from_object(vm, path_hooks)?;
78+
path_hooks.insert(0, zipimporter);
7279
Ok(())
73-
})?
74-
}
75-
Ok(())
80+
})();
81+
if zipimport_res.is_err() {
82+
warn!("couldn't init zipimport")
83+
}
84+
Ok(())
85+
})
7686
}
7787

7888
pub fn import_frozen(vm: &VirtualMachine, module_name: &str) -> PyResult {

vm/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub use self::convert::{TryFromBorrowedObject, TryFromObject};
8181
pub use self::object::{
8282
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyRefExact, PyResult, PyWeakRef,
8383
};
84-
pub use self::vm::{Context, InitParameter, Interpreter, Settings, VirtualMachine};
84+
pub use self::vm::{Context, Interpreter, Settings, VirtualMachine};
8585

8686
pub use rustpython_bytecode as bytecode;
8787
pub use rustpython_common as common;

vm/src/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ macro_rules! py_namespace {
7171
/// use rustpython_vm::builtins::{PyFloat, PyInt};
7272
/// use rustpython_vm::{PyPayload};
7373
///
74-
/// # rustpython_vm::Interpreter::default().enter(|vm| {
74+
/// # rustpython_vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
7575
/// let obj = PyInt::from(0).into_pyobject(vm);
7676
/// assert_eq!(
7777
/// "int",
@@ -95,7 +95,7 @@ macro_rules! py_namespace {
9595
/// use rustpython_vm::builtins::{PyFloat, PyInt};
9696
/// use rustpython_vm::{ PyPayload};
9797
///
98-
/// # rustpython_vm::Interpreter::default().enter(|vm| {
98+
/// # rustpython_vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
9999
/// let obj = PyInt::from(0).into_pyobject(vm);
100100
///
101101
/// let int_value = match_class!(match obj {

vm/src/prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ pub use crate::{
22
object::{
33
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyRefExact, PyResult, PyWeakRef,
44
},
5-
vm::{Context, InitParameter, Interpreter, Settings, VirtualMachine},
5+
vm::{Context, Interpreter, Settings, VirtualMachine},
66
};

vm/src/vm/interpreter.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{setting::Settings, thread, InitParameter, VirtualMachine};
1+
use super::{setting::Settings, thread, VirtualMachine};
22

33
/// The general interface for the VM
44
///
@@ -7,7 +7,7 @@ use super::{setting::Settings, thread, InitParameter, VirtualMachine};
77
/// ```
88
/// use rustpython_vm::Interpreter;
99
/// use rustpython_vm::compile::Mode;
10-
/// Interpreter::default().enter(|vm| {
10+
/// Interpreter::without_stdlib(Default::default()).enter(|vm| {
1111
/// let scope = vm.new_scope_with_builtins();
1212
/// let code_obj = vm.compile(r#"print("Hello World!")"#,
1313
/// Mode::Exec,
@@ -21,17 +21,28 @@ pub struct Interpreter {
2121
}
2222

2323
impl Interpreter {
24-
pub fn new(settings: Settings, init: InitParameter) -> Self {
25-
Self::new_with_init(settings, |_| init)
24+
/// To create with stdlib, use `with_init`
25+
pub fn without_stdlib(settings: Settings) -> Self {
26+
Self::with_init(settings, |_| {})
2627
}
2728

28-
pub fn new_with_init<F>(settings: Settings, init: F) -> Self
29+
/// Create with initialize function taking mutable vm reference.
30+
/// ```
31+
/// use rustpython_vm::Interpreter;
32+
/// Interpreter::with_init(Default::default(), |vm| {
33+
/// // put this line to add stdlib to the vm
34+
/// // vm.add_native_modules(rustpython_stdlib::get_module_inits());
35+
/// }).enter(|vm| {
36+
/// vm.run_code_string(vm.new_scope_with_builtins(), "print(1)", "<...>".to_owned());
37+
/// });
38+
/// ```
39+
pub fn with_init<F>(settings: Settings, init: F) -> Self
2940
where
30-
F: FnOnce(&mut VirtualMachine) -> InitParameter,
41+
F: FnOnce(&mut VirtualMachine),
3142
{
3243
let mut vm = VirtualMachine::new(settings);
33-
let init = init(&mut vm);
34-
vm.initialize(init);
44+
init(&mut vm);
45+
vm.initialize();
3546
Self { vm }
3647
}
3748

@@ -54,12 +65,6 @@ impl Interpreter {
5465
// pub fn shutdown(self) {}
5566
}
5667

57-
impl Default for Interpreter {
58-
fn default() -> Self {
59-
Self::new(Settings::default(), InitParameter::External)
60-
}
61-
}
62-
6368
#[cfg(test)]
6469
mod tests {
6570
use super::*;
@@ -71,7 +76,7 @@ mod tests {
7176

7277
#[test]
7378
fn test_add_py_integers() {
74-
Interpreter::default().enter(|vm| {
79+
Interpreter::without_stdlib(Default::default()).enter(|vm| {
7580
let a: PyObjectRef = vm.ctx.new_int(33_i32).into();
7681
let b: PyObjectRef = vm.ctx.new_int(12_i32).into();
7782
let res = vm._add(&a, &b).unwrap();
@@ -82,7 +87,7 @@ mod tests {
8287

8388
#[test]
8489
fn test_multiply_str() {
85-
Interpreter::default().enter(|vm| {
90+
Interpreter::without_stdlib(Default::default()).enter(|vm| {
8691
let a = vm.new_pyobj(crate::common::ascii!("Hello "));
8792
let b = vm.new_pyobj(4_i32);
8893
let res = vm._mul(&a, &b).unwrap();

0 commit comments

Comments
 (0)