Skip to content

READY FOR REVIEW - Fix #171. Pass newbytes's native value to its parent constructor. #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/future/types/newbytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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__()

Expand All @@ -140,15 +140,15 @@ 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))

@no(unicode)
def __radd__(self, left):
return newbytes(left) + self

@no(unicode)
def __mul__(self, other):
return newbytes(super(newbytes, self).__mul__(other))
Expand Down Expand Up @@ -371,32 +371,32 @@ 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)
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
Expand Down
14 changes: 11 additions & 3 deletions tests/test_future/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down
3 changes: 2 additions & 1 deletion tests/test_future/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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

Expand Down