Fix _cbson DBPointer decoding in py3 PYTHON-586

This commit is contained in:
Bernie Hackett 2013-10-15 16:58:08 -07:00
parent 43e4ca91de
commit 2b1ea1fdeb
3 changed files with 19 additions and 2 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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()}}