Skip to content

Commit 2c676bc

Browse files
committed
PYTHON-2719 RawBatchCursor must raise StopIteration instead of returning empty bytes when the cursor contains no results (#624)
(cherry picked from commit 048ee81)
1 parent 6ac83c1 commit 2c676bc

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

pymongo/message.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,9 @@ def raw_response(self, cursor_id=None):
15281528
error_object.get("$err"),
15291529
error_object.get("code"),
15301530
error_object)
1531-
return [self.documents]
1531+
if self.documents:
1532+
return [self.documents]
1533+
return []
15321534

15331535
def unpack_response(self, cursor_id=None,
15341536
codec_options=_UNICODE_REPLACE_CODEC_OPTIONS,

test/test_cursor.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,14 @@ def test_explain(self):
14831483
explanation = c.find_raw_batches().explain()
14841484
self.assertIsInstance(explanation, dict)
14851485

1486+
def test_empty(self):
1487+
self.db.test.drop()
1488+
cursor = self.db.test.find_raw_batches()
1489+
with self.assertRaises(StopIteration):
1490+
next(cursor)
1491+
14861492
def test_clone(self):
1493+
self.db.test.insert_one({})
14871494
cursor = self.db.test.find_raw_batches()
14881495
# Copy of a RawBatchCursor is also a RawBatchCursor, not a Cursor.
14891496
self.assertIsInstance(next(cursor.clone()), bytes)

0 commit comments

Comments
 (0)