@@ -11,7 +11,7 @@ use clap::{App, Arg};
1111use rustpython_parser:: parser;
1212use rustpython_vm:: obj:: objstr;
1313use rustpython_vm:: print_exception;
14- use rustpython_vm:: pyobject:: PyObjectRef ;
14+ use rustpython_vm:: pyobject:: { PyObjectRef , PyResult } ;
1515use rustpython_vm:: VirtualMachine ;
1616use rustpython_vm:: { compile, import} ;
1717use std:: io;
@@ -45,67 +45,62 @@ fn main() {
4545 )
4646 . get_matches ( ) ;
4747
48+ // Construct vm:
49+ let mut vm = VirtualMachine :: new ( ) ;
50+
4851 // Figure out if a -c option was given:
49- if let Some ( command) = matches. value_of ( "c" ) {
50- run_command ( & mut command. to_string ( ) ) ;
52+ let result = if let Some ( command) = matches. value_of ( "c" ) {
53+ run_command ( & mut vm , command. to_string ( ) )
5154 } else if let Some ( module) = matches. value_of ( "m" ) {
52- run_module ( module) ;
55+ run_module ( & mut vm , module)
5356 } else {
5457 // Figure out if a script was passed:
5558 match matches. value_of ( "script" ) {
56- None => run_shell ( ) ,
57- Some ( filename) => run_script ( & filename. to_string ( ) ) ,
59+ None => run_shell ( & mut vm ) ,
60+ Some ( filename) => run_script ( & mut vm , & filename. to_string ( ) ) ,
5861 }
59- }
62+ } ;
63+
64+ // See if any exception leaked out:
65+ handle_exception ( & mut vm, result) ;
6066}
6167
62- fn _run_string ( source : & str , source_path : Option < String > ) {
63- let mut vm = VirtualMachine :: new ( ) ;
64- let code_obj = compile:: compile (
65- & mut vm,
66- & source. to_string ( ) ,
67- compile:: Mode :: Exec ,
68- source_path,
69- )
70- . unwrap ( ) ;
68+ fn _run_string ( vm : & mut VirtualMachine , source : & str , source_path : Option < String > ) -> PyResult {
69+ let code_obj = compile:: compile ( vm, & source. to_string ( ) , compile:: Mode :: Exec , source_path) ?;
7170 debug ! ( "Code object: {:?}" , code_obj. borrow( ) ) ;
7271 let builtins = vm. get_builtin_scope ( ) ;
7372 let vars = vm. context ( ) . new_scope ( Some ( builtins) ) ; // Keep track of local variables
74- match vm. run_code_obj ( code_obj, vars) {
73+ vm. run_code_obj ( code_obj, vars)
74+ }
75+
76+ fn handle_exception ( vm : & mut VirtualMachine , result : PyResult ) {
77+ match result {
7578 Ok ( _value) => { }
76- Err ( exc) => {
77- // println!("X: {:?}", exc.get_attr("__traceback__"));
78- print_exception ( & mut vm, & exc) ;
79- panic ! ( "Exception: {:?}" , exc) ;
79+ Err ( err) => {
80+ print_exception ( vm, & err) ;
8081 }
8182 }
8283}
8384
84- fn run_command ( source : & mut String ) {
85+ fn run_command ( vm : & mut VirtualMachine , mut source : String ) -> PyResult {
8586 debug ! ( "Running command {}" , source) ;
8687
8788 // This works around https://github.com/RustPython/RustPython/issues/17
8889 source. push_str ( "\n " ) ;
89- _run_string ( source, None )
90+ _run_string ( vm , & source, None )
9091}
9192
92- fn run_module ( module : & str ) {
93+ fn run_module ( vm : & mut VirtualMachine , module : & str ) -> PyResult {
9394 debug ! ( "Running module {}" , module) ;
94- let mut vm = VirtualMachine :: new ( ) ;
95- match import:: import_module ( & mut vm, module) {
96- Ok ( _value) => { }
97- Err ( err) => {
98- print_exception ( & mut vm, & err) ;
99- }
100- }
95+ import:: import_module ( vm, module)
10196}
10297
103- fn run_script ( script_file : & str ) {
98+ fn run_script ( vm : & mut VirtualMachine , script_file : & str ) -> PyResult {
10499 debug ! ( "Running file {}" , script_file) ;
105100 // Parse an ast from it:
106101 let filepath = Path :: new ( script_file) ;
107102 match parser:: read_file ( filepath) {
108- Ok ( source) => _run_string ( & source, Some ( filepath. to_str ( ) . unwrap ( ) . to_string ( ) ) ) ,
103+ Ok ( source) => _run_string ( vm , & source, Some ( filepath. to_str ( ) . unwrap ( ) . to_string ( ) ) ) ,
109104 Err ( msg) => {
110105 error ! ( "Parsing went horribly wrong: {}" , msg) ;
111106 std:: process:: exit ( 1 ) ;
@@ -141,12 +136,11 @@ fn shell_exec(vm: &mut VirtualMachine, source: &str, scope: PyObjectRef) -> bool
141136 true
142137}
143138
144- fn run_shell ( ) {
139+ fn run_shell ( vm : & mut VirtualMachine ) -> PyResult {
145140 println ! (
146141 "Welcome to the magnificent Rust Python {} interpreter" ,
147142 crate_version!( )
148143 ) ;
149- let mut vm = VirtualMachine :: new ( ) ;
150144 let builtins = vm. get_builtin_scope ( ) ;
151145 let vars = vm. context ( ) . new_scope ( Some ( builtins) ) ; // Keep track of local variables
152146
@@ -167,7 +161,7 @@ fn run_shell() {
167161 }
168162 Ok ( _) => {
169163 debug ! ( "You entered {:?}" , input) ;
170- if shell_exec ( & mut vm, & input, vars. clone ( ) ) {
164+ if shell_exec ( vm, & input, vars. clone ( ) ) {
171165 // Line was complete.
172166 input = String :: new ( ) ;
173167 } else {
@@ -186,7 +180,7 @@ fn run_shell() {
186180 . trim_right_matches ( |c| c == '\r' || c == '\n' )
187181 . to_string ( ) ;
188182 if line. len ( ) == 0 {
189- if shell_exec ( & mut vm, & input, vars. clone ( ) ) {
183+ if shell_exec ( vm, & input, vars. clone ( ) ) {
190184 input = String :: new ( ) ;
191185 break ;
192186 }
@@ -203,4 +197,6 @@ fn run_shell() {
203197 Err ( msg) => panic ! ( "Error: {:?}" , msg) ,
204198 } ;
205199 }
200+
201+ Ok ( vm. get_none ( ) )
206202}
0 commit comments