Skip to content
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 the socket module #1176

Closed
windelbouwman opened this issue Jul 24, 2019 · 18 comments · Fixed by #3840
Closed

Extend the socket module #1176

windelbouwman opened this issue Jul 24, 2019 · 18 comments · Fixed by #3840
Labels
A-stdlib E-help-wanted Extra attention is needed good first issue Good for newcomers

Comments

@windelbouwman
Copy link
Contributor

windelbouwman commented Jul 24, 2019

The socket module is still incomplete. Many methods and types are missing. To add methods to this module, edit the file vm/src/stdlib/socket.rs accordingly.

@windelbouwman windelbouwman added E-help-wanted Extra attention is needed good first issue Good for newcomers labels Jul 24, 2019
@posutsai
Copy link

Hi, I would like to contribute to extending the socket module. Are there any remaining methods which are not assigned to anybody? Should I open another issue and specify which method I want, preventing others from doing the same thing? Or which method is the first priority?

@windelbouwman
Copy link
Contributor Author

@posutsai you could add a series of checkboxes in this issue, and mention what you implement. There is not a fixed list, but there is a helper script to determine the missing methods, called whats_left.sh

I think it is good to mention here what you will start on.

@palaviv
Copy link
Contributor

palaviv commented Aug 13, 2019

Hi, I would like to contribute to extending the socket module. Are there any remaining methods which are not assigned to anybody? Should I open another issue and specify which method I want, preventing others from doing the same thing? Or which method is the first priority?

I would recommend trying to implement the methods needed by socketserver as it is something that would be great to add. From a quick look we should add:

  • gettimeout
  • setsockopt
  • shutdown

@posutsai
Copy link

@palaviv
I start with setsockopt function. However, I meet issues at the beginning and I can't find a great existing solution on google. I would like to hear your comment or any suggestion.

According to the document on the official Python website, setsockopt takes three or four arguments. However, as I know, Rust doesn't provide function overloading. It is possible to do it with trait if it takes three arguments in each implementation.

I have referred to the implementation of sendto and there is also an optional argument flag on the document. Seems like current implementation simply ignores the optional argument.

Could you give me some hint to implement the function? Sorry for asking such an elementary question, since I just start to learn rust and try to understand it deeper by solving a real problem.

@windelbouwman
Copy link
Contributor Author

windelbouwman commented Aug 23, 2019

Hi @posutsai ,

Your best option at this point would be to use OptionalArg in some way like this:

fn setsockopt(level: PyIntRef, optname: PyObjectRef, value: OptionalArg<PyObjectRef>, optlen: OptionalArg<PyObjectRef>, vm: &VirtualMachine) {
    // handle value and optlen combinations here:
    if value.is(vm.get_none()) {
          if let Present = oplen {
          } else {
              return Err(vm.new_type_error("optlen must be supplied when value is None"));
          }
    } else {
          if let Present = oplen {
              return Err(vm.new_type_error("optlen must not be supplied when value is not None"));
          }
    }
}

We did not yet build function polymorphism macros into rustpython, and rust does not support it, so this is currently the way to go forwards.

@rodrigocam
Copy link
Contributor

Hello, may I implement sendfile method?

@coolreader18
Copy link
Member

@rodrigocam Yeah, feel free to take a shot at it!

@rodrigocam
Copy link
Contributor

What do you think of this implementation? I'm using the algorithm os::read uses to fill the byte buffer that we are going to send.

    #[pymethod]
    fn sendfile(&self, fd: i64, offset: OptionalArg<u64>, count: OptionalArg<u64>, vm: &VirtualMachine) -> PyResult<usize> {
        let mut file = rust_file(fd);
        let mut bufsize: u64 = 0;

        if let Ok(metadata) = file.metadata() {
            bufsize = metadata.len();
        }

        if let OptionalArg::Present(c) = count {
            bufsize = c;
        }

        let mut buffer = vec![0u8; bufsize as usize];

        let n = file
            .read(&mut buffer)
            .map_err(|err| err.into_pyexception(vm))?;

        buffer.truncate(n);

        let bytes = PyBytesLike::try_from_object(vm, vm.ctx.new_bytes(buffer))?;
        self.send(bytes, vm)
    }

@coolreader18
Copy link
Member

@rodrigocam looks pretty good! Do you wanna make a PR for that?

@rodrigocam
Copy link
Contributor

@coolreader18 done! #2311

@hk1997
Copy link

hk1997 commented Sep 9, 2021

Hi, I would like to contribute to this.
Is it still open?
Are there some methods which need to be implemented?

@DimitrisJim
Copy link
Member

From a quick comparison I see gethostbyname_ex missing and a good number of the constants that are exported.

@nbykovsky
Copy link

Hi there, I want to start contributing with something. Are here any methods which needs to be implemented?

@DimitrisJim
Copy link
Member

my previous comment still stands, (also, try running whats_left and seeing what it reports for socket).

@varun-krishnan
Copy link
Contributor

if no one has taken gethostbyname_ex, can I take a crack at it?

@InfRandomness
Copy link

Hi, I want to take a shot at something(s) from it,
I tried running ./whats_left | grep socket
is everything appearing in the stdout missing?

@youknowone
Copy link
Member

yes, exactly

@youknowone
Copy link
Member

it seems most of features are done but many properties left

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-stdlib E-help-wanted Extra attention is needed good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.