use subtype 0 as default for Binary, instead of 2
This commit is contained in:
parent
495763e014
commit
ce965158d6
@ -4,12 +4,13 @@
|
||||
.. automodule:: pymongo.binary
|
||||
:synopsis: Tools for representing binary data to be stored in MongoDB
|
||||
|
||||
.. autodata:: FUNCTION_SUBTYPE
|
||||
.. autodata:: BINARY_SUBTYPE
|
||||
.. autodata:: FUNCTION_SUBTYPE
|
||||
.. autodata:: OLD_BINARY_SUBTYPE
|
||||
.. autodata:: UUID_SUBTYPE
|
||||
.. autodata:: MD5_SUBTYPE
|
||||
.. autodata:: USER_DEFINED_SUBTYPE
|
||||
|
||||
.. autoclass:: Binary(data[, subtype=2])
|
||||
.. autoclass:: Binary(data[, subtype=BINARY_SUBTYPE])
|
||||
:members:
|
||||
:show-inheritance:
|
||||
|
||||
@ -15,18 +15,27 @@
|
||||
"""Tools for representing binary data to be stored in MongoDB.
|
||||
"""
|
||||
|
||||
BINARY_SUBTYPE = 0
|
||||
"""BSON binary subtype for binary data.
|
||||
|
||||
This is the default subtype and is the most commonly used.
|
||||
|
||||
.. versionadded:: 1.5
|
||||
"""
|
||||
|
||||
FUNCTION_SUBTYPE = 1
|
||||
"""BSON binary subtype for functions.
|
||||
|
||||
.. versionadded:: 1.5
|
||||
"""
|
||||
|
||||
BINARY_SUBTYPE = 2
|
||||
"""BSON binary subtype for binary data.
|
||||
OLD_BINARY_SUBTYPE = 2
|
||||
"""Old BSON binary subtype for binary data.
|
||||
|
||||
This is the default subtype and is the most commonly used.
|
||||
This used to be the default subtype, :data:`BINARY_SUBTYPE` should now
|
||||
be used instead.
|
||||
|
||||
.. versionadded:: 1.5
|
||||
.. versionadded:: 1.6+
|
||||
"""
|
||||
|
||||
UUID_SUBTYPE = 3
|
||||
@ -70,7 +79,7 @@ class Binary(str):
|
||||
to use
|
||||
"""
|
||||
|
||||
def __new__(cls, data, subtype=2):
|
||||
def __new__(cls, data, subtype=BINARY_SUBTYPE):
|
||||
if not isinstance(data, str):
|
||||
raise TypeError("data must be an instance of str")
|
||||
if not isinstance(subtype, int):
|
||||
|
||||
@ -48,7 +48,7 @@ class TestBinary(unittest.TestCase):
|
||||
|
||||
def test_subtype(self):
|
||||
b = Binary("hello")
|
||||
self.assertEqual(b.subtype, 2)
|
||||
self.assertEqual(b.subtype, 0)
|
||||
c = Binary("hello", 100)
|
||||
self.assertEqual(c.subtype, 100)
|
||||
|
||||
@ -63,9 +63,9 @@ class TestBinary(unittest.TestCase):
|
||||
|
||||
def test_repr(self):
|
||||
b = Binary("hello world")
|
||||
self.assertEqual(repr(b), "Binary('hello world', 2)")
|
||||
self.assertEqual(repr(b), "Binary('hello world', 0)")
|
||||
c = Binary("\x08\xFF")
|
||||
self.assertEqual(repr(c), "Binary('\\x08\\xff', 2)")
|
||||
self.assertEqual(repr(c), "Binary('\\x08\\xff', 0)")
|
||||
d = Binary("test", 100)
|
||||
self.assertEqual(repr(d), "Binary('test', 100)")
|
||||
|
||||
|
||||
@ -118,6 +118,9 @@ class TestBSON(unittest.TestCase):
|
||||
"\x10\x00\x00\x00\x03\x6E\x6F\x6E\x65\x00\x05\x00\x00"
|
||||
"\x00\x00\x00")
|
||||
self.assertEqual(BSON.from_dict({"test": Binary("test")}),
|
||||
"\x14\x00\x00\x00\x05\x74\x65\x73\x74\x00\x04\x00\x00"
|
||||
"\x00\x00\x74\x65\x73\x74\x00")
|
||||
self.assertEqual(BSON.from_dict({"test": Binary("test", 2)}),
|
||||
"\x18\x00\x00\x00\x05\x74\x65\x73\x74\x00\x08\x00\x00"
|
||||
"\x00\x02\x04\x00\x00\x00\x74\x65\x73\x74\x00")
|
||||
self.assertEqual(BSON.from_dict({"test": Binary("test", 128)}),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user