diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py index d773674fee..122c4ac16d 100644 --- a/Lib/test/test_cmd_line_script.py +++ b/Lib/test/test_cmd_line_script.py @@ -411,8 +411,6 @@ def test_issue8202(self): script_name, script_name, script_dir, 'test_pkg', importlib.machinery.SourceFileLoader) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_issue8202_dash_c_file_ignored(self): # Make sure a "-c" file in the current directory # does not alter the value of sys.path[0] @@ -713,8 +711,6 @@ def test_syntaxerror_null_bytes_in_multiline_string(self): ] ) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_consistent_sys_path_for_direct_execution(self): # This test case ensures that the following all give the same # sys.path configuration: diff --git a/src/lib.rs b/src/lib.rs index 8d27805893..8ee22d4eb5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -169,16 +169,9 @@ fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> { let scope = setup_main_module(vm)?; - if !vm.state.config.settings.safe_path { - // TODO: The prepending path depends on running mode - // See https://docs.python.org/3/using/cmdline.html#cmdoption-P - vm.run_code_string( - vm.new_scope_with_builtins(), - "import sys; sys.path.insert(0, '')", - "".to_owned(), - )?; - } - + // Import site first, before setting sys.path[0] + // This matches CPython's behavior where site.removeduppaths() runs + // before sys.path[0] is set, preventing '' from being converted to cwd let site_result = vm.import("site", 0); if site_result.is_err() { warn!( @@ -187,6 +180,22 @@ fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> { ); } + // _PyPathConfig_ComputeSysPath0 - set sys.path[0] after site import + if !vm.state.config.settings.safe_path { + let path0: Option = match &run_mode { + RunMode::Command(_) => Some(String::new()), + RunMode::Module(_) => env::current_dir() + .ok() + .and_then(|p| p.to_str().map(|s| s.to_owned())), + RunMode::Script(_) | RunMode::InstallPip(_) => None, // handled by run_script + RunMode::Repl => Some(String::new()), + }; + + if let Some(path) = path0 { + vm.insert_sys_path(vm.new_pyobj(path))?; + } + } + // Enable faulthandler if -X faulthandler, PYTHONFAULTHANDLER or -X dev is set // _PyFaulthandler_Init() if vm.state.config.settings.faulthandler { diff --git a/src/settings.rs b/src/settings.rs index f77db4d159..a63f1a07cc 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -339,6 +339,7 @@ pub fn parse_opts() -> Result<(Settings, RunMode), lexopt::Error> { /// Helper function to retrieve a sequence of paths from an environment variable. fn get_paths(env_variable_name: &str) -> impl Iterator + '_ { env::var_os(env_variable_name) + .filter(|v| !v.is_empty()) .into_iter() .flat_map(move |paths| { split_paths(&paths)