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
Next Next commit
Handle unknown address family
  • Loading branch information
palaviv committed Mar 8, 2019
commit 40c10ab70a7341a9d7c551176f3cff5fa00bfb62
4 changes: 4 additions & 0 deletions tests/snippets/stdlib_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@
assert recv_b == MESSAGE_B
sock1.close()
sock3.close()

### Errors
with assertRaises(OSError):
socket.socket(100, socket.SOCK_STREAM)
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
13 changes: 7 additions & 6 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
Copy Markdown
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 Down Expand Up @@ -146,7 +146,8 @@ fn socket_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
]
);

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

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