@@ -14,7 +14,7 @@ use rustpython_vm::{
14
14
print_exception, pyobject:: PyResult , util, VirtualMachine ,
15
15
} ;
16
16
use rustyline:: { error:: ReadlineError , Editor } ;
17
- use std:: path:: { Path , PathBuf } ;
17
+ use std:: path:: PathBuf ;
18
18
19
19
fn main ( ) {
20
20
env_logger:: init ( ) ;
@@ -96,11 +96,36 @@ fn run_module(vm: &VirtualMachine, module: &str) -> PyResult {
96
96
fn run_script ( vm : & VirtualMachine , script_file : & str ) -> PyResult {
97
97
debug ! ( "Running file {}" , script_file) ;
98
98
// Parse an ast from it:
99
- let file_path = Path :: new ( script_file) ;
100
- match util:: read_file ( file_path) {
99
+ let file_path = PathBuf :: from ( script_file) ;
100
+ let file_path = if file_path. is_file ( ) {
101
+ file_path
102
+ } else if file_path. is_dir ( ) {
103
+ let main_file_path = file_path. join ( "__main__.py" ) ;
104
+ if main_file_path. is_file ( ) {
105
+ main_file_path
106
+ } else {
107
+ error ! (
108
+ "can't find '__main__' module in '{}'" ,
109
+ file_path. to_str( ) . unwrap( )
110
+ ) ;
111
+ std:: process:: exit ( 1 ) ;
112
+ }
113
+ } else {
114
+ error ! (
115
+ "can't open file '{}': No such file or directory" ,
116
+ file_path. to_str( ) . unwrap( )
117
+ ) ;
118
+ std:: process:: exit ( 1 ) ;
119
+ } ;
120
+
121
+ match util:: read_file ( & file_path) {
101
122
Ok ( source) => _run_string ( vm, & source, file_path. to_str ( ) . unwrap ( ) . to_string ( ) ) ,
102
123
Err ( err) => {
103
- error ! ( "Failed reading file: {:?}" , err. kind( ) ) ;
124
+ error ! (
125
+ "Failed reading file '{}': {:?}" ,
126
+ file_path. to_str( ) . unwrap( ) ,
127
+ err. kind( )
128
+ ) ;
104
129
std:: process:: exit ( 1 ) ;
105
130
}
106
131
}
0 commit comments