@@ -123,29 +123,36 @@ fn run_script(vm: &mut VirtualMachine, script_file: &str) -> PyResult {
123123 }
124124}
125125
126- fn shell_exec ( vm : & mut VirtualMachine , source : & str , scope : PyObjectRef ) -> bool {
127- match compile:: compile ( source, & compile:: Mode :: Single , "<stdin>" . to_string ( ) , vm. ctx . code_type ( ) ) {
126+ fn shell_exec (
127+ vm : & mut VirtualMachine ,
128+ source : & str ,
129+ scope : PyObjectRef ,
130+ ) -> Result < ( ) , CompileError > {
131+ match compile:: compile (
132+ source,
133+ & compile:: Mode :: Single ,
134+ "<stdin>" . to_string ( ) ,
135+ vm. ctx . code_type ( ) ,
136+ ) {
128137 Ok ( code) => {
129138 if let Err ( err) = vm. run_code_obj ( code, scope) {
130139 print_exception ( vm, & err) ;
131140 }
141+ Ok ( ( ) )
132142 }
133143 // Don't inject syntax errors for line continuation
134- Err ( CompileError :: Parse ( ParseError :: EOF ( _) ) ) => {
135- return false ;
136- }
144+ Err ( err @ CompileError :: Parse ( ParseError :: EOF ( _) ) ) => Err ( err) ,
137145 Err ( err) => {
138146 let syntax_error = vm. context ( ) . exceptions . syntax_error . clone ( ) ;
139147 let exc = vm. new_exception ( syntax_error, format ! ( "{}" , err) ) ;
140148 print_exception ( vm, & exc) ;
149+ Err ( err)
141150 }
142- } ;
143- true
151+ }
144152}
145153
146154#[ cfg( not( target_family = "unix" ) ) ]
147155fn get_history_path ( ) -> PathBuf {
148- //Path buffer
149156 PathBuf :: from ( ".repl_history.txt" )
150157}
151158
@@ -169,57 +176,34 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult {
169176
170177 // Read a single line:
171178 let mut input = String :: new ( ) ;
172- let mut rl = Editor :: < ( ) > :: new ( ) ;
179+ let mut repl = Editor :: < ( ) > :: new ( ) ;
173180
174- //retrieve a history_path_str dependent to the os
181+ // Retrieve a ` history_path_str` dependent on the OS
175182 let repl_history_path_str = & get_history_path ( ) ;
176- if rl . load_history ( repl_history_path_str) . is_err ( ) {
183+ if repl . load_history ( repl_history_path_str) . is_err ( ) {
177184 println ! ( "No previous history." ) ;
178185 }
179186
180- loop {
181- // TODO: modules don't support getattr / setattr yet
182- //let prompt = match vm.get_attribute(vm.sys_module.clone(), "ps1") {
183- // Ok(value) => objstr::get_value(&value),
184- // Err(_) => ">>>>> ".to_string(),
185- //};
186-
187- // We can customize the prompt:
188- let ps1 = objstr:: get_value ( & vm. sys_module . get_attr ( "ps1" ) . unwrap ( ) ) ;
189- let ps2 = objstr:: get_value ( & vm. sys_module . get_attr ( "ps2" ) . unwrap ( ) ) ;
187+ let ps1 = & objstr:: get_value ( & vm. sys_module . get_attr ( "ps1" ) . unwrap ( ) ) ;
188+ let ps2 = & objstr:: get_value ( & vm. sys_module . get_attr ( "ps2" ) . unwrap ( ) ) ;
189+ let mut prompt = ps1;
190190
191- match rl. readline ( & ps1) {
191+ loop {
192+ match repl. readline ( prompt) {
192193 Ok ( line) => {
194+ debug ! ( "You entered {:?}" , line) ;
193195 input. push_str ( & line) ;
194196 input. push_str ( "\n " ) ;
197+ repl. add_history_entry ( line. trim_end ( ) . as_ref ( ) ) ;
195198
196- debug ! ( "You entered {:?}" , input) ;
197- if shell_exec ( vm, & input, vars. clone ( ) ) {
198- // Line was complete.
199- rl. add_history_entry ( input. trim_end ( ) ) ;
200- input = String :: new ( ) ;
201- } else {
202- loop {
203- // until an empty line is pressed AND the code is complete
204- //let prompt = match vm.get_attribute(vm.sys_module.clone(), "ps2") {
205- // Ok(value) => objstr::get_value(&value),
206- // Err(_) => "..... ".to_string(),
207- //};
208- match rl. readline ( & ps2) {
209- Ok ( line) => {
210- if line. is_empty ( ) {
211- if shell_exec ( vm, & input, vars. clone ( ) ) {
212- rl. add_history_entry ( input. trim_end ( ) ) ;
213- input = String :: new ( ) ;
214- break ;
215- }
216- } else {
217- input. push_str ( & line) ;
218- input. push_str ( "\n " ) ;
219- }
220- }
221- Err ( msg) => panic ! ( "Error: {:?}" , msg) ,
222- }
199+ match shell_exec ( vm, & input, vars. clone ( ) ) {
200+ Err ( CompileError :: Parse ( ParseError :: EOF ( _) ) ) => {
201+ prompt = ps2;
202+ continue ;
203+ }
204+ _ => {
205+ prompt = ps1;
206+ input = String :: new ( ) ;
223207 }
224208 }
225209 }
@@ -237,7 +221,7 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult {
237221 }
238222 } ;
239223 }
240- rl . save_history ( repl_history_path_str) . unwrap ( ) ;
224+ repl . save_history ( repl_history_path_str) . unwrap ( ) ;
241225
242226 Ok ( vm. get_none ( ) )
243227}
0 commit comments