Skip to content

Commit ce91a25

Browse files
committed
Added -m option to main program.
1 parent bc60969 commit ce91a25

4 files changed

Lines changed: 30 additions & 11 deletions

File tree

src/main.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ extern crate rustpython_vm;
99

1010
use clap::{App, Arg};
1111
use rustpython_parser::parser;
12-
use rustpython_vm::compile;
1312
use rustpython_vm::obj::objstr;
1413
use rustpython_vm::print_exception;
1514
use rustpython_vm::pyobject::PyObjectRef;
1615
use rustpython_vm::VirtualMachine;
16+
use rustpython_vm::{compile, import};
1717
use std::io;
1818
use std::io::prelude::*;
1919
use std::path::Path;
@@ -37,18 +37,25 @@ fn main() {
3737
.takes_value(true)
3838
.help("run the given string as a program"),
3939
)
40+
.arg(
41+
Arg::with_name("m")
42+
.short("m")
43+
.takes_value(true)
44+
.help("run library module as script"),
45+
)
4046
.get_matches();
4147

4248
// Figure out if a -c option was given:
4349
if let Some(command) = matches.value_of("c") {
4450
run_command(&mut command.to_string());
45-
return;
46-
}
47-
48-
// Figure out if a script was passed:
49-
match matches.value_of("script") {
50-
None => run_shell(),
51-
Some(filename) => run_script(&filename.to_string()),
51+
} else if let Some(module) = matches.value_of("m") {
52+
run_module(module);
53+
} else {
54+
// Figure out if a script was passed:
55+
match matches.value_of("script") {
56+
None => run_shell(),
57+
Some(filename) => run_script(&filename.to_string()),
58+
}
5259
}
5360
}
5461

@@ -82,6 +89,17 @@ fn run_command(source: &mut String) {
8289
_run_string(source, None)
8390
}
8491

92+
fn run_module(module: &str) {
93+
debug!("Running module {}", module);
94+
let mut vm = VirtualMachine::new();
95+
match import::import_module(&mut vm, module) {
96+
Ok(_value) => {}
97+
Err(err) => {
98+
print_exception(&mut vm, &err);
99+
}
100+
}
101+
}
102+
85103
fn run_script(script_file: &str) {
86104
debug!("Running file {}", script_file);
87105
// Parse an ast from it:

vm/src/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn import_uncached_module(vm: &mut VirtualMachine, module: &str) -> PyResult {
4949
Ok(vm.ctx.new_module(module, scope))
5050
}
5151

52-
fn import_module(vm: &mut VirtualMachine, module_name: &str) -> PyResult {
52+
pub fn import_module(vm: &mut VirtualMachine, module_name: &str) -> PyResult {
5353
// First, see if we already loaded the module:
5454
let sys_modules = vm.sys_module.get_item("modules").unwrap();
5555
if let Some(module) = sys_modules.get_item(module_name) {

vm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub mod compile;
1919
pub mod eval;
2020
mod exceptions;
2121
mod frame;
22-
mod import;
22+
pub mod import;
2323
pub mod obj;
2424
pub mod pyobject;
2525
pub mod stdlib;

vm/src/stdlib/json.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ fn loads(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
213213
Ok(de
214214
.deserialize(&mut serde_json::Deserializer::from_str(&objstr::get_value(
215215
&string,
216-
))).unwrap())
216+
)))
217+
.unwrap())
217218
}
218219

219220
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {

0 commit comments

Comments
 (0)