-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathmain.rs
More file actions
39 lines (34 loc) · 1.37 KB
/
main.rs
File metadata and controls
39 lines (34 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// spell-checker:ignore aheui
//! Setting up a project with a frozen stdlib can be done *either* by using `rustpython::InterpreterBuilder` or `rustpython_vm::Interpreter::builder`.
//! See each function for example.
//!
//! See also: `aheui-rust.md` for freezing your own package.
use rustpython::InterpreterBuilderExt;
use rustpython_vm::{PyResult, VirtualMachine};
fn run(keyword: &str, vm: &VirtualMachine) -> PyResult<()> {
let json = vm.import("json", 0)?;
let json_loads = json.get_attr("loads", vm)?;
let template = r#"{"key": "value"}"#;
let json_string = template.replace("value", keyword);
let dict = json_loads.call((vm.ctx.new_str(json_string),), vm)?;
vm.print((dict,))?;
Ok(())
}
fn interpreter_with_config() {
let interpreter = rustpython::InterpreterBuilder::new()
.init_stdlib()
.interpreter();
// Use interpreter.enter to reuse the same interpreter later
interpreter.run(|vm| run("rustpython::InterpreterBuilder", vm));
}
fn interpreter_with_vm() {
let interpreter = rustpython_vm::Interpreter::builder(Default::default())
.add_frozen_modules(rustpython_pylib::FROZEN_STDLIB)
.build();
// Use interpreter.enter to reuse the same interpreter later
interpreter.run(|vm| run("rustpython_vm::Interpreter::builder", vm));
}
fn main() {
interpreter_with_config();
interpreter_with_vm();
}