diff --git a/bson/__init__.py b/bson/__init__.py index 15045c38b..01339efa1 100644 --- a/bson/__init__.py +++ b/bson/__init__.py @@ -33,7 +33,6 @@ from bson.min_key import MinKey from bson.objectid import ObjectId from bson.py3compat import (b, PY3, - binary_type, iteritems, text_type, string_type, @@ -370,20 +369,17 @@ def _element_to_bson(key, value, check_keys, uuid_subtype): if isinstance(value, uuid.UUID): # Java Legacy if uuid_subtype == JAVA_LEGACY: - # Python 3.0(.1) returns a bytearray instance for bytes (3.1 - # and newer just return a bytes instance). Convert that to - # binary_type (here and below) for compatibility. - from_uuid = binary_type(value.bytes) + 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(binary_type(value.bytes_le), + value = Binary(value.bytes_le, subtype=OLD_UUID_SUBTYPE) # Python else: - value = Binary(binary_type(value.bytes), subtype=uuid_subtype) + value = Binary(value.bytes, subtype=uuid_subtype) if isinstance(value, Binary): subtype = value.subtype @@ -400,7 +396,7 @@ def _element_to_bson(key, value, check_keys, uuid_subtype): full_length = struct.pack("= 256 or subtype < 0: raise ValueError("subtype must be contained in [0, 256)") - self = binary_type.__new__(cls, data) + self = bytes.__new__(cls, data) self.__subtype = subtype return self @@ -150,14 +149,14 @@ class Binary(binary_type): def __getnewargs__(self): # Work around http://bugs.python.org/issue7382 data = super(Binary, self).__getnewargs__()[0] - if PY3 and not isinstance(data, binary_type): + if PY3 and not isinstance(data, bytes): data = data.encode('latin-1') return data, self.__subtype def __eq__(self, other): if isinstance(other, Binary): - return ((self.__subtype, binary_type(self)) == - (other.subtype, binary_type(other))) + return ((self.__subtype, bytes(self)) == + (other.subtype, bytes(other))) # We don't return NotImplemented here because if we did then # Binary("foo") == "foo" would return True, since Binary is a # subclass of str... @@ -167,7 +166,7 @@ class Binary(binary_type): return not self == other def __repr__(self): - return "Binary(%s, %s)" % (binary_type.__repr__(self), self.__subtype) + return "Binary(%s, %s)" % (bytes.__repr__(self), self.__subtype) class UUIDLegacy(Binary): @@ -210,10 +209,7 @@ class UUIDLegacy(Binary): def __new__(cls, obj): if not isinstance(obj, UUID): raise TypeError("obj must be an instance of uuid.UUID") - # Python 3.0(.1) returns a bytearray instance for bytes (3.1 and - # newer just return a bytes instance). Convert that to binary_type - # for compatibility. - self = Binary.__new__(cls, binary_type(obj.bytes), OLD_UUID_SUBTYPE) + self = Binary.__new__(cls, obj.bytes, OLD_UUID_SUBTYPE) self.__uuid = obj return self diff --git a/bson/json_util.py b/bson/json_util.py index 0b3a06b69..b1bf3ba61 100644 --- a/bson/json_util.py +++ b/bson/json_util.py @@ -97,7 +97,7 @@ from bson.objectid import ObjectId from bson.regex import Regex from bson.timestamp import Timestamp -from bson.py3compat import PY3, binary_type, iteritems, text_type +from bson.py3compat import PY3, iteritems, text_type _RE_OPT_TABLE = { @@ -152,8 +152,7 @@ def _json_convert(obj): """ if hasattr(obj, 'iteritems') or hasattr(obj, 'items'): # PY3 support return SON(((k, _json_convert(v)) for k, v in iteritems(obj))) - elif hasattr(obj, '__iter__') and not isinstance(obj, (text_type, - binary_type)): + elif hasattr(obj, '__iter__') and not isinstance(obj, (text_type, bytes)): return list((_json_convert(v) for v in obj)) try: return default(obj) @@ -248,7 +247,7 @@ def default(obj): return SON([ ('$binary', base64.b64encode(obj).decode()), ('$type', "%02x" % obj.subtype)]) - if PY3 and isinstance(obj, binary_type): + if PY3 and isinstance(obj, bytes): return SON([ ('$binary', base64.b64encode(obj).decode()), ('$type', "00")]) diff --git a/bson/objectid.py b/bson/objectid.py index 06abf08b2..2dd672798 100644 --- a/bson/objectid.py +++ b/bson/objectid.py @@ -33,7 +33,7 @@ import threading import time from bson.errors import InvalidId -from bson.py3compat import PY3, binary_type, text_type, bytes_from_hex +from bson.py3compat import PY3, bytes_from_hex, text_type from bson.tz_util import utc EMPTY = b"" @@ -180,9 +180,9 @@ class ObjectId(object): """ if isinstance(oid, ObjectId): self.__id = oid.__id - elif isinstance(oid, (text_type, binary_type)): + elif isinstance(oid, (bytes, text_type)): if len(oid) == 12: - if isinstance(oid, binary_type): + if isinstance(oid, bytes): self.__id = oid else: raise InvalidId("%s is not a valid ObjectId" % oid) @@ -194,9 +194,8 @@ class ObjectId(object): else: raise InvalidId("%s is not a valid ObjectId" % oid) else: - raise TypeError("id must be an instance of (%s, %s, ObjectId), " - "not %s" % (binary_type.__name__, - text_type.__name__, type(oid))) + raise TypeError("id must be an instance of (bytes, %s, ObjectId), " + "not %s" % (text_type.__name__, type(oid))) @property def binary(self): diff --git a/bson/py3compat.py b/bson/py3compat.py index 89ef61027..0f0be3e2b 100644 --- a/bson/py3compat.py +++ b/bson/py3compat.py @@ -53,7 +53,6 @@ if PY3: def _unicode(s): return s - binary_type = bytes text_type = str string_type = str integer_types = int @@ -89,7 +88,6 @@ else: _unicode = unicode - binary_type = str string_type = basestring text_type = unicode integer_types = (int, long) diff --git a/bson/regex.py b/bson/regex.py index 9885f9cd2..3139ea0f8 100644 --- a/bson/regex.py +++ b/bson/regex.py @@ -18,7 +18,7 @@ import re from bson.son import RE_TYPE -from bson.py3compat import text_type, binary_type, string_type +from bson.py3compat import string_type, text_type def str_flags_to_int(str_flags): @@ -86,7 +86,7 @@ class Regex(object): - `flags`: (optional) an integer bitmask, or a string of flag characters like "im" for IGNORECASE and MULTILINE """ - if not isinstance(pattern, (text_type, binary_type)): + if not isinstance(pattern, (text_type, bytes)): raise TypeError("pattern must be a string, not %s" % type(pattern)) self.pattern = pattern