Skip to content

Commit ef3df16

Browse files
committed
PYTHON-1918 Stop using BSON.encode and BSON.decode functions
1 parent 7d8ade1 commit ef3df16

14 files changed

+237
-245
lines changed

bson/raw_bson.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def __init__(self, bson_bytes, codec_options=None):
4545
class from the standard library so it can be used like a read-only
4646
``dict``::
4747
48-
>>> raw_doc = RawBSONDocument(BSON.encode({'_id': 'my_doc'}))
48+
>>> from bson import encode
49+
>>> raw_doc = RawBSONDocument(encode({'_id': 'my_doc'}))
4950
>>> raw_doc.raw
5051
b'...'
5152
>>> raw_doc['_id']

doc/migrate-to-pymongo3.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,10 @@ BSON
492492
......................................................
493493

494494
The `as_class`, `tz_aware`, and `uuid_subtype` parameters have been
495-
removed from the functions provided in :mod:`bson`. Code like this::
495+
removed from the functions provided in :mod:`bson`. Furthermore, the
496+
:func:`~bson.encode` and :func:`~bson.decode` functions have been added
497+
as more performant alternatives to the :meth:`bson.BSON.encode` and
498+
:meth:`bson.BSON.decode` methods. Code like this::
496499

497500
>>> from bson import BSON
498501
>>> from bson.son import SON
@@ -502,10 +505,10 @@ can be replaced by this in PyMongo 2.9 or later:
502505

503506
.. doctest::
504507

505-
>>> from bson import BSON
508+
>>> from bson import encode
506509
>>> from bson.codec_options import CodecOptions
507510
>>> from bson.son import SON
508-
>>> encoded = BSON.encode({"a": 1}, codec_options=CodecOptions(SON))
511+
>>> encoded = encode({"a": 1}, codec_options=CodecOptions(SON))
509512

510513
Removed features with no migration path
511514
---------------------------------------

pymongo/message.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
import bson
2828
from bson import (CodecOptions,
29+
decode,
30+
encode,
2931
_dict_to_bson,
3032
_make_c_string)
3133
from bson.codec_options import DEFAULT_CODEC_OPTIONS
@@ -1397,7 +1399,7 @@ def _batched_write_command_impl(
13971399

13981400
# Where to write command document length
13991401
command_start = buf.tell()
1400-
buf.write(bson.BSON.encode(command))
1402+
buf.write(encode(command))
14011403

14021404
# Start of payload
14031405
buf.seek(-1, 2)
@@ -1418,7 +1420,7 @@ def _batched_write_command_impl(
14181420
for doc in docs:
14191421
# Encode the current operation
14201422
key = b(str(idx))
1421-
value = bson.BSON.encode(doc, check_keys, opts)
1423+
value = encode(doc, check_keys, opts)
14221424
# Is there enough room to add this document? max_cmd_size accounts for
14231425
# the two trailing null bytes.
14241426
doc_too_large = len(value) > max_cmd_size

test/performance/perf_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
sys.path[0:0] = [""]
2929

30-
from bson import BSON
30+
from bson import encode
3131
from bson.json_util import loads
3232
from gridfs import GridFSBucket
3333
from pymongo import MongoClient
@@ -133,7 +133,7 @@ def setUp(self):
133133

134134
def do_task(self):
135135
for _ in range(NUM_DOCS):
136-
BSON.encode(self.document)
136+
encode(self.document)
137137

138138

139139
class BsonDecodingTest(PerformanceTest):
@@ -142,7 +142,7 @@ def setUp(self):
142142
with open(
143143
os.path.join(TEST_PATH,
144144
os.path.join('extended_bson', self.dataset))) as data:
145-
self.document = BSON.encode(json.loads(data.read()))
145+
self.document = encode(json.loads(data.read()))
146146

147147
def do_task(self):
148148
for _ in range(NUM_DOCS):

test/test_binary.py

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import bson
2828

29+
from bson import decode, encode
2930
from bson.binary import *
3031
from bson.codec_options import CodecOptions
3132
from bson.py3compat import PY3
@@ -146,10 +147,10 @@ def test_uuid_subtype_4(self):
146147
"""uuid_representation should be ignored when decoding subtype 4."""
147148
expected_uuid = uuid.uuid4()
148149
doc = {"uuid": Binary(expected_uuid.bytes, 4)}
149-
encoded = bson.BSON.encode(doc)
150+
encoded = encode(doc)
150151
for uuid_representation in ALL_UUID_REPRESENTATIONS:
151152
options = CodecOptions(uuid_representation=uuid_representation)
152-
self.assertEqual(expected_uuid, encoded.decode(options)["uuid"])
153+
self.assertEqual(expected_uuid, decode(encoded, options)["uuid"])
153154

154155
def test_legacy_java_uuid(self):
155156
# Test decoding
@@ -172,31 +173,23 @@ def test_legacy_java_uuid(self):
172173

173174
# Test encoding
174175
encoded = b''.join([
175-
bson.BSON.encode(doc,
176-
False,
177-
CodecOptions(uuid_representation=PYTHON_LEGACY))
176+
encode(doc, False, CodecOptions(uuid_representation=PYTHON_LEGACY))
178177
for doc in docs])
179178
self.assertNotEqual(data, encoded)
180179

181-
encoded = b''.join(
182-
[bson.BSON.encode(doc,
183-
False,
184-
CodecOptions(uuid_representation=STANDARD))
185-
for doc in docs])
180+
encoded = b''.join([
181+
encode(doc, False, CodecOptions(uuid_representation=STANDARD))
182+
for doc in docs])
186183
self.assertNotEqual(data, encoded)
187184

188-
encoded = b''.join(
189-
[bson.BSON.encode(doc,
190-
False,
191-
CodecOptions(uuid_representation=CSHARP_LEGACY))
192-
for doc in docs])
185+
encoded = b''.join([
186+
encode(doc, False, CodecOptions(uuid_representation=CSHARP_LEGACY))
187+
for doc in docs])
193188
self.assertNotEqual(data, encoded)
194189

195-
encoded = b''.join(
196-
[bson.BSON.encode(doc,
197-
False,
198-
CodecOptions(uuid_representation=JAVA_LEGACY))
199-
for doc in docs])
190+
encoded = b''.join([
191+
encode(doc, False, CodecOptions(uuid_representation=JAVA_LEGACY))
192+
for doc in docs])
200193
self.assertEqual(data, encoded)
201194

202195
@client_context.require_connection
@@ -242,31 +235,23 @@ def test_legacy_csharp_uuid(self):
242235

243236
# Test encoding
244237
encoded = b''.join([
245-
bson.BSON.encode(doc,
246-
False,
247-
CodecOptions(uuid_representation=PYTHON_LEGACY))
238+
encode(doc, False, CodecOptions(uuid_representation=PYTHON_LEGACY))
248239
for doc in docs])
249240
self.assertNotEqual(data, encoded)
250241

251242
encoded = b''.join([
252-
bson.BSON.encode(doc,
253-
False,
254-
CodecOptions(uuid_representation=STANDARD))
243+
encode(doc, False, CodecOptions(uuid_representation=STANDARD))
255244
for doc in docs])
256245
self.assertNotEqual(data, encoded)
257246

258-
encoded = b''.join(
259-
[bson.BSON.encode(doc,
260-
False,
261-
CodecOptions(uuid_representation=JAVA_LEGACY))
262-
for doc in docs])
247+
encoded = b''.join([
248+
encode(doc, False, CodecOptions(uuid_representation=JAVA_LEGACY))
249+
for doc in docs])
263250
self.assertNotEqual(data, encoded)
264251

265-
encoded = b''.join(
266-
[bson.BSON.encode(doc,
267-
False,
268-
CodecOptions(uuid_representation=CSHARP_LEGACY))
269-
for doc in docs])
252+
encoded = b''.join([
253+
encode(doc, False, CodecOptions(uuid_representation=CSHARP_LEGACY))
254+
for doc in docs])
270255
self.assertEqual(data, encoded)
271256

272257
@client_context.require_connection

0 commit comments

Comments
 (0)