Skip to content

Commit 3262cfc

Browse files
author
Mike Dirolf
committed
make subtype a property instead of a regular method
1 parent 06f1c92 commit 3262cfc

5 files changed

Lines changed: 6 additions & 5 deletions

File tree

examples/custom_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def transform_incoming(self, son, collection):
140140

141141
def transform_outgoing(self, son, collection):
142142
for (key, value) in son.items():
143-
if isinstance(value, Binary) and value.subtype() == 128:
143+
if isinstance(value, Binary) and value.subtype == 128:
144144
son[key] = from_binary(value)
145145
elif isinstance(value, dict):
146146
son[key] = self.transform_outgoing(value, collection)

pymongo/_cbsonmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ static int write_element_to_buffer(bson_buffer* buffer, int type_byte, PyObject*
250250
return 1;
251251
} else if (PyObject_IsInstance(value, Binary)) {
252252
*(buffer->buffer + type_byte) = 0x05;
253-
PyObject* subtype_object = PyObject_CallMethod(value, "subtype", NULL);
253+
PyObject* subtype_object = PyObject_GetAttrString(value, "subtype");
254254
if (!subtype_object) {
255255
return 0;
256256
}

pymongo/binary.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __new__(cls, data, subtype=2):
4949
self.__subtype = subtype
5050
return self
5151

52+
@property
5253
def subtype(self):
5354
"""Get the subtype of this binary data.
5455
"""

pymongo/bson.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def _element_to_bson(key, value):
296296
if isinstance(value, float):
297297
return "\x01" + name + struct.pack("<d", value)
298298
if isinstance(value, Binary):
299-
subtype = value.subtype()
299+
subtype = value.subtype
300300
if subtype == 2:
301301
value = struct.pack("<i", len(value)) + value
302302
return "\x05" + name + struct.pack("<i", len(value)) + chr(subtype) + value

test/test_binary.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ def test_exceptions(self):
4646

4747
def test_subtype(self):
4848
b = Binary("hello")
49-
self.assertEqual(b.subtype(), 2)
49+
self.assertEqual(b.subtype, 2)
5050
c = Binary("hello", 100)
51-
self.assertEqual(c.subtype(), 100)
51+
self.assertEqual(c.subtype, 100)
5252

5353
def test_equality(self):
5454
b = Binary("hello")

0 commit comments

Comments
 (0)