Skip to content

Commit 4ba2892

Browse files
youknowonefanninpm
andauthored
Better tips for Interpreter & InterpreterConfig (RustPython#5047)
Co-authored-by: fanninpm <[email protected]>
1 parent aee68d2 commit 4ba2892

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/interpreter.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,40 @@ use rustpython_vm::{Interpreter, Settings, VirtualMachine};
22

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

5+
/// The convenient way to create [rustpython_vm::Interpreter] with stdlib and other stuffs.
6+
///
7+
/// Basic usage:
8+
/// ```
9+
/// let interpreter = rustpython::InterpreterConfig::new()
10+
/// .init_stdlib()
11+
/// .interpreter();
12+
/// ```
13+
///
14+
/// To override [rustpython_vm::Settings]:
15+
/// ```
16+
/// use rustpython_vm::Settings;
17+
/// // Override your settings here.
18+
/// let mut settings = Settings::default();
19+
/// settings.debug = true;
20+
/// // You may want to add paths to `rustpython_vm::Settings::path_list` to allow import python libraries.
21+
/// settings.path_list.push("".to_owned()); // add current working directory
22+
/// let interpreter = rustpython::InterpreterConfig::new()
23+
/// .settings(settings)
24+
/// .interpreter();
25+
/// ```
26+
///
27+
/// To add native modules:
28+
/// ```compile_fail
29+
/// let interpreter = rustpython::InterpreterConfig::new()
30+
/// .init_stdlib()
31+
/// .init_hook(Box::new(|vm| {
32+
/// vm.add_native_module(
33+
/// "your_module_name".to_owned(),
34+
/// Box::new(your_module::make_module),
35+
/// );
36+
/// }))
37+
/// .interpreter();
38+
/// ```
539
#[derive(Default)]
640
pub struct InterpreterConfig {
741
settings: Option<Settings>,

vm/src/vm/interpreter.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ pub struct Interpreter {
2828
}
2929

3030
impl Interpreter {
31-
/// To create with stdlib, use `with_init`
31+
/// This is a bare unit to build up an interpreter without the standard library.
32+
/// To create an interpreter with the standard library with the `rustpython` crate, use `rustpython::InterpreterConfig`.
33+
/// To create an interpreter without the `rustpython` crate, but only with `rustpython-vm`,
34+
/// try to build one from the source code of `InterpreterConfig`. It will not be a one-liner but it also will not be too hard.
3235
pub fn without_stdlib(settings: Settings) -> Self {
3336
Self::with_init(settings, |_| {})
3437
}

0 commit comments

Comments
 (0)