Skip to content

Commit d58abd3

Browse files
committed
Add timeout test
1 parent 9677f77 commit d58abd3

File tree

3 files changed

+19
-36
lines changed

3 files changed

+19
-36
lines changed

tests/snippets/stdlib_socket.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,12 @@
138138
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
139139
pass
140140

141-
connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
142-
connection.settimeout(None)
143-
assert connection.getblocking() == True
144-
assert connection.gettimeout() == None
145-
146-
connection.settimeout(0)
147-
assert connection.getblocking() == False
148-
assert connection.gettimeout() == 0
149-
150-
connection.setblocking(True)
151-
assert connection.getblocking() == True
152-
assert connection.gettimeout() == None
153-
154-
connection.setblocking(False)
155-
assert connection.getblocking() == False
156-
assert connection.gettimeout() == 0.0
157-
158-
connection.settimeout(3)
159-
assert connection.gettimeout() == 3
160-
assert connection.getblocking() == True
141+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as listener:
142+
listener.bind(("127.0.0.1", 0))
143+
listener.listen(1)
144+
connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
145+
connector.connect(("127.0.0.1", listener.getsockname()[1]))
146+
(connection, addr) = listener.accept()
147+
connection.settimeout(1.0)
148+
with assertRaises(OSError):
149+
connection.recv(len(MESSAGE_A))

vm/src/obj/objfloat.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,6 @@ impl IntoPyObject for f32 {
4646
}
4747
}
4848

49-
impl TryFromObject for f64 {
50-
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
51-
try_float(&obj, vm)?.map_or_else(
52-
|| Err(vm.new_type_error(format!("Expect float object, but get {}", obj.class().name))),
53-
|val| Ok(val),
54-
)
55-
}
56-
}
57-
5849
impl From<f64> for PyFloat {
5950
fn from(value: f64) -> Self {
6051
PyFloat { value }

vm/src/stdlib/socket.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::cell::RefCell;
22
use std::io;
33
use std::io::Read;
44
use std::io::Write;
5-
use std::net::{Ipv4Addr, SocketAddr, TcpListener, TcpStream, ToSocketAddrs, UdpSocket };
5+
use std::net::{Ipv4Addr, SocketAddr, TcpListener, TcpStream, ToSocketAddrs, UdpSocket};
66
use std::time::Duration;
77

88
#[cfg(all(unix, not(target_os = "redox")))]
@@ -25,7 +25,6 @@ use crate::obj::objtype::PyClassRef;
2525
use crate::stdlib::os::convert_nix_error;
2626
use num_bigint::Sign;
2727
use num_traits::ToPrimitive;
28-
use itertools::Itertools;
2928

3029
#[derive(Debug, Copy, Clone)]
3130
enum AddressFamily {
@@ -225,8 +224,11 @@ impl SocketRef {
225224
Ok(mut sock_addrs) => {
226225
if sock_addrs.len() == 0 {
227226
let error_type = vm.class("socket", "gaierror");
228-
return Err(vm.new_exception(error_type, "nodename nor servname provided, or not known".to_string()))
229-
}else{
227+
return Err(vm.new_exception(
228+
error_type,
229+
"nodename nor servname provided, or not known".to_string(),
230+
));
231+
} else {
230232
sock_addrs.next().unwrap()
231233
}
232234
}
@@ -466,9 +468,10 @@ impl SocketRef {
466468
let block = timeout > 0.0;
467469

468470
if let Some(conn) = self.con.borrow_mut().as_mut() {
469-
conn.setblocking(block).and_then(|_| {
470-
conn.settimeout(Duration::from_secs(timeout as u64))
471-
}).map_err(|err| vm.new_os_error(err.to_string())).map(|_| ())
471+
conn.setblocking(block)
472+
.and_then(|_| conn.settimeout(Duration::from_secs(timeout as u64)))
473+
.map_err(|err| vm.new_os_error(err.to_string()))
474+
.map(|_| ())
472475
} else {
473476
Ok(())
474477
}

0 commit comments

Comments
 (0)