Visitar URL original
Socket OSError by palaviv · Pull Request #632 · RustPython/RustPython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
socket connect and bind throws OSError with indicative error message
  • Loading branch information
palaviv committed Mar 8, 2019
commit de0e76baf8890d42a97e7b2bb1e6fb9db1e62df8
12 changes: 12 additions & 0 deletions tests/snippets/stdlib_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@
with assertRaises(TypeError):
s.connect(("127.0.0.1", 8888, 8888))

with assertRaises(OSError):
# Lets hope nobody is listening on port 1
s.connect(("127.0.0.1", 1))

with assertRaises(TypeError):
s.bind(("127.0.0.1", 8888, 8888))

with assertRaises(OSError):
# Lets hope nobody run this test on machine with ip 1.2.3.4
s.bind(("1.2.3.4", 8888))

with assertRaises(TypeError):
s.bind((888, 8888))

Expand Down Expand Up @@ -74,6 +82,10 @@
sock1.close()
sock3.close()

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
with assertRaises(OSError):
s.bind(("1.2.3.4", 888))

### Errors
with assertRaises(OSError):
socket.socket(100, socket.SOCK_STREAM)
Expand Down
33 changes: 13 additions & 20 deletions vm/src/stdlib/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,18 @@ fn socket_connect(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let mut socket = get_socket(zelf);

match socket.socket_kind {
SocketKind::Stream => {
if let Ok(stream) = TcpStream::connect(address_string) {
SocketKind::Stream => match TcpStream::connect(address_string) {
Ok(stream) => {
socket.con = Some(Connection::TcpStream(stream));
Ok(vm.get_none())
} else {
// TODO: Socket error
Err(vm.new_type_error("socket failed".to_string()))
}
}
Err(s) => Err(vm.new_os_error(s.to_string())),
},
SocketKind::Dgram => {
if let Some(Connection::UdpSocket(con)) = &socket.con {
match con.connect(address_string) {
Ok(_) => Ok(vm.get_none()),
// TODO: Socket error
Err(_) => Err(vm.new_type_error("socket failed".to_string())),
Err(s) => Err(vm.new_os_error(s.to_string())),
}
} else {
Err(vm.new_type_error("".to_string()))
Expand All @@ -207,24 +204,20 @@ fn socket_bind(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let mut socket = get_socket(zelf);

match socket.socket_kind {
SocketKind::Stream => {
if let Ok(stream) = TcpListener::bind(address_string) {
SocketKind::Stream => match TcpListener::bind(address_string) {
Ok(stream) => {
socket.con = Some(Connection::TcpListener(stream));
Ok(vm.get_none())
} else {
// TODO: Socket error
Err(vm.new_type_error("socket failed".to_string()))
}
}
SocketKind::Dgram => {
if let Ok(dgram) = UdpSocket::bind(address_string) {
Err(s) => Err(vm.new_os_error(s.to_string())),
},
SocketKind::Dgram => match UdpSocket::bind(address_string) {
Ok(dgram) => {
socket.con = Some(Connection::UdpSocket(dgram));
Ok(vm.get_none())
} else {
// TODO: Socket error
Err(vm.new_type_error("socket failed".to_string()))
}
}
Err(s) => Err(vm.new_os_error(s.to_string())),
},
}
}

Expand Down