pymain_run_python
(vm: &VirtualMachine, run_mode: RunMode)
| 225 | |
| 226 | // pymain_run_python |
| 227 | fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> { |
| 228 | #[cfg(feature = "flame-it")] |
| 229 | let main_guard = flame::start_guard("RustPython main"); |
| 230 | |
| 231 | let scope = vm.new_scope_with_main()?; |
| 232 | |
| 233 | // Initialize warnings module to process sys.warnoptions |
| 234 | // _PyWarnings_Init() |
| 235 | if vm.import("warnings", 0).is_err() { |
| 236 | warn!("Failed to import warnings module"); |
| 237 | } |
| 238 | |
| 239 | // Import site first, before setting sys.path[0] |
| 240 | // This matches CPython's behavior where site.removeduppaths() runs |
| 241 | // before sys.path[0] is set, preventing '' from being converted to cwd |
| 242 | let site_result = vm.import("site", 0); |
| 243 | if site_result.is_err() { |
| 244 | warn!( |
| 245 | "Failed to import site, consider adding the Lib directory to your RUSTPYTHONPATH \ |
| 246 | environment variable", |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | // _PyPathConfig_ComputeSysPath0 - set sys.path[0] after site import |
| 251 | if !vm.state.config.settings.safe_path { |
| 252 | let path0: Option<String> = match &run_mode { |
| 253 | RunMode::Command(_) => Some(String::new()), |
| 254 | RunMode::Module(_) => env::current_dir() |
| 255 | .ok() |
| 256 | .and_then(|p| p.to_str().map(|s| s.to_owned())), |
| 257 | RunMode::Script(_) | RunMode::InstallPip(_) => None, // handled by run_script |
| 258 | RunMode::Repl => Some(String::new()), |
| 259 | }; |
| 260 | |
| 261 | if let Some(path) = path0 { |
| 262 | vm.insert_sys_path(vm.new_pyobj(path))?; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // Enable faulthandler if -X faulthandler, PYTHONFAULTHANDLER or -X dev is set |
| 267 | // _PyFaulthandler_Init() |
| 268 | if vm.state.config.settings.faulthandler { |
| 269 | let _ = vm.run_simple_string("import faulthandler; faulthandler.enable()"); |
| 270 | } |
| 271 | |
| 272 | let is_repl = matches!(run_mode, RunMode::Repl); |
| 273 | if !vm.state.config.settings.quiet |
| 274 | && (vm.state.config.settings.verbose > 0 || (is_repl && std::io::stdin().is_terminal())) |
| 275 | { |
| 276 | eprintln!( |
| 277 | "Welcome to the magnificent Rust Python {} interpreter \u{1f631} \u{1f596}", |
| 278 | env!("CARGO_PKG_VERSION") |
| 279 | ); |
| 280 | eprintln!( |
| 281 | "RustPython {}.{}.{}", |
| 282 | vm::version::MAJOR, |
| 283 | vm::version::MINOR, |
| 284 | vm::version::MICRO, |
no test coverage detected