mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 10:44:36 +08:00
Remove socket sendfile wrong implementation
This commit is contained in:
1 parent
a656c51696
commit
01e6f735a0
3 files changed
+17
-34
No files matched your search
@@ -23,6 +23,14 @@ recv_b = connector.recv(len(MESSAGE_B))
|
||||
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()
|
||||
|
||||
+9
-8
@@ -357,25 +357,26 @@ mod _os {
|
||||
#[cfg(any(target_os = "macos"))]
|
||||
#[pyfunction]
|
||||
pub(crate) fn sendfile(
|
||||
out_fd: i64,
|
||||
in_fd: i64,
|
||||
offset: u64,
|
||||
out_fd: i32,
|
||||
in_fd: i32,
|
||||
offset: i64,
|
||||
count: u64,
|
||||
headers: OptionalArg<PyObjectRef>,
|
||||
trailers: OptionalArg<PyObjectRef>,
|
||||
flags: OptionalArg<i64>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult {
|
||||
let res = nix::sys::sendfile::sendfile(
|
||||
let mut _count = count;
|
||||
let (res, written) = nix::sys::sendfile::sendfile(
|
||||
in_fd,
|
||||
out_fd,
|
||||
offset,
|
||||
Some(count as usize),
|
||||
headers,
|
||||
trailers,
|
||||
Some(&mut _count),
|
||||
headers.into_option(),
|
||||
trailers.into_option(),
|
||||
)
|
||||
.map_err(|err| err.into_pyexception(vm))?;
|
||||
Ok(vm.ctx.new_int(res as u64))
|
||||
Ok(vm.ctx.new_int(written as u64))
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
|
||||
@@ -21,7 +21,6 @@ use crate::pyobject::{
|
||||
BorrowValue, Either, IntoPyObject, PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue,
|
||||
StaticType, TryFromObject,
|
||||
};
|
||||
use crate::stdlib::os::rust_file;
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
#[cfg(unix)]
|
||||
@@ -221,31 +220,6 @@ impl PySocket {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[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)
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn close(&self) {
|
||||
*self.sock_mut() = invalid_sock();
|
||||
|
||||
Reference in new issue
Block a user