Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 57b4b4a

Browse files
authored
fix sys path (#6537)
1 parent 1856415 commit 57b4b4a

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

Lib/test/test_cmd_line_script.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,6 @@ def test_issue8202(self):
411411
script_name, script_name, script_dir, 'test_pkg',
412412
importlib.machinery.SourceFileLoader)
413413

414-
# TODO: RUSTPYTHON
415-
@unittest.expectedFailure
416414
def test_issue8202_dash_c_file_ignored(self):
417415
# Make sure a "-c" file in the current directory
418416
# does not alter the value of sys.path[0]
@@ -713,8 +711,6 @@ def test_syntaxerror_null_bytes_in_multiline_string(self):
713711
]
714712
)
715713

716-
# TODO: RUSTPYTHON
717-
@unittest.expectedFailure
718714
def test_consistent_sys_path_for_direct_execution(self):
719715
# This test case ensures that the following all give the same
720716
# sys.path configuration:

src/lib.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,9 @@ fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> {
169169

170170
let scope = setup_main_module(vm)?;
171171

172-
if !vm.state.config.settings.safe_path {
173-
// TODO: The prepending path depends on running mode
174-
// See https://docs.python.org/3/using/cmdline.html#cmdoption-P
175-
vm.run_code_string(
176-
vm.new_scope_with_builtins(),
177-
"import sys; sys.path.insert(0, '')",
178-
"<embedded>".to_owned(),
179-
)?;
180-
}
181-
172+
// Import site first, before setting sys.path[0]
173+
// This matches CPython's behavior where site.removeduppaths() runs
174+
// before sys.path[0] is set, preventing '' from being converted to cwd
182175
let site_result = vm.import("site", 0);
183176
if site_result.is_err() {
184177
warn!(
@@ -187,6 +180,22 @@ fn run_rustpython(vm: &VirtualMachine, run_mode: RunMode) -> PyResult<()> {
187180
);
188181
}
189182

183+
// _PyPathConfig_ComputeSysPath0 - set sys.path[0] after site import
184+
if !vm.state.config.settings.safe_path {
185+
let path0: Option<String> = match &run_mode {
186+
RunMode::Command(_) => Some(String::new()),
187+
RunMode::Module(_) => env::current_dir()
188+
.ok()
189+
.and_then(|p| p.to_str().map(|s| s.to_owned())),
190+
RunMode::Script(_) | RunMode::InstallPip(_) => None, // handled by run_script
191+
RunMode::Repl => Some(String::new()),
192+
};
193+
194+
if let Some(path) = path0 {
195+
vm.insert_sys_path(vm.new_pyobj(path))?;
196+
}
197+
}
198+
190199
// Enable faulthandler if -X faulthandler, PYTHONFAULTHANDLER or -X dev is set
191200
// _PyFaulthandler_Init()
192201
if vm.state.config.settings.faulthandler {

src/settings.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ pub fn parse_opts() -> Result<(Settings, RunMode), lexopt::Error> {
339339
/// Helper function to retrieve a sequence of paths from an environment variable.
340340
fn get_paths(env_variable_name: &str) -> impl Iterator<Item = String> + '_ {
341341
env::var_os(env_variable_name)
342+
.filter(|v| !v.is_empty())
342343
.into_iter()
343344
.flat_map(move |paths| {
344345
split_paths(&paths)

0 commit comments

Comments
 (0)