Skip to content
Merged
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
40 changes: 28 additions & 12 deletions crates/stdlib/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2619,15 +2619,15 @@ mod _socket {
#[inline]
fn detach(zelf: &Py<Self>) -> i64 {
let sock = zelf.take_sock();
sock.map_or(INVALID_SOCKET as i64, |s| into_sock_fileno(s) as i64)
sock.map_or(-1, |s| into_sock_fileno(s) as i64)
}

#[pymethod]
fn fileno(zelf: &Py<Self>) -> i64 {
zelf.sock
.read()
.as_ref()
.map_or(INVALID_SOCKET as i64, |s| sock_fileno(s) as i64)
.map_or(-1, |s| sock_fileno(s) as i64)
}

#[pymethod]
Expand Down Expand Up @@ -3938,16 +3938,8 @@ mod _socket {
}
}

pub(super) const INVALID_SOCKET: RawSocket = {
#[cfg(unix)]
{
-1
}
#[cfg(windows)]
{
host_socket::INVALID_RAW_SOCKET as RawSocket
}
};
#[cfg(windows)]
pub(super) const INVALID_SOCKET: RawSocket = host_socket::INVALID_RAW_SOCKET as RawSocket;

fn convert_socket_error(
vm: &VirtualMachine,
Expand Down Expand Up @@ -4069,6 +4061,30 @@ mod _socket {
.ok_or_else(|| vm.new_overflow_error("CMSG_SPACE() argument out of range"))
}

#[cfg(test)]
mod socket_tests {
use super::*;

#[test]
fn closed_socket_descriptors_are_negative_one() {
let builder = crate::vm::Interpreter::builder(Default::default());
let defs = crate::stdlib_module_defs(&builder.ctx);
let interp = builder.add_native_modules(&defs).build();
interp.enter(|vm| {
vm.import("_socket", 0).unwrap();
let socket = PySocket::default().into_ref(&vm.ctx);
for _ in 0..2 {
assert_eq!(PySocket::fileno(&socket), -1);
assert_eq!(PySocket::detach(&socket), -1);
}
assert_eq!(
PySocket::repr_str(&socket, vm).unwrap(),
"<socket object, fd=-1, family=0, type=0, proto=0>",
);
});
}
}

#[cfg(test)]
mod address_tests {
use super::*;
Expand Down
Loading