Skip to content

Commit 7fbeca9

Browse files
[PYTHON-4803] Big endian fix for binary bson vectors (#1885)
1 parent bfba548 commit 7fbeca9

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

bson/binary.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ def from_vector(
432432
raise NotImplementedError("%s not yet supported" % dtype)
433433

434434
metadata = struct.pack("<sB", dtype.value, padding)
435-
data = struct.pack(f"{len(vector)}{format_str}", *vector)
435+
data = struct.pack(f"<{len(vector)}{format_str}", *vector)
436436
return cls(metadata + data, subtype=VECTOR_SUBTYPE)
437437

438438
def as_vector(self) -> BinaryVector:
@@ -454,7 +454,7 @@ def as_vector(self) -> BinaryVector:
454454

455455
if dtype == BinaryVectorDtype.INT8:
456456
dtype_format = "b"
457-
format_string = f"{n_values}{dtype_format}"
457+
format_string = f"<{n_values}{dtype_format}"
458458
vector = list(struct.unpack_from(format_string, self, position))
459459
return BinaryVector(vector, dtype, padding)
460460

@@ -465,13 +465,16 @@ def as_vector(self) -> BinaryVector:
465465
raise ValueError(
466466
"Corrupt data. N bytes for a float32 vector must be a multiple of 4."
467467
)
468-
vector = list(struct.unpack_from(f"{n_values}f", self, position))
468+
dtype_format = "f"
469+
format_string = f"<{n_values}{dtype_format}"
470+
vector = list(struct.unpack_from(format_string, self, position))
469471
return BinaryVector(vector, dtype, padding)
470472

471473
elif dtype == BinaryVectorDtype.PACKED_BIT:
472474
# data packed as uint8
473475
dtype_format = "B"
474-
unpacked_uint8s = list(struct.unpack_from(f"{n_values}{dtype_format}", self, position))
476+
format_string = f"<{n_values}{dtype_format}"
477+
unpacked_uint8s = list(struct.unpack_from(format_string, self, position))
475478
return BinaryVector(unpacked_uint8s, dtype, padding)
476479

477480
else:

0 commit comments

Comments
 (0)