Skip to content

Commit

Permalink
map io::ErrorKind::IsADirectory/NotADirectory to Python on 1.83+ (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu authored Nov 30, 2024
1 parent 82ab509 commit 5df4706
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions newsfragments/4747.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Map `io::ErrorKind::IsADirectory`/`NotADirectory` to the corresponding Python exception on Rust 1.83+
5 changes: 5 additions & 0 deletions pyo3-build-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ pub fn print_feature_cfgs() {
if rustc_minor_version >= 79 {
println!("cargo:rustc-cfg=diagnostic_namespace");
}

if rustc_minor_version >= 83 {
println!("cargo:rustc-cfg=io_error_more");
}
}

/// Registers `pyo3`s config names as reachable cfg expressions
Expand All @@ -180,6 +184,7 @@ pub fn print_expected_cfgs() {
println!("cargo:rustc-check-cfg=cfg(diagnostic_namespace)");
println!("cargo:rustc-check-cfg=cfg(c_str_lit)");
println!("cargo:rustc-check-cfg=cfg(rustc_has_once_lock)");
println!("cargo:rustc-check-cfg=cfg(io_error_more)");

// allow `Py_3_*` cfgs from the minimum supported version up to the
// maximum minor version (+1 for development for the next)
Expand Down
17 changes: 17 additions & 0 deletions src/err/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ impl From<PyErr> for io::Error {
} else if err.is_instance_of::<exceptions::PyTimeoutError>(py) {
io::ErrorKind::TimedOut
} else {
#[cfg(io_error_more)]
if err.is_instance_of::<exceptions::PyIsADirectoryError>(py) {
io::ErrorKind::IsADirectory
} else if err.is_instance_of::<exceptions::PyNotADirectoryError>(py) {
io::ErrorKind::NotADirectory
} else {
io::ErrorKind::Other
}
#[cfg(not(io_error_more))]
io::ErrorKind::Other
}
});
Expand Down Expand Up @@ -54,6 +63,10 @@ impl From<io::Error> for PyErr {
io::ErrorKind::AlreadyExists => exceptions::PyFileExistsError::new_err(err),
io::ErrorKind::WouldBlock => exceptions::PyBlockingIOError::new_err(err),
io::ErrorKind::TimedOut => exceptions::PyTimeoutError::new_err(err),
#[cfg(io_error_more)]
io::ErrorKind::IsADirectory => exceptions::PyIsADirectoryError::new_err(err),
#[cfg(io_error_more)]
io::ErrorKind::NotADirectory => exceptions::PyNotADirectoryError::new_err(err),
_ => exceptions::PyOSError::new_err(err),
}
}
Expand Down Expand Up @@ -167,5 +180,9 @@ mod tests {
check_err(io::ErrorKind::AlreadyExists, "FileExistsError");
check_err(io::ErrorKind::WouldBlock, "BlockingIOError");
check_err(io::ErrorKind::TimedOut, "TimeoutError");
#[cfg(io_error_more)]
check_err(io::ErrorKind::IsADirectory, "IsADirectoryError");
#[cfg(io_error_more)]
check_err(io::ErrorKind::NotADirectory, "NotADirectoryError");
}
}

0 comments on commit 5df4706

Please sign in to comment.