MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / _get_binary

Function _get_binary

bson/__init__.py:376–410  ·  view source on GitHub ↗

Decode a BSON binary to bson.binary.Binary or python UUID.

(
    data: Any, _view: Any, position: int, obj_end: int, opts: CodecOptions[Any], dummy1: Any
)

Source from the content-addressed store, hash-verified

374
375
376def _get_binary(
377 data: Any, _view: Any, position: int, obj_end: int, opts: CodecOptions[Any], dummy1: Any
378) -> Tuple[Union[Binary, uuid.UUID], int]:
379 """Decode a BSON binary to bson.binary.Binary or python UUID."""
380 length, subtype = _UNPACK_LENGTH_SUBTYPE_FROM(data, position)
381 position += 5
382 if subtype == 2:
383 length2 = _UNPACK_INT_FROM(data, position)[0]
384 position += 4
385 if length2 != length - 4:
386 raise InvalidBSON("invalid binary (st 2) - lengths don't match!")
387 length = length2
388 end = position + length
389 if length < 0 or end > obj_end:
390 raise InvalidBSON("bad binary object length")
391
392 # Convert UUID subtypes to native UUIDs.
393 if subtype in ALL_UUID_SUBTYPES:
394 uuid_rep = opts.uuid_representation
395 binary_value = Binary(data[position:end], subtype)
396 if (
397 (uuid_rep == UuidRepresentation.UNSPECIFIED)
398 or (subtype == UUID_SUBTYPE and uuid_rep != STANDARD)
399 or (subtype == OLD_UUID_SUBTYPE and uuid_rep == STANDARD)
400 ):
401 return binary_value, end
402 return binary_value.as_uuid(uuid_rep), end
403
404 # Decode subtype 0 to 'bytes'.
405 if subtype == 0:
406 value = data[position:end]
407 else:
408 value = Binary(data[position:end], subtype)
409
410 return value, end
411
412
413def _get_oid(

Callers

nothing calls this directly

Calls 3

as_uuidMethod · 0.95
InvalidBSONClass · 0.90
BinaryClass · 0.90

Tested by

no test coverage detected