Skip to content

Commit e8c5936

Browse files
committed
Interpreter::run
1 parent c83d8fd commit e8c5936

File tree

2 files changed

+35
-40
lines changed

2 files changed

+35
-40
lines changed

src/lib.rs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ extern crate log;
4646
mod shell;
4747

4848
use clap::{App, AppSettings, Arg, ArgMatches};
49-
use rustpython_vm::{
50-
scope::Scope,
51-
stdlib::{atexit, sys},
52-
Interpreter, PyResult, Settings, VirtualMachine,
53-
};
49+
use rustpython_vm::{scope::Scope, Interpreter, PyResult, Settings, VirtualMachine};
5450
use std::{env, process, str::FromStr};
5551

5652
pub use rustpython_vm as vm;
@@ -87,23 +83,7 @@ where
8783
init(vm);
8884
});
8985

90-
let exitcode = interp.enter(move |vm| {
91-
let res = run_rustpython(vm, matches);
92-
93-
flush_std(vm);
94-
95-
// See if any exception leaked out:
96-
let exit_code = res
97-
.map(|_| 0)
98-
.map_err(|exc| vm.handle_exit_exception(exc))
99-
.unwrap_or_else(|code| code);
100-
101-
let _ = atexit::_run_exitfuncs(vm);
102-
103-
flush_std(vm);
104-
105-
exit_code
106-
});
86+
let exitcode = interp.run(move |vm| run_rustpython(vm, matches));
10787

10888
#[cfg(feature = "flame-it")]
10989
{
@@ -116,15 +96,6 @@ where
11696
process::exit(exitcode)
11797
}
11898

119-
fn flush_std(vm: &VirtualMachine) {
120-
if let Ok(stdout) = sys::get_stdout(vm) {
121-
let _ = vm.call_method(&stdout, "flush", ());
122-
}
123-
if let Ok(stderr) = sys::get_stderr(vm) {
124-
let _ = vm.call_method(&stderr, "flush", ());
125-
}
126-
}
127-
12899
fn parse_arguments<'a>(app: App<'a, '_>) -> ArgMatches<'a> {
129100
let app = app
130101
.setting(AppSettings::TrailingVarArg)

vm/src/vm/interpreter.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
use super::{setting::Settings, thread, VirtualMachine};
2+
use crate::{
3+
stdlib::{atexit, sys},
4+
PyResult,
5+
};
26

37
/// The general interface for the VM
48
///
@@ -53,16 +57,36 @@ impl Interpreter {
5357
thread::enter_vm(&self.vm, || f(&self.vm))
5458
}
5559

56-
// TODO: interpreter shutdown
57-
// pub fn run<F>(self, f: F)
58-
// where
59-
// F: FnOnce(&VirtualMachine),
60-
// {
61-
// self.enter(f);
62-
// self.shutdown();
63-
// }
60+
pub fn run<F, R>(self, f: F) -> i32
61+
where
62+
F: FnOnce(&VirtualMachine) -> PyResult<R>,
63+
{
64+
self.enter(|vm| {
65+
let res = f(vm);
66+
flush_std(vm);
67+
68+
// See if any exception leaked out:
69+
let exit_code = res
70+
.map(|_| 0)
71+
.map_err(|exc| vm.handle_exit_exception(exc))
72+
.unwrap_or_else(|code| code);
6473

65-
// pub fn shutdown(self) {}
74+
let _ = atexit::_run_exitfuncs(vm);
75+
76+
flush_std(vm);
77+
78+
exit_code
79+
})
80+
}
81+
}
82+
83+
fn flush_std(vm: &VirtualMachine) {
84+
if let Ok(stdout) = sys::get_stdout(vm) {
85+
let _ = vm.call_method(&stdout, "flush", ());
86+
}
87+
if let Ok(stderr) = sys::get_stderr(vm) {
88+
let _ = vm.call_method(&stderr, "flush", ());
89+
}
6690
}
6791

6892
#[cfg(test)]

0 commit comments

Comments
 (0)