Skip to content

Commit

Permalink
Make buffers have a length (#252)
Browse files Browse the repository at this point in the history
* Make buffers have a length

* Update pyo3-arrow/src/buffer.rs

Co-authored-by: Kyle Barron <[email protected]>

* Add `__len__` to typing

---------

Co-authored-by: Kyle Barron <[email protected]>
Co-authored-by: Kyle Barron <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2024
1 parent d771e39 commit 2c73b76
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions arro3-core/python/arro3/core/_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ class Buffer:
"""An Arrow Buffer"""
def __init__(self, buffer) -> None: ...
def __buffer__(self, flags: int) -> memoryview: ...
def __len__(self) -> int: ...
def as_bytes(self) -> bytes:
"""Copy this buffer into a Python `bytes` object."""

Expand Down
4 changes: 4 additions & 0 deletions pyo3-arrow/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ impl PyArrowBuffer {
PyBytes::new_bound(py, &self.0)
}

fn __len__(&self) -> usize {
self.0.len()
}

/// This is taken from opendal:
/// https://github.com/apache/opendal/blob/d001321b0f9834bc1e2e7d463bcfdc3683e968c9/bindings/python/src/utils.rs#L51-L72
unsafe fn __getbuffer__(
Expand Down
1 change: 1 addition & 0 deletions tests/core/test_buffer_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def test_multi_dimensional():
def test_round_trip_buffer():
arr = np.arange(5, dtype=np.uint8)
buffer = Buffer(arr)
assert len(buffer) == arr.nbytes
retour = np.frombuffer(buffer, dtype=np.uint8)
assert np.array_equal(arr, retour)

Expand Down

0 comments on commit 2c73b76

Please sign in to comment.