Skip to content

Commit ab005a8

Browse files
committed
Don't depend on PyMongo.
We only need PyMongo for its BSON codec, vendor in PyMongo's pure-Python codec, leave out its C extensions, and drop PyMongo as a requirement.
1 parent e42757c commit ab005a8

22 files changed

+2915
-24
lines changed

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ include CHANGELOG.rst
44
include LICENSE
55
include README.rst
66

7+
recursive-include mockupdb *
78
recursive-include tests *
89
recursive-exclude * __pycache__
910
recursive-exclude * *.py[co]

mockupdb/__init__.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@
7171
except ImportError:
7272
from cStringIO import StringIO
7373

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
7778

78-
CODEC_OPTIONS = bson.codec_options.CodecOptions(document_class=OrderedDict)
79+
CODEC_OPTIONS = _codec_options.CodecOptions(document_class=OrderedDict)
7980

8081
PY3 = sys.version_info[0] == 3
8182
if PY3:
@@ -176,7 +177,7 @@ def going(fn, *args, **kwargs):
176177
177178
If an exception is raised within the context, the result is lost:
178179
179-
>>> with going(lambda: 'return value' as future):
180+
>>> with going(lambda: 'return value') as future:
180181
... assert 1 == 0
181182
Traceback (most recent call last):
182183
...
@@ -471,8 +472,8 @@ def _matches_docs(self, docs, other_docs):
471472
return False
472473
elif other_doc.get(key, None) != value:
473474
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)):
476477
raise TypeError(
477478
"Can't compare ordered and unordered document types:"
478479
" %r, %r" % (doc, other_doc))
@@ -543,7 +544,7 @@ def unpack(cls, msg, client, server, request_id):
543544
pos += 4
544545
num_to_return, = _UNPACK_INT(msg[pos:pos + 4])
545546
pos += 4
546-
docs = bson.decode_all(msg[pos:], CODEC_OPTIONS)
547+
docs = _bson.decode_all(msg[pos:], CODEC_OPTIONS)
547548
if is_command:
548549
assert len(docs) == 1
549550
command_ns = namespace[:-len('.$cmd')]
@@ -736,7 +737,7 @@ def unpack(cls, msg, client, server, request_id):
736737
"""
737738
flags, = _UNPACK_INT(msg[:4])
738739
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)
740741
return cls(*docs, namespace=namespace, flags=flags, client=client,
741742
request_id=request_id, server=server)
742743

@@ -756,7 +757,7 @@ def unpack(cls, msg, client, server, request_id):
756757
# First 4 bytes of OP_UPDATE are "reserved".
757758
namespace, pos = _get_c_string(msg, 4)
758759
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)
760761
return cls(*docs, namespace=namespace, flags=flags, client=client,
761762
request_id=request_id, server=server)
762763

@@ -776,7 +777,7 @@ def unpack(cls, msg, client, server, request_id):
776777
# First 4 bytes of OP_DELETE are "reserved".
777778
namespace, pos = _get_c_string(msg, 4)
778779
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)
780781
return cls(*docs, namespace=namespace, flags=flags, client=client,
781782
request_id=request_id, server=server)
782783

@@ -829,7 +830,7 @@ def reply_bytes(self, request):
829830
response_to = request.request_id
830831

831832
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])
833834

834835
message = struct.pack("<i", 16 + len(data))
835836
message += struct.pack("<i", reply_id)
@@ -1726,15 +1727,15 @@ def docs_repr(*args):
17261727
>>> print(docs_repr(OrderedDict([(u'ts', now)])))
17271728
{"ts": {"$date": 123456000}}
17281729
>>>
1729-
>>> oid = bson.ObjectId(b'123456781234567812345678')
1730+
>>> oid = _bson.ObjectId(b'123456781234567812345678')
17301731
>>> print(docs_repr(OrderedDict([(u'oid', oid)])))
17311732
{"oid": {"$oid": "123456781234567812345678"}}
17321733
"""
17331734
sio = StringIO()
17341735
for doc_idx, doc in enumerate(args):
17351736
if doc_idx > 0:
17361737
sio.write(u', ')
1737-
sio.write(text_type(bson.json_util.dumps(doc)))
1738+
sio.write(text_type(_json_util.dumps(doc)))
17381739
return sio.getvalue()
17391740

17401741

0 commit comments

Comments
 (0)