Skip to content

Commit cda0b71

Browse files
committed
PYTHON-1696 Stop encouraging use of BSON.decode as a classmethod
1 parent 7049058 commit cda0b71

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

bson/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,10 +1059,10 @@ def decode(self, codec_options=DEFAULT_CODEC_OPTIONS):
10591059
>>> import bson
10601060
>>> from bson.codec_options import CodecOptions
10611061
>>> data = bson.BSON.encode({'a': 1})
1062-
>>> decoded_doc = bson.BSON.decode(data)
1062+
>>> decoded_doc = bson.BSON(data).decode()
10631063
<type 'dict'>
10641064
>>> options = CodecOptions(document_class=collections.OrderedDict)
1065-
>>> decoded_doc = bson.BSON.decode(data, codec_options=options)
1065+
>>> decoded_doc = bson.BSON(data).decode(codec_options=options)
10661066
>>> type(decoded_doc)
10671067
<class 'collections.OrderedDict'>
10681068

test/test_bson.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -585,12 +585,12 @@ def test_overflow(self):
585585

586586
def test_small_long_encode_decode(self):
587587
encoded1 = BSON.encode({'x': 256})
588-
decoded1 = BSON.decode(encoded1)['x']
588+
decoded1 = BSON(encoded1).decode()['x']
589589
self.assertEqual(256, decoded1)
590590
self.assertEqual(type(256), type(decoded1))
591591

592592
encoded2 = BSON.encode({'x': Int64(256)})
593-
decoded2 = BSON.decode(encoded2)['x']
593+
decoded2 = BSON(encoded2).decode()['x']
594594
expected = Int64(256)
595595
self.assertEqual(expected, decoded2)
596596
self.assertEqual(type(expected), type(decoded2))
@@ -956,63 +956,63 @@ def test_unicode_decode_error_handler(self):
956956
replaced_key = b'ke\xe9str'.decode('utf-8', 'replace')
957957
ignored_key = b'ke\xe9str'.decode('utf-8', 'ignore')
958958

959-
dec = BSON.decode(invalid_key, CodecOptions(
959+
dec = BSON(invalid_key).decode(CodecOptions(
960960
unicode_decode_error_handler="replace"))
961961
self.assertEqual(dec, {replaced_key: u"foobar"})
962962

963-
dec = BSON.decode(invalid_key, CodecOptions(
963+
dec = BSON(invalid_key).decode(CodecOptions(
964964
unicode_decode_error_handler="ignore"))
965965
self.assertEqual(dec, {ignored_key: u"foobar"})
966966

967-
self.assertRaises(InvalidBSON, BSON.decode, invalid_key, CodecOptions(
967+
self.assertRaises(InvalidBSON, BSON(invalid_key).decode, CodecOptions(
968968
unicode_decode_error_handler="strict"))
969-
self.assertRaises(InvalidBSON, BSON.decode, invalid_key,
969+
self.assertRaises(InvalidBSON, BSON(invalid_key).decode,
970970
CodecOptions())
971-
self.assertRaises(InvalidBSON, BSON.decode, invalid_key)
971+
self.assertRaises(InvalidBSON, BSON(invalid_key).decode)
972972

973973
# Test handing of bad string value.
974974
invalid_val = BSON(enc[:18] + b'\xe9' + enc[19:])
975975
replaced_val = b'fo\xe9bar'.decode('utf-8', 'replace')
976976
ignored_val = b'fo\xe9bar'.decode('utf-8', 'ignore')
977977

978-
dec = BSON.decode(invalid_val, CodecOptions(
978+
dec = BSON(invalid_val).decode(CodecOptions(
979979
unicode_decode_error_handler="replace"))
980980
self.assertEqual(dec, {u"keystr": replaced_val})
981981

982-
dec = BSON.decode(invalid_val, CodecOptions(
982+
dec = BSON(invalid_val).decode(CodecOptions(
983983
unicode_decode_error_handler="ignore"))
984984
self.assertEqual(dec, {u"keystr": ignored_val})
985985

986-
self.assertRaises(InvalidBSON, BSON.decode, invalid_val, CodecOptions(
986+
self.assertRaises(InvalidBSON, BSON(invalid_val).decode, CodecOptions(
987987
unicode_decode_error_handler="strict"))
988-
self.assertRaises(InvalidBSON, BSON.decode, invalid_val,
988+
self.assertRaises(InvalidBSON, BSON(invalid_val).decode,
989989
CodecOptions())
990-
self.assertRaises(InvalidBSON, BSON.decode, invalid_val)
990+
self.assertRaises(InvalidBSON, BSON(invalid_val).decode)
991991

992992
# Test handing bad key + bad value.
993993
invalid_both = BSON(
994994
enc[:7] + b'\xe9' + enc[8:18] + b'\xe9' + enc[19:])
995995

996-
dec = BSON.decode(invalid_both, CodecOptions(
996+
dec = BSON(invalid_both).decode(CodecOptions(
997997
unicode_decode_error_handler="replace"))
998998
self.assertEqual(dec, {replaced_key: replaced_val})
999999

1000-
dec = BSON.decode(invalid_both, CodecOptions(
1000+
dec = BSON(invalid_both).decode(CodecOptions(
10011001
unicode_decode_error_handler="ignore"))
10021002
self.assertEqual(dec, {ignored_key: ignored_val})
10031003

1004-
self.assertRaises(InvalidBSON, BSON.decode, invalid_both, CodecOptions(
1004+
self.assertRaises(InvalidBSON, BSON(invalid_both).decode, CodecOptions(
10051005
unicode_decode_error_handler="strict"))
1006-
self.assertRaises(InvalidBSON, BSON.decode, invalid_both,
1006+
self.assertRaises(InvalidBSON, BSON(invalid_both).decode,
10071007
CodecOptions())
1008-
self.assertRaises(InvalidBSON, BSON.decode, invalid_both)
1008+
self.assertRaises(InvalidBSON, BSON(invalid_both).decode)
10091009

10101010
# Test handling bad error mode.
1011-
dec = BSON.decode(enc, CodecOptions(
1011+
dec = BSON(enc).decode(CodecOptions(
10121012
unicode_decode_error_handler="junk"))
10131013
self.assertEqual(dec, {"keystr": "foobar"})
10141014

1015-
self.assertRaises(InvalidBSON, BSON.decode, invalid_both,
1015+
self.assertRaises(InvalidBSON, BSON(invalid_both).decode,
10161016
CodecOptions(unicode_decode_error_handler="junk"))
10171017

10181018

0 commit comments

Comments
 (0)