Skip to content

Commit 2eb97aa

Browse files
committed
Add __main__.py support
1 parent 7f82174 commit 2eb97aa

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/main.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustpython_vm::{
1414
print_exception, pyobject::PyResult, util, VirtualMachine,
1515
};
1616
use rustyline::{error::ReadlineError, Editor};
17-
use std::path::{Path, PathBuf};
17+
use std::path::PathBuf;
1818

1919
fn main() {
2020
env_logger::init();
@@ -96,11 +96,36 @@ fn run_module(vm: &VirtualMachine, module: &str) -> PyResult {
9696
fn run_script(vm: &VirtualMachine, script_file: &str) -> PyResult {
9797
debug!("Running file {}", script_file);
9898
// 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) {
101122
Ok(source) => _run_string(vm, &source, file_path.to_str().unwrap().to_string()),
102123
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+
);
104129
std::process::exit(1);
105130
}
106131
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print('Hello')

0 commit comments

Comments
 (0)