Skip to content

Commit 42c2ce4

Browse files
committed
Add a manipulate option to find(_one) PYTHON-224
Leaving this set to True by default so there are no surprises when folks upgrade. We probably want to make False the default in a later release.
1 parent 5c581a2 commit 42c2ce4

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

pymongo/cursor.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class Cursor(object):
4141
def __init__(self, collection, spec=None, fields=None, skip=0, limit=0,
4242
timeout=True, snapshot=False, tailable=False, sort=None,
4343
max_scan=None, as_class=None, slave_okay=False,
44-
await_data=False, partial=False, _must_use_master=False,
45-
_is_command=False, **kwargs):
44+
await_data=False, partial=False, manipulate=True,
45+
_must_use_master=False, _is_command=False, **kwargs):
4646
"""Create a new cursor.
4747
4848
Should not be called directly by application developers - see
@@ -109,6 +109,7 @@ def __init__(self, collection, spec=None, fields=None, skip=0, limit=0,
109109
self.__hint = None
110110
self.__as_class = as_class
111111
self.__slave_okay = slave_okay
112+
self.__manipulate = manipulate
112113
self.__tz_aware = collection.database.connection.tz_aware
113114
self.__must_use_master = _must_use_master
114115
self.__is_command = _is_command
@@ -173,6 +174,7 @@ def clone(self):
173174
copy.__slave_okay = self.__slave_okay
174175
copy.__await_data = self.__await_data
175176
copy.__partial = self.__partial
177+
copy.__manipulate = self.__manipulate
176178
copy.__must_use_master = self.__must_use_master
177179
copy.__is_command = self.__is_command
178180
copy.__query_flags = self.__query_flags
@@ -651,10 +653,12 @@ def next(self):
651653
raise StopIteration
652654
db = self.__collection.database
653655
if len(self.__data) or self._refresh():
654-
next = db._fix_outgoing(self.__data.pop(0), self.__collection)
656+
if self.__manipulate:
657+
return db._fix_outgoing(self.__data.pop(0), self.__collection)
658+
else:
659+
return self.__data.pop(0)
655660
else:
656661
raise StopIteration
657-
return next
658662

659663
def __enter__(self):
660664
return self

test/test_collection.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from bson.son import SON
3434
from pymongo import ASCENDING, DESCENDING
3535
from pymongo.collection import Collection
36+
from pymongo.son_manipulator import SONManipulator
3637
from pymongo.errors import (DuplicateKeyError,
3738
InvalidDocument,
3839
InvalidName,
@@ -1250,5 +1251,23 @@ def test_find_and_modify(self):
12501251
c.find_and_modify({'_id': 1}, {'$inc': {'i': 1}},
12511252
new=True, fields={'i': 1}))
12521253

1254+
def test_disabling_manipulators(self):
1255+
1256+
class IncByTwo(SONManipulator):
1257+
def transform_outgoing(self, son, collection):
1258+
if 'foo' in son:
1259+
son['foo'] += 2
1260+
return son
1261+
1262+
db = self.connection.pymongo_test
1263+
db.add_son_manipulator(IncByTwo())
1264+
c = db.test
1265+
c.drop()
1266+
c.insert({'foo': 0})
1267+
self.assertEqual(2, c.find_one()['foo'])
1268+
self.assertEqual(0, c.find_one(manipulate=False)['foo'])
1269+
self.assertEqual(2, c.find_one(manipulate=True)['foo'])
1270+
c.remove({})
1271+
12531272
if __name__ == "__main__":
12541273
unittest.main()

test/test_cursor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,8 @@ class MyClass(dict):
463463
as_class=MyClass,
464464
slave_okay=True,
465465
await_data=True,
466-
partial=True).limit(2)
466+
partial=True,
467+
manipulate=False).limit(2)
467468
cursor.add_option(64)
468469

469470
cursor2 = cursor.clone()
@@ -479,6 +480,8 @@ class MyClass(dict):
479480
self.assertEqual(cursor._Cursor__await_data,
480481
cursor2._Cursor__await_data)
481482
self.assertEqual(cursor._Cursor__partial, cursor2._Cursor__partial)
483+
self.assertEqual(cursor._Cursor__manipulate,
484+
cursor2._Cursor__manipulate)
482485
self.assertEqual(cursor._Cursor__query_flags,
483486
cursor2._Cursor__query_flags)
484487

0 commit comments

Comments
 (0)