-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Extend Socket module #1205
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
Extend Socket module #1205
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thank you for contributing to this project! I've left a few comments, and I'll merge this once you address them.
vm/src/stdlib/socket.rs
Outdated
fn socket_inet_aton(ip_string: PyStringRef, vm: &VirtualMachine) -> PyResult { | ||
match ip_string.as_str().parse::<Ipv4Addr>() { | ||
Ok(ip_addr) => { | ||
let out_bytes: [u8; 4] = unsafe { transmute(u32::from(ip_addr).to_be()) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try u32::from(ip_addr).to_be_bytes()
, so that we can avoid unsafe
use.
vm/src/stdlib/socket.rs
Outdated
let mut buf = [0u8; 1024]; | ||
match gethostname(&mut buf) { | ||
Ok(cstr) => vm.new_str(String::from(cstr.to_str().unwrap())), | ||
Err(e) => convert_nix_error(vm, e), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to wrap these in Ok and Err? As it is now, it'll just return the exception object if there's an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice.
When subprocess
will be merged we can test gethostname
easily as well.
vm/src/stdlib/socket.rs
Outdated
@@ -372,6 +378,30 @@ fn get_addr_tuple(vm: &VirtualMachine, addr: SocketAddr) -> PyResult { | |||
Ok(vm.ctx.new_tuple(vec![ip, port])) | |||
} | |||
|
|||
#[cfg(unix)] | |||
fn socket_gethostname(vm: &VirtualMachine) -> PyObjectRef { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The gethostname crate is not very popular but the windows implementation looks good. What do you think about using it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The gethostname crate is not very popular but the windows implementation looks good. What do you think about using it?
It sounds great. Does RustPython has any constraint to introduce new dependencies?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not particularly, one of the main things we want to do with RustPython is leverage Rust's large crate ecosystem to make developing the native standard library modules; so, feel free to use it.
tests/snippets/stdlib_socket.py
Outdated
socket.inet_aton("test") | ||
|
||
assert socket.inet_aton("127.0.0.1")==b"\x7f\x00\x00\x01" | ||
assert socket.inet_aton("127.0.0.1")==b"\xff\xff\xff\xff" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you wanted "255.255.255.255"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you wanted "255.255.255.255"?
Yes, Thank you
vm/src/stdlib/socket.rs
Outdated
fn socket_inet_aton(ip_string: PyStringRef, vm: &VirtualMachine) -> PyResult { | ||
match ip_string.as_str().parse::<Ipv4Addr>() { | ||
Ok(ip_addr) => { | ||
let out_bytes: [u8; 4] = unsafe { transmute(u32::from(ip_addr).to_be()) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using octet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great advice :). Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Real nice. Just a few changes and we can merge this.
vm/src/stdlib/socket.rs
Outdated
@@ -372,6 +382,44 @@ fn get_addr_tuple(vm: &VirtualMachine, addr: SocketAddr) -> PyResult { | |||
Ok(vm.ctx.new_tuple(vec![ip, port])) | |||
} | |||
|
|||
#[cfg(unix)] | |||
fn socket_gethostname(vm: &VirtualMachine) -> PyResult { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use the same function as in windows. The gothostname
module lib support it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
vm/Cargo.toml
Outdated
@@ -24,6 +24,7 @@ sha2 = "0.8" | |||
sha3 = "0.8" | |||
blake2 = "0.8" | |||
|
|||
gethostname = "0.2.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be included in WASM. You can look #1201 for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your guidance, it helps a lot.
Great job @Lynskylate |
For #1176 Add socket method gethostname sethostname inet_aton .
The convert_nix_error method is a common method, maybe it should be moved to exceptions.rs.