Skip to content

Commit 3851255

Browse files
authored
Merge pull request #162 from jakirkham/chk_object_ensure_bytes
Raise on `object` arrays in `ensure_bytes`
2 parents 23b8d50 + 5e036f0 commit 3851255

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

docs/release.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Release notes
44

55
* Drop support for 32-bit Windows. By :user:`Alistair Miles <alimanfoo>`, :issue:`97`, :issue:`156`.
66

7+
* Raise a ``TypeError`` if an ``object`` array is passed to ``ensure_bytes``.
8+
By :user:`John Kirkham <jakirkham>`, :issue:`162`.
9+
710

811
.. _release_0.6.2:
912

numcodecs/compat.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ def ensure_bytes(buf):
152152
# go via numpy, for convenience
153153
arr = ensure_ndarray(buf)
154154

155+
# check for object arrays, these are just memory pointers,
156+
# actual memory holding item data is scattered elsewhere
157+
if arr.dtype == object:
158+
raise TypeError('object arrays are not supported')
159+
155160
# create bytes
156161
buf = arr.tobytes(order='A')
157162

numcodecs/tests/test_compat.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ def test_ensure_contiguous_ndarray_shares_memory():
5151
assert np.shares_memory(a, memoryview(buf))
5252

5353

54+
def test_ensure_bytes_invalid_inputs():
55+
56+
# object array not allowed
57+
a = np.array([u'Xin chào thế giới'], dtype=object)
58+
for e in [a, memoryview(a)]:
59+
with pytest.raises(TypeError):
60+
ensure_bytes(e)
61+
62+
5463
def test_ensure_contiguous_ndarray_invalid_inputs():
5564

5665
# object array not allowed

0 commit comments

Comments
 (0)