Skip to content

Commit ce96515

Browse files
author
Mike Dirolf
committed
use subtype 0 as default for Binary, instead of 2
1 parent 495763e commit ce96515

4 files changed

Lines changed: 23 additions & 10 deletions

File tree

doc/api/pymongo/binary.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
.. automodule:: pymongo.binary
55
:synopsis: Tools for representing binary data to be stored in MongoDB
66

7-
.. autodata:: FUNCTION_SUBTYPE
87
.. autodata:: BINARY_SUBTYPE
8+
.. autodata:: FUNCTION_SUBTYPE
9+
.. autodata:: OLD_BINARY_SUBTYPE
910
.. autodata:: UUID_SUBTYPE
1011
.. autodata:: MD5_SUBTYPE
1112
.. autodata:: USER_DEFINED_SUBTYPE
1213

13-
.. autoclass:: Binary(data[, subtype=2])
14+
.. autoclass:: Binary(data[, subtype=BINARY_SUBTYPE])
1415
:members:
1516
:show-inheritance:

pymongo/binary.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,27 @@
1515
"""Tools for representing binary data to be stored in MongoDB.
1616
"""
1717

18+
BINARY_SUBTYPE = 0
19+
"""BSON binary subtype for binary data.
20+
21+
This is the default subtype and is the most commonly used.
22+
23+
.. versionadded:: 1.5
24+
"""
25+
1826
FUNCTION_SUBTYPE = 1
1927
"""BSON binary subtype for functions.
2028
2129
.. versionadded:: 1.5
2230
"""
2331

24-
BINARY_SUBTYPE = 2
25-
"""BSON binary subtype for binary data.
32+
OLD_BINARY_SUBTYPE = 2
33+
"""Old BSON binary subtype for binary data.
2634
27-
This is the default subtype and is the most commonly used.
35+
This used to be the default subtype, :data:`BINARY_SUBTYPE` should now
36+
be used instead.
2837
29-
.. versionadded:: 1.5
38+
.. versionadded:: 1.6+
3039
"""
3140

3241
UUID_SUBTYPE = 3
@@ -70,7 +79,7 @@ class Binary(str):
7079
to use
7180
"""
7281

73-
def __new__(cls, data, subtype=2):
82+
def __new__(cls, data, subtype=BINARY_SUBTYPE):
7483
if not isinstance(data, str):
7584
raise TypeError("data must be an instance of str")
7685
if not isinstance(subtype, int):

test/test_binary.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_exceptions(self):
4848

4949
def test_subtype(self):
5050
b = Binary("hello")
51-
self.assertEqual(b.subtype, 2)
51+
self.assertEqual(b.subtype, 0)
5252
c = Binary("hello", 100)
5353
self.assertEqual(c.subtype, 100)
5454

@@ -63,9 +63,9 @@ def test_equality(self):
6363

6464
def test_repr(self):
6565
b = Binary("hello world")
66-
self.assertEqual(repr(b), "Binary('hello world', 2)")
66+
self.assertEqual(repr(b), "Binary('hello world', 0)")
6767
c = Binary("\x08\xFF")
68-
self.assertEqual(repr(c), "Binary('\\x08\\xff', 2)")
68+
self.assertEqual(repr(c), "Binary('\\x08\\xff', 0)")
6969
d = Binary("test", 100)
7070
self.assertEqual(repr(d), "Binary('test', 100)")
7171

test/test_bson.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ def test_basic_from_dict(self):
118118
"\x10\x00\x00\x00\x03\x6E\x6F\x6E\x65\x00\x05\x00\x00"
119119
"\x00\x00\x00")
120120
self.assertEqual(BSON.from_dict({"test": Binary("test")}),
121+
"\x14\x00\x00\x00\x05\x74\x65\x73\x74\x00\x04\x00\x00"
122+
"\x00\x00\x74\x65\x73\x74\x00")
123+
self.assertEqual(BSON.from_dict({"test": Binary("test", 2)}),
121124
"\x18\x00\x00\x00\x05\x74\x65\x73\x74\x00\x08\x00\x00"
122125
"\x00\x02\x04\x00\x00\x00\x74\x65\x73\x74\x00")
123126
self.assertEqual(BSON.from_dict({"test": Binary("test", 128)}),

0 commit comments

Comments
 (0)