Skip to content

Commit 77495a2

Browse files
committed
Move stdlib::os::errno to common
1 parent b382d3c commit 77495a2

File tree

6 files changed

+28
-42
lines changed

6 files changed

+28
-42
lines changed

common/src/crt_fd.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,10 @@ pub type Offset = libc::off_t;
2020
#[cfg(windows)]
2121
pub type Offset = libc::c_longlong;
2222

23-
// copied from stdlib::os
24-
#[cfg(windows)]
25-
fn errno() -> io::Error {
26-
let err = io::Error::last_os_error();
27-
// FIXME: probably not ideal, we need a bigger dichotomy between GetLastError and errno
28-
if err.raw_os_error() == Some(0) {
29-
extern "C" {
30-
fn _get_errno(pValue: *mut i32) -> i32;
31-
}
32-
let mut e = 0;
33-
unsafe { suppress_iph!(_get_errno(&mut e)) };
34-
io::Error::from_raw_os_error(e)
35-
} else {
36-
err
37-
}
38-
}
39-
#[cfg(not(windows))]
40-
fn errno() -> io::Error {
41-
io::Error::last_os_error()
42-
}
43-
4423
#[inline]
4524
fn cvt<T, I: num_traits::PrimInt>(ret: I, f: impl FnOnce(I) -> T) -> io::Result<T> {
4625
if ret < I::zero() {
47-
Err(errno())
26+
Err(crate::os::errno())
4827
} else {
4928
Ok(f(ret))
5029
}

common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod float_ops;
1717
pub mod hash;
1818
pub mod linked_list;
1919
pub mod lock;
20+
pub mod os;
2021
pub mod rc;
2122
pub mod refcount;
2223
pub mod static_cell;

common/src/os.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// TODO: we can move more os-specific bindings/interfaces from stdlib::{os, posix, nt} to here
2+
3+
use std::io;
4+
5+
#[cfg(windows)]
6+
pub fn errno() -> io::Error {
7+
let err = io::Error::last_os_error();
8+
// FIXME: probably not ideal, we need a bigger dichotomy between GetLastError and errno
9+
if err.raw_os_error() == Some(0) {
10+
extern "C" {
11+
fn _get_errno(pValue: *mut i32) -> i32;
12+
}
13+
let mut e = 0;
14+
unsafe { suppress_iph!(_get_errno(&mut e)) };
15+
io::Error::from_raw_os_error(e)
16+
} else {
17+
err
18+
}
19+
}
20+
#[cfg(not(windows))]
21+
pub fn errno() -> io::Error {
22+
io::Error::last_os_error()
23+
}

stdlib/src/socket.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1927,7 +1927,7 @@ mod _socket {
19271927
use winapi::um::winsock2::closesocket as close;
19281928
let ret = unsafe { close(x as _) };
19291929
if ret < 0 {
1930-
let err = crate::vm::stdlib::os::errno();
1930+
let err = crate::common::os::errno();
19311931
if err.raw_os_error() != Some(errcode!(ECONNRESET)) {
19321932
return Err(err.to_pyexception(vm));
19331933
}

vm/src/stdlib/nt.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
1212
pub(crate) mod module {
1313
use crate::{
1414
builtins::{PyStrRef, PyTupleRef},
15-
common::{crt_fd::Fd, suppress_iph},
15+
common::{crt_fd::Fd, os::errno, suppress_iph},
1616
convert::ToPyException,
1717
function::Either,
1818
function::OptionalArg,
1919
stdlib::os::{
2020
errno_err, DirFd, FollowSymlinks, PyPathLike, SupportFunc, TargetIsDirectory, _os,
21-
errno,
2221
},
2322
PyResult, TryFromObject, VirtualMachine,
2423
};

vm/src/stdlib/os.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -288,23 +288,7 @@ impl ToPyException for IOErrorBuilder {
288288
/// Convert the error stored in the `errno` variable into an Exception
289289
#[inline]
290290
pub fn errno_err(vm: &VirtualMachine) -> PyBaseExceptionRef {
291-
errno().to_pyexception(vm)
292-
}
293-
294-
#[cfg(windows)]
295-
pub fn errno() -> io::Error {
296-
let err = io::Error::last_os_error();
297-
// FIXME: probably not ideal, we need a bigger dichotomy between GetLastError and errno
298-
if err.raw_os_error() == Some(0) {
299-
io::Error::from_raw_os_error(super::msvcrt::get_errno())
300-
} else {
301-
err
302-
}
303-
}
304-
305-
#[cfg(not(windows))]
306-
pub fn errno() -> io::Error {
307-
io::Error::last_os_error()
291+
crate::common::os::errno().to_pyexception(vm)
308292
}
309293

310294
#[allow(dead_code)]

0 commit comments

Comments
 (0)