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

refactor(bindings/python): Change the return type of File::read to PyResult<&PyBytes> #4360

Merged
merged 2 commits into from
Mar 14, 2024
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
4 changes: 2 additions & 2 deletions bindings/python/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl File {
}
};

Buffer::new(buffer).into_memory_view_ref(py)
Buffer::new(buffer).into_bytes_ref(py)
}

/// Write bytes into the file.
Expand Down Expand Up @@ -256,7 +256,7 @@ impl AsyncFile {
}
};

Python::with_gil(|py| Buffer::new(buffer).into_memory_view(py))
Python::with_gil(|py| Buffer::new(buffer).into_bytes(py))
})
}

Expand Down
4 changes: 2 additions & 2 deletions bindings/python/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Operator {
/// Read the whole path into bytes.
pub fn read<'p>(&'p self, py: Python<'p>, path: &str) -> PyResult<&'p PyAny> {
let buffer = self.0.read(path).map_err(format_pyerr)?;
Buffer::new(buffer).into_memory_view_ref(py)
Buffer::new(buffer).into_bytes_ref(py)
}

/// Write bytes into given path.
Expand Down Expand Up @@ -261,7 +261,7 @@ impl AsyncOperator {
let this = self.0.clone();
future_into_py(py, async move {
let res: Vec<u8> = this.read(&path).await.map_err(format_pyerr)?;
Python::with_gil(|py| Buffer::new(res).into_memory_view(py))
Python::with_gil(|py| Buffer::new(res).into_bytes(py))
})
}

Expand Down
15 changes: 6 additions & 9 deletions bindings/python/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,17 @@ impl Buffer {
Buffer { inner }
}

/// Consume self to build a memory view
pub fn into_memory_view(self, py: Python) -> PyResult<Py<PyAny>> {
/// Consume self to build a bytes
pub fn into_bytes(self, py: Python) -> PyResult<Py<PyAny>> {
let buffer = self.into_py(py);

unsafe {
PyObject::from_owned_ptr_or_err(py, ffi::PyMemoryView_FromObject(buffer.as_ptr()))
}
unsafe { PyObject::from_owned_ptr_or_err(py, ffi::PyBytes_FromObject(buffer.as_ptr())) }
}

/// Consume self to build a memory view ref.
pub fn into_memory_view_ref(self, py: Python) -> PyResult<&PyAny> {
/// Consume self to build a bytes
pub fn into_bytes_ref(self, py: Python) -> PyResult<&PyAny> {
let buffer = self.into_py(py);
let view =
unsafe { py.from_owned_ptr_or_err(ffi::PyMemoryView_FromObject(buffer.as_ptr()))? };
let view = unsafe { py.from_owned_ptr_or_err(ffi::PyBytes_FromObject(buffer.as_ptr()))? };

Ok(view)
}
Expand Down
Loading