Skip to content

Commit 01e6f73

Browse files
committed
Remove socket sendfile wrong implementation
1 parent a656c51 commit 01e6f73

File tree

3 files changed

+17
-34
lines changed

3 files changed

+17
-34
lines changed

extra_tests/snippets/stdlib_socket.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
assert recv_a == MESSAGE_A
2424
assert recv_b == MESSAGE_B
2525

26+
fd = open('README.md', 'rb')
27+
connector.sendfile(fd)
28+
recv_readme = connection.recv(os.stat('README.md').st_size)
29+
# need this because sendfile leaves the cursor at the end of the file
30+
fd.seek(0)
31+
assert recv_readme == fd.read()
32+
fd.close()
33+
2634
# fileno
2735
if os.name == "posix":
2836
connector_fd = connector.fileno()

vm/src/stdlib/os.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,25 +357,26 @@ mod _os {
357357
#[cfg(any(target_os = "macos"))]
358358
#[pyfunction]
359359
pub(crate) fn sendfile(
360-
out_fd: i64,
361-
in_fd: i64,
362-
offset: u64,
360+
out_fd: i32,
361+
in_fd: i32,
362+
offset: i64,
363363
count: u64,
364364
headers: OptionalArg<PyObjectRef>,
365365
trailers: OptionalArg<PyObjectRef>,
366366
flags: OptionalArg<i64>,
367367
vm: &VirtualMachine,
368368
) -> PyResult {
369-
let res = nix::sys::sendfile::sendfile(
369+
let mut _count = count;
370+
let (res, written) = nix::sys::sendfile::sendfile(
370371
in_fd,
371372
out_fd,
372373
offset,
373-
Some(count as usize),
374-
headers,
375-
trailers,
374+
Some(&mut _count),
375+
headers.into_option(),
376+
trailers.into_option(),
376377
)
377378
.map_err(|err| err.into_pyexception(vm))?;
378-
Ok(vm.ctx.new_int(res as u64))
379+
Ok(vm.ctx.new_int(written as u64))
379380
}
380381

381382
#[pyfunction]

vm/src/stdlib/socket.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use crate::pyobject::{
2121
BorrowValue, Either, IntoPyObject, PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue,
2222
StaticType, TryFromObject,
2323
};
24-
use crate::stdlib::os::rust_file;
2524
use crate::vm::VirtualMachine;
2625

2726
#[cfg(unix)]
@@ -221,31 +220,6 @@ impl PySocket {
221220
Ok(())
222221
}
223222

224-
#[pymethod]
225-
fn sendfile(&self, fd: i64, offset: OptionalArg<u64>, count: OptionalArg<u64>, vm: &VirtualMachine) -> PyResult<usize> {
226-
let mut file = rust_file(fd);
227-
let mut bufsize: u64 = 0;
228-
229-
if let Ok(metadata) = file.metadata() {
230-
bufsize = metadata.len();
231-
}
232-
233-
if let OptionalArg::Present(c) = count {
234-
bufsize = c;
235-
}
236-
237-
let mut buffer = vec![0u8; bufsize as usize];
238-
239-
let n = file
240-
.read(&mut buffer)
241-
.map_err(|err| err.into_pyexception(vm))?;
242-
243-
buffer.truncate(n);
244-
245-
let bytes = PyBytesLike::try_from_object(vm, vm.ctx.new_bytes(buffer))?;
246-
self.send(bytes, vm)
247-
}
248-
249223
#[pymethod]
250224
fn close(&self) {
251225
*self.sock_mut() = invalid_sock();

0 commit comments

Comments
 (0)