@@ -50,13 +50,14 @@ mod interpreter;
50
50
mod settings;
51
51
mod shell;
52
52
53
- use atty:: Stream ;
54
53
use rustpython_vm:: { scope:: Scope , PyResult , VirtualMachine } ;
55
- use std:: { env, process:: ExitCode } ;
54
+ use std:: env;
55
+ use std:: io:: IsTerminal ;
56
+ use std:: process:: ExitCode ;
56
57
57
58
pub use interpreter:: InterpreterConfig ;
58
59
pub use rustpython_vm as vm;
59
- pub use settings:: { opts_with_clap, RunMode } ;
60
+ pub use settings:: { opts_with_clap, InstallPipMode , RunMode } ;
60
61
61
62
/// The main cli of the `rustpython` interpreter. This function will return `std::process::ExitCode`
62
63
/// based on the return code of the python code ran through the cli.
@@ -73,9 +74,6 @@ pub fn run(init: impl FnOnce(&mut VirtualMachine) + 'static) -> ExitCode {
73
74
74
75
let ( settings, run_mode) = opts_with_clap ( ) ;
75
76
76
- // Be quiet if "quiet" arg is set OR stdin is not connected to a terminal
77
- let quiet_var = settings. quiet || !atty:: is ( Stream :: Stdin ) ;
78
-
79
77
// don't translate newlines (\r\n <=> \n)
80
78
#[ cfg( windows) ]
81
79
{
@@ -97,7 +95,7 @@ pub fn run(init: impl FnOnce(&mut VirtualMachine) + 'static) -> ExitCode {
97
95
config = config. init_hook ( Box :: new ( init) ) ;
98
96
99
97
let interp = config. interpreter ( ) ;
100
- let exitcode = interp. run ( move |vm| run_rustpython ( vm, run_mode, quiet_var ) ) ;
98
+ let exitcode = interp. run ( move |vm| run_rustpython ( vm, run_mode) ) ;
101
99
102
100
ExitCode :: from ( exitcode)
103
101
}
@@ -117,7 +115,6 @@ fn setup_main_module(vm: &VirtualMachine) -> PyResult<Scope> {
117
115
Ok ( scope)
118
116
}
119
117
120
- #[ cfg( feature = "ssl" ) ]
121
118
fn get_pip ( scope : Scope , vm : & VirtualMachine ) -> PyResult < ( ) > {
122
119
let get_getpip = rustpython_vm:: py_compile!(
123
120
source = r#"\
@@ -128,7 +125,7 @@ __import__("io").TextIOWrapper(
128
125
mode = "eval"
129
126
) ;
130
127
eprintln ! ( "downloading get-pip.py..." ) ;
131
- let getpip_code = vm. run_code_obj ( vm. ctx . new_code ( get_getpip) , scope . clone ( ) ) ?;
128
+ let getpip_code = vm. run_code_obj ( vm. ctx . new_code ( get_getpip) , vm . new_scope_with_builtins ( ) ) ?;
132
129
let getpip_code: rustpython_vm:: builtins:: PyStrRef = getpip_code
133
130
. downcast ( )
134
131
. expect ( "TextIOWrapper.read() should return str" ) ;
@@ -137,29 +134,21 @@ __import__("io").TextIOWrapper(
137
134
Ok ( ( ) )
138
135
}
139
136
140
- #[ cfg( feature = "ssl" ) ]
141
- fn ensurepip ( _: Scope , vm : & VirtualMachine ) -> PyResult < ( ) > {
142
- vm. run_module ( "ensurepip" )
143
- }
144
-
145
- fn install_pip ( _installer : & str , _scope : Scope , vm : & VirtualMachine ) -> PyResult < ( ) > {
146
- #[ cfg( feature = "ssl" ) ]
147
- {
148
- match _installer {
149
- "ensurepip" => ensurepip ( _scope, vm) ,
150
- "get-pip" => get_pip ( _scope, vm) ,
151
- _ => unreachable ! ( ) ,
152
- }
137
+ fn install_pip ( installer : InstallPipMode , scope : Scope , vm : & VirtualMachine ) -> PyResult < ( ) > {
138
+ if cfg ! ( not( feature = "ssl" ) ) {
139
+ return Err ( vm. new_exception_msg (
140
+ vm. ctx . exceptions . system_error . to_owned ( ) ,
141
+ "install-pip requires rustpython be build with '--features=ssl'" . to_owned ( ) ,
142
+ ) ) ;
153
143
}
154
144
155
- #[ cfg( not( feature = "ssl" ) ) ]
156
- Err ( vm. new_exception_msg (
157
- vm. ctx . exceptions . system_error . to_owned ( ) ,
158
- "install-pip requires rustpython be build with '--features=ssl'" . to_owned ( ) ,
159
- ) )
145
+ match installer {
146
+ InstallPipMode :: Ensurepip => vm. run_module ( "ensurepip" ) ,
147
+ InstallPipMode :: GetPip => get_pip ( scope, vm) ,
148
+ }
160
149
}
161
150
162
- fn run_rustpython ( vm : & VirtualMachine , run_mode : RunMode , quiet : bool ) -> PyResult < ( ) > {
151
+ fn run_rustpython ( vm : & VirtualMachine , run_mode : RunMode ) -> PyResult < ( ) > {
163
152
#[ cfg( feature = "flame-it" ) ]
164
153
let main_guard = flame:: start_guard ( "RustPython main" ) ;
165
154
@@ -183,33 +172,46 @@ fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode, quiet: bool) -> PyResu
183
172
) ;
184
173
}
185
174
186
- match run_mode {
175
+ let is_repl = matches ! ( run_mode, RunMode :: Repl ) ;
176
+ if !vm. state . settings . quiet
177
+ && ( vm. state . settings . verbose > 0 || ( is_repl && std:: io:: stdin ( ) . is_terminal ( ) ) )
178
+ {
179
+ eprintln ! (
180
+ "Welcome to the magnificent Rust Python {} interpreter \u{1f631} \u{1f596} " ,
181
+ env!( "CARGO_PKG_VERSION" )
182
+ ) ;
183
+ eprintln ! (
184
+ "RustPython {}.{}.{}" ,
185
+ vm:: version:: MAJOR ,
186
+ vm:: version:: MINOR ,
187
+ vm:: version:: MICRO ,
188
+ ) ;
189
+
190
+ eprintln ! ( "Type \" help\" , \" copyright\" , \" credits\" or \" license\" for more information." ) ;
191
+ }
192
+ let res = match run_mode {
187
193
RunMode :: Command ( command) => {
188
194
debug ! ( "Running command {}" , command) ;
189
- vm. run_code_string ( scope, & command, "<stdin>" . to_owned ( ) ) ?;
195
+ vm. run_code_string ( scope. clone ( ) , & command, "<stdin>" . to_owned ( ) )
196
+ . map ( drop)
190
197
}
191
198
RunMode :: Module ( module) => {
192
199
debug ! ( "Running module {}" , module) ;
193
- vm. run_module ( & module) ?;
194
- }
195
- RunMode :: InstallPip ( installer) => {
196
- install_pip ( & installer, scope, vm) ?;
200
+ vm. run_module ( & module)
197
201
}
198
- RunMode :: ScriptInteractive ( script, interactive) => {
199
- if let Some ( script) = script {
200
- debug ! ( "Running script {}" , & script) ;
201
- vm. run_script ( scope. clone ( ) , & script) ?;
202
- } else if !quiet {
203
- println ! (
204
- "Welcome to the magnificent Rust Python {} interpreter \u{1f631} \u{1f596} " ,
205
- crate_version!( )
206
- ) ;
207
- }
208
- if interactive {
209
- shell:: run_shell ( vm, scope) ?;
210
- }
202
+ RunMode :: InstallPip ( installer) => install_pip ( installer, scope. clone ( ) , vm) ,
203
+ RunMode :: Script ( script) => {
204
+ debug ! ( "Running script {}" , & script) ;
205
+ vm. run_script ( scope. clone ( ) , & script)
211
206
}
207
+ RunMode :: Repl => Ok ( ( ) ) ,
208
+ } ;
209
+ if is_repl || vm. state . settings . inspect {
210
+ shell:: run_shell ( vm, scope) ?;
211
+ } else {
212
+ res?;
212
213
}
214
+
213
215
#[ cfg( feature = "flame-it" ) ]
214
216
{
215
217
main_guard. end ( ) ;
0 commit comments