From e68d9c266fe78cfef3ff72f0e53abcbd4c00b5ca Mon Sep 17 00:00:00 2001 From: Leesoo Ahn Date: Sun, 19 Jul 2026 13:32:46 +0900 Subject: [PATCH] interpreter: canonicalize when initializing search paths dynamically (#8299) Dual-Root Mismatch in RustPython: Unlike CPython, RustPython manages the standard library in two separate locations in the workspace: - Workspace 'Lib/': Contains the test suite ('Lib/test/...') and standard library files. - crates/pylib/Lib/: A helper directory used for packaging. 'crates/pylib/Lib' is a symlink pointing to the real workspace '../../Lib/'. Runtime Directory Discrepancy: - 'sys.path' contains 'crates/pylib/Lib' (the path to the symlink). - Thus, when 'test.support' is imported, its '__file__' is resolved relative to the symlink: '/opt/RustPython/crates/pylib/Lib/test/support/__init__.py'. - Consequently, 'STDLIB_DIR' resolves to the symlink root: '/opt/RustPython/crates/pylib/Lib'. - However, since 'test_program.py' is executed directly from the workspace, its 'pkg_dir' is: '/opt/RustPython/Lib/test/test_unittest/testmock'. - When 'os.path.relpath(pkg_dir, top_dir)' is called: --- os.path.relpath( "/opt/RustPython/Lib/test/test_unittest/testmock", "/opt/RustPython/crates/pylib/Lib" ) # Result: "../../../Lib/test/test_unittest/testmock" --- Because the relative path starts with '..', the assertion fails. Assisted-by: Gemini --- Lib/test/test_unittest/test_program.py | 1 - src/interpreter.rs | 18 +++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_unittest/test_program.py b/Lib/test/test_unittest/test_program.py index 26808dc00..8ed92373e 100644 --- a/Lib/test/test_unittest/test_program.py +++ b/Lib/test/test_unittest/test_program.py @@ -505,7 +505,6 @@ class TestCommandLineArgs(unittest.TestCase): self.assertEqual(program.testNamePatterns, ['*foo*', '*bar*', '*pat*']) - @unittest.expectedFailureIf(sys.platform != "win32", "TODO: RUSTPYTHON") def testSelectedTestNamesFunctionalTest(self): def run_unittest(args): # Use -E to ignore PYTHONSAFEPATH env var diff --git a/src/interpreter.rs b/src/interpreter.rs index 230192d1e..89e24c25b 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -57,7 +57,23 @@ fn setup_dynamic_stdlib(vm: &mut crate::VirtualMachine) { use rustpython_vm::common::rc::PyRc; let state = PyRc::get_mut(&mut vm.state).unwrap(); - let paths = collect_stdlib_paths(); + let paths: Vec = collect_stdlib_paths() + .into_iter() + .map(|p| { + std::fs::canonicalize(&p) + .map(|canonical| { + let s = canonical.to_string_lossy(); + #[cfg(windows)] + { + if let Some(stripped) = s.strip_prefix(r"\\?\") { + return stripped.to_owned(); + } + } + s.into_owned() + }) + .unwrap_or(p) + }) + .collect(); // Set stdlib_dir to the first stdlib path if available if let Some(first_path) = paths.first() {