Skip to content

Commit 719b025

Browse files
committed
PYTHON-2254 Fix Cursor.clone with various options
This change adds support for cloning cursors with: "empty", "show_record_id", "return_key", "allow_disk_use", "snapshot", and "exhaust".
1 parent 4be8282 commit 719b025

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

pymongo/cursor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,9 @@ def _clone(self, deepcopy=True, base=None):
285285
"max_time_ms", "max_await_time_ms", "comment",
286286
"max", "min", "ordering", "explain", "hint",
287287
"batch_size", "max_scan", "manipulate",
288-
"query_flags", "modifiers", "collation")
288+
"query_flags", "modifiers", "collation", "empty",
289+
"show_record_id", "return_key", "allow_disk_use",
290+
"snapshot", "exhaust")
289291
data = dict((k, v) for k, v in iteritems(self.__dict__)
290292
if k.startswith('_Cursor__') and k[9:] in values_to_clone)
291293
if deepcopy:

test/test_cursor.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,8 @@ def test_rewind(self):
914914

915915
self.assertEqual(cursor, cursor.rewind())
916916

917+
# manipulate, oplog_reply, and snapshot are all deprecated.
918+
@ignore_deprecations
917919
def test_clone(self):
918920
self.db.test.insert_many([{"x": i} for i in range(1, 4)])
919921

@@ -952,31 +954,30 @@ def test_clone(self):
952954

953955
# Just test attributes
954956
cursor = self.db.test.find({"x": re.compile("^hello.*")},
957+
projection={'_id': False},
955958
skip=1,
956959
no_cursor_timeout=True,
957960
cursor_type=CursorType.TAILABLE_AWAIT,
961+
sort=[("x", 1)],
958962
allow_partial_results=True,
963+
oplog_replay=True,
964+
batch_size=123,
959965
manipulate=False,
960-
projection={'_id': False}).limit(2)
966+
collation={'locale': 'en_US'},
967+
hint=[("_id", 1)],
968+
max_scan=100,
969+
max_time_ms=1000,
970+
return_key=True,
971+
show_record_id=True,
972+
snapshot=True,
973+
allow_disk_use=True).limit(2)
961974
cursor.min([('a', 1)]).max([('b', 3)])
962975
cursor.add_option(128)
963976
cursor.comment('hi!')
964977

978+
# Every attribute should be the same.
965979
cursor2 = cursor.clone()
966-
self.assertEqual(cursor._Cursor__skip, cursor2._Cursor__skip)
967-
self.assertEqual(cursor._Cursor__limit, cursor2._Cursor__limit)
968-
self.assertEqual(type(cursor._Cursor__codec_options),
969-
type(cursor2._Cursor__codec_options))
970-
self.assertEqual(cursor._Cursor__manipulate,
971-
cursor2._Cursor__manipulate)
972-
self.assertEqual(cursor._Cursor__query_flags,
973-
cursor2._Cursor__query_flags)
974-
self.assertEqual(cursor._Cursor__comment,
975-
cursor2._Cursor__comment)
976-
self.assertEqual(cursor._Cursor__min,
977-
cursor2._Cursor__min)
978-
self.assertEqual(cursor._Cursor__max,
979-
cursor2._Cursor__max)
980+
self.assertEqual(cursor.__dict__, cursor2.__dict__)
980981

981982
# Shallow copies can so can mutate
982983
cursor2 = copy.copy(cursor)
@@ -1011,6 +1012,14 @@ def test_clone(self):
10111012
self.assertTrue(isinstance(cursor2._Cursor__hint, SON))
10121013
self.assertEqual(cursor._Cursor__hint, cursor2._Cursor__hint)
10131014

1015+
def test_clone_empty(self):
1016+
self.db.test.delete_many({})
1017+
self.db.test.insert_many([{"x": i} for i in range(1, 4)])
1018+
cursor = self.db.test.find()[2:2]
1019+
cursor2 = cursor.clone()
1020+
self.assertRaises(StopIteration, cursor.next)
1021+
self.assertRaises(StopIteration, cursor2.next)
1022+
10141023
@ignore_deprecations
10151024
def test_count_with_fields(self):
10161025
self.db.test.drop()

0 commit comments

Comments
 (0)