diff --git a/bson/__init__.py b/bson/__init__.py index 8a4ee1606..650220f9c 100644 --- a/bson/__init__.py +++ b/bson/__init__.py @@ -270,8 +270,8 @@ def _get_regex(data, position, as_class, tz_aware, uuid_subtype): def _get_ref(data, position, as_class, tz_aware, uuid_subtype): - position += 4 - collection, position = _get_c_string(data, position) + collection, position = _get_string(data, position, + as_class, tz_aware, uuid_subtype) oid, position = _get_oid(data, position) return DBRef(collection, oid), position diff --git a/bson/_cbsonmodule.c b/bson/_cbsonmodule.c index 5544c0abc..e547b9561 100644 --- a/bson/_cbsonmodule.c +++ b/bson/_cbsonmodule.c @@ -1883,7 +1883,11 @@ static PyObject* get_value(PyObject* self, const char* buffer, unsigned* positio *position += coll_length; if ((objectid_type = _get_object(state->ObjectId, "bson.objectid", "ObjectId"))) { +#if PY_MAJOR_VERSION >= 3 + id = PyObject_CallFunction(objectid_type, "y#", buffer + *position, 12); +#else id = PyObject_CallFunction(objectid_type, "s#", buffer + *position, 12); +#endif Py_DECREF(objectid_type); } if (!id) { diff --git a/test/test_bson.py b/test/test_bson.py index 57039e69c..99ef945d7 100644 --- a/test/test_bson.py +++ b/test/test_bson.py @@ -220,6 +220,19 @@ class TestBSON(unittest.TestCase): qcheck.check_unittest(self, encode_then_decode, qcheck.gen_mongo_dict(3)) + def test_dbpointer(self): + # *Note* - DBPointer and DBRef are *not* the same thing. DBPointer + # is a deprecated BSON type. DBRef is a convention that does not + # exist in the BSON spec, meant to replace DBPointer. PyMongo does + # not support creation of the DBPointer type, but will decode + # DBPointer to DBRef. + + bs = b("\x18\x00\x00\x00\x0c\x00\x01\x00\x00" + "\x00\x00RY\xb5j\xfa[\xd8A\xd6X]\x99\x00") + + self.assertEqual({'': DBRef('', ObjectId('5259b56afa5bd841d6585d99'))}, + bson.BSON(bs).decode()) + def test_bad_dbref(self): ref_only = {'ref': {'$ref': 'collection'}} id_only = {'ref': {'$id': ObjectId()}}