Skip to content

Commit

Permalink
Merge pull request #91 from claydugo/use_bytearray_frombuffer
Browse files Browse the repository at this point in the history
Use bytearray in frombuffer
  • Loading branch information
mverleg authored Jul 19, 2023
2 parents 51d6135 + 91497cd commit dcea525
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion json_tricks/decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def _bin_str_to_ndarray(data, order, shape, np_type_name, data_endianness):
np_type = np_type.newbyteorder('>')
elif data_endianness != 'native':
warnings.warn('array of shape {} has unknown endianness \'{}\''.format(shape, data_endianness))
data = frombuffer(data, dtype=np_type)
data = frombuffer(bytearray(data), dtype=np_type)
return data.reshape(shape)


Expand Down
15 changes: 15 additions & 0 deletions tests/test_np.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,18 @@ def test_empty():
json = dumps(data)
assert_equal(loads(json), data, 'shape = {} ; json = {}'.format(data.shape, json))

def test_decode_writeable():
# issue https://github.com/mverleg/pyjson_tricks/issues/90
data = zeros((2, 2))

data_uncompressed = dumps(data)
data_compressed = dumps(data, properties={'ndarray_compact': True})

reloaded_uncompressed = loads(data_uncompressed)
reloaded_compressed = loads(data_compressed)

assert array_equal(data, reloaded_uncompressed)
assert array_equal(data, reloaded_compressed)

assert reloaded_uncompressed.flags.writeable
assert reloaded_compressed.flags.writeable

0 comments on commit dcea525

Please sign in to comment.