Skip to content

Commit

Permalink
fix: support placeholder arrays in ArrayModuleNumpyLike.frombuffer (#…
Browse files Browse the repository at this point in the history
…2693)

* fix: make singletons trivially pickle-able

* fix: support placeholders in `frombuffer`

* test: add simple test

---------

Co-authored-by: Jim Pivarski <jpivarski@users.noreply.github.com>
  • Loading branch information
agoose77 and jpivarski authored Sep 5, 2023
1 parent a8f8552 commit 684a84d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/awkward/_nplikes/array_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ def ascontiguousarray(self, x: ArrayLike) -> ArrayLike:
def frombuffer(
self, buffer, *, dtype: np.dtype | None = None, count: int = -1
) -> ArrayLike:
assert not isinstance(buffer, PlaceholderArray)
assert not isinstance(count, PlaceholderArray)
return self._module.frombuffer(buffer, dtype=dtype, count=count)
if isinstance(buffer, PlaceholderArray):
if count == -1:
return self.asarray(buffer)
else:
return self.asarray(buffer[:count])
else:
return self._module.frombuffer(buffer, dtype=dtype, count=count)

def from_dlpack(self, x: Any) -> ArrayLike:
return self._module.from_dlpack(x)
Expand Down
3 changes: 3 additions & 0 deletions src/awkward/_singleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class Singleton(Protocol):
def __new__(cls, *args, **kwargs):
return super().__new__(cls, *args, **kwargs)

def __reduce__(self):
return type(self).instance, ()

@classmethod
def instance(cls) -> Self:
try:
Expand Down
34 changes: 34 additions & 0 deletions tests/test_2693_to_buffers_placeholders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE
import pickle

import numpy as np

import awkward as ak
from awkward._nplikes.numpy import Numpy
from awkward.typetracer import PlaceholderArray

nplike = Numpy.instance()


def test_round_trip_placeholder():
layout = ak.contents.RecordArray(
[
ak.contents.NumpyArray(
PlaceholderArray(nplike, (4,), np.dtype(np.float64))
),
ak.contents.NumpyArray([1, 2, 3, 4]),
],
["x", "y"],
)

form, length, container = ak.to_buffers(layout)
result = ak.from_buffers(form, length, container, highlevel=False)

assert isinstance(result.content("x").data, PlaceholderArray)

assert result.content("x").data.shape == layout.content("x").data.shape
assert result.content("x").data.dtype == layout.content("x").data.dtype


def test_pickle_nplike():
assert pickle.loads(pickle.dumps(nplike)) is nplike

0 comments on commit 684a84d

Please sign in to comment.