Skip to content

Commit b26459c

Browse files
committed
Remove useless uuid module checks.
These were only needed for python 2.4 which does not provide a uuid module.
1 parent bd8d4a3 commit b26459c

File tree

9 files changed

+111
-170
lines changed

9 files changed

+111
-170
lines changed

bson/__init__.py

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import re
2121
import struct
2222
import sys
23+
import uuid
2324

2425
from bson.binary import (Binary, OLD_UUID_SUBTYPE,
2526
JAVA_LEGACY, CSHARP_LEGACY)
@@ -49,11 +50,6 @@
4950
except ImportError:
5051
_use_c = False
5152

52-
try:
53-
import uuid
54-
_use_uuid = True
55-
except ImportError:
56-
_use_uuid = False
5753

5854
if PY3:
5955
long = int
@@ -190,7 +186,7 @@ def _get_binary(data, position, as_class, tz_aware, uuid_subtype, compile_re):
190186
if length2 != length - 4:
191187
raise InvalidBSON("invalid binary (st 2) - lengths don't match!")
192188
length = length2
193-
if subtype in (3, 4) and _use_uuid:
189+
if subtype in (3, 4):
194190
# Java Legacy
195191
if uuid_subtype == JAVA_LEGACY:
196192
java = data[position:position + length]
@@ -365,21 +361,23 @@ def _element_to_bson(key, value, check_keys, uuid_subtype):
365361
if isinstance(value, float):
366362
return BSONNUM + name + struct.pack("<d", value)
367363

368-
if _use_uuid:
369-
if isinstance(value, uuid.UUID):
370-
# Java Legacy
371-
if uuid_subtype == JAVA_LEGACY:
372-
from_uuid = value.bytes
373-
as_legacy_java = from_uuid[0:8][::-1] + from_uuid[8:16][::-1]
374-
value = Binary(as_legacy_java, subtype=OLD_UUID_SUBTYPE)
375-
# C# legacy
376-
elif uuid_subtype == CSHARP_LEGACY:
377-
# Microsoft GUID representation.
378-
value = Binary(value.bytes_le,
379-
subtype=OLD_UUID_SUBTYPE)
380-
# Python
381-
else:
382-
value = Binary(value.bytes, subtype=uuid_subtype)
364+
if isinstance(value, uuid.UUID):
365+
# Java Legacy
366+
if uuid_subtype == JAVA_LEGACY:
367+
from_uuid = value.bytes
368+
data = from_uuid[0:8][::-1] + from_uuid[8:16][::-1]
369+
subtype = OLD_UUID_SUBTYPE
370+
# C# legacy
371+
elif uuid_subtype == CSHARP_LEGACY:
372+
# Microsoft GUID representation.
373+
data = value.bytes_le
374+
subtype = OLD_UUID_SUBTYPE
375+
# Python
376+
else:
377+
data = value.bytes
378+
subtype = uuid_subtype
379+
return (BSONBIN + name +
380+
struct.pack("<i", len(data)) + b(chr(subtype)) + data)
383381

384382
if isinstance(value, Binary):
385383
subtype = value.subtype
@@ -634,11 +632,3 @@ def has_c():
634632
.. versionadded:: 1.9
635633
"""
636634
return _use_c
637-
638-
639-
def has_uuid():
640-
"""Is the uuid module available?
641-
642-
.. versionadded:: 2.3
643-
"""
644-
return _use_uuid

bson/_cbsonmodule.c

Lines changed: 66 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,10 @@ static int _load_python_objects(PyObject* module) {
329329
_load_object(&state->MaxKey, "bson.max_key", "MaxKey") ||
330330
_load_object(&state->UTC, "bson.tz_util", "utc") ||
331331
_load_object(&state->RECompile, "re", "compile") ||
332-
_load_object(&state->Regex, "bson.regex", "Regex")) {
332+
_load_object(&state->Regex, "bson.regex", "Regex") ||
333+
_load_object(&state->UUID, "uuid", "UUID")) {
333334
return 1;
334335
}
335-
/* If we couldn't import uuid then we must be on 2.4. Just ignore. */
336-
if (_load_object(&state->UUID, "uuid", "UUID") == 1) {
337-
state->UUID = NULL;
338-
PyErr_Clear();
339-
}
340336
/* Reload our REType hack too. */
341337
#if PY_MAJOR_VERSION >= 3
342338
empty_string = PyBytes_FromString("");
@@ -555,6 +551,8 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer,
555551
unsigned char uuid_subtype) {
556552
struct module_state *state = GETSTATE(self);
557553

554+
PyObject* uuid_type;
555+
558556
/*
559557
* Don't use PyObject_IsInstance for our custom types. It causes
560558
* problems with python sub interpreters. Our custom types should
@@ -1002,85 +1000,79 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer,
10021000
/*
10031001
* Try UUID last since we have to import
10041002
* it if we're in a sub-interpreter.
1005-
*
1006-
* If we're running under python 2.4 there likely
1007-
* isn't a uuid module.
10081003
*/
1009-
if (state->UUID) {
1010-
PyObject* uuid_type = _get_object(state->UUID, "uuid", "UUID");
1011-
if (uuid_type && PyObject_IsInstance(value, uuid_type)) {
1012-
/* Just a special case of Binary above, but
1013-
* simpler to do as a separate case. */
1014-
PyObject* bytes;
1015-
/* Could be bytes, bytearray, str... */
1016-
const char* data;
1017-
/* UUID is always 16 bytes */
1018-
int size = 16;
1019-
int subtype;
1020-
1021-
Py_DECREF(uuid_type);
1022-
1023-
if (uuid_subtype == JAVA_LEGACY || uuid_subtype == CSHARP_LEGACY) {
1024-
subtype = 3;
1025-
}
1026-
else {
1027-
subtype = uuid_subtype;
1028-
}
1004+
uuid_type = _get_object(state->UUID, "uuid", "UUID");
1005+
if (uuid_type && PyObject_IsInstance(value, uuid_type)) {
1006+
/* Just a special case of Binary above, but
1007+
* simpler to do as a separate case. */
1008+
PyObject* bytes;
1009+
/* Could be bytes, bytearray, str... */
1010+
const char* data;
1011+
/* UUID is always 16 bytes */
1012+
int size = 16;
1013+
int subtype;
10291014

1030-
*(buffer_get_buffer(buffer) + type_byte) = 0x05;
1031-
if (!buffer_write_bytes(buffer, (const char*)&size, 4)) {
1032-
return 0;
1033-
}
1034-
if (!buffer_write_bytes(buffer, (const char*)&subtype, 1)) {
1035-
return 0;
1036-
}
1015+
Py_DECREF(uuid_type);
10371016

1038-
if (uuid_subtype == CSHARP_LEGACY) {
1039-
/* Legacy C# byte order */
1040-
bytes = PyObject_GetAttrString(value, "bytes_le");
1041-
}
1042-
else {
1043-
bytes = PyObject_GetAttrString(value, "bytes");
1044-
}
1045-
if (!bytes) {
1046-
return 0;
1047-
}
1017+
if (uuid_subtype == JAVA_LEGACY || uuid_subtype == CSHARP_LEGACY) {
1018+
subtype = 3;
1019+
}
1020+
else {
1021+
subtype = uuid_subtype;
1022+
}
1023+
1024+
*(buffer_get_buffer(buffer) + type_byte) = 0x05;
1025+
if (!buffer_write_bytes(buffer, (const char*)&size, 4)) {
1026+
return 0;
1027+
}
1028+
if (!buffer_write_bytes(buffer, (const char*)&subtype, 1)) {
1029+
return 0;
1030+
}
1031+
1032+
if (uuid_subtype == CSHARP_LEGACY) {
1033+
/* Legacy C# byte order */
1034+
bytes = PyObject_GetAttrString(value, "bytes_le");
1035+
}
1036+
else {
1037+
bytes = PyObject_GetAttrString(value, "bytes");
1038+
}
1039+
if (!bytes) {
1040+
return 0;
1041+
}
10481042
#if PY_MAJOR_VERSION >= 3
1049-
/* Work around http://bugs.python.org/issue7380 */
1050-
if (PyByteArray_Check(bytes)) {
1051-
data = PyByteArray_AsString(bytes);
1052-
}
1053-
else {
1054-
data = PyBytes_AsString(bytes);
1055-
}
1043+
/* Work around http://bugs.python.org/issue7380 */
1044+
if (PyByteArray_Check(bytes)) {
1045+
data = PyByteArray_AsString(bytes);
1046+
}
1047+
else {
1048+
data = PyBytes_AsString(bytes);
1049+
}
10561050
#else
1057-
data = PyString_AsString(bytes);
1051+
data = PyString_AsString(bytes);
10581052
#endif
1059-
if (data == NULL) {
1053+
if (data == NULL) {
1054+
Py_DECREF(bytes);
1055+
return 0;
1056+
}
1057+
if (uuid_subtype == JAVA_LEGACY) {
1058+
/* Store in legacy java byte order. */
1059+
char as_legacy_java[16];
1060+
_fix_java(data, as_legacy_java);
1061+
if (!buffer_write_bytes(buffer, as_legacy_java, size)) {
10601062
Py_DECREF(bytes);
10611063
return 0;
10621064
}
1063-
if (uuid_subtype == JAVA_LEGACY) {
1064-
/* Store in legacy java byte order. */
1065-
char as_legacy_java[16];
1066-
_fix_java(data, as_legacy_java);
1067-
if (!buffer_write_bytes(buffer, as_legacy_java, size)) {
1068-
Py_DECREF(bytes);
1069-
return 0;
1070-
}
1071-
}
1072-
else {
1073-
if (!buffer_write_bytes(buffer, data, size)) {
1074-
Py_DECREF(bytes);
1075-
return 0;
1076-
}
1065+
}
1066+
else {
1067+
if (!buffer_write_bytes(buffer, data, size)) {
1068+
Py_DECREF(bytes);
1069+
return 0;
10771070
}
1078-
Py_DECREF(bytes);
1079-
return 1;
1080-
} else {
1081-
Py_XDECREF(uuid_type);
10821071
}
1072+
Py_DECREF(bytes);
1073+
return 1;
10831074
}
1075+
Py_XDECREF(uuid_type);
10841076
/* We can't determine value's type. Fail. */
10851077
_set_cannot_encode(value);
10861078
return 0;
@@ -1621,7 +1613,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, unsigned* positio
16211613
goto invalid;
16221614
}
16231615
/* Encode as UUID, not Binary */
1624-
if ((subtype == 3 || subtype == 4) && state->UUID) {
1616+
if (subtype == 3 || subtype == 4) {
16251617
PyObject* kwargs;
16261618
PyObject* args = PyTuple_New(0);
16271619
/* UUID should always be 16 bytes */

bson/binary.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
try:
16-
from uuid import UUID
17-
except ImportError:
18-
# Python2.4 doesn't have a uuid module.
19-
pass
15+
from uuid import UUID
2016

2117
from bson.py3compat import PY3
2218

bson/json_util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import calendar
7777
import datetime
7878
import re
79+
import uuid
7980

8081
json_lib = True
8182
try:
@@ -86,7 +87,6 @@
8687
except ImportError:
8788
json_lib = False
8889

89-
import bson
9090
from bson import EPOCH_AWARE, RE_TYPE, SON
9191
from bson.binary import Binary
9292
from bson.code import Code
@@ -191,8 +191,8 @@ def object_hook(dct, compile_re=True):
191191
return Binary(base64.b64decode(dct["$binary"].encode()), subtype)
192192
if "$code" in dct:
193193
return Code(dct["$code"], dct.get("$scope"))
194-
if bson.has_uuid() and "$uuid" in dct:
195-
return bson.uuid.UUID(dct["$uuid"])
194+
if "$uuid" in dct:
195+
return uuid.UUID(dct["$uuid"])
196196
return dct
197197

198198

@@ -251,6 +251,6 @@ def default(obj):
251251
return SON([
252252
('$binary', base64.b64encode(obj).decode()),
253253
('$type', "00")])
254-
if bson.has_uuid() and isinstance(obj, bson.uuid.UUID):
254+
if isinstance(obj, uuid.UUID):
255255
return {"$uuid": obj.hex}
256256
raise TypeError("%r is not JSON serializable" % obj)

test/test_binary.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
import pickle
2020
import sys
2121
import unittest
22-
try:
23-
import uuid
24-
should_test_uuid = True
25-
except ImportError:
26-
should_test_uuid = False
22+
import uuid
2723

2824
sys.path[0:0] = [""]
2925

@@ -96,8 +92,6 @@ def test_repr(self):
9692
"Binary(%s, 100)" % (repr(b"test"),))
9793

9894
def test_legacy_java_uuid(self):
99-
if not should_test_uuid:
100-
raise SkipTest("No uuid module")
10195

10296
# Generated by the Java driver
10397
from_java = (b'bAAAAAdfaWQAUCBQxkVm+XdxJ9tOBW5ld2d1aWQAEAAAAAMIQkfACFu'
@@ -169,8 +163,6 @@ def test_legacy_java_uuid(self):
169163
client.pymongo_test.drop_collection('java_uuid')
170164

171165
def test_legacy_csharp_uuid(self):
172-
if not should_test_uuid:
173-
raise SkipTest("No uuid module")
174166

175167
# Generated by the .net driver
176168
from_csharp = (b'ZAAAABBfaWQAAAAAAAVuZXdndWlkABAAAAAD+MkoCd/Jy0iYJ7Vhl'
@@ -241,16 +233,12 @@ def test_legacy_csharp_uuid(self):
241233
client.pymongo_test.drop_collection('csharp_uuid')
242234

243235
def test_uri_to_uuid(self):
244-
if not should_test_uuid:
245-
raise SkipTest("No uuid module")
246236

247237
uri = "mongodb://foo/?uuidrepresentation=csharpLegacy"
248238
client = MongoClient(uri, _connect=False)
249239
self.assertEqual(client.pymongo_test.test.uuid_subtype, CSHARP_LEGACY)
250240

251241
def test_uuid_queries(self):
252-
if not should_test_uuid:
253-
raise SkipTest("No uuid module")
254242

255243
c = get_client()
256244
coll = c.pymongo_test.test
@@ -300,15 +288,14 @@ def test_pickle(self):
300288
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
301289
self.assertEqual(b1, pickle.loads(pickle.dumps(b1, proto)))
302290

303-
if should_test_uuid:
304-
uu = uuid.uuid4()
305-
uul = UUIDLegacy(uu)
291+
uu = uuid.uuid4()
292+
uul = UUIDLegacy(uu)
306293

307-
self.assertEqual(uul, copy.copy(uul))
308-
self.assertEqual(uul, copy.deepcopy(uul))
294+
self.assertEqual(uul, copy.copy(uul))
295+
self.assertEqual(uul, copy.deepcopy(uul))
309296

310-
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
311-
self.assertEqual(uul, pickle.loads(pickle.dumps(uul, proto)))
297+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
298+
self.assertEqual(uul, pickle.loads(pickle.dumps(uul, proto)))
312299

313300

314301
if __name__ == "__main__":

0 commit comments

Comments
 (0)