Decode a BSON data to multiple documents.
(data: _ReadableBuffer, opts: CodecOptions[_DocumentType])
| 1098 | |
| 1099 | |
| 1100 | def _decode_all(data: _ReadableBuffer, opts: CodecOptions[_DocumentType]) -> list[_DocumentType]: |
| 1101 | """Decode a BSON data to multiple documents.""" |
| 1102 | data, view = get_data_and_view(data) |
| 1103 | data_len = len(data) |
| 1104 | docs: list[_DocumentType] = [] |
| 1105 | position = 0 |
| 1106 | end = data_len - 1 |
| 1107 | use_raw = _raw_document_class(opts.document_class) |
| 1108 | try: |
| 1109 | while position < end: |
| 1110 | obj_size = _UNPACK_INT_FROM(data, position)[0] |
| 1111 | if data_len - position < obj_size: |
| 1112 | raise InvalidBSON( |
| 1113 | f"invalid object size: expected {obj_size}, got {data_len - position}" |
| 1114 | ) |
| 1115 | obj_end = position + obj_size - 1 |
| 1116 | if data[obj_end] != 0: |
| 1117 | raise InvalidBSON("bad eoo") |
| 1118 | if use_raw: |
| 1119 | docs.append(opts.document_class(data[position : obj_end + 1], opts)) # type: ignore |
| 1120 | else: |
| 1121 | docs.append(_elements_to_dict(data, view, position + 4, obj_end, opts)) |
| 1122 | position += obj_size |
| 1123 | return docs |
| 1124 | except InvalidBSON: |
| 1125 | raise |
| 1126 | except Exception: |
| 1127 | # Change exception type to InvalidBSON but preserve traceback. |
| 1128 | _, exc_value, exc_tb = sys.exc_info() |
| 1129 | raise InvalidBSON(str(exc_value)).with_traceback(exc_tb) from None |
| 1130 | |
| 1131 | |
| 1132 | if _USE_C: |
no test coverage detected