diff --git a/bson/binary.py b/bson/binary.py index a1f63adf27..926263eee7 100644 --- a/bson/binary.py +++ b/bson/binary.py @@ -14,6 +14,7 @@ from __future__ import annotations import struct +import warnings from enum import Enum from typing import TYPE_CHECKING, Any, Optional, Sequence, Tuple, Type, Union, overload from uuid import UUID @@ -255,6 +256,9 @@ def __eq__(self, other: Any) -> bool: self.dtype == other.dtype and self.padding == other.padding and self.data == other.data ) + def __len__(self) -> int: + return len(self.data) + class Binary(bytes): """Representation of BSON binary data. @@ -471,6 +475,10 @@ def from_vector( metadata = struct.pack(" BinaryVector: @@ -522,6 +530,12 @@ def as_vector(self) -> BinaryVector: dtype_format = "B" format_string = f"<{n_values}{dtype_format}" unpacked_uint8s = list(struct.unpack_from(format_string, self, position)) + if padding and n_values and unpacked_uint8s[-1] & (1 << padding) - 1 != 0: + warnings.warn( + "Vector has a padding P, but bits in the final byte lower than P are non-zero. For pymongo>=5.0, they must be zero.", + DeprecationWarning, + stacklevel=2, + ) return BinaryVector(unpacked_uint8s, dtype, padding) else: diff --git a/doc/changelog.rst b/doc/changelog.rst index ca4784f919..6363cd7bcb 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -51,6 +51,9 @@ PyMongo 4.13 brings a number of changes including: or the `migration guide `_ for more information. - Fixed a bug where :class:`pymongo.write_concern.WriteConcern` repr was not eval-able when using ``w="majority"``. +- Ignored bits in a BSON BinaryVector of PACKED_BIT dtype should be set to zero. + On writes, this is enforced and is a breaking change. + Reads from the database will not fail, however a warning will be triggered. Issues Resolved ............... diff --git a/test/test_bson.py b/test/test_bson.py index 23e0a29c4f..4d25d67ebe 100644 --- a/test/test_bson.py +++ b/test/test_bson.py @@ -739,7 +739,7 @@ def test_vector(self): """Tests of subtype 9""" # We start with valid cases, across the 3 dtypes implemented. # Work with a simple vector that can be interpreted as int8, float32, or ubyte - list_vector = [127, 7] + list_vector = [127, 8] # As INT8, vector has length 2 binary_vector = Binary.from_vector(list_vector, BinaryVectorDtype.INT8) vector = binary_vector.as_vector() @@ -764,18 +764,18 @@ def test_vector(self): uncompressed = "" for val in list_vector: uncompressed += format(val, "08b") - assert uncompressed[:-padding] == "0111111100000" + assert uncompressed[:-padding] == "0111111100001" # It is worthwhile explicitly showing the values encoded to BSON padded_doc = {"padded_vec": padded_vec} assert ( encode(padded_doc) - == b"\x1a\x00\x00\x00\x05padded_vec\x00\x04\x00\x00\x00\t\x10\x03\x7f\x07\x00" + == b"\x1a\x00\x00\x00\x05padded_vec\x00\x04\x00\x00\x00\t\x10\x03\x7f\x08\x00" ) # and dumped to json assert ( json_util.dumps(padded_doc) - == '{"padded_vec": {"$binary": {"base64": "EAN/Bw==", "subType": "09"}}}' + == '{"padded_vec": {"$binary": {"base64": "EAN/CA==", "subType": "09"}}}' ) # FLOAT32 is also implemented @@ -784,15 +784,14 @@ def test_vector(self): # Now some invalid cases for x in [-1, 257]: - try: + with self.assertRaises(struct.error): Binary.from_vector([x], BinaryVectorDtype.PACKED_BIT) - except Exception as exc: - self.assertIsInstance(exc, struct.error) - else: - self.fail("Failed to raise an exception.") - # Test form of Binary.from_vector(BinaryVector) + # Test one must pass zeros for all ignored bits + with self.assertRaises(ValueError): + Binary.from_vector([255], BinaryVectorDtype.PACKED_BIT, padding=7) + # Test form of Binary.from_vector(BinaryVector) assert padded_vec == Binary.from_vector( BinaryVector(list_vector, BinaryVectorDtype.PACKED_BIT, padding) )