Always decode to UUID PYTHON-267
This commit is contained in:
parent
a88a79f474
commit
8bdb7821a9
@ -21,7 +21,7 @@ import re
|
||||
import struct
|
||||
import warnings
|
||||
|
||||
from bson.binary import Binary, UUIDLegacy
|
||||
from bson.binary import Binary
|
||||
from bson.code import Code
|
||||
from bson.dbref import DBRef
|
||||
from bson.errors import (InvalidBSON,
|
||||
@ -146,8 +146,6 @@ def _get_binary(data, position, as_class, tz_aware):
|
||||
length = length2
|
||||
if subtype in (3, 4) and _use_uuid:
|
||||
value = uuid.UUID(bytes=data[position:position + length])
|
||||
if subtype == 3:
|
||||
value = UUIDLegacy(value)
|
||||
position += length
|
||||
return (value, position)
|
||||
value = Binary(data[position:position + length], subtype)
|
||||
|
||||
@ -36,7 +36,6 @@ static PyObject* ObjectId = NULL;
|
||||
static PyObject* DBRef = NULL;
|
||||
static PyObject* RECompile = NULL;
|
||||
static PyObject* UUID = NULL;
|
||||
static PyObject* UUIDLegacy = NULL;
|
||||
static PyObject* Timestamp = NULL;
|
||||
static PyObject* MinKey = NULL;
|
||||
static PyObject* MaxKey = NULL;
|
||||
@ -190,7 +189,6 @@ static int _reload_object(PyObject** object, char* module_name, char* object_nam
|
||||
* Returns non-zero on failure. */
|
||||
static int _reload_python_objects(void) {
|
||||
if (_reload_object(&Binary, "bson.binary", "Binary") ||
|
||||
_reload_object(&UUIDLegacy, "bson.binary", "UUIDLegacy") ||
|
||||
_reload_object(&Code, "bson.code", "Code") ||
|
||||
_reload_object(&ObjectId, "bson.objectid", "ObjectId") ||
|
||||
_reload_object(&DBRef, "bson.dbref", "DBRef") ||
|
||||
@ -1010,12 +1008,6 @@ static PyObject* get_value(const char* buffer, int* position, int type,
|
||||
|
||||
PyDict_SetItemString(kwargs, "bytes", data);
|
||||
value = PyObject_Call(UUID, args, kwargs);
|
||||
if (subtype == 3) {
|
||||
PyObject* ul;
|
||||
ul = PyObject_CallFunctionObjArgs(UUIDLegacy, value, NULL);
|
||||
Py_DECREF(value);
|
||||
value = ul;
|
||||
}
|
||||
|
||||
Py_DECREF(args);
|
||||
Py_DECREF(kwargs);
|
||||
|
||||
@ -129,13 +129,21 @@ class UUIDLegacy(Binary):
|
||||
>>> id = uuid.uuid4()
|
||||
>>> db.test.insert({'uuid': Binary(id.bytes, 3)})
|
||||
ObjectId('...')
|
||||
>>> db.test.find({'uuid': id}).count()
|
||||
0
|
||||
>>> db.test.find({'uuid': UUIDLegacy(id)}).count()
|
||||
1
|
||||
>>> db.test.find({'uuid': UUIDLegacy(id)})[0]['uuid']
|
||||
UUIDLegacy('...')
|
||||
>>> db.test.find({'uuid': UUIDLegacy(id)})[0]['uuid'].uuid
|
||||
UUID('...')
|
||||
>>>
|
||||
>>> # Convert from subtype 3 to subtype 4
|
||||
>>> db.test.update({'uuid': UUIDLegacy(id)}, {'$set': {'uuid': id}})
|
||||
>>> doc = db.test.find_one({'uuid': UUIDLegacy(id)})
|
||||
>>> db.test.save(doc)
|
||||
ObjectId('...')
|
||||
>>> db.test.find({'uuid': UUIDLegacy(id)}).count()
|
||||
0
|
||||
>>> db.test.find({'uuid': {'$in': [UUIDLegacy(id), id]}}).count()
|
||||
1
|
||||
>>> db.test.find_one({'uuid': id})['uuid']
|
||||
UUID('...')
|
||||
|
||||
|
||||
@ -91,26 +91,25 @@ class TestBinary(unittest.TestCase):
|
||||
coll.drop()
|
||||
|
||||
uu = uuid.uuid4()
|
||||
ul = UUIDLegacy(uu)
|
||||
coll.insert({'uuid': uu})
|
||||
coll.insert({'uuid': uuid.uuid4()})
|
||||
coll.insert({'uuid': uuid.uuid4()})
|
||||
coll.insert({'uuid': ul})
|
||||
coll.insert({'uuid': Binary(uu.bytes, 3)})
|
||||
|
||||
# Test UUIDLegacy queries.
|
||||
cur = coll.find({'uuid': ul})
|
||||
self.assertEquals(0, coll.find({'uuid': uu}).count())
|
||||
cur = coll.find({'uuid': UUIDLegacy(uu)})
|
||||
self.assertEquals(1, cur.count())
|
||||
retrieved = cur.next()['uuid']
|
||||
self.assertEquals(ul, retrieved)
|
||||
self.assertEquals(uu, retrieved.uuid)
|
||||
self.assertEquals(uu, retrieved)
|
||||
|
||||
# Test regular UUID queries.
|
||||
coll.insert({'uuid': uu})
|
||||
cur = coll.find({'uuid': uu})
|
||||
self.assertEquals(1, cur.count())
|
||||
self.assertEquals(uu, cur.next()['uuid'])
|
||||
|
||||
# Test both.
|
||||
cur = coll.find({'uuid': {'$in': [uu, ul]}})
|
||||
cur = coll.find({'uuid': {'$in': [uu, UUIDLegacy(uu)]}})
|
||||
self.assertEquals(2, cur.count())
|
||||
coll.drop()
|
||||
|
||||
|
||||
@ -285,11 +285,10 @@ class TestBSON(unittest.TestCase):
|
||||
id = uuid.uuid4()
|
||||
legacy = UUIDLegacy(id)
|
||||
self.assertEquals(3, legacy.subtype)
|
||||
transformed = (BSON.encode({"legacy": legacy})).decode()["legacy"]
|
||||
self.assert_(isinstance(transformed, UUIDLegacy))
|
||||
self.assertEqual(legacy, transformed)
|
||||
self.assertEqual(id, transformed.uuid)
|
||||
self.assertNotEqual(UUIDLegacy(uuid.uuid4()), transformed)
|
||||
transformed = (BSON.encode({"uuid": legacy})).decode()["uuid"]
|
||||
self.assert_(isinstance(transformed, uuid.UUID))
|
||||
self.assertEqual(id, transformed)
|
||||
self.assertNotEqual(UUIDLegacy(uuid.uuid4()), UUIDLegacy(transformed))
|
||||
|
||||
# The C extension was segfaulting on unicode RegExs, so we have this test
|
||||
# that doesn't really test anything but the lack of a segfault.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user