to call an arbitrary Python string within Rust.
\nExample usage:
\nrun_py(\"print('hi')\", \"main.py\");How can I print the traceback when the program fails? There is no apparent way to turn a traceback to a rust string.
","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Figured it out! I iterated over e.args().to_vec() and called .str().unwrap().to_string() on all of them to see all the traceback messages.
-
|
When using RustPython for embedded scripting purposes, I want to be able to print out the traceback if the input fails. Currently I am using this function: use rustpython::vm;
pub fn run_py(code: &str, filename: &str) {
match vm::Interpreter::with_init(Default::default(), |vm| {
vm.add_frozen(rustpython_pylib::frozen_stdlib())
})
.enter(|vm| {
let scope = vm.new_scope_with_builtins();
let code_obj = vm
.compile(code, vm::compiler::Mode::Exec, String::from(filename))
.map_err(|err| vm.new_syntax_error(&err))?;
return vm.run_code_obj(code_obj, scope);
}) {
Ok(_) => {}
Err(e) => {
println!("{:?}", e.traceback().unwrap()); // !!!HERE!!!
}
};
}to call an arbitrary Python string within Rust. Example usage: run_py("print('hi')", "main.py");How can I print the traceback when the program fails? There is no apparent way to turn a traceback to a rust string. |
Beta Was this translation helpful? Give feedback.
-
|
Figured it out! I iterated over |
Beta Was this translation helpful? Give feedback.
Figured it out! I iterated over
e.args().to_vec()and called.str().unwrap().to_string()on all of them to see all the traceback messages.