Skip to content
Open
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
42 changes: 34 additions & 8 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,34 @@ fn secs_since_epoch(ft: u64) -> f64 {
/// Sets ``errno`` on the exception so CPython tests that check
/// ``exception.errno == errno.ENOENT`` pass.
fn io_err_to_pyerr(err: io::Error) -> PyErr {
let raw_os_error = err.raw_os_error();
let msg = err.to_string();
Python::with_gil(|py| {
let exc_type: Bound<'_, pyo3::types::PyType> = match err.kind() {
io::ErrorKind::NotFound => py.get_type::<pyo3::exceptions::PyFileNotFoundError>(),
io::ErrorKind::PermissionDenied => py.get_type::<pyo3::exceptions::PyPermissionError>(),
io::ErrorKind::AlreadyExists => py.get_type::<pyo3::exceptions::PyFileExistsError>(),
_ => py.get_type::<pyo3::exceptions::PyOSError>(),
let (exc_type, errno) = match err.kind() {
io::ErrorKind::NotFound => (
py.get_type::<pyo3::exceptions::PyFileNotFoundError>(),
ENOENT,
),
io::ErrorKind::PermissionDenied => {
(py.get_type::<pyo3::exceptions::PyPermissionError>(), EACCES)
}
io::ErrorKind::AlreadyExists => {
(py.get_type::<pyo3::exceptions::PyFileExistsError>(), EEXIST)
}
_ => (
py.get_type::<pyo3::exceptions::PyOSError>(),
err.raw_os_error().unwrap_or(0),
),
};
let errno_val: PyObject = raw_os_error.into_pyobject(py).unwrap().into_any().unbind();
let errno_val: PyObject = errno.into_pyobject(py).unwrap().into_any().unbind();
PyErr::from_type(exc_type, (errno_val, msg))
})
}

// POSIX errno constants (available on all platforms via libc)
const ENOENT: i32 = 2;
const EACCES: i32 = 13;
const EEXIST: i32 = 17;

/// Retrieve ``std::fs::Metadata``, releasing the GIL.
///
/// If ``follow_symlinks`` is true, follows symlinks (``std::fs::metadata``).
Expand Down Expand Up @@ -427,11 +441,22 @@ pub fn resolve(path: &OsStr, strict: bool) -> PyResult<std::path::PathBuf> {
})
});
match result {
Ok(p) => Ok(p),
Ok(p) => Ok(strip_extended_prefix(p)),
Err(e) => Err(io_err_to_pyerr(e)),
}
}

/// Strip Windows extended-length prefix (``\\?\``) from a path.
/// ``std::fs::canonicalize`` on Windows returns paths with this prefix.
fn strip_extended_prefix(path: std::path::PathBuf) -> std::path::PathBuf {
let s = path.to_string_lossy();
if let Some(rest) = s.strip_prefix("\\\\?\\") {
std::path::PathBuf::from(rest)
} else {
path
}
}

// ═══════════════════════════════════════════════════════════════════════
// PathInfo — cached stat result (CPython 3.12+)
// ═══════════════════════════════════════════════════════════════════════
Expand Down Expand Up @@ -543,6 +568,7 @@ fn resolve_non_strict(path: &StdPath) -> Result<std::path::PathBuf, io::Error> {
}
Err(e)
if e.kind() == io::ErrorKind::NotFound
|| e.kind() == io::ErrorKind::PermissionDenied
|| e.kind() == io::ErrorKind::NotADirectory
|| is_eloop(&e) =>
{
Expand Down
Loading
Loading