71
71
except ImportError :
72
72
from cStringIO import StringIO
73
73
74
- import bson # From PyMongo 3.0.
75
- import bson .codec_options # From PyMongo 3.0.
76
- import bson .json_util # From PyMongo 3.0.
74
+ # Pure-Python bson lib vendored in from PyMongo 3.0.3.
75
+ from mockupdb import _bson
76
+ import mockupdb ._bson .codec_options as _codec_options
77
+ import mockupdb ._bson .json_util as _json_util
77
78
78
- CODEC_OPTIONS = bson . codec_options .CodecOptions (document_class = OrderedDict )
79
+ CODEC_OPTIONS = _codec_options .CodecOptions (document_class = OrderedDict )
79
80
80
81
PY3 = sys .version_info [0 ] == 3
81
82
if PY3 :
@@ -176,7 +177,7 @@ def going(fn, *args, **kwargs):
176
177
177
178
If an exception is raised within the context, the result is lost:
178
179
179
- >>> with going(lambda: 'return value' as future) :
180
+ >>> with going(lambda: 'return value') as future:
180
181
... assert 1 == 0
181
182
Traceback (most recent call last):
182
183
...
@@ -471,8 +472,8 @@ def _matches_docs(self, docs, other_docs):
471
472
return False
472
473
elif other_doc .get (key , None ) != value :
473
474
return False
474
- if isinstance (doc , (OrderedDict , bson .SON )):
475
- if not isinstance (other_doc , (OrderedDict , bson .SON )):
475
+ if isinstance (doc , (OrderedDict , _bson .SON )):
476
+ if not isinstance (other_doc , (OrderedDict , _bson .SON )):
476
477
raise TypeError (
477
478
"Can't compare ordered and unordered document types:"
478
479
" %r, %r" % (doc , other_doc ))
@@ -543,7 +544,7 @@ def unpack(cls, msg, client, server, request_id):
543
544
pos += 4
544
545
num_to_return , = _UNPACK_INT (msg [pos :pos + 4 ])
545
546
pos += 4
546
- docs = bson .decode_all (msg [pos :], CODEC_OPTIONS )
547
+ docs = _bson .decode_all (msg [pos :], CODEC_OPTIONS )
547
548
if is_command :
548
549
assert len (docs ) == 1
549
550
command_ns = namespace [:- len ('.$cmd' )]
@@ -736,7 +737,7 @@ def unpack(cls, msg, client, server, request_id):
736
737
"""
737
738
flags , = _UNPACK_INT (msg [:4 ])
738
739
namespace , pos = _get_c_string (msg , 4 )
739
- docs = bson .decode_all (msg [pos :], CODEC_OPTIONS )
740
+ docs = _bson .decode_all (msg [pos :], CODEC_OPTIONS )
740
741
return cls (* docs , namespace = namespace , flags = flags , client = client ,
741
742
request_id = request_id , server = server )
742
743
@@ -756,7 +757,7 @@ def unpack(cls, msg, client, server, request_id):
756
757
# First 4 bytes of OP_UPDATE are "reserved".
757
758
namespace , pos = _get_c_string (msg , 4 )
758
759
flags , = _UNPACK_INT (msg [pos :pos + 4 ])
759
- docs = bson .decode_all (msg [pos + 4 :], CODEC_OPTIONS )
760
+ docs = _bson .decode_all (msg [pos + 4 :], CODEC_OPTIONS )
760
761
return cls (* docs , namespace = namespace , flags = flags , client = client ,
761
762
request_id = request_id , server = server )
762
763
@@ -776,7 +777,7 @@ def unpack(cls, msg, client, server, request_id):
776
777
# First 4 bytes of OP_DELETE are "reserved".
777
778
namespace , pos = _get_c_string (msg , 4 )
778
779
flags , = _UNPACK_INT (msg [pos :pos + 4 ])
779
- docs = bson .decode_all (msg [pos + 4 :], CODEC_OPTIONS )
780
+ docs = _bson .decode_all (msg [pos + 4 :], CODEC_OPTIONS )
780
781
return cls (* docs , namespace = namespace , flags = flags , client = client ,
781
782
request_id = request_id , server = server )
782
783
@@ -829,7 +830,7 @@ def reply_bytes(self, request):
829
830
response_to = request .request_id
830
831
831
832
data = b'' .join ([flags , cursor_id , starting_from , number_returned ])
832
- data += b'' .join ([bson .BSON .encode (doc ) for doc in self ._docs ])
833
+ data += b'' .join ([_bson .BSON .encode (doc ) for doc in self ._docs ])
833
834
834
835
message = struct .pack ("<i" , 16 + len (data ))
835
836
message += struct .pack ("<i" , reply_id )
@@ -1726,15 +1727,15 @@ def docs_repr(*args):
1726
1727
>>> print(docs_repr(OrderedDict([(u'ts', now)])))
1727
1728
{"ts": {"$date": 123456000}}
1728
1729
>>>
1729
- >>> oid = bson .ObjectId(b'123456781234567812345678')
1730
+ >>> oid = _bson .ObjectId(b'123456781234567812345678')
1730
1731
>>> print(docs_repr(OrderedDict([(u'oid', oid)])))
1731
1732
{"oid": {"$oid": "123456781234567812345678"}}
1732
1733
"""
1733
1734
sio = StringIO ()
1734
1735
for doc_idx , doc in enumerate (args ):
1735
1736
if doc_idx > 0 :
1736
1737
sio .write (u', ' )
1737
- sio .write (text_type (bson . json_util .dumps (doc )))
1738
+ sio .write (text_type (_json_util .dumps (doc )))
1738
1739
return sio .getvalue ()
1739
1740
1740
1741
0 commit comments