Skip to content

Commit 5df4706

Browse files
authored
map io::ErrorKind::IsADirectory/NotADirectory to Python on 1.83+ (#4747)
1 parent 82ab509 commit 5df4706

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

newsfragments/4747.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Map `io::ErrorKind::IsADirectory`/`NotADirectory` to the corresponding Python exception on Rust 1.83+

pyo3-build-config/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ pub fn print_feature_cfgs() {
156156
if rustc_minor_version >= 79 {
157157
println!("cargo:rustc-cfg=diagnostic_namespace");
158158
}
159+
160+
if rustc_minor_version >= 83 {
161+
println!("cargo:rustc-cfg=io_error_more");
162+
}
159163
}
160164

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

184189
// allow `Py_3_*` cfgs from the minimum supported version up to the
185190
// maximum minor version (+1 for development for the next)

src/err/impls.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ impl From<PyErr> for io::Error {
2727
} else if err.is_instance_of::<exceptions::PyTimeoutError>(py) {
2828
io::ErrorKind::TimedOut
2929
} else {
30+
#[cfg(io_error_more)]
31+
if err.is_instance_of::<exceptions::PyIsADirectoryError>(py) {
32+
io::ErrorKind::IsADirectory
33+
} else if err.is_instance_of::<exceptions::PyNotADirectoryError>(py) {
34+
io::ErrorKind::NotADirectory
35+
} else {
36+
io::ErrorKind::Other
37+
}
38+
#[cfg(not(io_error_more))]
3039
io::ErrorKind::Other
3140
}
3241
});
@@ -54,6 +63,10 @@ impl From<io::Error> for PyErr {
5463
io::ErrorKind::AlreadyExists => exceptions::PyFileExistsError::new_err(err),
5564
io::ErrorKind::WouldBlock => exceptions::PyBlockingIOError::new_err(err),
5665
io::ErrorKind::TimedOut => exceptions::PyTimeoutError::new_err(err),
66+
#[cfg(io_error_more)]
67+
io::ErrorKind::IsADirectory => exceptions::PyIsADirectoryError::new_err(err),
68+
#[cfg(io_error_more)]
69+
io::ErrorKind::NotADirectory => exceptions::PyNotADirectoryError::new_err(err),
5770
_ => exceptions::PyOSError::new_err(err),
5871
}
5972
}
@@ -167,5 +180,9 @@ mod tests {
167180
check_err(io::ErrorKind::AlreadyExists, "FileExistsError");
168181
check_err(io::ErrorKind::WouldBlock, "BlockingIOError");
169182
check_err(io::ErrorKind::TimedOut, "TimeoutError");
183+
#[cfg(io_error_more)]
184+
check_err(io::ErrorKind::IsADirectory, "IsADirectoryError");
185+
#[cfg(io_error_more)]
186+
check_err(io::ErrorKind::NotADirectory, "NotADirectoryError");
170187
}
171188
}

0 commit comments

Comments
 (0)