xtquant.xtbson.bson36.binary

  1# Copyright 2009-present MongoDB, Inc.
  2#
  3# Licensed under the Apache License, Version 2.0 (the "License");
  4# you may not use this file except in compliance with the License.
  5# You may obtain a copy of the License at
  6#
  7# http://www.apache.org/licenses/LICENSE-2.0
  8#
  9# Unless required by applicable law or agreed to in writing, software
 10# distributed under the License is distributed on an "AS IS" BASIS,
 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12# See the License for the specific language governing permissions and
 13# limitations under the License.
 14
 15from uuid import UUID
 16from warnings import warn
 17
 18"""Tools for representing BSON binary data.
 19"""
 20
 21BINARY_SUBTYPE = 0
 22"""BSON binary subtype for binary data.
 23
 24This is the default subtype for binary data.
 25"""
 26
 27FUNCTION_SUBTYPE = 1
 28"""BSON binary subtype for functions.
 29"""
 30
 31OLD_BINARY_SUBTYPE = 2
 32"""Old BSON binary subtype for binary data.
 33
 34This is the old default subtype, the current
 35default is :data:`BINARY_SUBTYPE`.
 36"""
 37
 38OLD_UUID_SUBTYPE = 3
 39"""Old BSON binary subtype for a UUID.
 40
 41:class:`uuid.UUID` instances will automatically be encoded
 42by :mod:`bson` using this subtype when using
 43:data:`UuidRepresentation.PYTHON_LEGACY`,
 44:data:`UuidRepresentation.JAVA_LEGACY`, or
 45:data:`UuidRepresentation.CSHARP_LEGACY`.
 46
 47.. versionadded:: 2.1
 48"""
 49
 50UUID_SUBTYPE = 4
 51"""BSON binary subtype for a UUID.
 52
 53This is the standard BSON binary subtype for UUIDs.
 54:class:`uuid.UUID` instances will automatically be encoded
 55by :mod:`bson` using this subtype when using
 56:data:`UuidRepresentation.STANDARD`.
 57"""
 58
 59
 60class UuidRepresentation:
 61    UNSPECIFIED = 0
 62    """An unspecified UUID representation.
 63
 64    When configured, :class:`uuid.UUID` instances will **not** be
 65    automatically encoded to or decoded from :class:`~bson.binary.Binary`.
 66    When encoding a :class:`uuid.UUID` instance, an error will be raised.
 67    To encode a :class:`uuid.UUID` instance with this configuration, it must
 68    be wrapped in the :class:`~bson.binary.Binary` class by the application
 69    code. When decoding a BSON binary field with a UUID subtype, a
 70    :class:`~bson.binary.Binary` instance will be returned instead of a
 71    :class:`uuid.UUID` instance.
 72
 73    See :ref:`unspecified-representation-details` for details.
 74
 75    .. versionadded:: 3.11
 76    """
 77
 78    STANDARD = UUID_SUBTYPE
 79    """The standard UUID representation.
 80
 81    :class:`uuid.UUID` instances will automatically be encoded to
 82    and decoded from . binary, using RFC-4122 byte order with
 83    binary subtype :data:`UUID_SUBTYPE`.
 84
 85    See :ref:`standard-representation-details` for details.
 86
 87    .. versionadded:: 3.11
 88    """
 89
 90    PYTHON_LEGACY = OLD_UUID_SUBTYPE
 91    """The Python legacy UUID representation.
 92
 93    :class:`uuid.UUID` instances will automatically be encoded to
 94    and decoded from . binary, using RFC-4122 byte order with
 95    binary subtype :data:`OLD_UUID_SUBTYPE`.
 96
 97    See :ref:`python-legacy-representation-details` for details.
 98
 99    .. versionadded:: 3.11
100    """
101
102    JAVA_LEGACY = 5
103    """The Java legacy UUID representation.
104
105    :class:`uuid.UUID` instances will automatically be encoded to
106    and decoded from . binary subtype :data:`OLD_UUID_SUBTYPE`,
107    using the Java driver's legacy byte order.
108
109    See :ref:`java-legacy-representation-details` for details.
110
111    .. versionadded:: 3.11
112    """
113
114    CSHARP_LEGACY = 6
115    """The C#/.net legacy UUID representation.
116
117    :class:`uuid.UUID` instances will automatically be encoded to
118    and decoded from . binary subtype :data:`OLD_UUID_SUBTYPE`,
119    using the C# driver's legacy byte order.
120
121    See :ref:`csharp-legacy-representation-details` for details.
122
123    .. versionadded:: 3.11
124    """
125
126
127STANDARD = UuidRepresentation.STANDARD
128"""An alias for :data:`UuidRepresentation.STANDARD`.
129
130.. versionadded:: 3.0
131"""
132
133PYTHON_LEGACY = UuidRepresentation.PYTHON_LEGACY
134"""An alias for :data:`UuidRepresentation.PYTHON_LEGACY`.
135
136.. versionadded:: 3.0
137"""
138
139JAVA_LEGACY = UuidRepresentation.JAVA_LEGACY
140"""An alias for :data:`UuidRepresentation.JAVA_LEGACY`.
141
142.. versionchanged:: 3.6
143   BSON binary subtype 4 is decoded using RFC-4122 byte order.
144.. versionadded:: 2.3
145"""
146
147CSHARP_LEGACY = UuidRepresentation.CSHARP_LEGACY
148"""An alias for :data:`UuidRepresentation.CSHARP_LEGACY`.
149
150.. versionchanged:: 3.6
151   BSON binary subtype 4 is decoded using RFC-4122 byte order.
152.. versionadded:: 2.3
153"""
154
155ALL_UUID_SUBTYPES = (OLD_UUID_SUBTYPE, UUID_SUBTYPE)
156ALL_UUID_REPRESENTATIONS = (
157    UuidRepresentation.UNSPECIFIED,
158    UuidRepresentation.STANDARD,
159    UuidRepresentation.PYTHON_LEGACY,
160    UuidRepresentation.JAVA_LEGACY,
161    UuidRepresentation.CSHARP_LEGACY,
162)
163UUID_REPRESENTATION_NAMES = {
164    UuidRepresentation.UNSPECIFIED: "UuidRepresentation.UNSPECIFIED",
165    UuidRepresentation.STANDARD: "UuidRepresentation.STANDARD",
166    UuidRepresentation.PYTHON_LEGACY: "UuidRepresentation.PYTHON_LEGACY",
167    UuidRepresentation.JAVA_LEGACY: "UuidRepresentation.JAVA_LEGACY",
168    UuidRepresentation.CSHARP_LEGACY: "UuidRepresentation.CSHARP_LEGACY",
169}
170
171MD5_SUBTYPE = 5
172"""BSON binary subtype for an MD5 hash.
173"""
174
175COLUMN_SUBTYPE = 7
176"""BSON binary subtype for columns.
177
178.. versionadded:: 4.0
179"""
180
181USER_DEFINED_SUBTYPE = 128
182"""BSON binary subtype for any user defined structure.
183"""
184
185
186class Binary(bytes):
187    """Representation of BSON binary data.
188
189    This is necessary because we want to represent Python strings as
190    the BSON string type. We need to wrap binary data so we can tell
191    the difference between what should be considered binary data and
192    what should be considered a string when we encode to BSON.
193
194    Raises TypeError if `data` is not an instance of :class:`bytes`
195    (:class:`str` in python 2) or `subtype` is not an instance of
196    :class:`int`. Raises ValueError if `subtype` is not in [0, 256).
197
198    .. note::
199      In python 3 instances of Binary with subtype 0 will be decoded
200      directly to :class:`bytes`.
201
202    :Parameters:
203      - `data`: the binary data to represent. Can be any bytes-like type
204        that implements the buffer protocol.
205      - `subtype` (optional): the `binary subtype
206        <http://bsonspec.org/#/specification>`_
207        to use
208
209    .. versionchanged:: 3.9
210      Support any bytes-like type that implements the buffer protocol.
211    """
212
213    _type_marker = 5
214
215    def __new__(cls, data, subtype=BINARY_SUBTYPE):
216        if not isinstance(subtype, int):
217            raise TypeError("subtype must be an instance of int")
218        if subtype >= 256 or subtype < 0:
219            raise ValueError("subtype must be contained in [0, 256)")
220        # Support any type that implements the buffer protocol.
221        self = bytes.__new__(cls, memoryview(data).tobytes())
222        self.__subtype = subtype
223        return self
224
225    @classmethod
226    def from_uuid(cls, uuid, uuid_representation=UuidRepresentation.STANDARD):
227        """Create a BSON Binary object from a Python UUID.
228
229        Creates a :class:`~bson.binary.Binary` object from a
230        :class:`uuid.UUID` instance. Assumes that the native
231        :class:`uuid.UUID` instance uses the byte-order implied by the
232        provided ``uuid_representation``.
233
234        Raises :exc:`TypeError` if `uuid` is not an instance of
235        :class:`~uuid.UUID`.
236
237        :Parameters:
238          - `uuid`: A :class:`uuid.UUID` instance.
239          - `uuid_representation`: A member of
240            :class:`~bson.binary.UuidRepresentation`. Default:
241            :const:`~bson.binary.UuidRepresentation.STANDARD`.
242            See :ref:`handling-uuid-data-example` for details.
243
244        .. versionadded:: 3.11
245        """
246        if not isinstance(uuid, UUID):
247            raise TypeError("uuid must be an instance of uuid.UUID")
248
249        if uuid_representation not in ALL_UUID_REPRESENTATIONS:
250            raise ValueError(
251                "uuid_representation must be a value " "from .binary.UuidRepresentation"
252            )
253
254        if uuid_representation == UuidRepresentation.UNSPECIFIED:
255            raise ValueError(
256                "cannot encode native uuid.UUID with "
257                "UuidRepresentation.UNSPECIFIED. UUIDs can be manually "
258                "converted to bson.Binary instances using "
259                "bson.Binary.from_uuid() or a different UuidRepresentation "
260                "can be configured. See the documentation for "
261                "UuidRepresentation for more information."
262            )
263
264        subtype = OLD_UUID_SUBTYPE
265        if uuid_representation == UuidRepresentation.PYTHON_LEGACY:
266            payload = uuid.bytes
267        elif uuid_representation == UuidRepresentation.JAVA_LEGACY:
268            from_uuid = uuid.bytes
269            payload = from_uuid[0:8][::-1] + from_uuid[8:16][::-1]
270        elif uuid_representation == UuidRepresentation.CSHARP_LEGACY:
271            payload = uuid.bytes_le
272        else:
273            # uuid_representation == UuidRepresentation.STANDARD
274            subtype = UUID_SUBTYPE
275            payload = uuid.bytes
276
277        return cls(payload, subtype)
278
279    def as_uuid(self, uuid_representation=UuidRepresentation.STANDARD):
280        """Create a Python UUID from this BSON Binary object.
281
282        Decodes this binary object as a native :class:`uuid.UUID` instance
283        with the provided ``uuid_representation``.
284
285        Raises :exc:`ValueError` if this :class:`~bson.binary.Binary` instance
286        does not contain a UUID.
287
288        :Parameters:
289          - `uuid_representation`: A member of
290            :class:`~bson.binary.UuidRepresentation`. Default:
291            :const:`~bson.binary.UuidRepresentation.STANDARD`.
292            See :ref:`handling-uuid-data-example` for details.
293
294        .. versionadded:: 3.11
295        """
296        if self.subtype not in ALL_UUID_SUBTYPES:
297            raise ValueError("cannot decode subtype %s as a uuid" % (self.subtype,))
298
299        if uuid_representation not in ALL_UUID_REPRESENTATIONS:
300            raise ValueError(
301                "uuid_representation must be a value from " "bson.binary.UuidRepresentation"
302            )
303
304        if uuid_representation == UuidRepresentation.UNSPECIFIED:
305            raise ValueError("uuid_representation cannot be UNSPECIFIED")
306        elif uuid_representation == UuidRepresentation.PYTHON_LEGACY:
307            if self.subtype == OLD_UUID_SUBTYPE:
308                return UUID(bytes=self)
309        elif uuid_representation == UuidRepresentation.JAVA_LEGACY:
310            if self.subtype == OLD_UUID_SUBTYPE:
311                return UUID(bytes=self[0:8][::-1] + self[8:16][::-1])
312        elif uuid_representation == UuidRepresentation.CSHARP_LEGACY:
313            if self.subtype == OLD_UUID_SUBTYPE:
314                return UUID(bytes_le=self)
315        else:
316            # uuid_representation == UuidRepresentation.STANDARD
317            if self.subtype == UUID_SUBTYPE:
318                return UUID(bytes=self)
319
320        raise ValueError(
321            "cannot decode subtype %s to %s"
322            % (self.subtype, UUID_REPRESENTATION_NAMES[uuid_representation])
323        )
324
325    @property
326    def subtype(self):
327        """Subtype of this binary data."""
328        return self.__subtype
329
330    def __getnewargs__(self):
331        # Work around http://bugs.python.org/issue7382
332        data = super(Binary, self).__getnewargs__()[0]
333        if not isinstance(data, bytes):
334            data = data.encode("latin-1")
335        return data, self.__subtype
336
337    def __eq__(self, other):
338        if isinstance(other, Binary):
339            return (self.__subtype, bytes(self)) == (other.subtype, bytes(other))
340        # We don't return NotImplemented here because if we did then
341        # Binary("foo") == "foo" would return True, since Binary is a
342        # subclass of str...
343        return False
344
345    def __hash__(self):
346        return super(Binary, self).__hash__() ^ hash(self.__subtype)
347
348    def __ne__(self, other):
349        return not self == other
350
351    def __repr__(self):
352        return "Binary(%s, %s)" % (bytes.__repr__(self), self.__subtype)
BINARY_SUBTYPE = 0

BSON binary subtype for binary data.

This is the default subtype for binary data.

FUNCTION_SUBTYPE = 1

BSON binary subtype for functions.

OLD_BINARY_SUBTYPE = 2

Old BSON binary subtype for binary data.

This is the old default subtype, the current default is BINARY_SUBTYPE.

OLD_UUID_SUBTYPE = 3

Old BSON binary subtype for a UUID.

uuid.UUID instances will automatically be encoded by bson using this subtype when using UuidRepresentation.PYTHON_LEGACY, UuidRepresentation.JAVA_LEGACY, or UuidRepresentation.CSHARP_LEGACY.

New in version 2.1.

UUID_SUBTYPE = 4

BSON binary subtype for a UUID.

This is the standard BSON binary subtype for UUIDs. uuid.UUID instances will automatically be encoded by bson using this subtype when using UuidRepresentation.STANDARD.

class UuidRepresentation:
 61class UuidRepresentation:
 62    UNSPECIFIED = 0
 63    """An unspecified UUID representation.
 64
 65    When configured, :class:`uuid.UUID` instances will **not** be
 66    automatically encoded to or decoded from :class:`~bson.binary.Binary`.
 67    When encoding a :class:`uuid.UUID` instance, an error will be raised.
 68    To encode a :class:`uuid.UUID` instance with this configuration, it must
 69    be wrapped in the :class:`~bson.binary.Binary` class by the application
 70    code. When decoding a BSON binary field with a UUID subtype, a
 71    :class:`~bson.binary.Binary` instance will be returned instead of a
 72    :class:`uuid.UUID` instance.
 73
 74    See :ref:`unspecified-representation-details` for details.
 75
 76    .. versionadded:: 3.11
 77    """
 78
 79    STANDARD = UUID_SUBTYPE
 80    """The standard UUID representation.
 81
 82    :class:`uuid.UUID` instances will automatically be encoded to
 83    and decoded from . binary, using RFC-4122 byte order with
 84    binary subtype :data:`UUID_SUBTYPE`.
 85
 86    See :ref:`standard-representation-details` for details.
 87
 88    .. versionadded:: 3.11
 89    """
 90
 91    PYTHON_LEGACY = OLD_UUID_SUBTYPE
 92    """The Python legacy UUID representation.
 93
 94    :class:`uuid.UUID` instances will automatically be encoded to
 95    and decoded from . binary, using RFC-4122 byte order with
 96    binary subtype :data:`OLD_UUID_SUBTYPE`.
 97
 98    See :ref:`python-legacy-representation-details` for details.
 99
100    .. versionadded:: 3.11
101    """
102
103    JAVA_LEGACY = 5
104    """The Java legacy UUID representation.
105
106    :class:`uuid.UUID` instances will automatically be encoded to
107    and decoded from . binary subtype :data:`OLD_UUID_SUBTYPE`,
108    using the Java driver's legacy byte order.
109
110    See :ref:`java-legacy-representation-details` for details.
111
112    .. versionadded:: 3.11
113    """
114
115    CSHARP_LEGACY = 6
116    """The C#/.net legacy UUID representation.
117
118    :class:`uuid.UUID` instances will automatically be encoded to
119    and decoded from . binary subtype :data:`OLD_UUID_SUBTYPE`,
120    using the C# driver's legacy byte order.
121
122    See :ref:`csharp-legacy-representation-details` for details.
123
124    .. versionadded:: 3.11
125    """
UNSPECIFIED = 0

An unspecified UUID representation.

When configured, uuid.UUID instances will not be automatically encoded to or decoded from ~bson.binary.Binary. When encoding a uuid.UUID instance, an error will be raised. To encode a uuid.UUID instance with this configuration, it must be wrapped in the ~bson.binary.Binary class by the application code. When decoding a BSON binary field with a UUID subtype, a ~bson.binary.Binary instance will be returned instead of a uuid.UUID instance.

See :ref:unspecified-representation-details for details.

New in version 3.11.

STANDARD = 4

The standard UUID representation.

uuid.UUID instances will automatically be encoded to and decoded from . binary, using RFC-4122 byte order with binary subtype UUID_SUBTYPE.

See :ref:standard-representation-details for details.

New in version 3.11.

PYTHON_LEGACY = 3

The Python legacy UUID representation.

uuid.UUID instances will automatically be encoded to and decoded from . binary, using RFC-4122 byte order with binary subtype OLD_UUID_SUBTYPE.

See :ref:python-legacy-representation-details for details.

New in version 3.11.

JAVA_LEGACY = 5

The Java legacy UUID representation.

uuid.UUID instances will automatically be encoded to and decoded from . binary subtype OLD_UUID_SUBTYPE, using the Java driver's legacy byte order.

See :ref:java-legacy-representation-details for details.

New in version 3.11.

CSHARP_LEGACY = 6

The C#/.net legacy UUID representation.

uuid.UUID instances will automatically be encoded to and decoded from . binary subtype OLD_UUID_SUBTYPE, using the C# driver's legacy byte order.

See :ref:csharp-legacy-representation-details for details.

New in version 3.11.

STANDARD = 4

An alias for UuidRepresentation.STANDARD.

New in version 3.0.

PYTHON_LEGACY = 3

An alias for UuidRepresentation.PYTHON_LEGACY.

New in version 3.0.

JAVA_LEGACY = 5

An alias for UuidRepresentation.JAVA_LEGACY.

Changed in version 3.6: BSON binary subtype 4 is decoded using RFC-4122 byte order.

New in version 2.3.

CSHARP_LEGACY = 6

An alias for UuidRepresentation.CSHARP_LEGACY.

Changed in version 3.6: BSON binary subtype 4 is decoded using RFC-4122 byte order.

New in version 2.3.

ALL_UUID_SUBTYPES = (3, 4)
ALL_UUID_REPRESENTATIONS = (0, 4, 3, 5, 6)
MD5_SUBTYPE = 5

BSON binary subtype for an MD5 hash.

COLUMN_SUBTYPE = 7

BSON binary subtype for columns.

New in version 4.0.

USER_DEFINED_SUBTYPE = 128

BSON binary subtype for any user defined structure.

class Binary(builtins.bytes):
187class Binary(bytes):
188    """Representation of BSON binary data.
189
190    This is necessary because we want to represent Python strings as
191    the BSON string type. We need to wrap binary data so we can tell
192    the difference between what should be considered binary data and
193    what should be considered a string when we encode to BSON.
194
195    Raises TypeError if `data` is not an instance of :class:`bytes`
196    (:class:`str` in python 2) or `subtype` is not an instance of
197    :class:`int`. Raises ValueError if `subtype` is not in [0, 256).
198
199    .. note::
200      In python 3 instances of Binary with subtype 0 will be decoded
201      directly to :class:`bytes`.
202
203    :Parameters:
204      - `data`: the binary data to represent. Can be any bytes-like type
205        that implements the buffer protocol.
206      - `subtype` (optional): the `binary subtype
207        <http://bsonspec.org/#/specification>`_
208        to use
209
210    .. versionchanged:: 3.9
211      Support any bytes-like type that implements the buffer protocol.
212    """
213
214    _type_marker = 5
215
216    def __new__(cls, data, subtype=BINARY_SUBTYPE):
217        if not isinstance(subtype, int):
218            raise TypeError("subtype must be an instance of int")
219        if subtype >= 256 or subtype < 0:
220            raise ValueError("subtype must be contained in [0, 256)")
221        # Support any type that implements the buffer protocol.
222        self = bytes.__new__(cls, memoryview(data).tobytes())
223        self.__subtype = subtype
224        return self
225
226    @classmethod
227    def from_uuid(cls, uuid, uuid_representation=UuidRepresentation.STANDARD):
228        """Create a BSON Binary object from a Python UUID.
229
230        Creates a :class:`~bson.binary.Binary` object from a
231        :class:`uuid.UUID` instance. Assumes that the native
232        :class:`uuid.UUID` instance uses the byte-order implied by the
233        provided ``uuid_representation``.
234
235        Raises :exc:`TypeError` if `uuid` is not an instance of
236        :class:`~uuid.UUID`.
237
238        :Parameters:
239          - `uuid`: A :class:`uuid.UUID` instance.
240          - `uuid_representation`: A member of
241            :class:`~bson.binary.UuidRepresentation`. Default:
242            :const:`~bson.binary.UuidRepresentation.STANDARD`.
243            See :ref:`handling-uuid-data-example` for details.
244
245        .. versionadded:: 3.11
246        """
247        if not isinstance(uuid, UUID):
248            raise TypeError("uuid must be an instance of uuid.UUID")
249
250        if uuid_representation not in ALL_UUID_REPRESENTATIONS:
251            raise ValueError(
252                "uuid_representation must be a value " "from .binary.UuidRepresentation"
253            )
254
255        if uuid_representation == UuidRepresentation.UNSPECIFIED:
256            raise ValueError(
257                "cannot encode native uuid.UUID with "
258                "UuidRepresentation.UNSPECIFIED. UUIDs can be manually "
259                "converted to bson.Binary instances using "
260                "bson.Binary.from_uuid() or a different UuidRepresentation "
261                "can be configured. See the documentation for "
262                "UuidRepresentation for more information."
263            )
264
265        subtype = OLD_UUID_SUBTYPE
266        if uuid_representation == UuidRepresentation.PYTHON_LEGACY:
267            payload = uuid.bytes
268        elif uuid_representation == UuidRepresentation.JAVA_LEGACY:
269            from_uuid = uuid.bytes
270            payload = from_uuid[0:8][::-1] + from_uuid[8:16][::-1]
271        elif uuid_representation == UuidRepresentation.CSHARP_LEGACY:
272            payload = uuid.bytes_le
273        else:
274            # uuid_representation == UuidRepresentation.STANDARD
275            subtype = UUID_SUBTYPE
276            payload = uuid.bytes
277
278        return cls(payload, subtype)
279
280    def as_uuid(self, uuid_representation=UuidRepresentation.STANDARD):
281        """Create a Python UUID from this BSON Binary object.
282
283        Decodes this binary object as a native :class:`uuid.UUID` instance
284        with the provided ``uuid_representation``.
285
286        Raises :exc:`ValueError` if this :class:`~bson.binary.Binary` instance
287        does not contain a UUID.
288
289        :Parameters:
290          - `uuid_representation`: A member of
291            :class:`~bson.binary.UuidRepresentation`. Default:
292            :const:`~bson.binary.UuidRepresentation.STANDARD`.
293            See :ref:`handling-uuid-data-example` for details.
294
295        .. versionadded:: 3.11
296        """
297        if self.subtype not in ALL_UUID_SUBTYPES:
298            raise ValueError("cannot decode subtype %s as a uuid" % (self.subtype,))
299
300        if uuid_representation not in ALL_UUID_REPRESENTATIONS:
301            raise ValueError(
302                "uuid_representation must be a value from " "bson.binary.UuidRepresentation"
303            )
304
305        if uuid_representation == UuidRepresentation.UNSPECIFIED:
306            raise ValueError("uuid_representation cannot be UNSPECIFIED")
307        elif uuid_representation == UuidRepresentation.PYTHON_LEGACY:
308            if self.subtype == OLD_UUID_SUBTYPE:
309                return UUID(bytes=self)
310        elif uuid_representation == UuidRepresentation.JAVA_LEGACY:
311            if self.subtype == OLD_UUID_SUBTYPE:
312                return UUID(bytes=self[0:8][::-1] + self[8:16][::-1])
313        elif uuid_representation == UuidRepresentation.CSHARP_LEGACY:
314            if self.subtype == OLD_UUID_SUBTYPE:
315                return UUID(bytes_le=self)
316        else:
317            # uuid_representation == UuidRepresentation.STANDARD
318            if self.subtype == UUID_SUBTYPE:
319                return UUID(bytes=self)
320
321        raise ValueError(
322            "cannot decode subtype %s to %s"
323            % (self.subtype, UUID_REPRESENTATION_NAMES[uuid_representation])
324        )
325
326    @property
327    def subtype(self):
328        """Subtype of this binary data."""
329        return self.__subtype
330
331    def __getnewargs__(self):
332        # Work around http://bugs.python.org/issue7382
333        data = super(Binary, self).__getnewargs__()[0]
334        if not isinstance(data, bytes):
335            data = data.encode("latin-1")
336        return data, self.__subtype
337
338    def __eq__(self, other):
339        if isinstance(other, Binary):
340            return (self.__subtype, bytes(self)) == (other.subtype, bytes(other))
341        # We don't return NotImplemented here because if we did then
342        # Binary("foo") == "foo" would return True, since Binary is a
343        # subclass of str...
344        return False
345
346    def __hash__(self):
347        return super(Binary, self).__hash__() ^ hash(self.__subtype)
348
349    def __ne__(self, other):
350        return not self == other
351
352    def __repr__(self):
353        return "Binary(%s, %s)" % (bytes.__repr__(self), self.__subtype)

Representation of BSON binary data.

This is necessary because we want to represent Python strings as the BSON string type. We need to wrap binary data so we can tell the difference between what should be considered binary data and what should be considered a string when we encode to BSON.

Raises TypeError if data is not an instance of bytes (str in python 2) or subtype is not an instance of int. Raises ValueError if subtype is not in [0, 256).

In python 3 instances of Binary with subtype 0 will be decoded directly to bytes.

:Parameters:

  • data: the binary data to represent. Can be any bytes-like type that implements the buffer protocol.
  • subtype (optional): the binary subtype to use

Changed in version 3.9: Support any bytes-like type that implements the buffer protocol.

@classmethod
def from_uuid(cls, uuid, uuid_representation=4):
226    @classmethod
227    def from_uuid(cls, uuid, uuid_representation=UuidRepresentation.STANDARD):
228        """Create a BSON Binary object from a Python UUID.
229
230        Creates a :class:`~bson.binary.Binary` object from a
231        :class:`uuid.UUID` instance. Assumes that the native
232        :class:`uuid.UUID` instance uses the byte-order implied by the
233        provided ``uuid_representation``.
234
235        Raises :exc:`TypeError` if `uuid` is not an instance of
236        :class:`~uuid.UUID`.
237
238        :Parameters:
239          - `uuid`: A :class:`uuid.UUID` instance.
240          - `uuid_representation`: A member of
241            :class:`~bson.binary.UuidRepresentation`. Default:
242            :const:`~bson.binary.UuidRepresentation.STANDARD`.
243            See :ref:`handling-uuid-data-example` for details.
244
245        .. versionadded:: 3.11
246        """
247        if not isinstance(uuid, UUID):
248            raise TypeError("uuid must be an instance of uuid.UUID")
249
250        if uuid_representation not in ALL_UUID_REPRESENTATIONS:
251            raise ValueError(
252                "uuid_representation must be a value " "from .binary.UuidRepresentation"
253            )
254
255        if uuid_representation == UuidRepresentation.UNSPECIFIED:
256            raise ValueError(
257                "cannot encode native uuid.UUID with "
258                "UuidRepresentation.UNSPECIFIED. UUIDs can be manually "
259                "converted to bson.Binary instances using "
260                "bson.Binary.from_uuid() or a different UuidRepresentation "
261                "can be configured. See the documentation for "
262                "UuidRepresentation for more information."
263            )
264
265        subtype = OLD_UUID_SUBTYPE
266        if uuid_representation == UuidRepresentation.PYTHON_LEGACY:
267            payload = uuid.bytes
268        elif uuid_representation == UuidRepresentation.JAVA_LEGACY:
269            from_uuid = uuid.bytes
270            payload = from_uuid[0:8][::-1] + from_uuid[8:16][::-1]
271        elif uuid_representation == UuidRepresentation.CSHARP_LEGACY:
272            payload = uuid.bytes_le
273        else:
274            # uuid_representation == UuidRepresentation.STANDARD
275            subtype = UUID_SUBTYPE
276            payload = uuid.bytes
277
278        return cls(payload, subtype)

Create a BSON Binary object from a Python UUID.

Creates a ~bson.binary.Binary object from a uuid.UUID instance. Assumes that the native uuid.UUID instance uses the byte-order implied by the provided uuid_representation.

Raises TypeError if uuid is not an instance of ~uuid.UUID.

:Parameters:

  • uuid: A uuid.UUID instance.
  • uuid_representation: A member of ~bson.binary.UuidRepresentation. Default: ~bson.binary.UuidRepresentation.STANDARD. See :ref:handling-uuid-data-example for details.

New in version 3.11.

def as_uuid(self, uuid_representation=4):
280    def as_uuid(self, uuid_representation=UuidRepresentation.STANDARD):
281        """Create a Python UUID from this BSON Binary object.
282
283        Decodes this binary object as a native :class:`uuid.UUID` instance
284        with the provided ``uuid_representation``.
285
286        Raises :exc:`ValueError` if this :class:`~bson.binary.Binary` instance
287        does not contain a UUID.
288
289        :Parameters:
290          - `uuid_representation`: A member of
291            :class:`~bson.binary.UuidRepresentation`. Default:
292            :const:`~bson.binary.UuidRepresentation.STANDARD`.
293            See :ref:`handling-uuid-data-example` for details.
294
295        .. versionadded:: 3.11
296        """
297        if self.subtype not in ALL_UUID_SUBTYPES:
298            raise ValueError("cannot decode subtype %s as a uuid" % (self.subtype,))
299
300        if uuid_representation not in ALL_UUID_REPRESENTATIONS:
301            raise ValueError(
302                "uuid_representation must be a value from " "bson.binary.UuidRepresentation"
303            )
304
305        if uuid_representation == UuidRepresentation.UNSPECIFIED:
306            raise ValueError("uuid_representation cannot be UNSPECIFIED")
307        elif uuid_representation == UuidRepresentation.PYTHON_LEGACY:
308            if self.subtype == OLD_UUID_SUBTYPE:
309                return UUID(bytes=self)
310        elif uuid_representation == UuidRepresentation.JAVA_LEGACY:
311            if self.subtype == OLD_UUID_SUBTYPE:
312                return UUID(bytes=self[0:8][::-1] + self[8:16][::-1])
313        elif uuid_representation == UuidRepresentation.CSHARP_LEGACY:
314            if self.subtype == OLD_UUID_SUBTYPE:
315                return UUID(bytes_le=self)
316        else:
317            # uuid_representation == UuidRepresentation.STANDARD
318            if self.subtype == UUID_SUBTYPE:
319                return UUID(bytes=self)
320
321        raise ValueError(
322            "cannot decode subtype %s to %s"
323            % (self.subtype, UUID_REPRESENTATION_NAMES[uuid_representation])
324        )

Create a Python UUID from this BSON Binary object.

Decodes this binary object as a native uuid.UUID instance with the provided uuid_representation.

Raises ValueError if this ~bson.binary.Binary instance does not contain a UUID.

:Parameters:

  • uuid_representation: A member of ~bson.binary.UuidRepresentation. Default: ~bson.binary.UuidRepresentation.STANDARD. See :ref:handling-uuid-data-example for details.

New in version 3.11.

subtype

Subtype of this binary data.

Inherited Members
builtins.bytes
capitalize
center
count
decode
endswith
expandtabs
find
fromhex
hex
index
isalnum
isalpha
isascii
isdigit
islower
isspace
istitle
isupper
join
ljust
lower
lstrip
maketrans
partition
replace
removeprefix
removesuffix
rfind
rindex
rjust
rpartition
rsplit
rstrip
split
splitlines
startswith
strip
swapcase
title
translate
upper
zfill