Content-Length: 502705 | pFad | http://github.com/RustPython/RustPython/pull/632/files

E1 Socket OSError by palaviv · Pull Request #632 · RustPython/RustPython · GitHub
Skip to content

Socket OSError #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 29 additions & 0 deletions tests/snippets/stdlib_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,29 @@
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))

s.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", 0))
with assertRaises(OSError):
s.recv(100)

with assertRaises(OSError):
s.send(MESSAGE_A)

s.close()

# UDP
Expand Down Expand Up @@ -73,3 +90,15 @@
assert recv_b == MESSAGE_B
sock1.close()
sock3.close()

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

s.close()
### Errors
with assertRaises(OSError):
socket.socket(100, socket.SOCK_STREAM)

with assertRaises(OSError):
socket.socket(socket.AF_INET, 1000)
1 change: 1 addition & 0 deletions vm/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
"StopIteration" => ctx.exceptions.stop_iteration.clone(),
"ZeroDivisionError" => ctx.exceptions.zero_division_error.clone(),
"KeyError" => ctx.exceptions.key_error.clone(),
"OSError" => ctx.exceptions.os_error.clone(),
});

#[cfg(not(target_arch = "wasm32"))]
Expand Down
98 changes: 50 additions & 48 deletions vm/src/stdlib/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ enum AddressFamily {
}

impl AddressFamily {
fn from_i32(value: i32) -> AddressFamily {
fn from_i32(vm: &mut VirtualMachine, value: i32) -> Result<AddressFamily, PyObjectRef> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use a templated PyResult --> PyResult<AddressFamily>

match value {
1 => AddressFamily::Unix,
2 => AddressFamily::Inet,
3 => AddressFamily::Inet6,
_ => panic!("Unknown value: {}", value),
1 => Ok(AddressFamily::Unix),
2 => Ok(AddressFamily::Inet),
3 => Ok(AddressFamily::Inet6),
_ => Err(vm.new_os_error(format!("Unknown address family value: {}", value))),
}
}
}
Expand All @@ -41,11 +41,11 @@ enum SocketKind {
}

impl SocketKind {
fn from_i32(value: i32) -> SocketKind {
fn from_i32(vm: &mut VirtualMachine, value: i32) -> Result<SocketKind, PyObjectRef> {
match value {
1 => SocketKind::Stream,
2 => SocketKind::Dgram,
_ => panic!("Unknown value: {}", value),
1 => Ok(SocketKind::Stream),
2 => Ok(SocketKind::Dgram),
_ => Err(vm.new_os_error(format!("Unknown socket kind value: {}", value))),
}
}
}
Expand Down Expand Up @@ -146,8 +146,9 @@ fn socket_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
]
);

let address_family = AddressFamily::from_i32(objint::get_value(family_int).to_i32().unwrap());
let kind = SocketKind::from_i32(objint::get_value(kind_int).to_i32().unwrap());
let address_family =
AddressFamily::from_i32(vm, objint::get_value(family_int).to_i32().unwrap())?;
let kind = SocketKind::from_i32(vm, objint::get_value(kind_int).to_i32().unwrap())?;

let socket = RefCell::new(Socket::new(address_family, kind));

Expand All @@ -171,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 @@ -206,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 Expand Up @@ -272,7 +266,7 @@ fn socket_accept(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

let (tcp_stream, addr) = match ret {
Ok((socket, addr)) => (socket, addr),
_ => return Err(vm.new_type_error("".to_string())),
Err(s) => return Err(vm.new_os_error(s.to_string())),
};

let socket = RefCell::new(Socket {
Expand Down Expand Up @@ -303,7 +297,10 @@ fn socket_recv(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

let mut buffer = vec![0u8; objint::get_value(bufsize).to_usize().unwrap()];
match socket.con {
Some(ref mut v) => v.read_exact(&mut buffer).unwrap(),
Some(ref mut v) => match v.read_exact(&mut buffer) {
Ok(_) => (),
Err(s) => return Err(vm.new_os_error(s.to_string())),
},
None => return Err(vm.new_type_error("".to_string())),
};
Ok(vm.ctx.new_bytes(buffer))
Expand All @@ -326,7 +323,7 @@ fn socket_recvfrom(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

let addr = match ret {
Ok((_size, addr)) => addr,
_ => return Err(vm.new_type_error("".to_string())),
Err(s) => return Err(vm.new_os_error(s.to_string())),
};

let addr_tuple = get_addr_tuple(vm, addr)?;
Expand All @@ -343,7 +340,10 @@ fn socket_send(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let mut socket = get_socket(zelf);

match socket.con {
Some(ref mut v) => v.write(&objbytes::get_value(&bytes)).unwrap(),
Some(ref mut v) => match v.write(&objbytes::get_value(&bytes)) {
Ok(_) => (),
Err(s) => return Err(vm.new_os_error(s.to_string())),
},
None => return Err(vm.new_type_error("".to_string())),
};
Ok(vm.get_none())
Expand All @@ -366,22 +366,24 @@ fn socket_sendto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
match socket.socket_kind {
SocketKind::Dgram => {
match socket.con {
Some(ref mut v) => {
if let Ok(_) = v.send_to(&objbytes::get_value(&bytes), address_string) {
Ok(vm.get_none())
} else {
Err(vm.new_type_error("socket failed".to_string()))
}
}
Some(ref mut v) => match v.send_to(&objbytes::get_value(&bytes), address_string) {
Ok(_) => Ok(vm.get_none()),
Err(s) => Err(vm.new_os_error(s.to_string())),
},
None => {
// Doing implicit bind
if let Ok(dgram) = UdpSocket::bind("0.0.0.0:0") {
if let Ok(_) = dgram.send_to(&objbytes::get_value(&bytes), address_string) {
socket.con = Some(Connection::UdpSocket(dgram));
return Ok(vm.get_none());
match UdpSocket::bind("0.0.0.0:0") {
Ok(dgram) => {
match dgram.send_to(&objbytes::get_value(&bytes), address_string) {
Ok(_) => {
socket.con = Some(Connection::UdpSocket(dgram));
Ok(vm.get_none())
}
Err(s) => Err(vm.new_os_error(s.to_string())),
}
}
Err(s) => Err(vm.new_os_error(s.to_string())),
}
Err(vm.new_type_error("socket failed".to_string()))
}
}
}
Expand All @@ -408,7 +410,7 @@ fn socket_getsockname(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

match addr {
Ok(addr) => get_addr_tuple(vm, addr),
_ => Err(vm.new_type_error("".to_string())),
Err(s) => Err(vm.new_os_error(s.to_string())),
}
}

Expand Down








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/RustPython/RustPython/pull/632/files

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy