Skip to content

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

Merged
merged 5 commits into from
Aug 8, 2019
Merged

Extend Socket module #1205

merged 5 commits into from
Aug 8, 2019

Conversation

Lynskylate
Copy link
Contributor

@Lynskylate Lynskylate commented Aug 4, 2019

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.

Copy link
Member

@coolreader18 coolreader18 left a 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.

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()) };
Copy link
Member

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.

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),
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@palaviv palaviv left a 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.

@@ -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 {
Copy link
Contributor

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?

Copy link
Contributor Author

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?

Copy link
Member

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.

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"
Copy link
Contributor

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"?

Copy link
Contributor Author

@Lynskylate Lynskylate Aug 4, 2019

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

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()) };
Copy link
Contributor

Choose a reason for hiding this comment

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

How about using octet?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great advice :). Done

Copy link
Contributor

@palaviv palaviv left a 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.

@@ -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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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"
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@palaviv palaviv merged commit d63839a into RustPython:master Aug 8, 2019
@palaviv
Copy link
Contributor

palaviv commented Aug 8, 2019

Great job @Lynskylate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants