Skip to content
Merged
13 changes: 13 additions & 0 deletions src/zarr/core/buffer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,19 @@
"""
...

def as_bytes_like(self) -> BytesLike:
"""Returns the buffer as a bytes-like object.

Notes
-----
Might have to copy data, since the implementation uses `.as_numpy_array()`.

Returns
-------
A bytes-like object that implements the Python buffer protocol
"""
return memoryview(self.as_numpy_array().view(np.uint8)) # type: ignore[arg-type]

Check warning on line 265 in src/zarr/core/buffer/core.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/core/buffer/core.py#L265

Added line #L265 was not covered by tests

def to_bytes(self) -> bytes:
"""Returns the buffer as `bytes` (host memory).

Expand Down
4 changes: 2 additions & 2 deletions src/zarr/storage/_obstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@

self._check_writable()

buf = value.to_bytes()
buf = value.as_bytes_like()

Check warning on line 148 in src/zarr/storage/_obstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/storage/_obstore.py#L148

Added line #L148 was not covered by tests
await obs.put_async(self.store, key, buf)

async def set_if_not_exists(self, key: str, value: Buffer) -> None:
# docstring inherited
import obstore as obs

self._check_writable()
buf = value.to_bytes()
buf = value.as_bytes_like()

Check warning on line 156 in src/zarr/storage/_obstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/storage/_obstore.py#L156

Added line #L156 was not covered by tests
with contextlib.suppress(obs.exceptions.AlreadyExistsError):
await obs.put_async(self.store, key, buf, mode="create")

Expand Down