Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Printing Traceback#4395

Answeredbybaris-inandi
baris-inandi asked this question inQ&A
Discussion options

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;pubfnrun_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.

You must be logged in to vote

Figured it out! I iterated overe.args().to_vec() and called.str().unwrap().to_string() on all of them to see all the traceback messages.

Replies: 1 comment

Comment options

Figured it out! I iterated overe.args().to_vec() and called.str().unwrap().to_string() on all of them to see all the traceback messages.

You must be logged in to vote
0 replies
Answer selected byDimitrisJim
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
1 participant
@baris-inandi
Converted from issue

This discussion was converted from issue #4393 on January 01, 2023 08:43.


[8]ページ先頭

©2009-2025 Movatter.jp