forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage_embed.rs
More file actions
30 lines (28 loc) · 1.09 KB
/
package_embed.rs
File metadata and controls
30 lines (28 loc) · 1.09 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
use rustpython_vm as vm;
use std::process::ExitCode;
use vm::{Interpreter, builtins::PyStrRef};
fn py_main(interp: &Interpreter) -> vm::PyResult<PyStrRef> {
interp.enter(|vm| {
// Add local library path
vm.insert_sys_path(vm.new_pyobj("examples"))
.expect("add examples to sys.path failed");
let module = vm.import("package_embed", 0)?;
let name_func = module.get_attr("context", vm)?;
let result = name_func.call((), vm)?;
let result: PyStrRef = result.get_attr("name", vm)?.try_into_value(vm)?;
vm::PyResult::Ok(result)
})
}
fn main() -> ExitCode {
// Add standard library path
let mut settings = vm::Settings::default();
settings.path_list.push("Lib".to_owned());
let builder = vm::Interpreter::builder(settings);
let defs = rustpython_stdlib::stdlib_module_defs(&builder.ctx);
let interp = builder.add_native_modules(&defs).build();
let result = py_main(&interp);
let result = result.map(|result| {
println!("name: {result}");
});
vm::common::os::exit_code(interp.run(|_vm| result))
}