From 956d0d0c946190148825e8c2914fbb61e862c0a9 Mon Sep 17 00:00:00 2001 From: godara-alt Date: Sat, 21 Nov 2026 02:28:36 -0600 Subject: [PATCH 1/3] fix(blob): write character arrays in MATLAB column-major order (#1702) --- src/datajoint/blob.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/datajoint/blob.py b/src/datajoint/blob.py index d8bf7a416..6206430d1 100644 --- a/src/datajoint/blob.py +++ b/src/datajoint/blob.py @@ -342,7 +342,7 @@ def pack_array(self, array: np.ndarray) -> bytes: blob += b"".join(len_u64(it) + it for it in (self.pack_blob(e) for e in array.flatten(order="F"))) self.set_dj0() # not supported by original mym elif serialize_lookup[array.dtype]["scalar_type"] == "CHAR": - blob += array.view(np.uint8).astype(np.uint16).tobytes() # convert to 16-bit chars for MATLAB + blob += array.view(np.uint8).astype(np.uint16).tobytes(order="F") # MATLAB column-major order else: # numeric arrays if array.ndim == 0: # not supported by original mym self.set_dj0() From 56368f23bf1e9235b2db316a5579a4008bc6d7c7 Mon Sep 17 00:00:00 2001 From: godara-alt Date: Sat, 21 Nov 2026 02:28:38 -0600 Subject: [PATCH 2/3] test(blob): cover character matrices and MATLAB byte order (#1702) --- tests/unit/test_blob_character_arrays.py | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/unit/test_blob_character_arrays.py diff --git a/tests/unit/test_blob_character_arrays.py b/tests/unit/test_blob_character_arrays.py new file mode 100644 index 000000000..328bd50d5 --- /dev/null +++ b/tests/unit/test_blob_character_arrays.py @@ -0,0 +1,40 @@ +"""Regression tests for MATLAB-compatible character-array blob serialization.""" + +import numpy as np +import pytest + +from datajoint.blob import pack, unpack + + +@pytest.mark.parametrize( + "characters", + [ + [[b"a", b"b", b"c"], [b"d", b"e", b"f"]], + [[b"a", b"b"], [b"c", b"d"], [b"e", b"f"]], + [[b"a", b"b"], [b"c", b"d"]], + ], + ids=["two-by-three", "three-by-two", "two-by-two"], +) +def test_char_matrix_round_trip(characters): + matrix = np.asarray(characters, dtype="S1") + actual = unpack(pack(matrix, compress=False)) + np.testing.assert_array_equal(actual, matrix.astype("U1")) + + +def test_char_matrix_uses_matlab_column_major_bytes(): + matrix = np.array([[b"a", b"b", b"c"], [b"d", b"e", b"f"]], dtype="S1") + encoded = pack(matrix, compress=False) + expected_data = matrix.view(np.uint8).astype(np.uint16).tobytes(order="F") + + assert encoded.startswith(b"mYm\\0") + assert encoded.endswith(expected_data) + np.testing.assert_array_equal(unpack(encoded), matrix.astype("U1")) + + +def test_compressed_char_matrix_round_trip(): + small = np.array([[b"a", b"b", b"c"], [b"d", b"e", b"f"]], dtype="S1") + matrix = np.tile(small, (50, 30)) + encoded = pack(matrix) + + assert encoded.startswith(b"ZL123\\0") + np.testing.assert_array_equal(unpack(encoded), matrix.astype("U1")) From f8cf4c6ae9f2b811b5cb4bc43e7ea8e80c11495f Mon Sep 17 00:00:00 2001 From: godara-alt Date: Sat, 21 Nov 2026 02:29:03 -0600 Subject: [PATCH 3/3] test(blob): correct binary protocol prefix assertions --- tests/unit/test_blob_character_arrays.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_blob_character_arrays.py b/tests/unit/test_blob_character_arrays.py index 328bd50d5..9dec44d61 100644 --- a/tests/unit/test_blob_character_arrays.py +++ b/tests/unit/test_blob_character_arrays.py @@ -26,7 +26,7 @@ def test_char_matrix_uses_matlab_column_major_bytes(): encoded = pack(matrix, compress=False) expected_data = matrix.view(np.uint8).astype(np.uint16).tobytes(order="F") - assert encoded.startswith(b"mYm\\0") + assert encoded.startswith(b"mYm\0") assert encoded.endswith(expected_data) np.testing.assert_array_equal(unpack(encoded), matrix.astype("U1")) @@ -36,5 +36,5 @@ def test_compressed_char_matrix_round_trip(): matrix = np.tile(small, (50, 30)) encoded = pack(matrix) - assert encoded.startswith(b"ZL123\\0") + assert encoded.startswith(b"ZL123\0") np.testing.assert_array_equal(unpack(encoded), matrix.astype("U1"))