-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add socket sendfile function #2311
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
Changes from 3 commits
00920ba
a656c51
01e6f73
a8d579d
a64ecbf
6de2dc6
7d45672
ad3922a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -337,6 +337,48 @@ mod _os { | |
| Err(vm.new_os_error("os.open not implemented on this platform".to_owned())) | ||
| } | ||
|
|
||
| #[cfg(any(target_os = "linux"))] | ||
| #[pyfunction] | ||
| pub(crate) fn sendfile( | ||
| out_fd: i32, | ||
| in_fd: i32, | ||
| offset: i64, | ||
| count: u64, | ||
| vm: &VirtualMachine, | ||
| ) -> PyResult { | ||
| let mut file_offset = offset; | ||
|
|
||
| let res = | ||
| nix::sys::sendfile::sendfile(out_fd, in_fd, Some(&mut file_offset), count as usize) | ||
| .map_err(|err| err.into_pyexception(vm))?; | ||
| Ok(vm.ctx.new_int(res as u64)) | ||
| } | ||
|
|
||
| #[cfg(any(target_os = "macos"))] | ||
| #[pyfunction] | ||
| pub(crate) fn sendfile( | ||
| out_fd: i32, | ||
| in_fd: i32, | ||
| offset: i64, | ||
| count: u64, | ||
| headers: OptionalArg<PyObjectRef>, | ||
| trailers: OptionalArg<PyObjectRef>, | ||
| flags: OptionalArg<i64>, | ||
| vm: &VirtualMachine, | ||
| ) -> PyResult { | ||
| let mut _count = count; | ||
| let (res, written) = nix::sys::sendfile::sendfile( | ||
| in_fd, | ||
| out_fd, | ||
| offset, | ||
| Some(&mut _count), | ||
| headers.into_option(), | ||
| trailers.into_option(), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have to process these into an array of buffers ( let headers = match headers.into_option() {
Some(x) => Some(vm.extract_elements::<PyBytesLike>(&x)?),
None => None,
};
let headers = headers.as_ref().map(|v| v.iter().map(|b| b.borrow_value()).collect::<Vec<_>>());
let headers = headers.as_ref().map(|v| v.iter().map(|borrowed| &**borrowed).collect::<Vec<_>>());
let headers = headers.as_deref();And then the same thing for trailers. That's really inefficient, what with creating like 3 different vecs, but I think I could submit a PR to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So basically with those lines we are transforming PyObject into bytes? I put those lines and it worked, now the macos tests give me this error: 359 | pub(crate) fn sendfile(
| ^^^^^^^^ the trait `function::sealed::PyNativeFuncInternal<_, _, _>` is not implemented for `for<'r> fn(i32, i32, i64, i64, function::OptionalArg, function::OptionalArg, function::OptionalArg<i64>, &'r vm::VirtualMachine) -> std::result::Result<pyobjectrc::PyObjectRc, pyobject::PyRef<exceptions::PyBaseException>> {stdlib::os::_os::sendfile}`
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, to fix that you can add another macro call (or 2?) after line 642 in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And yep,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! Now that |
||
| ) | ||
| .map_err(|err| err.into_pyexception(vm))?; | ||
| Ok(vm.ctx.new_int(written as u64)) | ||
| } | ||
|
|
||
| #[pyfunction] | ||
| fn error(message: OptionalArg<PyStrRef>, vm: &VirtualMachine) -> PyResult { | ||
| let msg = message.map_or("".to_owned(), |msg| msg.borrow_value().to_owned()); | ||
|
|
||
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 probably want to put this in a
if hasattr(os, "sendfile"):blockThere 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!