Skip to content

Commit 8bdb782

Browse files
committed
Always decode to UUID PYTHON-267
1 parent a88a79f commit 8bdb782

5 files changed

Lines changed: 22 additions & 26 deletions

File tree

bson/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import struct
2222
import warnings
2323

24-
from bson.binary import Binary, UUIDLegacy
24+
from bson.binary import Binary
2525
from bson.code import Code
2626
from bson.dbref import DBRef
2727
from bson.errors import (InvalidBSON,
@@ -146,8 +146,6 @@ def _get_binary(data, position, as_class, tz_aware):
146146
length = length2
147147
if subtype in (3, 4) and _use_uuid:
148148
value = uuid.UUID(bytes=data[position:position + length])
149-
if subtype == 3:
150-
value = UUIDLegacy(value)
151149
position += length
152150
return (value, position)
153151
value = Binary(data[position:position + length], subtype)

bson/_cbsonmodule.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ static PyObject* ObjectId = NULL;
3636
static PyObject* DBRef = NULL;
3737
static PyObject* RECompile = NULL;
3838
static PyObject* UUID = NULL;
39-
static PyObject* UUIDLegacy = NULL;
4039
static PyObject* Timestamp = NULL;
4140
static PyObject* MinKey = NULL;
4241
static PyObject* MaxKey = NULL;
@@ -190,7 +189,6 @@ static int _reload_object(PyObject** object, char* module_name, char* object_nam
190189
* Returns non-zero on failure. */
191190
static int _reload_python_objects(void) {
192191
if (_reload_object(&Binary, "bson.binary", "Binary") ||
193-
_reload_object(&UUIDLegacy, "bson.binary", "UUIDLegacy") ||
194192
_reload_object(&Code, "bson.code", "Code") ||
195193
_reload_object(&ObjectId, "bson.objectid", "ObjectId") ||
196194
_reload_object(&DBRef, "bson.dbref", "DBRef") ||
@@ -1010,12 +1008,6 @@ static PyObject* get_value(const char* buffer, int* position, int type,
10101008

10111009
PyDict_SetItemString(kwargs, "bytes", data);
10121010
value = PyObject_Call(UUID, args, kwargs);
1013-
if (subtype == 3) {
1014-
PyObject* ul;
1015-
ul = PyObject_CallFunctionObjArgs(UUIDLegacy, value, NULL);
1016-
Py_DECREF(value);
1017-
value = ul;
1018-
}
10191011

10201012
Py_DECREF(args);
10211013
Py_DECREF(kwargs);

bson/binary.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,21 @@ class UUIDLegacy(Binary):
129129
>>> id = uuid.uuid4()
130130
>>> db.test.insert({'uuid': Binary(id.bytes, 3)})
131131
ObjectId('...')
132+
>>> db.test.find({'uuid': id}).count()
133+
0
134+
>>> db.test.find({'uuid': UUIDLegacy(id)}).count()
135+
1
132136
>>> db.test.find({'uuid': UUIDLegacy(id)})[0]['uuid']
133-
UUIDLegacy('...')
134-
>>> db.test.find({'uuid': UUIDLegacy(id)})[0]['uuid'].uuid
135137
UUID('...')
136138
>>>
137139
>>> # Convert from subtype 3 to subtype 4
138-
>>> db.test.update({'uuid': UUIDLegacy(id)}, {'$set': {'uuid': id}})
140+
>>> doc = db.test.find_one({'uuid': UUIDLegacy(id)})
141+
>>> db.test.save(doc)
142+
ObjectId('...')
143+
>>> db.test.find({'uuid': UUIDLegacy(id)}).count()
144+
0
145+
>>> db.test.find({'uuid': {'$in': [UUIDLegacy(id), id]}}).count()
146+
1
139147
>>> db.test.find_one({'uuid': id})['uuid']
140148
UUID('...')
141149

test/test_binary.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,25 @@ def test_uuid_queries(self):
9191
coll.drop()
9292

9393
uu = uuid.uuid4()
94-
ul = UUIDLegacy(uu)
95-
coll.insert({'uuid': uu})
9694
coll.insert({'uuid': uuid.uuid4()})
9795
coll.insert({'uuid': uuid.uuid4()})
98-
coll.insert({'uuid': ul})
96+
coll.insert({'uuid': Binary(uu.bytes, 3)})
9997

10098
# Test UUIDLegacy queries.
101-
cur = coll.find({'uuid': ul})
99+
self.assertEquals(0, coll.find({'uuid': uu}).count())
100+
cur = coll.find({'uuid': UUIDLegacy(uu)})
102101
self.assertEquals(1, cur.count())
103102
retrieved = cur.next()['uuid']
104-
self.assertEquals(ul, retrieved)
105-
self.assertEquals(uu, retrieved.uuid)
103+
self.assertEquals(uu, retrieved)
106104

107105
# Test regular UUID queries.
106+
coll.insert({'uuid': uu})
108107
cur = coll.find({'uuid': uu})
109108
self.assertEquals(1, cur.count())
110109
self.assertEquals(uu, cur.next()['uuid'])
111110

112111
# Test both.
113-
cur = coll.find({'uuid': {'$in': [uu, ul]}})
112+
cur = coll.find({'uuid': {'$in': [uu, UUIDLegacy(uu)]}})
114113
self.assertEquals(2, cur.count())
115114
coll.drop()
116115

test/test_bson.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,10 @@ def test_uuid_legacy(self):
285285
id = uuid.uuid4()
286286
legacy = UUIDLegacy(id)
287287
self.assertEquals(3, legacy.subtype)
288-
transformed = (BSON.encode({"legacy": legacy})).decode()["legacy"]
289-
self.assert_(isinstance(transformed, UUIDLegacy))
290-
self.assertEqual(legacy, transformed)
291-
self.assertEqual(id, transformed.uuid)
292-
self.assertNotEqual(UUIDLegacy(uuid.uuid4()), transformed)
288+
transformed = (BSON.encode({"uuid": legacy})).decode()["uuid"]
289+
self.assert_(isinstance(transformed, uuid.UUID))
290+
self.assertEqual(id, transformed)
291+
self.assertNotEqual(UUIDLegacy(uuid.uuid4()), UUIDLegacy(transformed))
293292

294293
# The C extension was segfaulting on unicode RegExs, so we have this test
295294
# that doesn't really test anything but the lack of a segfault.

0 commit comments

Comments
 (0)