Remove useless uuid module checks.
These were only needed for python 2.4 which does not provide a uuid module.
This commit is contained in:
parent
bd8d4a3b10
commit
b26459cd6a
@ -20,6 +20,7 @@ import datetime
|
||||
import re
|
||||
import struct
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
from bson.binary import (Binary, OLD_UUID_SUBTYPE,
|
||||
JAVA_LEGACY, CSHARP_LEGACY)
|
||||
@ -49,11 +50,6 @@ try:
|
||||
except ImportError:
|
||||
_use_c = False
|
||||
|
||||
try:
|
||||
import uuid
|
||||
_use_uuid = True
|
||||
except ImportError:
|
||||
_use_uuid = False
|
||||
|
||||
if PY3:
|
||||
long = int
|
||||
@ -190,7 +186,7 @@ def _get_binary(data, position, as_class, tz_aware, uuid_subtype, compile_re):
|
||||
if length2 != length - 4:
|
||||
raise InvalidBSON("invalid binary (st 2) - lengths don't match!")
|
||||
length = length2
|
||||
if subtype in (3, 4) and _use_uuid:
|
||||
if subtype in (3, 4):
|
||||
# Java Legacy
|
||||
if uuid_subtype == JAVA_LEGACY:
|
||||
java = data[position:position + length]
|
||||
@ -365,21 +361,23 @@ def _element_to_bson(key, value, check_keys, uuid_subtype):
|
||||
if isinstance(value, float):
|
||||
return BSONNUM + name + struct.pack("<d", value)
|
||||
|
||||
if _use_uuid:
|
||||
if isinstance(value, uuid.UUID):
|
||||
# Java Legacy
|
||||
if uuid_subtype == JAVA_LEGACY:
|
||||
from_uuid = value.bytes
|
||||
as_legacy_java = from_uuid[0:8][::-1] + from_uuid[8:16][::-1]
|
||||
value = Binary(as_legacy_java, subtype=OLD_UUID_SUBTYPE)
|
||||
# C# legacy
|
||||
elif uuid_subtype == CSHARP_LEGACY:
|
||||
# Microsoft GUID representation.
|
||||
value = Binary(value.bytes_le,
|
||||
subtype=OLD_UUID_SUBTYPE)
|
||||
# Python
|
||||
else:
|
||||
value = Binary(value.bytes, subtype=uuid_subtype)
|
||||
if isinstance(value, uuid.UUID):
|
||||
# Java Legacy
|
||||
if uuid_subtype == JAVA_LEGACY:
|
||||
from_uuid = value.bytes
|
||||
data = from_uuid[0:8][::-1] + from_uuid[8:16][::-1]
|
||||
subtype = OLD_UUID_SUBTYPE
|
||||
# C# legacy
|
||||
elif uuid_subtype == CSHARP_LEGACY:
|
||||
# Microsoft GUID representation.
|
||||
data = value.bytes_le
|
||||
subtype = OLD_UUID_SUBTYPE
|
||||
# Python
|
||||
else:
|
||||
data = value.bytes
|
||||
subtype = uuid_subtype
|
||||
return (BSONBIN + name +
|
||||
struct.pack("<i", len(data)) + b(chr(subtype)) + data)
|
||||
|
||||
if isinstance(value, Binary):
|
||||
subtype = value.subtype
|
||||
@ -634,11 +632,3 @@ def has_c():
|
||||
.. versionadded:: 1.9
|
||||
"""
|
||||
return _use_c
|
||||
|
||||
|
||||
def has_uuid():
|
||||
"""Is the uuid module available?
|
||||
|
||||
.. versionadded:: 2.3
|
||||
"""
|
||||
return _use_uuid
|
||||
|
||||
@ -329,14 +329,10 @@ static int _load_python_objects(PyObject* module) {
|
||||
_load_object(&state->MaxKey, "bson.max_key", "MaxKey") ||
|
||||
_load_object(&state->UTC, "bson.tz_util", "utc") ||
|
||||
_load_object(&state->RECompile, "re", "compile") ||
|
||||
_load_object(&state->Regex, "bson.regex", "Regex")) {
|
||||
_load_object(&state->Regex, "bson.regex", "Regex") ||
|
||||
_load_object(&state->UUID, "uuid", "UUID")) {
|
||||
return 1;
|
||||
}
|
||||
/* If we couldn't import uuid then we must be on 2.4. Just ignore. */
|
||||
if (_load_object(&state->UUID, "uuid", "UUID") == 1) {
|
||||
state->UUID = NULL;
|
||||
PyErr_Clear();
|
||||
}
|
||||
/* Reload our REType hack too. */
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
empty_string = PyBytes_FromString("");
|
||||
@ -555,6 +551,8 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer,
|
||||
unsigned char uuid_subtype) {
|
||||
struct module_state *state = GETSTATE(self);
|
||||
|
||||
PyObject* uuid_type;
|
||||
|
||||
/*
|
||||
* Don't use PyObject_IsInstance for our custom types. It causes
|
||||
* problems with python sub interpreters. Our custom types should
|
||||
@ -1002,85 +1000,79 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer,
|
||||
/*
|
||||
* Try UUID last since we have to import
|
||||
* it if we're in a sub-interpreter.
|
||||
*
|
||||
* If we're running under python 2.4 there likely
|
||||
* isn't a uuid module.
|
||||
*/
|
||||
if (state->UUID) {
|
||||
PyObject* uuid_type = _get_object(state->UUID, "uuid", "UUID");
|
||||
if (uuid_type && PyObject_IsInstance(value, uuid_type)) {
|
||||
/* Just a special case of Binary above, but
|
||||
* simpler to do as a separate case. */
|
||||
PyObject* bytes;
|
||||
/* Could be bytes, bytearray, str... */
|
||||
const char* data;
|
||||
/* UUID is always 16 bytes */
|
||||
int size = 16;
|
||||
int subtype;
|
||||
uuid_type = _get_object(state->UUID, "uuid", "UUID");
|
||||
if (uuid_type && PyObject_IsInstance(value, uuid_type)) {
|
||||
/* Just a special case of Binary above, but
|
||||
* simpler to do as a separate case. */
|
||||
PyObject* bytes;
|
||||
/* Could be bytes, bytearray, str... */
|
||||
const char* data;
|
||||
/* UUID is always 16 bytes */
|
||||
int size = 16;
|
||||
int subtype;
|
||||
|
||||
Py_DECREF(uuid_type);
|
||||
Py_DECREF(uuid_type);
|
||||
|
||||
if (uuid_subtype == JAVA_LEGACY || uuid_subtype == CSHARP_LEGACY) {
|
||||
subtype = 3;
|
||||
}
|
||||
else {
|
||||
subtype = uuid_subtype;
|
||||
}
|
||||
if (uuid_subtype == JAVA_LEGACY || uuid_subtype == CSHARP_LEGACY) {
|
||||
subtype = 3;
|
||||
}
|
||||
else {
|
||||
subtype = uuid_subtype;
|
||||
}
|
||||
|
||||
*(buffer_get_buffer(buffer) + type_byte) = 0x05;
|
||||
if (!buffer_write_bytes(buffer, (const char*)&size, 4)) {
|
||||
return 0;
|
||||
}
|
||||
if (!buffer_write_bytes(buffer, (const char*)&subtype, 1)) {
|
||||
return 0;
|
||||
}
|
||||
*(buffer_get_buffer(buffer) + type_byte) = 0x05;
|
||||
if (!buffer_write_bytes(buffer, (const char*)&size, 4)) {
|
||||
return 0;
|
||||
}
|
||||
if (!buffer_write_bytes(buffer, (const char*)&subtype, 1)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (uuid_subtype == CSHARP_LEGACY) {
|
||||
/* Legacy C# byte order */
|
||||
bytes = PyObject_GetAttrString(value, "bytes_le");
|
||||
}
|
||||
else {
|
||||
bytes = PyObject_GetAttrString(value, "bytes");
|
||||
}
|
||||
if (!bytes) {
|
||||
return 0;
|
||||
}
|
||||
if (uuid_subtype == CSHARP_LEGACY) {
|
||||
/* Legacy C# byte order */
|
||||
bytes = PyObject_GetAttrString(value, "bytes_le");
|
||||
}
|
||||
else {
|
||||
bytes = PyObject_GetAttrString(value, "bytes");
|
||||
}
|
||||
if (!bytes) {
|
||||
return 0;
|
||||
}
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
/* Work around http://bugs.python.org/issue7380 */
|
||||
if (PyByteArray_Check(bytes)) {
|
||||
data = PyByteArray_AsString(bytes);
|
||||
}
|
||||
else {
|
||||
data = PyBytes_AsString(bytes);
|
||||
}
|
||||
/* Work around http://bugs.python.org/issue7380 */
|
||||
if (PyByteArray_Check(bytes)) {
|
||||
data = PyByteArray_AsString(bytes);
|
||||
}
|
||||
else {
|
||||
data = PyBytes_AsString(bytes);
|
||||
}
|
||||
#else
|
||||
data = PyString_AsString(bytes);
|
||||
data = PyString_AsString(bytes);
|
||||
#endif
|
||||
if (data == NULL) {
|
||||
if (data == NULL) {
|
||||
Py_DECREF(bytes);
|
||||
return 0;
|
||||
}
|
||||
if (uuid_subtype == JAVA_LEGACY) {
|
||||
/* Store in legacy java byte order. */
|
||||
char as_legacy_java[16];
|
||||
_fix_java(data, as_legacy_java);
|
||||
if (!buffer_write_bytes(buffer, as_legacy_java, size)) {
|
||||
Py_DECREF(bytes);
|
||||
return 0;
|
||||
}
|
||||
if (uuid_subtype == JAVA_LEGACY) {
|
||||
/* Store in legacy java byte order. */
|
||||
char as_legacy_java[16];
|
||||
_fix_java(data, as_legacy_java);
|
||||
if (!buffer_write_bytes(buffer, as_legacy_java, size)) {
|
||||
Py_DECREF(bytes);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!buffer_write_bytes(buffer, data, size)) {
|
||||
Py_DECREF(bytes);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Py_DECREF(bytes);
|
||||
return 1;
|
||||
} else {
|
||||
Py_XDECREF(uuid_type);
|
||||
}
|
||||
else {
|
||||
if (!buffer_write_bytes(buffer, data, size)) {
|
||||
Py_DECREF(bytes);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Py_DECREF(bytes);
|
||||
return 1;
|
||||
}
|
||||
Py_XDECREF(uuid_type);
|
||||
/* We can't determine value's type. Fail. */
|
||||
_set_cannot_encode(value);
|
||||
return 0;
|
||||
@ -1621,7 +1613,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, unsigned* positio
|
||||
goto invalid;
|
||||
}
|
||||
/* Encode as UUID, not Binary */
|
||||
if ((subtype == 3 || subtype == 4) && state->UUID) {
|
||||
if (subtype == 3 || subtype == 4) {
|
||||
PyObject* kwargs;
|
||||
PyObject* args = PyTuple_New(0);
|
||||
/* UUID should always be 16 bytes */
|
||||
|
||||
@ -12,11 +12,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
try:
|
||||
from uuid import UUID
|
||||
except ImportError:
|
||||
# Python2.4 doesn't have a uuid module.
|
||||
pass
|
||||
from uuid import UUID
|
||||
|
||||
from bson.py3compat import PY3
|
||||
|
||||
|
||||
@ -76,6 +76,7 @@ import base64
|
||||
import calendar
|
||||
import datetime
|
||||
import re
|
||||
import uuid
|
||||
|
||||
json_lib = True
|
||||
try:
|
||||
@ -86,7 +87,6 @@ except ImportError:
|
||||
except ImportError:
|
||||
json_lib = False
|
||||
|
||||
import bson
|
||||
from bson import EPOCH_AWARE, RE_TYPE, SON
|
||||
from bson.binary import Binary
|
||||
from bson.code import Code
|
||||
@ -191,8 +191,8 @@ def object_hook(dct, compile_re=True):
|
||||
return Binary(base64.b64decode(dct["$binary"].encode()), subtype)
|
||||
if "$code" in dct:
|
||||
return Code(dct["$code"], dct.get("$scope"))
|
||||
if bson.has_uuid() and "$uuid" in dct:
|
||||
return bson.uuid.UUID(dct["$uuid"])
|
||||
if "$uuid" in dct:
|
||||
return uuid.UUID(dct["$uuid"])
|
||||
return dct
|
||||
|
||||
|
||||
@ -251,6 +251,6 @@ def default(obj):
|
||||
return SON([
|
||||
('$binary', base64.b64encode(obj).decode()),
|
||||
('$type', "00")])
|
||||
if bson.has_uuid() and isinstance(obj, bson.uuid.UUID):
|
||||
if isinstance(obj, uuid.UUID):
|
||||
return {"$uuid": obj.hex}
|
||||
raise TypeError("%r is not JSON serializable" % obj)
|
||||
|
||||
@ -19,11 +19,7 @@ import copy
|
||||
import pickle
|
||||
import sys
|
||||
import unittest
|
||||
try:
|
||||
import uuid
|
||||
should_test_uuid = True
|
||||
except ImportError:
|
||||
should_test_uuid = False
|
||||
import uuid
|
||||
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
@ -96,8 +92,6 @@ class TestBinary(unittest.TestCase):
|
||||
"Binary(%s, 100)" % (repr(b"test"),))
|
||||
|
||||
def test_legacy_java_uuid(self):
|
||||
if not should_test_uuid:
|
||||
raise SkipTest("No uuid module")
|
||||
|
||||
# Generated by the Java driver
|
||||
from_java = (b'bAAAAAdfaWQAUCBQxkVm+XdxJ9tOBW5ld2d1aWQAEAAAAAMIQkfACFu'
|
||||
@ -169,8 +163,6 @@ class TestBinary(unittest.TestCase):
|
||||
client.pymongo_test.drop_collection('java_uuid')
|
||||
|
||||
def test_legacy_csharp_uuid(self):
|
||||
if not should_test_uuid:
|
||||
raise SkipTest("No uuid module")
|
||||
|
||||
# Generated by the .net driver
|
||||
from_csharp = (b'ZAAAABBfaWQAAAAAAAVuZXdndWlkABAAAAAD+MkoCd/Jy0iYJ7Vhl'
|
||||
@ -241,16 +233,12 @@ class TestBinary(unittest.TestCase):
|
||||
client.pymongo_test.drop_collection('csharp_uuid')
|
||||
|
||||
def test_uri_to_uuid(self):
|
||||
if not should_test_uuid:
|
||||
raise SkipTest("No uuid module")
|
||||
|
||||
uri = "mongodb://foo/?uuidrepresentation=csharpLegacy"
|
||||
client = MongoClient(uri, _connect=False)
|
||||
self.assertEqual(client.pymongo_test.test.uuid_subtype, CSHARP_LEGACY)
|
||||
|
||||
def test_uuid_queries(self):
|
||||
if not should_test_uuid:
|
||||
raise SkipTest("No uuid module")
|
||||
|
||||
c = get_client()
|
||||
coll = c.pymongo_test.test
|
||||
@ -300,15 +288,14 @@ class TestBinary(unittest.TestCase):
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||
self.assertEqual(b1, pickle.loads(pickle.dumps(b1, proto)))
|
||||
|
||||
if should_test_uuid:
|
||||
uu = uuid.uuid4()
|
||||
uul = UUIDLegacy(uu)
|
||||
uu = uuid.uuid4()
|
||||
uul = UUIDLegacy(uu)
|
||||
|
||||
self.assertEqual(uul, copy.copy(uul))
|
||||
self.assertEqual(uul, copy.deepcopy(uul))
|
||||
self.assertEqual(uul, copy.copy(uul))
|
||||
self.assertEqual(uul, copy.deepcopy(uul))
|
||||
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||
self.assertEqual(uul, pickle.loads(pickle.dumps(uul, proto)))
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||
self.assertEqual(uul, pickle.loads(pickle.dumps(uul, proto)))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@ -21,11 +21,8 @@ import re
|
||||
import sys
|
||||
import traceback
|
||||
import unittest
|
||||
try:
|
||||
import uuid
|
||||
should_test_uuid = True
|
||||
except ImportError:
|
||||
should_test_uuid = False
|
||||
import uuid
|
||||
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
from nose.plugins.skip import SkipTest
|
||||
@ -398,8 +395,6 @@ class TestBSON(unittest.TestCase):
|
||||
BSON.encode({"tuple": (1, 2)}).decode())
|
||||
|
||||
def test_uuid(self):
|
||||
if not should_test_uuid:
|
||||
raise SkipTest("No uuid module")
|
||||
|
||||
id = uuid.uuid4()
|
||||
transformed_id = (BSON.encode({"id": id})).decode()["id"]
|
||||
@ -409,8 +404,6 @@ class TestBSON(unittest.TestCase):
|
||||
self.assertNotEqual(uuid.uuid4(), transformed_id)
|
||||
|
||||
def test_uuid_legacy(self):
|
||||
if not should_test_uuid:
|
||||
raise SkipTest("No uuid module")
|
||||
|
||||
id = uuid.uuid4()
|
||||
legacy = UUIDLegacy(id)
|
||||
|
||||
@ -22,6 +22,7 @@ import sys
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
import uuid
|
||||
import warnings
|
||||
|
||||
from nose.plugins.skip import SkipTest
|
||||
@ -57,13 +58,6 @@ from test import (qcheck,
|
||||
version)
|
||||
|
||||
|
||||
have_uuid = True
|
||||
try:
|
||||
import uuid
|
||||
except ImportError:
|
||||
have_uuid = False
|
||||
|
||||
|
||||
class TestCollection(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
@ -1932,15 +1926,14 @@ class TestCollection(unittest.TestCase):
|
||||
self.assertRaises(DuplicateKeyError, do_insert, coe_args)
|
||||
self.assertTrue(2, self.db.test.count())
|
||||
|
||||
if have_uuid:
|
||||
doc = {'_id': 2, 'uuid': uuid.uuid4()}
|
||||
uuid_sub_args = (name, [doc],
|
||||
True, True, {'w': 1}, True, 6)
|
||||
do_insert(uuid_sub_args)
|
||||
coll = self.db.test
|
||||
self.assertNotEqual(doc, coll.find_one({'_id': 2}))
|
||||
coll.uuid_subtype = 6
|
||||
self.assertEqual(doc, coll.find_one({'_id': 2}))
|
||||
doc = {'_id': 2, 'uuid': uuid.uuid4()}
|
||||
uuid_sub_args = (name, [doc],
|
||||
True, True, {'w': 1}, True, 6)
|
||||
do_insert(uuid_sub_args)
|
||||
coll = self.db.test
|
||||
self.assertNotEqual(doc, coll.find_one({'_id': 2}))
|
||||
coll.uuid_subtype = 6
|
||||
self.assertEqual(doc, coll.find_one({'_id': 2}))
|
||||
|
||||
def test_map_reduce(self):
|
||||
if not version.at_least(self.db.connection, (1, 1, 1)):
|
||||
|
||||
@ -16,6 +16,7 @@
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
import uuid
|
||||
import warnings
|
||||
|
||||
sys.path[0:0] = [""]
|
||||
@ -32,18 +33,10 @@ from pymongo.errors import ConfigurationError, OperationFailure
|
||||
from test import host, port, pair, version
|
||||
from test.utils import drop_collections
|
||||
|
||||
have_uuid = True
|
||||
try:
|
||||
import uuid
|
||||
except ImportError:
|
||||
have_uuid = False
|
||||
|
||||
|
||||
class TestCommon(unittest.TestCase):
|
||||
|
||||
def test_uuid_subtype(self):
|
||||
if not have_uuid:
|
||||
raise SkipTest("No uuid module")
|
||||
|
||||
self.client = MongoClient(pair)
|
||||
self.db = self.client.pymongo_test
|
||||
|
||||
@ -18,12 +18,12 @@ import unittest
|
||||
import datetime
|
||||
import re
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
from nose.plugins.skip import SkipTest
|
||||
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
import bson
|
||||
from bson import json_util
|
||||
from bson.binary import Binary, MD5_SUBTYPE, USER_DEFINED_SUBTYPE
|
||||
from bson.code import Code
|
||||
@ -154,11 +154,8 @@ class TestJsonUtil(unittest.TestCase):
|
||||
self.assertEqual(dct['ts']['i'], 13)
|
||||
|
||||
def test_uuid(self):
|
||||
if not bson.has_uuid():
|
||||
raise SkipTest("No uuid module")
|
||||
self.round_trip(
|
||||
{'uuid': bson.uuid.UUID(
|
||||
'f47ac10b-58cc-4372-a567-0e02b2c3d479')})
|
||||
{'uuid': uuid.UUID('f47ac10b-58cc-4372-a567-0e02b2c3d479')})
|
||||
|
||||
def test_binary(self):
|
||||
bin_type_dict = {"bin": Binary(b"\x00\x01\x02\x03\x04")}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user