GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — RustPython/src/shell.rs at stack/python-3.15-8950 · RustPython/RustPython Expand file tree/ Copy pathshell.rsMore file actions 230 lines (213 loc) · 7.83 KB / Copy pathshell.rsFile metadata and controls230 lines (213 loc) · 7.83 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 mod helper; use rustpython_compiler::{ CompileError, ParseError, parser::InterpolatedStringErrorType, parser::LexicalErrorType, parser::ParseErrorType, }; use rustpython_vm::{ AsObject, PyResult, VirtualMachine, builtins::PyBaseExceptionRef, bytecode::CodeFlags, compiler::{self}, readline::{Readline, ReadlineResult}, scope::Scope, vm::VmCompileError, }; enum ShellExecResult { Ok, PyErr(PyBaseExceptionRef), ContinueBlock, ContinueLine, } fn shell_exec( vm: &VirtualMachine, source: &str, scope: Scope, empty_line_given: bool, continuing_block: bool, future_features: &mut CodeFlags, ) -> ShellExecResult { // compiling expects only UNIX style line endings, and will replace windows line endings // internally. Since we might need to analyze the source to determine if an error could be // resolved by future input, we need the location from the error to match the source code that // was actually compiled. #[cfg(windows)] let source = &source.replace("\r\n", "\n"); let opts = compiler::CompileOpts { future_features: *future_features, ..vm.compile_opts() }; match vm.compile_with_opts(source, compiler::Mode::Single, "<stdin>", opts) { Ok(code) => { *future_features |= code.code.flags & CodeFlags::FUTURE_MASK; let _ = vm.register_code_in_linecache(&code, source); if empty_line_given || !continuing_block { match vm.run_code_obj(code, scope) { Ok(_val) => ShellExecResult::Ok, Err(err) => ShellExecResult::PyErr(err), } } else { // We can just return an ok result ShellExecResult::Ok } } Err(err) => { if matches!( &err, VmCompileError::Compile(CompileError::Parse(ParseError { error: ParseErrorType::Lexical( LexicalErrorType::Eof | LexicalErrorType::FStringError( InterpolatedStringErrorType::UnterminatedTripleQuotedString { .. }, ) | LexicalErrorType::UnclosedStringError { triple_quoted: true, .. } ), .. })) ) { return ShellExecResult::ContinueLine; } // An unfinished suite is _IncompleteInputError, not IndentationError. let exc = err.into_pyexception_maybe_incomplete(vm, Some(source), !empty_line_given); if !empty_line_given && exc.fast_isinstance(vm.ctx.exceptions.incomplete_input_error) { ShellExecResult::ContinueBlock } else { ShellExecResult::PyErr(exc) } } } } fn flush_stdio(vm: &VirtualMachine) { for name in ["stdout", "stderr"] { if let Ok(stream) = vm.sys_module.get_attr(name, vm) { let _ = vm.call_method(&stream, "flush", ()); } } } /// Enter a repl loop pub fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> { let mut repl = Readline::new(helper::ShellHelper::new(vm, scope.globals.clone())); let mut full_input = String::new(); // Retrieve a `history_path_str` dependent on the OS let repl_history_path = match dirs::config_dir() { Some(mut path) => { path.push("rustpython"); path.push("repl_history.txt"); path } None => ".repl_history.txt".into(), }; let _ = repl.load_history(&repl_history_path); // We might either be waiting to know if a block is complete, or waiting to know if a multiline // statement is complete. In the former case, we need to ensure that we read one extra new line // to know that the block is complete. In the latter, we can execute as soon as the statement is // valid. let mut continuing_block = false; let mut continuing_line = false; let mut future_features = CodeFlags::empty(); loop { let prompt_name = if continuing_block || continuing_line { "ps2" } else { "ps1" }; let prompt = vm .sys_module .get_attr(prompt_name, vm) .and_then(|prompt| prompt.str(vm)); let prompt = match prompt { Ok(ref s) => s.expect_str(), Err(_) => "", }; continuing_line = false; let result = match repl.readline(prompt) { ReadlineResult::Line(line) => { #[cfg(debug_assertions)] debug!("You entered {line:?}"); repl.add_history_entry(line.trim_end()).unwrap(); let empty_line_given = line.is_empty(); if full_input.is_empty() { full_input = line; } else { full_input.push_str(&line); } full_input.push('\n'); match shell_exec( vm, &full_input, scope.clone(), empty_line_given, continuing_block, &mut future_features, ) { ShellExecResult::Ok => { if continuing_block { if empty_line_given { // We should exit continue mode since the block successfully executed continuing_block = false; full_input.clear(); } } else { // We aren't in continue mode so proceed normally full_input.clear(); } Ok(()) } // Continue, but don't change the mode ShellExecResult::ContinueLine => { continuing_line = true; Ok(()) } ShellExecResult::ContinueBlock => { continuing_block = true; Ok(()) } ShellExecResult::PyErr(err) => { continuing_block = false; full_input.clear(); Err(err) } } } ReadlineResult::Interrupt => { continuing_block = false; full_input.clear(); let keyboard_interrupt = vm.new_exception_empty(vm.ctx.exceptions.keyboard_interrupt.to_owned()); Err(keyboard_interrupt) } ReadlineResult::Eof => { break; } #[cfg(unix)] ReadlineResult::OsError(num) => { let os_error = vm.new_exception_msg( vm.ctx.exceptions.os_error.to_owned(), format!("{num:?}").into(), ); vm.print_exception(&os_error); break; } ReadlineResult::Other(err) => { eprintln!("Readline error: {err:?}"); break; } ReadlineResult::Io(err) => { eprintln!("IO error: {err:?}"); break; } }; if let Err(exc) = result { if exc.fast_isinstance(vm.ctx.exceptions.system_exit) { let _ = repl.save_history(&repl_history_path); return Err(exc); } vm.print_exception(&exc); } flush_stdio(vm); } let _ = repl.save_history(&repl_history_path); Ok(()) } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |