@@ -47,14 +47,13 @@ mod shell;
4747
4848use clap:: { App , AppSettings , Arg , ArgMatches } ;
4949use rustpython_vm:: {
50- builtins:: { PyDictRef , PyInt } ,
51- compile , match_class,
50+ builtins:: PyInt ,
51+ match_class,
5252 scope:: Scope ,
5353 stdlib:: { atexit, sys} ,
54- AsObject , InitParameter , Interpreter , PyObjectRef , PyResult , Settings , TryFromObject ,
55- VirtualMachine ,
54+ AsObject , Interpreter , PyResult , Settings , VirtualMachine ,
5655} ;
57- use std:: { env, path :: Path , process, str:: FromStr } ;
56+ use std:: { env, process, str:: FromStr } ;
5857
5958pub use rustpython_vm as vm;
6059
8483 }
8584 }
8685
87- let interp = Interpreter :: new_with_init ( settings, |vm| {
86+ let interp = Interpreter :: with_init ( settings, |vm| {
8887 add_stdlib ( vm) ;
8988 init ( vm) ;
90- InitParameter :: External
9189 } ) ;
9290
9391 let exitcode = interp. enter ( move |vm| {
@@ -574,7 +572,7 @@ __import__("io").TextIOWrapper(
574572 . downcast ( )
575573 . expect ( "TextIOWrapper.read() should return str" ) ;
576574 eprintln ! ( "running get-pip.py..." ) ;
577- _run_string ( vm , scope, getpip_code. as_str ( ) , "get-pip.py" . to_owned ( ) )
575+ vm . run_code_string ( scope, getpip_code. as_str ( ) , "get-pip.py" . to_owned ( ) )
578576}
579577
580578#[ cfg( not( feature = "ssl" ) ) ]
@@ -599,13 +597,16 @@ fn run_rustpython(vm: &VirtualMachine, matches: &ArgMatches) -> PyResult<()> {
599597
600598 // Figure out if a -c option was given:
601599 if let Some ( command) = matches. value_of ( "c" ) {
602- run_command ( vm, scope, command. to_owned ( ) ) ?;
600+ debug ! ( "Running command {}" , command) ;
601+ vm. run_code_string ( scope, command, "<stdin>" . to_owned ( ) ) ?;
603602 } else if let Some ( module) = matches. value_of ( "m" ) {
604- run_module ( vm, module) ?;
603+ debug ! ( "Running module {}" , module) ;
604+ vm. run_module ( module) ?;
605605 } else if matches. is_present ( "install_pip" ) {
606606 install_pip ( scope, vm) ?;
607607 } else if let Some ( filename) = matches. value_of ( "script" ) {
608- run_script ( vm, scope. clone ( ) , filename) ?;
608+ debug ! ( "Running file {}" , filename) ;
609+ vm. run_script ( scope. clone ( ) , filename) ?;
609610 if matches. is_present ( "inspect" ) {
610611 shell:: run_shell ( vm, scope) ?;
611612 }
@@ -620,97 +621,13 @@ fn run_rustpython(vm: &VirtualMachine, matches: &ArgMatches) -> PyResult<()> {
620621 Ok ( ( ) )
621622}
622623
623- fn _run_string ( vm : & VirtualMachine , scope : Scope , source : & str , source_path : String ) -> PyResult {
624- let code_obj = vm
625- . compile ( source, compile:: Mode :: Exec , source_path. clone ( ) )
626- . map_err ( |err| vm. new_syntax_error ( & err) ) ?;
627- // trace!("Code object: {:?}", code_obj.borrow());
628- scope
629- . globals
630- . set_item ( "__file__" , vm. new_pyobj ( source_path) , vm) ?;
631- vm. run_code_obj ( code_obj, scope)
632- }
633-
634- fn run_command ( vm : & VirtualMachine , scope : Scope , source : String ) -> PyResult < ( ) > {
635- debug ! ( "Running command {}" , source) ;
636- _run_string ( vm, scope, & source, "<stdin>" . to_owned ( ) ) ?;
637- Ok ( ( ) )
638- }
639-
640- fn run_module ( vm : & VirtualMachine , module : & str ) -> PyResult < ( ) > {
641- debug ! ( "Running module {}" , module) ;
642- let runpy = vm. import ( "runpy" , None , 0 ) ?;
643- let run_module_as_main = runpy. get_attr ( "_run_module_as_main" , vm) ?;
644- vm. invoke ( & run_module_as_main, ( module, ) ) ?;
645- Ok ( ( ) )
646- }
647-
648- fn get_importer ( path : & str , vm : & VirtualMachine ) -> PyResult < Option < PyObjectRef > > {
649- let path_importer_cache = vm. sys_module . get_attr ( "path_importer_cache" , vm) ?;
650- let path_importer_cache = PyDictRef :: try_from_object ( vm, path_importer_cache) ?;
651- if let Some ( importer) = path_importer_cache. get_item_opt ( path, vm) ? {
652- return Ok ( Some ( importer) ) ;
653- }
654- let path = vm. ctx . new_str ( path) ;
655- let path_hooks = vm. sys_module . get_attr ( "path_hooks" , vm) ?;
656- let mut importer = None ;
657- let path_hooks: Vec < PyObjectRef > = path_hooks. try_into_value ( vm) ?;
658- for path_hook in path_hooks {
659- match vm. invoke ( & path_hook, ( path. clone ( ) , ) ) {
660- Ok ( imp) => {
661- importer = Some ( imp) ;
662- break ;
663- }
664- Err ( e) if e. fast_isinstance ( & vm. ctx . exceptions . import_error ) => continue ,
665- Err ( e) => return Err ( e) ,
666- }
667- }
668- Ok ( if let Some ( imp) = importer {
669- let imp = path_importer_cache. get_or_insert ( vm, path. into ( ) , || imp. clone ( ) ) ?;
670- Some ( imp)
671- } else {
672- None
673- } )
674- }
675-
676- fn insert_sys_path ( vm : & VirtualMachine , obj : PyObjectRef ) -> PyResult < ( ) > {
677- let sys_path = vm. sys_module . get_attr ( "path" , vm) . unwrap ( ) ;
678- vm. call_method ( & sys_path, "insert" , ( 0 , obj) ) ?;
679- Ok ( ( ) )
680- }
681-
682- fn run_script ( vm : & VirtualMachine , scope : Scope , script_file : & str ) -> PyResult < ( ) > {
683- debug ! ( "Running file {}" , script_file) ;
684- if get_importer ( script_file, vm) ?. is_some ( ) {
685- insert_sys_path ( vm, vm. ctx . new_str ( script_file) . into ( ) ) ?;
686- let runpy = vm. import ( "runpy" , None , 0 ) ?;
687- let run_module_as_main = runpy. get_attr ( "_run_module_as_main" , vm) ?;
688- vm. invoke ( & run_module_as_main, ( vm. ctx . new_str ( "__main__" ) , false ) ) ?;
689- return Ok ( ( ) ) ;
690- }
691- let dir = Path :: new ( script_file) . parent ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
692- insert_sys_path ( vm, vm. ctx . new_str ( dir) . into ( ) ) ?;
693-
694- match std:: fs:: read_to_string ( script_file) {
695- Ok ( source) => {
696- _run_string ( vm, scope, & source, script_file. to_owned ( ) ) ?;
697- }
698- Err ( err) => {
699- error ! ( "Failed reading file '{}': {}" , script_file, err) ;
700- process:: exit ( 1 ) ;
701- }
702- }
703- Ok ( ( ) )
704- }
705-
706624#[ cfg( test) ]
707625mod tests {
708626 use super :: * ;
709627
710628 fn interpreter ( ) -> Interpreter {
711- Interpreter :: new_with_init ( Settings :: default ( ) , |vm| {
629+ Interpreter :: with_init ( Settings :: default ( ) , |vm| {
712630 add_stdlib ( vm) ;
713- InitParameter :: External
714631 } )
715632 }
716633
@@ -720,11 +637,11 @@ mod tests {
720637 vm. unwrap_pyresult ( ( || {
721638 let scope = setup_main_module ( vm) ?;
722639 // test file run
723- run_script ( vm , scope, "extra_tests/snippets/dir_main/__main__.py" ) ?;
640+ vm . run_script ( scope, "extra_tests/snippets/dir_main/__main__.py" ) ?;
724641
725642 let scope = setup_main_module ( vm) ?;
726643 // test module run
727- run_script ( vm , scope, "extra_tests/snippets/dir_main" ) ?;
644+ vm . run_script ( scope, "extra_tests/snippets/dir_main" ) ?;
728645
729646 Ok ( ( ) )
730647 } ) ( ) ) ;
0 commit comments