(&self, vm: &VirtualMachine)
| 1380 | |
| 1381 | impl ToOSErrorBuilder for std::io::Error { |
| 1382 | fn to_os_error_builder(&self, vm: &VirtualMachine) -> OSErrorBuilder { |
| 1383 | use crate::common::os::ErrorExt; |
| 1384 | |
| 1385 | let errno = self.posix_errno(); |
| 1386 | #[cfg(windows)] |
| 1387 | let msg = 'msg: { |
| 1388 | // Use C runtime's strerror for POSIX errno values. |
| 1389 | // For Windows-specific error codes, fall back to FormatMessage. |
| 1390 | const MAX_POSIX_ERRNO: i32 = 127; |
| 1391 | if errno > 0 && errno <= MAX_POSIX_ERRNO { |
| 1392 | let ptr = unsafe { libc::strerror(errno) }; |
| 1393 | if !ptr.is_null() { |
| 1394 | let s = unsafe { core::ffi::CStr::from_ptr(ptr) }.to_string_lossy(); |
| 1395 | if !s.starts_with("Unknown error") { |
| 1396 | break 'msg s.into_owned(); |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | self.to_string() |
| 1401 | }; |
| 1402 | #[cfg(unix)] |
| 1403 | let msg = { |
| 1404 | let ptr = unsafe { libc::strerror(errno) }; |
| 1405 | if !ptr.is_null() { |
| 1406 | unsafe { core::ffi::CStr::from_ptr(ptr) } |
| 1407 | .to_string_lossy() |
| 1408 | .into_owned() |
| 1409 | } else { |
| 1410 | self.to_string() |
| 1411 | } |
| 1412 | }; |
| 1413 | #[cfg(not(any(windows, unix)))] |
| 1414 | let msg = self.to_string(); |
| 1415 | |
| 1416 | #[allow(unused_mut)] |
| 1417 | let mut builder = OSErrorBuilder::with_errno(errno, msg, vm); |
| 1418 | #[cfg(windows)] |
| 1419 | if let Some(winerror) = self.raw_os_error() { |
| 1420 | builder = builder.winerror(winerror.to_pyobject(vm)); |
| 1421 | } |
| 1422 | builder |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | impl ToPyException for std::io::Error { |
no test coverage detected