Skip to content

Commit

Permalink
fix: catch Content objects (#986)
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 authored Oct 12, 2023
1 parent 9f5e4c7 commit f999bd9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/uproot/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,18 @@ def ensure_numpy(array, types=(numpy.bool_, numpy.integer, numpy.floating)):
Returns an ``np.ndarray`` if ``array`` can be converted to an array of the
desired type and raises TypeError if it cannot.
"""
import uproot

awkward = uproot.extras.awkward()
with warnings.catch_warnings():
warnings.simplefilter("error", numpy.VisibleDeprecationWarning)
try:
out = numpy.asarray(array)
except (ValueError, numpy.VisibleDeprecationWarning) as err:
raise TypeError("cannot be converted to a NumPy array") from err
if isinstance(array, awkward.contents.Content):
out = awkward.to_numpy(array)
else:
try:
out = numpy.asarray(array)
except (ValueError, numpy.VisibleDeprecationWarning) as err:
raise TypeError("cannot be converted to a NumPy array") from err
if not issubclass(out.dtype.type, types):
raise TypeError(f"cannot be converted to a NumPy array of type {types}")
return out
Expand Down
4 changes: 3 additions & 1 deletion src/uproot/writing/_cascadetree.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,9 @@ def extend(self, file, sink, data):
)
)
else:
big_endian = numpy.asarray(branch_array, dtype=datum["dtype"])
big_endian = uproot._util.ensure_numpy(branch_array).astype(
datum["dtype"]
)
if big_endian.shape != (len(branch_array),) + datum["shape"]:
raise ValueError(
"'extend' must fill branches with a consistent shape: has {}, trying to fill with {}".format(
Expand Down

0 comments on commit f999bd9

Please sign in to comment.