Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Lib/test/test_cmd_line_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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:
Expand Down
29 changes: 19 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, '')",
"<embedded>".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!(
Expand All @@ -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<String> = 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))?;
}
}
Comment on lines +183 to +197
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Search for current_dir usage patterns and error handling
rg -nP -C3 'current_dir\(\)' --type=rust

Repository: RustPython/RustPython

Length of output: 944


🏁 Script executed:

#!/bin/bash
# Read the context around the reviewed lines to understand the broader context
cat -n src/lib.rs | sed -n '170,210p'

Repository: RustPython/RustPython

Length of output: 1904


🏁 Script executed:

#!/bin/bash
# Search for RunMode enum definition and Module variant usage
rg -nP 'enum RunMode|RunMode::Module' --type=rust -B2 -A2 | head -50

# Search for tests related to this initialization code
rg -nP 'test.*current_dir|test.*path0|test.*Module' --type=rust | head -30

Repository: RustPython/RustPython

Length of output: 1151


Consider logging a warning for Module mode when current directory is unavailable, consistent with site import error handling.

The sys.path[0] initialization for RunMode::Module silently converts env::current_dir() errors to None (lines 187-189), which is inconsistent with the site import error handling above it (lines 177-180). While graceful degradation is acceptable, adding a warning log for Module mode failures would improve debugging and match the established error handling pattern in this initialization sequence.

🤖 Prompt for AI Agents
In src/lib.rs around lines 183 to 197, the RunMode::Module branch silently drops
errors from env::current_dir(); instead of swallowing the error, capture the
Result error and emit a warning using the same warning/logging mechanism used
above for site import failures, then fall back to None; i.e., if
env::current_dir() fails, log a warning that includes the error and context
(Module mode failing to determine current directory) and then proceed with None
so behavior is unchanged but diagnostics are improved.


// Enable faulthandler if -X faulthandler, PYTHONFAULTHANDLER or -X dev is set
// _PyFaulthandler_Init()
if vm.state.config.settings.faulthandler {
Expand Down
1 change: 1 addition & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = String> + '_ {
env::var_os(env_variable_name)
.filter(|v| !v.is_empty())
.into_iter()
.flat_map(move |paths| {
split_paths(&paths)
Expand Down
Loading