Skip to content

Commit 6507d26

Browse files
committed
PYTHON-1398 - Raw batch methods do not support sessions
1 parent 6c8f5a2 commit 6507d26

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

doc/api/pymongo/collection.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@
4545
.. automethod:: delete_one
4646
.. automethod:: delete_many
4747
.. automethod:: aggregate
48+
.. automethod:: aggregate_raw_batches
4849
.. automethod:: watch
49-
.. automethod:: find(filter=None, projection=None, skip=0, limit=0, no_cursor_timeout=False, cursor_type=CursorType.NON_TAILABLE, sort=None, allow_partial_results=False, oplog_replay=False, modifiers=None, manipulate=True)
50+
.. automethod:: find(filter=None, projection=None, skip=0, limit=0, no_cursor_timeout=False, cursor_type=CursorType.NON_TAILABLE, sort=None, allow_partial_results=False, oplog_replay=False, modifiers=None, batch_size=0, manipulate=True, collation=None, hint=None, max_scan=None, max_time_ms=None, max=None, min=None, return_key=False, show_record_id=False, snapshot=False, comment=None, session=None)
51+
.. automethod:: find_raw_batches(filter=None, projection=None, skip=0, limit=0, no_cursor_timeout=False, cursor_type=CursorType.NON_TAILABLE, sort=None, allow_partial_results=False, oplog_replay=False, modifiers=None, batch_size=0, manipulate=True, collation=None, hint=None, max_scan=None, max_time_ms=None, max=None, min=None, return_key=False, show_record_id=False, snapshot=False, comment=None)
5052
.. automethod:: find_one(filter=None, *args, **kwargs)
5153
.. automethod:: find_one_and_delete
5254
.. automethod:: find_one_and_replace(filter, replacement, projection=None, sort=None, return_document=ReturnDocument.BEFORE, **kwargs)

pymongo/collection.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,7 @@ def find(self, *args, **kwargs):
14031403
def find_raw_batches(self, *args, **kwargs):
14041404
"""Query the database and retrieve batches of raw BSON.
14051405
1406-
Takes the same parameters as :meth:`find` but returns a
1406+
Similar to the :meth:`find` method but returns a
14071407
:class:`~pymongo.cursor.RawBatchCursor`.
14081408
14091409
This example demonstrates how to work with raw batches, but in practice
@@ -1416,11 +1416,15 @@ def find_raw_batches(self, *args, **kwargs):
14161416
>>> for batch in cursor:
14171417
... print(bson.decode_all(batch))
14181418
1419-
Unlike most PyMongo methods, this method sends no session id to the
1420-
server.
1419+
.. note:: find_raw_batches does not support sessions.
14211420
14221421
.. versionadded:: 3.6
14231422
"""
1423+
# OP_MSG with document stream returns is required to support
1424+
# sessions.
1425+
if "session" in kwargs:
1426+
raise ConfigurationError(
1427+
"find_raw_batches does not support sessions")
14241428
return RawBatchCursor(self, *args, **kwargs)
14251429

14261430
def parallel_scan(self, num_cursors, session=None, **kwargs):
@@ -2181,7 +2185,7 @@ def aggregate(self, pipeline, session=None, **kwargs):
21812185
def aggregate_raw_batches(self, pipeline, **kwargs):
21822186
"""Perform an aggregation and retrieve batches of raw BSON.
21832187
2184-
Takes the same parameters as :meth:`aggregate` but returns a
2188+
Similar to the :meth:`aggregate` method but returns a
21852189
:class:`~pymongo.cursor.RawBatchCursor`.
21862190
21872191
This example demonstrates how to work with raw batches, but in practice
@@ -2195,11 +2199,15 @@ def aggregate_raw_batches(self, pipeline, **kwargs):
21952199
>>> for batch in cursor:
21962200
... print(bson.decode_all(batch))
21972201
2198-
Unlike most PyMongo methods, this method sends no session id to the
2199-
server.
2202+
.. note:: aggregate_raw_batches does not support sessions.
22002203
22012204
.. versionadded:: 3.6
22022205
"""
2206+
# OP_MSG with document stream returns is required to support
2207+
# sessions.
2208+
if "session" in kwargs:
2209+
raise ConfigurationError(
2210+
"aggregate_raw_batches does not support sessions")
22032211
return self._aggregate(pipeline, RawBatchCommandCursor, 0,
22042212
None, False, **kwargs)
22052213

test/test_session.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,6 @@ def test_cursor(self):
337337
# Test all cursor methods.
338338
ops = [
339339
('find', lambda session: list(coll.find(session=session))),
340-
('find_raw_batches',
341-
lambda session: list(coll.find_raw_batches(session=session))),
342340
('getitem', lambda session: coll.find(session=session)[0]),
343341
('count', lambda session: coll.find(session=session).count()),
344342
('distinct',
@@ -696,16 +694,8 @@ def test_reads(self):
696694
'ismaster', session=session))
697695
self._test_reads(
698696
lambda coll, session: list(coll.aggregate([], session=session)))
699-
# PYTHON-1398
700-
#self._test_reads(
701-
# lambda coll, session: list(
702-
# coll.aggregate_raw_batches([], session=session)))
703697
self._test_reads(
704698
lambda coll, session: list(coll.find({}, session=session)))
705-
# PYTHON-1398
706-
#self._test_reads(
707-
# lambda coll, session: list(
708-
# coll.find_raw_batches({}, session=session)))
709699
self._test_reads(
710700
lambda coll, session: coll.find_one({}, session=session))
711701
self._test_reads(
@@ -729,6 +719,17 @@ def test_reads(self):
729719
lambda coll, session: list(
730720
coll.parallel_scan(1, session=session)))
731721

722+
self.assertRaises(
723+
ConfigurationError,
724+
self._test_reads,
725+
lambda coll, session: list(
726+
coll.aggregate_raw_batches([], session=session)))
727+
self.assertRaises(
728+
ConfigurationError,
729+
self._test_reads,
730+
lambda coll, session: list(
731+
coll.find_raw_batches({}, session=session)))
732+
732733
def _test_writes(self, op):
733734
coll = self.client.pymongo_test.test
734735
with self.client.start_session() as sess:

0 commit comments

Comments
 (0)