@@ -26,7 +26,9 @@ impl VirtualMachine {
2626 compiler:: compile ( source, mode, & source_path, opts) . map ( |code| self . ctx . new_code ( code) )
2727 }
2828
29+ // pymain_run_file_obj
2930 pub fn run_script ( & self , scope : Scope , path : & str ) -> PyResult < ( ) > {
31+ // when pymain_run_module?
3032 if get_importer ( path, self ) ?. is_some ( ) {
3133 self . insert_sys_path ( self . new_pyobj ( path) ) ?;
3234 let runpy = self . import ( "runpy" , 0 ) ?;
@@ -35,6 +37,7 @@ impl VirtualMachine {
3537 return Ok ( ( ) ) ;
3638 }
3739
40+ // TODO: check if this is proper place
3841 if !self . state . settings . safe_path {
3942 let dir = std:: path:: Path :: new ( path)
4043 . parent ( )
@@ -44,19 +47,61 @@ impl VirtualMachine {
4447 self . insert_sys_path ( self . new_pyobj ( dir) ) ?;
4548 }
4649
47- match std:: fs:: read_to_string ( path) {
48- Ok ( source) => {
49- self . run_code_string ( scope, & source, path. to_owned ( ) ) ?;
50+ self . run_any_file ( scope, path)
51+ }
52+
53+ // = _PyRun_AnyFileObject
54+ fn run_any_file ( & self , scope : Scope , path : & str ) -> PyResult < ( ) > {
55+ let path = if path. is_empty ( ) { "???" } else { path } ;
56+
57+ self . run_simple_file ( scope, path)
58+ }
59+
60+ // = _PyRun_SimpleFileObject
61+ fn run_simple_file ( & self , scope : Scope , path : & str ) -> PyResult < ( ) > {
62+ // __main__ is given by scope
63+ let sys_modules = self . sys_module . get_attr ( identifier ! ( self , modules) , self ) ?;
64+ let main_module = sys_modules. get_item ( identifier ! ( self , __main__) , self ) ?;
65+ let module_dict = main_module. dict ( ) . expect ( "main module must have __dict__" ) ;
66+ if !module_dict. contains_key ( identifier ! ( self , __file__) , self ) {
67+ module_dict. set_item (
68+ identifier ! ( self , __file__) ,
69+ self . ctx . new_str ( path) . into ( ) ,
70+ self ,
71+ ) ?;
72+ }
73+
74+ // Consider to use enum to distinguish `path`
75+ // https://github.com/RustPython/RustPython/pull/6276#discussion_r2529849479
76+
77+ // TODO: check .pyc here
78+ let pyc = false ;
79+ if pyc {
80+ todo ! ( "running pyc is not implemented yet" ) ;
81+ } else {
82+ if path != "<stdin>" {
83+ // TODO: set_main_loader(dict, filename, "SourceFileLoader");
5084 }
51- Err ( err) => {
52- error ! ( "Failed reading file '{path}': {err}" ) ;
53- // TODO: Need to change to ExitCode or Termination
54- std:: process:: exit ( 1 ) ;
85+ // TODO: replace to something equivalent to py_run_file
86+ match std:: fs:: read_to_string ( path) {
87+ Ok ( source) => {
88+ let code_obj = self
89+ . compile ( & source, compiler:: Mode :: Exec , path. to_owned ( ) )
90+ . map_err ( |err| self . new_syntax_error ( & err, Some ( & source) ) ) ?;
91+ // trace!("Code object: {:?}", code_obj.borrow());
92+ self . run_code_obj ( code_obj, scope) ?;
93+ }
94+ Err ( err) => {
95+ error ! ( "Failed reading file '{path}': {err}" ) ;
96+ // TODO: Need to change to ExitCode or Termination
97+ std:: process:: exit ( 1 ) ;
98+ }
5599 }
56100 }
57101 Ok ( ( ) )
58102 }
59103
104+ // TODO: deprecate or reimplement using other primitive functions
60105 pub fn run_code_string ( & self , scope : Scope , source : & str , source_path : String ) -> PyResult {
61106 let code_obj = self
62107 . compile ( source, compiler:: Mode :: Exec , source_path. clone ( ) )
0 commit comments