MCPcopy Index your code
hub / github.com/RustPython/RustPython / chdir

Function chdir

crates/vm/src/stdlib/os.rs:1491–1527  ·  view source on GitHub ↗
(path: OsPath, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

1489
1490 #[pyfunction]
1491 fn chdir(path: OsPath, vm: &VirtualMachine) -> PyResult<()> {
1492 env::set_current_dir(&path.path)
1493 .map_err(|err| OSErrorBuilder::with_filename(&err, path, vm))?;
1494
1495 #[cfg(windows)]
1496 {
1497 // win32_wchdir()
1498
1499 // On Windows, set the per-drive CWD environment variable (=X:)
1500 // This is required for GetFullPathNameW to work correctly with drive-relative paths
1501
1502 use std::os::windows::ffi::OsStrExt;
1503 use windows_sys::Win32::System::Environment::SetEnvironmentVariableW;
1504
1505 if let Ok(cwd) = env::current_dir() {
1506 let cwd_str = cwd.as_os_str();
1507 let mut cwd_wide: Vec<u16> = cwd_str.encode_wide().collect();
1508
1509 // Check for UNC-like paths (\\server\share or //server/share)
1510 // wcsncmp(new_path, L"\\\\", 2) == 0 || wcsncmp(new_path, L"//", 2) == 0
1511 let is_unc_like_path = cwd_wide.len() >= 2
1512 && ((cwd_wide[0] == b'\\' as u16 && cwd_wide[1] == b'\\' as u16)
1513 || (cwd_wide[0] == b'/' as u16 && cwd_wide[1] == b'/' as u16));
1514
1515 if !is_unc_like_path {
1516 // Create env var name "=X:" where X is the drive letter
1517 let env_name: [u16; 4] = [b'=' as u16, cwd_wide[0], b':' as u16, 0];
1518 cwd_wide.push(0); // null-terminate the path
1519 unsafe {
1520 SetEnvironmentVariableW(env_name.as_ptr(), cwd_wide.as_ptr());
1521 }
1522 }
1523 }
1524 }
1525
1526 Ok(())
1527 }
1528
1529 #[pyfunction]
1530 fn fspath(path: PyObjectRef, vm: &VirtualMachine) -> PyResult<FsPath> {

Callers 1

exec_innerFunction · 0.50

Calls 6

as_os_strMethod · 0.80
collectMethod · 0.80
encode_wideMethod · 0.80
lenMethod · 0.45
pushMethod · 0.45
as_ptrMethod · 0.45

Tested by

no test coverage detected