From cf0a9207438c54948988830ef4a96a6ab05bfff3 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Fri, 19 Apr 2019 15:50:06 +0300 Subject: [PATCH 1/4] Add socket.fileno for unix --- tests/snippets/stdlib_socket.py | 13 +++++++++++++ vm/src/stdlib/socket.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/tests/snippets/stdlib_socket.py b/tests/snippets/stdlib_socket.py index 79efaa3639..d7afe1bd35 100644 --- a/tests/snippets/stdlib_socket.py +++ b/tests/snippets/stdlib_socket.py @@ -1,4 +1,5 @@ import socket +import os from testutils import assertRaises MESSAGE_A = b'aaaa' @@ -21,6 +22,18 @@ recv_b = connector.recv(len(MESSAGE_B)) assert recv_a == MESSAGE_A assert recv_b == MESSAGE_B + +# fileno +if os.name == "posix": + connector_fd = connector.fileno() + connection_fd = connection.fileno() + os.write(connector_fd, MESSAGE_A) + connection.send(MESSAGE_B) + recv_a = connection.recv(len(MESSAGE_A)) + recv_b = os.read(connector_fd, (len(MESSAGE_B))) + assert recv_a == MESSAGE_A + assert recv_b == MESSAGE_B + connection.close() connector.close() listener.close() diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index e893e0dab7..a7a87d554c 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -111,6 +111,19 @@ impl Write for Connection { } } +#[cfg(unix)] +use std::os::unix::io::{AsRawFd, RawFd}; +#[cfg(unix)] +impl AsRawFd for Connection { + fn as_raw_fd(&self) -> RawFd { + match self { + Connection::TcpListener(con) => con.as_raw_fd(), + Connection::UdpSocket(con) => con.as_raw_fd(), + Connection::TcpStream(con) => con.as_raw_fd(), + } + } +} + #[derive(Debug)] pub struct Socket { address_family: AddressFamily, @@ -387,6 +400,25 @@ fn socket_close(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { Ok(vm.get_none()) } +#[cfg(unix)] +fn socket_fileno(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { + use std::os::unix::io::AsRawFd; + arg_check!(vm, args, required = [(zelf, None)]); + + let socket = get_socket(zelf); + + let fileno = match socket.con.borrow_mut().as_mut() { + Some(v) => v.as_raw_fd(), + None => return Err(vm.new_type_error("".to_string())), + }; + Ok(vm.ctx.new_int(i64::from(fileno))) +} + +#[cfg(all(not(unix)))] +fn socket_fileno(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { + unimplemented!(); +} + fn socket_getsockname(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(zelf, None)]); let socket = get_socket(zelf); @@ -424,6 +456,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { "getsockname" => ctx.new_rustfunc(socket_getsockname), "sendto" => ctx.new_rustfunc(socket_sendto), "recvfrom" => ctx.new_rustfunc(socket_recvfrom), + "fileno" => ctx.new_rustfunc(socket_fileno), }); py_module!(vm, "socket", { From e7b059ed9c7c4b961dfce3614a0a135f72305f5a Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Fri, 19 Apr 2019 16:00:00 +0300 Subject: [PATCH 2/4] Add socket.fileno for windows --- tests/snippets/stdlib_socket.py | 17 ++++++++--------- vm/src/stdlib/socket.rs | 29 ++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/tests/snippets/stdlib_socket.py b/tests/snippets/stdlib_socket.py index d7afe1bd35..16c2f96ce1 100644 --- a/tests/snippets/stdlib_socket.py +++ b/tests/snippets/stdlib_socket.py @@ -24,15 +24,14 @@ assert recv_b == MESSAGE_B # fileno -if os.name == "posix": - connector_fd = connector.fileno() - connection_fd = connection.fileno() - os.write(connector_fd, MESSAGE_A) - connection.send(MESSAGE_B) - recv_a = connection.recv(len(MESSAGE_A)) - recv_b = os.read(connector_fd, (len(MESSAGE_B))) - assert recv_a == MESSAGE_A - assert recv_b == MESSAGE_B +connector_fd = connector.fileno() +connection_fd = connection.fileno() +os.write(connector_fd, MESSAGE_A) +connection.send(MESSAGE_B) +recv_a = connection.recv(len(MESSAGE_A)) +recv_b = os.read(connector_fd, (len(MESSAGE_B))) +assert recv_a == MESSAGE_A +assert recv_b == MESSAGE_B connection.close() connector.close() diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index a7a87d554c..6e65ddfd9b 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -124,6 +124,19 @@ impl AsRawFd for Connection { } } +#[cfg(windows)] +use std::os::windows::io::{AsRawSocket, RawSocket}; +#[cfg(windows)] +impl AsRawSocket for Connection { + fn as_raw_socket(&self) -> RawSocket { + match self { + Connection::TcpListener(con) => con.as_raw_socket(), + Connection::UdpSocket(con) => con.as_raw_socket(), + Connection::TcpStream(con) => con.as_raw_socket(), + } + } +} + #[derive(Debug)] pub struct Socket { address_family: AddressFamily, @@ -414,7 +427,21 @@ fn socket_fileno(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { Ok(vm.ctx.new_int(i64::from(fileno))) } -#[cfg(all(not(unix)))] +#[cfg(windows)] +fn socket_fileno(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { + use std::os::windows::io::AsRawSocket; + arg_check!(vm, args, required = [(zelf, None)]); + + let socket = get_socket(zelf); + + let fileno = match socket.con.borrow_mut().as_mut() { + Some(v) => v.as_raw_socket(), + None => return Err(vm.new_type_error("".to_string())), + }; + Ok(vm.ctx.new_int(i64::from(fileno))) +} + +#[cfg(all(not(unix), not(windows)))] fn socket_fileno(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { unimplemented!(); } From 2bc72dae9ff03f4afdf357d31e933d04f2db5ae3 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Fri, 19 Apr 2019 16:10:55 +0300 Subject: [PATCH 3/4] Move fileno logic to Connection impl --- vm/src/stdlib/socket.rs | 78 +++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 49 deletions(-) diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index 6e65ddfd9b..9d7a703e1f 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -86,6 +86,33 @@ impl Connection { _ => Err(io::Error::new(io::ErrorKind::Other, "oh no!")), } } + + #[cfg(unix)] + fn fileno(&self) -> i64 { + use std::os::unix::io::AsRawFd; + let raw_fd = match self { + Connection::TcpListener(con) => con.as_raw_fd(), + Connection::UdpSocket(con) => con.as_raw_fd(), + Connection::TcpStream(con) => con.as_raw_fd(), + }; + raw_fd as i64 + } + + #[cfg(windows)] + fn fileno(&self) -> i64 { + use std::os::windows::io::AsRawSocket; + let raw_fd = match self { + Connection::TcpListener(con) => con.as_raw_socket(), + Connection::UdpSocket(con) => con.as_raw_socket(), + Connection::TcpStream(con) => con.as_raw_socket(), + }; + raw_fd as i64 + } + + #[cfg(all(not(unix), not(windows)))] + fn fileno(&self) -> i64 { + unimplemented!(); + } } impl Read for Connection { @@ -111,32 +138,6 @@ impl Write for Connection { } } -#[cfg(unix)] -use std::os::unix::io::{AsRawFd, RawFd}; -#[cfg(unix)] -impl AsRawFd for Connection { - fn as_raw_fd(&self) -> RawFd { - match self { - Connection::TcpListener(con) => con.as_raw_fd(), - Connection::UdpSocket(con) => con.as_raw_fd(), - Connection::TcpStream(con) => con.as_raw_fd(), - } - } -} - -#[cfg(windows)] -use std::os::windows::io::{AsRawSocket, RawSocket}; -#[cfg(windows)] -impl AsRawSocket for Connection { - fn as_raw_socket(&self) -> RawSocket { - match self { - Connection::TcpListener(con) => con.as_raw_socket(), - Connection::UdpSocket(con) => con.as_raw_socket(), - Connection::TcpStream(con) => con.as_raw_socket(), - } - } -} - #[derive(Debug)] pub struct Socket { address_family: AddressFamily, @@ -413,37 +414,16 @@ fn socket_close(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { Ok(vm.get_none()) } -#[cfg(unix)] -fn socket_fileno(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - use std::os::unix::io::AsRawFd; - arg_check!(vm, args, required = [(zelf, None)]); - - let socket = get_socket(zelf); - - let fileno = match socket.con.borrow_mut().as_mut() { - Some(v) => v.as_raw_fd(), - None => return Err(vm.new_type_error("".to_string())), - }; - Ok(vm.ctx.new_int(i64::from(fileno))) -} - -#[cfg(windows)] fn socket_fileno(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - use std::os::windows::io::AsRawSocket; arg_check!(vm, args, required = [(zelf, None)]); let socket = get_socket(zelf); let fileno = match socket.con.borrow_mut().as_mut() { - Some(v) => v.as_raw_socket(), + Some(v) => v.fileno(), None => return Err(vm.new_type_error("".to_string())), }; - Ok(vm.ctx.new_int(i64::from(fileno))) -} - -#[cfg(all(not(unix), not(windows)))] -fn socket_fileno(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { - unimplemented!(); + Ok(vm.ctx.new_int(fileno)) } fn socket_getsockname(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { From f42c2a6ba527901eadc1429a5a3a7008d34395e9 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Fri, 19 Apr 2019 16:58:43 +0300 Subject: [PATCH 4/4] Test fileno only on posix --- tests/snippets/stdlib_socket.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/snippets/stdlib_socket.py b/tests/snippets/stdlib_socket.py index 16c2f96ce1..d7afe1bd35 100644 --- a/tests/snippets/stdlib_socket.py +++ b/tests/snippets/stdlib_socket.py @@ -24,14 +24,15 @@ assert recv_b == MESSAGE_B # fileno -connector_fd = connector.fileno() -connection_fd = connection.fileno() -os.write(connector_fd, MESSAGE_A) -connection.send(MESSAGE_B) -recv_a = connection.recv(len(MESSAGE_A)) -recv_b = os.read(connector_fd, (len(MESSAGE_B))) -assert recv_a == MESSAGE_A -assert recv_b == MESSAGE_B +if os.name == "posix": + connector_fd = connector.fileno() + connection_fd = connection.fileno() + os.write(connector_fd, MESSAGE_A) + connection.send(MESSAGE_B) + recv_a = connection.recv(len(MESSAGE_A)) + recv_b = os.read(connector_fd, (len(MESSAGE_B))) + assert recv_a == MESSAGE_A + assert recv_b == MESSAGE_B connection.close() connector.close()