diff --git a/src/future/types/newbytes.py b/src/future/types/newbytes.py index fb8bee53..234ac5e7 100644 --- a/src/future/types/newbytes.py +++ b/src/future/types/newbytes.py @@ -9,7 +9,7 @@ from numbers import Integral import string -from future.utils import istext, isbytes, PY3, with_metaclass +from future.utils import istext, isbytes, native, PY3, with_metaclass from future.types import no, issubset from future.types.newobject import newobject @@ -42,14 +42,14 @@ def __new__(cls, *args, **kwargs): bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer bytes(int) -> bytes object of size given by the parameter initialized with null bytes bytes() -> empty bytes object - + Construct an immutable array of bytes from: - an iterable yielding integers in range(256) - a text string encoded using the specified encoding - any object implementing the buffer API. - an integer """ - + encoding = None errors = None @@ -64,7 +64,7 @@ def __new__(cls, *args, **kwargs): # We use type() instead of the above because we're redefining # this to be True for all unicode string subclasses. Warning: # This may render newstr un-subclassable. - if type(args[0]) == newbytes: + if type(args[0]) is newbytes: # Special-case: for consistency with Py3.3, we return the same object # (with the same id) if a newbytes object is passed into the # newbytes constructor. @@ -90,8 +90,8 @@ def __new__(cls, *args, **kwargs): newargs = [encoding] if errors is not None: newargs.append(errors) - value = args[0].encode(*newargs) - ### + value = native(args[0].encode(*newargs)) + ### elif isinstance(args[0], Iterable): if len(args[0]) == 0: # This could be an empty list or tuple. Return b'' as on Py3. @@ -113,7 +113,7 @@ def __new__(cls, *args, **kwargs): else: value = args[0] return super(newbytes, cls).__new__(cls, value) - + def __repr__(self): return 'b' + super(newbytes, self).__repr__() @@ -140,7 +140,7 @@ def __contains__(self, key): else: newbyteskey = newbytes(key) return issubset(list(newbyteskey), list(self)) - + @no(unicode) def __add__(self, other): return newbytes(super(newbytes, self).__add__(other)) @@ -148,7 +148,7 @@ def __add__(self, other): @no(unicode) def __radd__(self, left): return newbytes(left) + self - + @no(unicode) def __mul__(self, other): return newbytes(super(newbytes, self).__mul__(other)) @@ -371,7 +371,7 @@ def rstrip(self, bytes_to_strip=None): """ Strip trailing bytes contained in the argument. If the argument is omitted, strip trailing ASCII whitespace. - """ + """ return newbytes(super(newbytes, self).rstrip(bytes_to_strip)) @no(unicode) @@ -379,24 +379,24 @@ def strip(self, bytes_to_strip=None): """ Strip leading and trailing bytes contained in the argument. If the argument is omitted, strip trailing ASCII whitespace. - """ + """ return newbytes(super(newbytes, self).strip(bytes_to_strip)) def lower(self): """ b.lower() -> copy of b - + Return a copy of b with all ASCII characters converted to lowercase. - """ + """ return newbytes(super(newbytes, self).lower()) @no(unicode) def upper(self): """ b.upper() -> copy of b - + Return a copy of b with all ASCII characters converted to uppercase. - """ + """ return newbytes(super(newbytes, self).upper()) @classmethod diff --git a/tests/test_future/test_bytes.py b/tests/test_future/test_bytes.py index f30e7406..ca40d63e 100644 --- a/tests/test_future/test_bytes.py +++ b/tests/test_future/test_bytes.py @@ -29,6 +29,10 @@ def test_bytes_encoding_arg(self): b = bytes(u, encoding='utf-8') self.assertEqual(b, u.encode('utf-8')) + nu = str(u) + b = bytes(nu, encoding='utf-8') + self.assertEqual(b, u.encode('utf-8')) + def test_bytes_encoding_arg_non_kwarg(self): """ As above, but with a positional argument @@ -37,6 +41,10 @@ def test_bytes_encoding_arg_non_kwarg(self): b = bytes(u, 'utf-8') self.assertEqual(b, u.encode('utf-8')) + nu = str(u) + b = bytes(nu, 'utf-8') + self.assertEqual(b, u.encode('utf-8')) + def test_bytes_string_no_encoding(self): with self.assertRaises(TypeError): bytes(u'ABC') @@ -290,7 +298,7 @@ def test_endswith(self): exc = str(cm.exception) # self.assertIn('bytes', exc) # self.assertIn('tuple', exc) - + def test_decode(self): b = bytes(b'abcd') s = b.decode('utf-8') @@ -357,7 +365,7 @@ def test_hash(self): d[s] = s self.assertEqual(len(d), 2) self.assertEqual(set(d.keys()), set([s, b])) - + @unittest.expectedFailure def test_hash_with_native_types(self): # Warning: initializing the dict with native Py2 types throws the @@ -478,7 +486,7 @@ def test_bytes_within_range(self): ValueError ... ValueError: bytes must be in range(0, 256) - + Ensure our bytes() constructor has the same behaviour """ b1 = bytes([254, 255]) diff --git a/tests/test_future/test_str.py b/tests/test_future/test_str.py index 7e37a62f..a1ef6868 100644 --- a/tests/test_future/test_str.py +++ b/tests/test_future/test_str.py @@ -18,6 +18,7 @@ def test_str(self): self.assertFalse(str is bytes) self.assertEqual(str('blah'), u'blah') # u'' prefix: Py3.3 and Py2 only self.assertEqual(str(b'1234'), "b'1234'") + self.assertEqual(str(bytes(b'1234')), "b'1234'") def test_bool_str(self): s1 = str(u'abc') @@ -258,7 +259,7 @@ def test_str_contains_something(self): if utils.PY2: self.assertTrue(b'A' in s) with self.assertRaises(TypeError): - bytes(b'A') in s + bytes(b'A') in s with self.assertRaises(TypeError): 65 in s # unlike bytes