Skip to content

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

Merged
merged 8 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,16 @@ def _fastcopy_sendfile(fsrc, fdst):
err.filename = fsrc.name
err.filename2 = fdst.name

if err.errno == errno.ENOTSOCK:
# XXX RUSTPYTHON TODO: consistent OSError.errno
if hasattr(err, "errno") and err.errno == errno.ENOTSOCK:
# sendfile() on this platform (probably Linux < 2.6.33)
# does not support copies between regular files (only
# sockets).
_USE_CP_SENDFILE = False
raise _GiveupOnFastCopy(err)

if err.errno == errno.ENOSPC: # filesystem is full
# XXX RUSTPYTHON TODO: consistent OSError.errno
if hasattr(err, "errno") and err.errno == errno.ENOSPC: # filesystem is full
raise err from None

# Give up on first call and if no data was copied.
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -3067,6 +3067,7 @@ def handle_error(self):
raise


@unittest.skip("TODO: RUSTPYTHON (ValueError: invalid mode: 'xb')")
@unittest.skipUnless(hasattr(os, 'sendfile'), "test needs os.sendfile()")
class TestSendfile(unittest.TestCase):

Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -2366,6 +2366,8 @@ def test_non_regular_file_dst(self):
dst.seek(0)
self.assertEqual(dst.read(), self.FILEDATA)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_exception_on_second_call(self):
def sendfile(*args, **kwargs):
if not flag:
Expand Down Expand Up @@ -2438,6 +2440,7 @@ def test_blocksize_arg(self):
blocksize = m.call_args[0][3]
self.assertEqual(blocksize, 2 ** 23)

@unittest.skip("TODO: RUSTPYTHON, unittest.mock")
def test_file2file_not_supported(self):
# Emulate a case where sendfile() only support file->socket
# fds. In such a case copyfile() is supposed to skip the
Expand Down
13 changes: 13 additions & 0 deletions extra_tests/snippets/stdlib_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@
assert_raises(FileNotFoundError,
lambda: os.rename('DOES_NOT_EXIST', 'DOES_NOT_EXIST 2'))

if hasattr(os, "sendfile"):
src_fd = os.open('README.md', os.O_RDONLY)
dest_fd = os.open('destination.md', os.O_RDWR | os.O_CREAT)
src_len = os.stat('README.md').st_size

bytes_sent = os.sendfile(dest_fd, src_fd, 0, src_len)
assert src_len == bytes_sent

os.lseek(dest_fd, 0, 0)
assert os.read(src_fd, src_len) == os.read(dest_fd, bytes_sent)
os.close(src_fd)
os.close(dest_fd)

try:
os.open('DOES_NOT_EXIST', 0)
except OSError as err:
Expand Down
8 changes: 8 additions & 0 deletions extra_tests/snippets/stdlib_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
assert recv_a == MESSAGE_A
assert recv_b == MESSAGE_B

fd = open('README.md', 'rb')
connector.sendfile(fd)
recv_readme = connection.recv(os.stat('README.md').st_size)
# need this because sendfile leaves the cursor at the end of the file
fd.seek(0)
assert recv_readme == fd.read()
fd.close()

# fileno
if os.name == "posix":
connector_fd = connector.fileno()
Expand Down
16 changes: 14 additions & 2 deletions vm/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,15 +506,17 @@ macro_rules! tuple_from_py_func_args {
};
}

// Implement `FromArgs` for up to 5-tuples, allowing built-in functions to bind
// up to 5 top-level parameters (note that `Args`, `KwArgs`, nested tuples, etc.
// Implement `FromArgs` for up to 7-tuples, allowing built-in functions to bind
// up to 7 top-level parameters (note that `Args`, `KwArgs`, nested tuples, etc.
// count as 1, so this should actually be more than enough).
tuple_from_py_func_args!(A);
tuple_from_py_func_args!(A, B);
tuple_from_py_func_args!(A, B, C);
tuple_from_py_func_args!(A, B, C, D);
tuple_from_py_func_args!(A, B, C, D, E);
tuple_from_py_func_args!(A, B, C, D, E, F);
tuple_from_py_func_args!(A, B, C, D, E, F, G);
tuple_from_py_func_args!(A, B, C, D, E, F, G, H);

/// A built-in Python function.
pub type PyNativeFunc = Box<py_dyn_fn!(dyn Fn(&VirtualMachine, FuncArgs) -> PyResult)>;
Expand Down Expand Up @@ -640,6 +642,16 @@ into_py_native_func_tuple!((v1, T1), (v2, T2));
into_py_native_func_tuple!((v1, T1), (v2, T2), (v3, T3));
into_py_native_func_tuple!((v1, T1), (v2, T2), (v3, T3), (v4, T4));
into_py_native_func_tuple!((v1, T1), (v2, T2), (v3, T3), (v4, T4), (v5, T5));
into_py_native_func_tuple!((v1, T1), (v2, T2), (v3, T3), (v4, T4), (v5, T5), (v6, T6));
into_py_native_func_tuple!(
(v1, T1),
(v2, T2),
(v3, T3),
(v4, T4),
(v5, T5),
(v6, T6),
(v7, T7)
);

/// Tests that the predicate is True on a single value, or if the value is a tuple a tuple, then
/// test that any of the values contained within the tuples satisfies the predicate. Type parameter
Expand Down
55 changes: 55 additions & 0 deletions vm/src/stdlib/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,61 @@ mod _os {
Err(vm.new_os_error("os.open not implemented on this platform".to_owned()))
}

#[cfg(any(target_os = "linux"))]
#[pyfunction]
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]
fn sendfile(
out_fd: i32,
in_fd: i32,
offset: i64,
count: i64,
headers: OptionalArg<PyObjectRef>,
trailers: OptionalArg<PyObjectRef>,
flags: OptionalArg<i32>,
Copy link
Member

Choose a reason for hiding this comment

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

Adding _ prefix like _flags will remove this warning

vm: &VirtualMachine,
) -> PyResult {
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();

let trailers = match trailers.into_option() {
Some(x) => Some(vm.extract_elements::<PyBytesLike>(&x)?),
None => None,
};

let trailers = trailers
.as_ref()
.map(|v| v.iter().map(|b| b.borrow_value()).collect::<Vec<_>>());
let trailers = trailers
.as_ref()
.map(|v| v.iter().map(|borrowed| &**borrowed).collect::<Vec<_>>());
let trailers = trailers.as_deref();

let (res, written) =
nix::sys::sendfile::sendfile(in_fd, out_fd, offset, Some(count), headers, trailers);
res.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());
Expand Down