From 103edd21c7f1689bc1007c91ecbbc9242b18d34a Mon Sep 17 00:00:00 2001 From: Weijie Guo Date: Thu, 14 Mar 2024 18:33:59 +0800 Subject: [PATCH] refactor(bindings/python): Change the return type of `File::read` to `PyResult<&PyBytes>` --- bindings/python/src/file.rs | 4 ++-- bindings/python/src/operator.rs | 4 ++-- bindings/python/src/utils.rs | 15 ++++++--------- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/bindings/python/src/file.rs b/bindings/python/src/file.rs index f8a98c8f51c..42d2c6bc022 100644 --- a/bindings/python/src/file.rs +++ b/bindings/python/src/file.rs @@ -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. @@ -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)) }) } diff --git a/bindings/python/src/operator.rs b/bindings/python/src/operator.rs index 9c7dfba13e1..e5194886e38 100644 --- a/bindings/python/src/operator.rs +++ b/bindings/python/src/operator.rs @@ -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. @@ -261,7 +261,7 @@ impl AsyncOperator { let this = self.0.clone(); future_into_py(py, async move { let res: Vec = 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)) }) } diff --git a/bindings/python/src/utils.rs b/bindings/python/src/utils.rs index 619743066c8..f51306a6451 100644 --- a/bindings/python/src/utils.rs +++ b/bindings/python/src/utils.rs @@ -31,20 +31,17 @@ impl Buffer { Buffer { inner } } - /// Consume self to build a memory view - pub fn into_memory_view(self, py: Python) -> PyResult> { + /// Consume self to build a bytes + pub fn into_bytes(self, py: Python) -> PyResult> { 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) }