From bd72869c345f3d4fa2f887990ae85e7adda29641 Mon Sep 17 00:00:00 2001 From: lynskylate Date: Mon, 26 Aug 2019 01:04:04 +0800 Subject: [PATCH] Repalce OptionalArg to Option --- vm/src/stdlib/socket.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index 868b9bf6a..5ba62110c 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -12,12 +12,12 @@ use gethostname::gethostname; use byteorder::{BigEndian, ByteOrder}; -use crate::function::{OptionalArg, PyFuncArgs}; +use crate::function::PyFuncArgs; use crate::obj::objbytes::PyBytesRef; use crate::obj::objint::PyIntRef; use crate::obj::objstr::PyStringRef; use crate::obj::objtuple::PyTupleRef; -use crate::pyobject::{PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol}; +use crate::pyobject::{PyObjectRef, PyRef, PyResult, PyValue, TryFromObject}; use crate::vm::VirtualMachine; use crate::obj::objtype::PyClassRef; @@ -421,9 +421,9 @@ impl SocketRef { } } - fn setblocking(self, block: OptionalArg, vm: &VirtualMachine) -> PyResult<()> { + fn setblocking(self, block: Option, vm: &VirtualMachine) -> PyResult<()> { match block { - OptionalArg::Present(value) => { + Some(value) => { if value { self.timeout.replace(None); } else { @@ -438,9 +438,9 @@ impl SocketRef { Ok(()) } } - OptionalArg::Missing => { - self.timeout.replace(None); - Ok(()) + None => { + // Avoid converting None to bool + Err(vm.new_type_error("an bool is required".to_string())) } } } @@ -458,9 +458,9 @@ impl SocketRef { } } - fn settimeout(self, option_timeout: OptionalArg, vm: &VirtualMachine) -> PyResult<()> { + fn settimeout(self, option_timeout: Option, vm: &VirtualMachine) -> PyResult<()> { match option_timeout { - OptionalArg::Present(timeout) => { + Some(timeout) => { self.timeout .borrow_mut() .replace(Duration::from_secs(timeout as u64)); @@ -476,7 +476,10 @@ impl SocketRef { Ok(()) } } - OptionalArg::Missing => Ok(()), + None => { + self.timeout.replace(None); + Ok(()) + } } } } @@ -586,6 +589,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let module = py_module!(vm, "socket", { "error" => ctx.exceptions.os_error.clone(), "timeout" => socket_timeout, + "gaierror" => socket_gaierror, "AF_INET" => ctx.new_int(AddressFamily::Inet as i32), "SOCK_STREAM" => ctx.new_int(SocketKind::Stream as i32), "SOCK_DGRAM" => ctx.new_int(SocketKind::Dgram as i32),