Skip to content

Commit de0e76b

Browse files
committed
socket connect and bind throws OSError with indicative error message
1 parent 3fae8cc commit de0e76b

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

tests/snippets/stdlib_socket.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,17 @@
2929
with assertRaises(TypeError):
3030
s.connect(("127.0.0.1", 8888, 8888))
3131

32+
with assertRaises(OSError):
33+
# Lets hope nobody is listening on port 1
34+
s.connect(("127.0.0.1", 1))
35+
3236
with assertRaises(TypeError):
3337
s.bind(("127.0.0.1", 8888, 8888))
3438

39+
with assertRaises(OSError):
40+
# Lets hope nobody run this test on machine with ip 1.2.3.4
41+
s.bind(("1.2.3.4", 8888))
42+
3543
with assertRaises(TypeError):
3644
s.bind((888, 8888))
3745

@@ -74,6 +82,10 @@
7482
sock1.close()
7583
sock3.close()
7684

85+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
86+
with assertRaises(OSError):
87+
s.bind(("1.2.3.4", 888))
88+
7789
### Errors
7890
with assertRaises(OSError):
7991
socket.socket(100, socket.SOCK_STREAM)

vm/src/stdlib/socket.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,18 @@ fn socket_connect(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
172172
let mut socket = get_socket(zelf);
173173

174174
match socket.socket_kind {
175-
SocketKind::Stream => {
176-
if let Ok(stream) = TcpStream::connect(address_string) {
175+
SocketKind::Stream => match TcpStream::connect(address_string) {
176+
Ok(stream) => {
177177
socket.con = Some(Connection::TcpStream(stream));
178178
Ok(vm.get_none())
179-
} else {
180-
// TODO: Socket error
181-
Err(vm.new_type_error("socket failed".to_string()))
182179
}
183-
}
180+
Err(s) => Err(vm.new_os_error(s.to_string())),
181+
},
184182
SocketKind::Dgram => {
185183
if let Some(Connection::UdpSocket(con)) = &socket.con {
186184
match con.connect(address_string) {
187185
Ok(_) => Ok(vm.get_none()),
188-
// TODO: Socket error
189-
Err(_) => Err(vm.new_type_error("socket failed".to_string())),
186+
Err(s) => Err(vm.new_os_error(s.to_string())),
190187
}
191188
} else {
192189
Err(vm.new_type_error("".to_string()))
@@ -207,24 +204,20 @@ fn socket_bind(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
207204
let mut socket = get_socket(zelf);
208205

209206
match socket.socket_kind {
210-
SocketKind::Stream => {
211-
if let Ok(stream) = TcpListener::bind(address_string) {
207+
SocketKind::Stream => match TcpListener::bind(address_string) {
208+
Ok(stream) => {
212209
socket.con = Some(Connection::TcpListener(stream));
213210
Ok(vm.get_none())
214-
} else {
215-
// TODO: Socket error
216-
Err(vm.new_type_error("socket failed".to_string()))
217211
}
218-
}
219-
SocketKind::Dgram => {
220-
if let Ok(dgram) = UdpSocket::bind(address_string) {
212+
Err(s) => Err(vm.new_os_error(s.to_string())),
213+
},
214+
SocketKind::Dgram => match UdpSocket::bind(address_string) {
215+
Ok(dgram) => {
221216
socket.con = Some(Connection::UdpSocket(dgram));
222217
Ok(vm.get_none())
223-
} else {
224-
// TODO: Socket error
225-
Err(vm.new_type_error("socket failed".to_string()))
226218
}
227-
}
219+
Err(s) => Err(vm.new_os_error(s.to_string())),
220+
},
228221
}
229222
}
230223

0 commit comments

Comments
 (0)