Skip to content

Commit 8e50781

Browse files
serbautMike Dirolf
authored andcommitted
* ObjectId: changed str and repr to hex
* ObjectId: can now take a hex string in the constructor Signed-off-by: Mike Dirolf <mike@10gen.com>
1 parent 4fb11e4 commit 8e50781

File tree

5 files changed

+30
-18
lines changed

5 files changed

+30
-18
lines changed

pymongo/_cbsonmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ static int write_element_to_buffer(bson_buffer* buffer, int type_byte, PyObject*
374374
*(buffer->buffer + type_byte) = 0x09;
375375
return buffer_write_bytes(buffer, (const char*)&time_since_epoch, 8);
376376
} else if (PyObject_IsInstance(value, ObjectId)) {
377-
PyObject* pystring = PyObject_Str(value);
377+
PyObject* pystring = PyObject_GetAttrString(value, "_ObjectId__id");
378378
if (!pystring) {
379379
return 0;
380380
}

pymongo/bson.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ def _element_to_bson(key, value, check_keys):
397397
as_dict = SON(zip([str(i) for i in range(len(value))], value))
398398
return "\x04" + name + _dict_to_bson(as_dict, check_keys)
399399
if isinstance(value, ObjectId):
400-
return "\x07" + name + str(value)
400+
return "\x07" + name + value.binary
401401
if value is True:
402402
return "\x08" + name + "\x01"
403403
if value is False:

pymongo/objectid.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def __init__(self, id=None):
4747
raised.
4848
4949
:Parameters:
50-
- `id` (optional): a valid ObjectId
50+
- `id` (optional): a valid ObjectId (12 byte binary or 24 character
51+
hex string)
5152
"""
5253
if id is None:
5354
self.__generate()
@@ -92,6 +93,8 @@ def __validate(self, oid):
9293
elif isinstance(oid, types.StringType):
9394
if len(oid) == 12:
9495
self.__id = oid
96+
elif len(oid) == 24:
97+
self.__id = oid.decode("hex")
9598
else:
9699
raise InvalidId("%s is not a valid ObjectId" % oid)
97100
else:
@@ -145,13 +148,19 @@ def from_legacy_str(cls, legacy_str):
145148
return cls(legacy_str[7::-1] + legacy_str[:7:-1])
146149
from_legacy_str = classmethod(from_legacy_str)
147150

148-
def __str__(self):
151+
@property
152+
def binary(self):
149153
return self.__id
150154

155+
def __str__(self):
156+
return self.__id.encode("hex")
157+
151158
def __repr__(self):
152-
return "ObjectId(%r)" % self.__id
159+
return "ObjectId('%s')" % self.__id.encode("hex")
153160

154161
def __cmp__(self, other):
155162
if isinstance(other, ObjectId):
156163
return cmp(self.__id, other.__id)
157164
return NotImplemented
165+
166+

test/test_dbref.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ def bar():
5555
self.assertRaises(AttributeError, bar)
5656

5757
def test_repr(self):
58-
self.assertEqual(repr(DBRef("coll", ObjectId("123456789012"))),
59-
"DBRef(u'coll', ObjectId('123456789012'))")
60-
self.assertEqual(repr(DBRef(u"coll", ObjectId("123456789012"))),
61-
"DBRef(u'coll', ObjectId('123456789012'))")
58+
self.assertEqual(repr(DBRef("coll", ObjectId("1234567890abcdef12345678"))),
59+
"DBRef(u'coll', ObjectId('1234567890abcdef12345678'))")
60+
self.assertEqual(repr(DBRef(u"coll", ObjectId("1234567890abcdef12345678"))),
61+
"DBRef(u'coll', ObjectId('1234567890abcdef12345678'))")
6262

6363
def test_cmp(self):
64-
self.assertEqual(DBRef("coll", ObjectId("123456789012")),
65-
DBRef(u"coll", ObjectId("123456789012")))
66-
self.assertNotEqual(DBRef("coll", ObjectId("123456789012")),
67-
DBRef("col", ObjectId("123456789012")))
68-
self.assertNotEqual(DBRef("coll", ObjectId("123456789012")),
64+
self.assertEqual(DBRef("coll", ObjectId("1234567890abcdef12345678")),
65+
DBRef(u"coll", ObjectId("1234567890abcdef12345678")))
66+
self.assertNotEqual(DBRef("coll", ObjectId("1234567890abcdef12345678")),
67+
DBRef("col", ObjectId("1234567890abcdef12345678")))
68+
self.assertNotEqual(DBRef("coll", ObjectId("1234567890abcdef12345678")),
6969
DBRef("coll", ObjectId("123456789011")))
70-
self.assertNotEqual(DBRef("coll", ObjectId("123456789012")), 4)
70+
self.assertNotEqual(DBRef("coll", ObjectId("1234567890abcdef12345678")), 4)
7171

7272
if __name__ == "__main__":
7373
unittest.main()

test/test_objectid.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ def test_creation(self):
4242
self.assert_(ObjectId(a))
4343

4444
def test_repr_str(self):
45-
self.assertEqual(repr(ObjectId("123456789012")),
46-
"ObjectId('123456789012')")
47-
self.assertEqual(str(ObjectId("123456789012")), "123456789012")
45+
self.assertEqual(repr(ObjectId("1234567890abcdef12345678")),
46+
"ObjectId('1234567890abcdef12345678')")
47+
self.assertEqual(str(ObjectId("1234567890abcdef12345678")), "1234567890abcdef12345678")
48+
self.assertEqual(str(ObjectId("123456789012")), "313233343536373839303132")
49+
self.assertEqual(ObjectId("1234567890abcdef12345678").binary, '\x124Vx\x90\xab\xcd\xef\x124Vx')
50+
self.assertEqual(str(ObjectId('\x124Vx\x90\xab\xcd\xef\x124Vx')), "1234567890abcdef12345678")
4851

4952
def test_cmp(self):
5053
a = ObjectId()

0 commit comments

Comments
 (0)