Skip to content

Commit

Permalink
refactor(bindings/python): Change the return type of File::read to …
Browse files Browse the repository at this point in the history
…`PyResult<&PyBytes>`
  • Loading branch information
reswqa committed Mar 14, 2024
1 parent 7533627 commit 103edd2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
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

0 comments on commit 103edd2

Please sign in to comment.