@@ -1352,15 +1352,17 @@ class _OpReply:
1352
1352
UNPACK_FROM = struct .Struct ("<iqii" ).unpack_from
1353
1353
OP_CODE = 1
1354
1354
1355
- def __init__ (self , flags : int , cursor_id : int , number_returned : int , documents : bytes ):
1355
+ def __init__ (
1356
+ self , flags : int , cursor_id : int , number_returned : int , documents : bytes | memoryview
1357
+ ):
1356
1358
self .flags = flags
1357
1359
self .cursor_id = Int64 (cursor_id )
1358
1360
self .number_returned = number_returned
1359
1361
self .documents = documents
1360
1362
1361
1363
def raw_response (
1362
1364
self , cursor_id : Optional [int ] = None , user_fields : Optional [Mapping [str , Any ]] = None
1363
- ) -> list [bytes ]:
1365
+ ) -> list [bytes | memoryview ]:
1364
1366
"""Check the response header from the database, without decoding BSON.
1365
1367
1366
1368
Check the response for errors and unpack.
@@ -1448,7 +1450,7 @@ def more_to_come(self) -> bool:
1448
1450
return False
1449
1451
1450
1452
@classmethod
1451
- def unpack (cls , msg : bytes ) -> _OpReply :
1453
+ def unpack (cls , msg : bytes | memoryview ) -> _OpReply :
1452
1454
"""Construct an _OpReply from raw bytes."""
1453
1455
# PYTHON-945: ignore starting_from field.
1454
1456
flags , cursor_id , _ , number_returned = cls .UNPACK_FROM (msg )
@@ -1470,7 +1472,7 @@ class _OpMsg:
1470
1472
MORE_TO_COME = 1 << 1
1471
1473
EXHAUST_ALLOWED = 1 << 16 # Only present on requests.
1472
1474
1473
- def __init__ (self , flags : int , payload_document : bytes ):
1475
+ def __init__ (self , flags : int , payload_document : bytes | memoryview ):
1474
1476
self .flags = flags
1475
1477
self .payload_document = payload_document
1476
1478
@@ -1512,7 +1514,7 @@ def command_response(self, codec_options: CodecOptions[Any]) -> dict[str, Any]:
1512
1514
"""Unpack a command response."""
1513
1515
return self .unpack_response (codec_options = codec_options )[0 ]
1514
1516
1515
- def raw_command_response (self ) -> bytes :
1517
+ def raw_command_response (self ) -> bytes | memoryview :
1516
1518
"""Return the bytes of the command response."""
1517
1519
return self .payload_document
1518
1520
@@ -1522,7 +1524,7 @@ def more_to_come(self) -> bool:
1522
1524
return bool (self .flags & self .MORE_TO_COME )
1523
1525
1524
1526
@classmethod
1525
- def unpack (cls , msg : bytes ) -> _OpMsg :
1527
+ def unpack (cls , msg : bytes | memoryview ) -> _OpMsg :
1526
1528
"""Construct an _OpMsg from raw bytes."""
1527
1529
flags , first_payload_type , first_payload_size = cls .UNPACK_FROM (msg )
1528
1530
if flags != 0 :
@@ -1541,7 +1543,7 @@ def unpack(cls, msg: bytes) -> _OpMsg:
1541
1543
return cls (flags , payload_document )
1542
1544
1543
1545
1544
- _UNPACK_REPLY : dict [int , Callable [[bytes ], Union [_OpReply , _OpMsg ]]] = {
1546
+ _UNPACK_REPLY : dict [int , Callable [[bytes | memoryview ], Union [_OpReply , _OpMsg ]]] = {
1545
1547
_OpReply .OP_CODE : _OpReply .unpack ,
1546
1548
_OpMsg .OP_CODE : _OpMsg .unpack ,
1547
1549
}
0 commit comments