Printing Traceback #4395
Answered
by
baris-inandi
baris-inandi
asked this question in
Q&A
-
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.
Answered by
baris-inandi
Dec 31, 2022
Replies: 1 comment
-
Figured it out! I iterated over |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
DimitrisJim
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.