Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r48164:913552c73f58
Date: 2011-10-17 21:13 +0200
http://bitbucket.org/pypy/pypy/changeset/913552c73f58/
Log: Fix test_bytes.py, test_buffer.py
diff --git a/pypy/interpreter/buffer.py b/pypy/interpreter/buffer.py
--- a/pypy/interpreter/buffer.py
+++ b/pypy/interpreter/buffer.py
@@ -51,9 +51,9 @@
def descr_getitem(self, space, w_index):
start, stop, step, size = space.decode_index4(w_index,
self.getlength())
if step == 0: # index only
- return space.wrap(self.getitem(start))
+ return space.wrapbytes(self.getitem(start))
res = self.getslice(start, stop, step, size)
- return space.wrap(res)
+ return space.wrapbytes(res)
@unwrap_spec(newstring='bufferstr')
def descr_setitem(self, space, w_index, newstring):
@@ -86,7 +86,7 @@
@unwrap_spec(other='bufferstr')
def descr_add(self, space, other):
- return space.wrap(self.as_str() + other)
+ return space.wrapbytes(self.as_str() + other)
def _make_descr__cmp(name):
def descr__cmp(self, space, w_other):
@@ -112,7 +112,7 @@
def descr_mul(self, space, w_times):
# xxx not the most efficient implementation
- w_string = space.wrap(self.as_str())
+ w_string = space.wrapbytes(self.as_str())
# use the __mul__ method instead of space.mul() so that we
# return NotImplemented instead of raising a TypeError
return space.call_method(w_string, '__mul__', w_times)
diff --git a/pypy/interpreter/test/test_buffer.py
b/pypy/interpreter/test/test_buffer.py
--- a/pypy/interpreter/test/test_buffer.py
+++ b/pypy/interpreter/test/test_buffer.py
@@ -9,7 +9,7 @@
def test_buffer_w(self):
space = self.space
- w_hello = space.wrap('hello world')
+ w_hello = space.wrapbytes('hello world')
buf = space.buffer_w(w_hello)
assert isinstance(buf, Buffer)
assert buf.getlength() == 11
@@ -23,7 +23,7 @@
def test_file_write(self):
space = self.space
- w_buffer = space.buffer(space.wrap('hello world'))
+ w_buffer = space.buffer(space.wrapbytes('hello world'))
filename = str(testdir.join('test_file_write'))
space.appexec([w_buffer, space.wrap(filename)], """(buffer, filename):
f = open(filename, 'wb')
@@ -35,13 +35,4 @@
f.close()
assert data == 'hello world'
- def test_unicode(self):
- space = self.space
- s = space.bufferstr_w(space.wrap(u'hello'))
- assert type(s) is str
- assert s == 'hello'
- space.raises_w(space.w_UnicodeEncodeError,
- space.bufferstr_w, space.wrap(u'\xe9'))
-
-
# Note: some app-level tests for buffer are in module/__builtin__/test/.
diff --git a/pypy/objspace/std/bytearrayobject.py
b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -127,13 +127,13 @@
def contains__Bytearray_String(space, w_bytearray, w_str):
# XXX slow - copies, needs rewriting
- w_str2 = str__Bytearray(space, w_bytearray)
+ w_str2 = _to_bytes(space, w_bytearray)
return stringobject.contains__String_String(space, w_str2, w_str)
def contains__Bytearray_ANY(space, w_bytearray, w_sub):
# XXX slow - copies, needs rewriting
- w_str = space.wrap(space.bufferstr_new_w(w_sub))
- w_str2 = str__Bytearray(space, w_bytearray)
+ w_str = space.wrapbytes(space.bufferstr_new_w(w_sub))
+ w_str2 = _to_bytes(space, w_bytearray)
return stringobject.contains__String_String(space, w_str2, w_str)
def add__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2):
@@ -148,7 +148,7 @@
def add__String_Bytearray(space, w_str, w_bytearray):
data2 = w_bytearray.data
- data1 = [c for c in space.str_w(w_str)]
+ data1 = [c for c in space.bytes_w(w_str)]
return W_BytearrayObject(data1 + data2)
def mul_bytearray_times(space, w_bytearray, w_times):
@@ -188,14 +188,14 @@
return space.w_True
def String2Bytearray(space, w_str):
- data = [c for c in space.str_w(w_str)]
+ data = [c for c in space.bytes_w(w_str)]
return W_BytearrayObject(data)
def eq__Bytearray_String(space, w_bytearray, w_other):
- return space.eq(str__Bytearray(space, w_bytearray), w_other)
+ return space.eq(_to_bytes(space, w_bytearray), w_other)
def ne__Bytearray_String(space, w_bytearray, w_other):
- return space.ne(str__Bytearray(space, w_bytearray), w_other)
+ return space.ne(_to_bytes(space, w_bytearray), w_other)
def _min(a, b):
if a < b:
@@ -226,7 +226,7 @@
def str_translate__Bytearray_ANY_ANY(space, w_bytearray1, w_table,
w_deletechars):
# XXX slow, copies *twice* needs proper implementation
- w_str_copy = str__Bytearray(space, w_bytearray1)
+ w_str_copy = _to_bytes(space, w_bytearray1)
w_res = stringobject.str_translate__String_ANY_ANY(space, w_str_copy,
w_table, w_deletechars)
return String2Bytearray(space, w_res)
@@ -265,8 +265,8 @@
return space.wrap(buf.build())
-def str__Bytearray(space, w_bytearray):
- return space.wrap(''.join(w_bytearray.data))
+def _to_bytes(space, w_bytearray):
+ return space.wrapbytes(''.join(w_bytearray.data))
def _convert_idx_params(space, w_self, w_start, w_stop):
start = slicetype.eval_slice_index(space, w_start)
@@ -294,42 +294,42 @@
def str_count__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_char, w_start,
w_stop):
w_char = space.wrap(space.bufferstr_new_w(w_char))
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
return stringobject.str_count__String_String_ANY_ANY(space, w_str, w_char,
w_start, w_stop)
def str_index__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_char, w_start,
w_stop):
w_char = space.wrap(space.bufferstr_new_w(w_char))
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
return stringobject.str_index__String_String_ANY_ANY(space, w_str, w_char,
w_start, w_stop)
def str_rindex__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_char, w_start,
w_stop):
w_char = space.wrap(space.bufferstr_new_w(w_char))
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
return stringobject.str_rindex__String_String_ANY_ANY(space, w_str, w_char,
w_start, w_stop)
def str_find__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_char, w_start,
w_stop):
w_char = space.wrap(space.bufferstr_new_w(w_char))
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
return stringobject.str_find__String_String_ANY_ANY(space, w_str, w_char,
w_start, w_stop)
def str_rfind__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_char, w_start,
w_stop):
w_char = space.wrap(space.bufferstr_new_w(w_char))
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
return stringobject.str_rfind__String_String_ANY_ANY(space, w_str, w_char,
w_start, w_stop)
def str_startswith__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_prefix,
w_start, w_stop):
w_prefix = space.wrap(space.bufferstr_new_w(w_prefix))
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
return stringobject.str_startswith__String_String_ANY_ANY(space, w_str,
w_prefix,
w_start, w_stop)
def str_startswith__Bytearray_Tuple_ANY_ANY(space, w_bytearray, w_prefix,
w_start, w_stop):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
w_prefix = space.newtuple([space.wrap(space.bufferstr_new_w(w_entry)) for
w_entry in
space.unpackiterable(w_prefix)])
return stringobject.str_startswith__String_Tuple_ANY_ANY(space, w_str,
w_prefix,
@@ -337,12 +337,12 @@
def str_endswith__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_suffix, w_start,
w_stop):
w_suffix = space.wrap(space.bufferstr_new_w(w_suffix))
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
return stringobject.str_endswith__String_String_ANY_ANY(space, w_str,
w_suffix,
w_start, w_stop)
def str_endswith__Bytearray_Tuple_ANY_ANY(space, w_bytearray, w_suffix,
w_start, w_stop):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
w_suffix = space.newtuple([space.wrap(space.bufferstr_new_w(w_entry)) for
w_entry in
space.unpackiterable(w_suffix)])
return stringobject.str_endswith__String_Tuple_ANY_ANY(space, w_str,
w_suffix,
@@ -369,35 +369,35 @@
return W_BytearrayObject(newdata)
def str_decode__Bytearray_ANY_ANY(space, w_bytearray, w_encoding, w_errors):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
return stringobject.str_decode__String_ANY_ANY(space, w_str, w_encoding,
w_errors)
def str_islower__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
return stringobject.str_islower__String(space, w_str)
def str_isupper__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
return stringobject.str_isupper__String(space, w_str)
def str_isalpha__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
return stringobject.str_isalpha__String(space, w_str)
def str_isalnum__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
return stringobject.str_isalnum__String(space, w_str)
def str_isdigit__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
return stringobject.str_isdigit__String(space, w_str)
def str_istitle__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
return stringobject.str_istitle__String(space, w_str)
def str_isspace__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
return stringobject.str_isspace__String(space, w_str)
def bytearray_insert__Bytearray_Int_ANY(space, w_bytearray, w_idx, w_other):
@@ -455,66 +455,67 @@
# These methods could just delegate to the string implementation,
# but they have to return a bytearray.
def str_replace__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_str1, w_str2,
w_max):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
+ w_str2, w_max)
w_res = stringobject.str_replace__String_ANY_ANY_ANY(space, w_str, w_str1,
w_str2, w_max)
return String2Bytearray(space, w_res)
def str_upper__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
w_res = stringobject.str_upper__String(space, w_str)
return String2Bytearray(space, w_res)
def str_lower__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
w_res = stringobject.str_lower__String(space, w_str)
return String2Bytearray(space, w_res)
def str_title__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
w_res = stringobject.str_title__String(space, w_str)
return String2Bytearray(space, w_res)
def str_swapcase__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
w_res = stringobject.str_swapcase__String(space, w_str)
return String2Bytearray(space, w_res)
def str_capitalize__Bytearray(space, w_bytearray):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
w_res = stringobject.str_capitalize__String(space, w_str)
return String2Bytearray(space, w_res)
def str_ljust__Bytearray_ANY_ANY(space, w_bytearray, w_width, w_fillchar):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
w_res = stringobject.str_ljust__String_ANY_ANY(space, w_str, w_width,
w_fillchar)
return String2Bytearray(space, w_res)
def str_rjust__Bytearray_ANY_ANY(space, w_bytearray, w_width, w_fillchar):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
w_res = stringobject.str_rjust__String_ANY_ANY(space, w_str, w_width,
w_fillchar)
return String2Bytearray(space, w_res)
def str_center__Bytearray_ANY_ANY(space, w_bytearray, w_width, w_fillchar):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
w_res = stringobject.str_center__String_ANY_ANY(space, w_str, w_width,
w_fillchar)
return String2Bytearray(space, w_res)
def str_zfill__Bytearray_ANY(space, w_bytearray, w_width):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
w_res = stringobject.str_zfill__String_ANY(space, w_str, w_width)
return String2Bytearray(space, w_res)
def str_expandtabs__Bytearray_ANY(space, w_bytearray, w_tabsize):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
w_res = stringobject.str_expandtabs__String_ANY(space, w_str, w_tabsize)
return String2Bytearray(space, w_res)
def str_splitlines__Bytearray_ANY(space, w_bytearray, w_keepends):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
w_result = stringobject.str_splitlines__String_ANY(space, w_str,
w_keepends)
return space.newlist([
new_bytearray(space, space.w_bytearray, makebytesdata_w(space,
w_entry))
@@ -522,9 +523,9 @@
])
def str_split__Bytearray_ANY_ANY(space, w_bytearray, w_by, w_maxsplit=-1):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
if not space.is_w(w_by, space.w_None):
- w_by = space.wrap(space.bufferstr_new_w(w_by))
+ w_by = space.wrapbytes(space.bufferstr_new_w(w_by))
w_list = space.call_method(w_str, "split", w_by, w_maxsplit)
length = space.int_w(space.len(w_list))
for i in range(length):
@@ -533,9 +534,9 @@
return w_list
def str_rsplit__Bytearray_ANY_ANY(space, w_bytearray, w_by, w_maxsplit=-1):
- w_str = str__Bytearray(space, w_bytearray)
+ w_str = _to_bytes(space, w_bytearray)
if not space.is_w(w_by, space.w_None):
- w_by = space.wrap(space.bufferstr_new_w(w_by))
+ w_by = space.wrapbytes(space.bufferstr_new_w(w_by))
w_list = space.call_method(w_str, "rsplit", w_by, w_maxsplit)
length = space.int_w(space.len(w_list))
for i in range(length):
@@ -544,8 +545,8 @@
return w_list
def str_partition__Bytearray_ANY(space, w_bytearray, w_sub):
- w_str = str__Bytearray(space, w_bytearray)
- w_sub = space.wrap(space.bufferstr_new_w(w_sub))
+ w_str = _to_bytes(space, w_bytearray)
+ w_sub = space.wrapbytes(space.bufferstr_new_w(w_sub))
w_tuple = stringobject.str_partition__String_String(space, w_str, w_sub)
w_a, w_b, w_c = space.fixedview(w_tuple, 3)
return space.newtuple([
@@ -554,8 +555,8 @@
String2Bytearray(space, w_c)])
def str_rpartition__Bytearray_ANY(space, w_bytearray, w_sub):
- w_str = str__Bytearray(space, w_bytearray)
- w_sub = space.wrap(space.bufferstr_new_w(w_sub))
+ w_str = _to_bytes(space, w_bytearray)
+ w_sub = space.wrapbytes(space.bufferstr_new_w(w_sub))
w_tuple = stringobject.str_rpartition__String_String(space, w_str, w_sub)
w_a, w_b, w_c = space.fixedview(w_tuple, 3)
return space.newtuple([
@@ -567,7 +568,6 @@
# Mutability methods
def list_append__Bytearray_ANY(space, w_bytearray, w_item):
- from pypy.objspace.std.bytearraytype import getbytevalue
w_bytearray.data.append(getbytevalue(space, w_item))
def list_extend__Bytearray_Bytearray(space, w_bytearray, w_other):
@@ -585,7 +585,6 @@
return w_bytearray1
def setitem__Bytearray_ANY_ANY(space, w_bytearray, w_index, w_item):
- from pypy.objspace.std.bytearraytype import getbytevalue
idx = space.getindex_w(w_index, space.w_IndexError, "bytearray index")
try:
w_bytearray.data[idx] = getbytevalue(space, w_item)
diff --git a/pypy/objspace/std/bytearraytype.py
b/pypy/objspace/std/bytearraytype.py
--- a/pypy/objspace/std/bytearraytype.py
+++ b/pypy/objspace/std/bytearraytype.py
@@ -87,7 +87,7 @@
"from a string of hexadecimal numbers.\nSpaces between two numbers are "
"accepted.\nExample: bytearray.fromhex('B9 01EF') -> "
"bytearray(b'\\xb9\\x01\\xef')."
- hexstring = space.str_w(w_hexstring)
+ hexstring = space.unicode_w(w_hexstring)
hexstring = hexstring.lower()
data = []
length = len(hexstring)
diff --git a/pypy/objspace/std/stringobject.py
b/pypy/objspace/std/stringobject.py
--- a/pypy/objspace/std/stringobject.py
+++ b/pypy/objspace/std/stringobject.py
@@ -153,11 +153,11 @@
def str_upper__String(space, w_self):
self = w_self._value
- return space.wrap(self.upper())
+ return space.wrapbytes(self.upper())
def str_lower__String(space, w_self):
self = w_self._value
- return space.wrap(self.lower())
+ return space.wrapbytes(self.lower())
def str_swapcase__String(space, w_self):
self = w_self._value
@@ -173,7 +173,7 @@
else:
builder.append(ch)
- return space.wrap(builder.build())
+ return space.wrapbytes(builder.build())
def str_capitalize__String(space, w_self):
@@ -195,7 +195,7 @@
else:
builder.append(ch)
- return space.wrap(builder.build())
+ return space.wrapbytes(builder.build())
def str_title__String(space, w_self):
input = w_self._value
@@ -213,7 +213,7 @@
prev_letter = ch
- return space.wrap(builder.build())
+ return space.wrapbytes(builder.build())
def str_split__String_None_ANY(space, w_self, w_none, w_maxsplit=-1):
maxsplit = space.int_w(w_maxsplit)
@@ -402,7 +402,7 @@
fillchar = fillchar[0] # annotator hint: it's a single character
u_self = d * fillchar + u_self
- return space.wrap(u_self)
+ return space.wrapbytes(u_self)
def str_ljust__String_ANY_ANY(space, w_self, w_arg, w_fillchar):
@@ -418,7 +418,7 @@
fillchar = fillchar[0] # annotator hint: it's a single character
u_self += d * fillchar
- return space.wrap(u_self)
+ return space.wrapbytes(u_self)
def _convert_idx_params(space, w_self, w_sub, w_start, w_end,
upper_bound=False):
self = w_self._value
@@ -503,7 +503,7 @@
def _string_replace(space, input, sub, by, maxsplit):
if maxsplit == 0:
- return space.wrap(input)
+ return space.wrapbytes(input)
#print "from replace, input: %s, sub: %s, by: %s" % (input, sub, by)
@@ -542,7 +542,7 @@
space.w_OverflowError,
space.wrap("replace string is too long"))
- return space.wrap(by.join(substrings_w))
+ return space.wrapbytes(by.join(substrings_w))
def str_replace__String_ANY_ANY_ANY(space, w_self, w_sub, w_by, w_maxsplit):
@@ -759,7 +759,7 @@
num_zeros = width - len(input)
if num_zeros <= 0:
# cannot return w_self, in case it is a subclass of str
- return space.wrap(input)
+ return space.wrapbytes(input)
builder = StringBuilder(width)
if len(input) > 0 and (input[0] == '+' or input[0] == '-'):
@@ -770,7 +770,7 @@
builder.append_multiple_char('0', num_zeros)
builder.append_slice(input, start, len(input))
- return space.wrap(builder.build())
+ return space.wrapbytes(builder.build())
def hash__String(space, w_str):
diff --git a/pypy/objspace/std/test/test_bytes.py
b/pypy/objspace/std/test/test_bytes.py
--- a/pypy/objspace/std/test/test_bytes.py
+++ b/pypy/objspace/std/test/test_bytes.py
@@ -6,12 +6,11 @@
assert b.__class__ is bytearray
def test_constructor(self):
- assert bytearray() == ""
- assert bytearray('abc') == "abc"
- assert bytearray(['a', 'b', 'c']) == "abc"
- assert bytearray([65, 66, 67]) == "ABC"
- assert bytearray(5) == '\0' * 5
- raises(ValueError, bytearray, ['a', 'bc'])
+ assert bytearray() == b""
+ assert bytearray(b'abc') == b"abc"
+ assert bytearray([65, 66, 67]) == b"ABC"
+ assert bytearray(5) == b'\0' * 5
+ raises(TypeError, bytearray, ['a', 'bc'])
raises(ValueError, bytearray, [65, -3])
raises(TypeError, bytearray, [65.0])
raises(ValueError, bytearray, -1)
@@ -20,8 +19,8 @@
class subclass(bytearray):
def __init__(self, newarg=1, *args, **kwargs):
bytearray.__init__(self, *args, **kwargs)
- x = subclass(4, source="abcd")
- assert x == "abcd"
+ x = subclass(4, source=b"abcd")
+ assert x == b"abcd"
def test_encoding(self):
data = u"Hello world\n\u1234\u5678\u9abc\def0\def0"
@@ -33,10 +32,10 @@
def test_encoding_with_ignore_errors(self):
data = u"H\u1234"
b = bytearray(data, "latin1", errors="ignore")
- assert b == "H"
+ assert b == b"H"
def test_len(self):
- b = bytearray('test')
+ b = bytearray(b'test')
assert len(b) == 4
def test_nohash(self):
@@ -44,146 +43,142 @@
def test_repr(self):
assert repr(bytearray()) == "bytearray(b'')"
- assert repr(bytearray('test')) == "bytearray(b'test')"
- assert repr(bytearray("d'oh")) == r"bytearray(b'd\'oh')"
+ assert repr(bytearray(b'test')) == "bytearray(b'test')"
+ assert repr(bytearray(b"d'oh")) == r"bytearray(b'd\'oh')"
def test_str(self):
- assert str(bytearray()) == ""
- assert str(bytearray('test')) == "test"
- assert str(bytearray("d'oh")) == "d'oh"
+ assert str(bytearray()) == "bytearray(b'')"
+ assert str(bytearray(b'test')) == "bytearray(b'test')"
+ assert str(bytearray(b"d'oh")) == r"bytearray(b'd\'oh')"
def test_getitem(self):
- b = bytearray('test')
+ b = bytearray(b'test')
assert b[0] == ord('t')
assert b[2] == ord('s')
raises(IndexError, b.__getitem__, 4)
- assert b[1:5] == bytearray('est')
- assert b[slice(1,5)] == bytearray('est')
+ assert b[1:5] == bytearray(b'est')
+ assert b[slice(1,5)] == bytearray(b'est')
def test_arithmetic(self):
- b1 = bytearray('hello ')
- b2 = bytearray('world')
- assert b1 + b2 == bytearray('hello world')
- assert b1 * 2 == bytearray('hello hello ')
+ b1 = bytearray(b'hello ')
+ b2 = bytearray(b'world')
+ assert b1 + b2 == bytearray(b'hello world')
+ assert b1 * 2 == bytearray(b'hello hello ')
assert b1 * 1 is not b1
b3 = b1
b3 *= 3
- assert b3 == 'hello hello hello '
+ assert b3 == b'hello hello hello '
assert type(b3) == bytearray
assert b3 is b1
def test_contains(self):
- assert ord('l') in bytearray('hello')
- assert 'l' in bytearray('hello')
- assert bytearray('ll') in bytearray('hello')
- assert memoryview('ll') in bytearray('hello')
+ assert ord('l') in bytearray(b'hello')
+ assert b'l' in bytearray(b'hello')
+ assert bytearray(b'll') in bytearray(b'hello')
+ assert memoryview(b'll') in bytearray(b'hello')
- raises(TypeError, lambda: u'foo' in bytearray('foobar'))
+ raises(TypeError, lambda: u'foo' in bytearray(b'foobar'))
def test_splitlines(self):
- b = bytearray('1234')
+ b = bytearray(b'1234')
assert b.splitlines()[0] == b
assert b.splitlines()[0] is not b
- assert len(bytearray('foo\nbar').splitlines()) == 2
- for item in bytearray('foo\nbar').splitlines():
+ assert len(bytearray(b'foo\nbar').splitlines()) == 2
+ for item in bytearray(b'foo\nbar').splitlines():
assert isinstance(item, bytearray)
def test_ord(self):
- b = bytearray('\0A\x7f\x80\xff')
+ b = bytearray(b'\0A\x7f\x80\xff')
assert ([ord(b[i:i+1]) for i in range(len(b))] ==
[0, 65, 127, 128, 255])
- raises(TypeError, ord, bytearray('ll'))
+ raises(TypeError, ord, bytearray(b'll'))
raises(TypeError, ord, bytearray())
def test_translate(self):
- b = 'hello'
+ b = b'hello'
ba = bytearray(b)
rosetta = bytearray(range(0, 256))
rosetta[ord('o')] = ord('e')
- for table in rosetta, str(rosetta):
- c = ba.translate(table)
- assert ba == bytearray('hello')
- assert c == bytearray('helle')
-
- c = ba.translate(rosetta, 'l')
- assert c == bytearray('hee')
- assert isinstance(c, bytearray)
+ c = ba.translate(rosetta)
+ assert ba == bytearray(b'hello')
+ assert c == bytearray(b'helle')
+ assert isinstance(c, bytearray)
def test_strip(self):
- b = bytearray('mississippi ')
+ b = bytearray(b'mississippi ')
- assert b.strip() == 'mississippi'
- assert b.strip(None) == 'mississippi'
+ assert b.strip() == b'mississippi'
+ assert b.strip(None) == b'mississippi'
- b = bytearray('mississippi')
+ b = bytearray(b'mississippi')
- for strip_type in str, memoryview:
- assert b.strip(strip_type('i')) == 'mississipp'
- assert b.strip(strip_type('m')) == 'ississippi'
- assert b.strip(strip_type('pi')) == 'mississ'
- assert b.strip(strip_type('im')) == 'ssissipp'
- assert b.strip(strip_type('pim')) == 'ssiss'
- assert b.strip(strip_type(b)) == ''
+ for strip_type in bytes, memoryview:
+ assert b.strip(strip_type(b'i')) == b'mississipp'
+ assert b.strip(strip_type(b'm')) == b'ississippi'
+ assert b.strip(strip_type(b'pi')) == b'mississ'
+ assert b.strip(strip_type(b'im')) == b'ssissipp'
+ assert b.strip(strip_type(b'pim')) == b'ssiss'
+ assert b.strip(strip_type(b)) == b''
def test_iter(self):
- assert list(bytearray('hello')) == [104, 101, 108, 108, 111]
+ assert list(bytearray(b'hello')) == [104, 101, 108, 108, 111]
def test_compare(self):
- assert bytearray('hello') == bytearray('hello')
- assert bytearray('hello') < bytearray('world')
- assert bytearray('world') > bytearray('hello')
+ assert bytearray(b'hello') == bytearray(b'hello')
+ assert bytearray(b'hello') < bytearray(b'world')
+ assert bytearray(b'world') > bytearray(b'hello')
def test_compare_str(self):
- assert bytearray('hello1') == 'hello1'
- assert not (bytearray('hello1') != 'hello1')
- assert 'hello2' == bytearray('hello2')
- assert not ('hello1' != bytearray('hello1'))
+ assert bytearray(b'hello1') == b'hello1'
+ assert not (bytearray(b'hello1') != b'hello1')
+ assert b'hello2' == bytearray(b'hello2')
+ assert not (b'hello1' != bytearray(b'hello1'))
# unicode is always different
- assert not (bytearray('hello3') == unicode('world'))
- assert bytearray('hello3') != unicode('hello3')
- assert unicode('hello3') != bytearray('world')
- assert unicode('hello4') != bytearray('hello4')
- assert not (bytearray('') == u'')
- assert not (u'' == bytearray(''))
- assert bytearray('') != u''
- assert u'' != bytearray('')
+ assert not (bytearray(b'hello3') == 'world')
+ assert bytearray(b'hello3') != 'hello3'
+ assert 'hello3' != bytearray(b'world')
+ assert 'hello4' != bytearray(b'hello4')
+ assert not (bytearray(b'') == u'')
+ assert not (u'' == bytearray(b''))
+ assert bytearray(b'') != u''
+ assert u'' != bytearray(b'')
def test_stringlike_operations(self):
- assert bytearray('hello').islower()
- assert bytearray('HELLO').isupper()
- assert bytearray('hello').isalpha()
- assert not bytearray('hello2').isalpha()
- assert bytearray('hello2').isalnum()
- assert bytearray('1234').isdigit()
- assert bytearray(' ').isspace()
- assert bytearray('Abc').istitle()
+ assert bytearray(b'hello').islower()
+ assert bytearray(b'HELLO').isupper()
+ assert bytearray(b'hello').isalpha()
+ assert not bytearray(b'hello2').isalpha()
+ assert bytearray(b'hello2').isalnum()
+ assert bytearray(b'1234').isdigit()
+ assert bytearray(b' ').isspace()
+ assert bytearray(b'Abc').istitle()
- assert bytearray('hello').count('l') == 2
- assert bytearray('hello').count(bytearray('l')) == 2
- assert bytearray('hello').count(memoryview('l')) == 2
- assert bytearray('hello').count(ord('l')) == 2
+ assert bytearray(b'hello').count(b'l') == 2
+ assert bytearray(b'hello').count(bytearray(b'l')) == 2
+ assert bytearray(b'hello').count(memoryview(b'l')) == 2
+ assert bytearray(b'hello').count(ord('l')) == 2
- assert bytearray('hello').index('e') == 1
- assert bytearray('hello').rindex('l') == 3
- assert bytearray('hello').index(bytearray('e')) == 1
- assert bytearray('hello').find('l') == 2
- assert bytearray('hello').rfind('l') == 3
+ assert bytearray(b'hello').index(b'e') == 1
+ assert bytearray(b'hello').rindex(b'l') == 3
+ assert bytearray(b'hello').index(bytearray(b'e')) == 1
+ assert bytearray(b'hello').find(b'l') == 2
+ assert bytearray(b'hello').rfind(b'l') == 3
# these checks used to not raise in pypy but they should
- raises(TypeError, bytearray('hello').index, ord('e'))
- raises(TypeError, bytearray('hello').rindex, ord('e'))
- raises(TypeError, bytearray('hello').find, ord('e'))
- raises(TypeError, bytearray('hello').rfind, ord('e'))
+ raises(TypeError, bytearray(b'hello').index, ord('e'))
+ raises(TypeError, bytearray(b'hello').rindex, ord('e'))
+ raises(TypeError, bytearray(b'hello').find, ord('e'))
+ raises(TypeError, bytearray(b'hello').rfind, ord('e'))
- assert bytearray('hello').startswith('he')
- assert bytearray('hello').startswith(bytearray('he'))
- assert bytearray('hello').startswith(('lo', bytearray('he')))
- assert bytearray('hello').endswith('lo')
- assert bytearray('hello').endswith(bytearray('lo'))
- assert bytearray('hello').endswith((bytearray('lo'), 'he'))
+ assert bytearray(b'hello').startswith(b'he')
+ assert bytearray(b'hello').startswith(bytearray(b'he'))
+ assert bytearray(b'hello').startswith((b'lo', bytearray(b'he')))
+ assert bytearray(b'hello').endswith(b'lo')
+ assert bytearray(b'hello').endswith(bytearray(b'lo'))
+ assert bytearray(b'hello').endswith((bytearray(b'lo'), b'he'))
def test_stringlike_conversions(self):
# methods that should return bytearray (and not str)
@@ -191,27 +186,27 @@
assert result == expected
assert type(result) is bytearray
- check(bytearray('abc').replace('b', bytearray('d')), 'adc')
- check(bytearray('abc').replace('b', 'd'), 'adc')
+ check(bytearray(b'abc').replace(b'b', bytearray(b'd')), b'adc')
+ check(bytearray(b'abc').replace(b'b', b'd'), b'adc')
- check(bytearray('abc').upper(), 'ABC')
- check(bytearray('ABC').lower(), 'abc')
- check(bytearray('abc').title(), 'Abc')
- check(bytearray('AbC').swapcase(), 'aBc')
- check(bytearray('abC').capitalize(), 'Abc')
+ check(bytearray(b'abc').upper(), b'ABC')
+ check(bytearray(b'ABC').lower(), b'abc')
+ check(bytearray(b'abc').title(), b'Abc')
+ check(bytearray(b'AbC').swapcase(), b'aBc')
+ check(bytearray(b'abC').capitalize(), b'Abc')
- check(bytearray('abc').ljust(5), 'abc ')
- check(bytearray('abc').rjust(5), ' abc')
- check(bytearray('abc').center(5), ' abc ')
- check(bytearray('1').zfill(5), '00001')
- check(bytearray('1\t2').expandtabs(5), '1 2')
+ check(bytearray(b'abc').ljust(5), b'abc ')
+ check(bytearray(b'abc').rjust(5), b' abc')
+ check(bytearray(b'abc').center(5), b' abc ')
+ check(bytearray(b'1').zfill(5), b'00001')
+ check(bytearray(b'1\t2').expandtabs(5), b'1 2')
- check(bytearray(',').join(['a', bytearray('b')]), 'a,b')
- check(bytearray('abca').lstrip('a'), 'bca')
- check(bytearray('cabc').rstrip('c'), 'cab')
- check(bytearray('abc').lstrip(memoryview('a')), 'bc')
- check(bytearray('abc').rstrip(memoryview('c')), 'ab')
- check(bytearray('aba').strip('a'), 'b')
+ check(bytearray(b',').join([b'a', bytearray(b'b')]), b'a,b')
+ check(bytearray(b'abca').lstrip(b'a'), b'bca')
+ check(bytearray(b'cabc').rstrip(b'c'), b'cab')
+ check(bytearray(b'abc').lstrip(memoryview(b'a')), b'bc')
+ check(bytearray(b'abc').rstrip(memoryview(b'c')), b'ab')
+ check(bytearray(b'aba').strip(b'a'), b'b')
def test_split(self):
# methods that should return a sequence of bytearrays
@@ -219,66 +214,67 @@
assert result == expected
assert set(type(x) for x in result) == set([bytearray])
- b = bytearray('mississippi')
- check(b.split('i'), ['m', 'ss', 'ss', 'pp', ''])
- check(b.split(memoryview('i')), ['m', 'ss', 'ss', 'pp', ''])
- check(b.rsplit('i'), ['m', 'ss', 'ss', 'pp', ''])
- check(b.rsplit(memoryview('i')), ['m', 'ss', 'ss', 'pp', ''])
- check(b.rsplit('i', 2), ['mississ', 'pp', ''])
+ b = bytearray(b'mississippi')
+ check(b.split(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
+ check(b.split(memoryview(b'i')), [b'm', b'ss', b'ss', b'pp', b''])
+ check(b.rsplit(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
+ check(b.rsplit(memoryview(b'i')), [b'm', b'ss', b'ss', b'pp', b''])
+ check(b.rsplit(b'i', 2), [b'mississ', b'pp', b''])
- check(bytearray('foo bar').split(), ['foo', 'bar'])
- check(bytearray('foo bar').split(None), ['foo', 'bar'])
+ check(bytearray(b'foo bar').split(), [b'foo', b'bar'])
+ check(bytearray(b'foo bar').split(None), [b'foo', b'bar'])
- check(b.partition('ss'), ('mi', 'ss', 'issippi'))
- check(b.partition(memoryview('ss')), ('mi', 'ss', 'issippi'))
- check(b.rpartition('ss'), ('missi', 'ss', 'ippi'))
- check(b.rpartition(memoryview('ss')), ('missi', 'ss', 'ippi'))
+ check(b.partition(b'ss'), (b'mi', b'ss', b'issippi'))
+ check(b.partition(memoryview(b'ss')), (b'mi', b'ss', b'issippi'))
+ check(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi'))
+ check(b.rpartition(memoryview(b'ss')), (b'missi', b'ss', b'ippi'))
def test_append(self):
- b = bytearray('abc')
- b.append('d')
+ b = bytearray(b'abc')
+ b.append(ord('d'))
b.append(ord('e'))
- assert b == 'abcde'
+ assert b == b'abcde'
def test_insert(self):
- b = bytearray('abc')
- b.insert(0, 'd')
- assert b == bytearray('dabc')
+ b = bytearray(b'abc')
+ b.insert(0, ord('d'))
+ assert b == bytearray(b'dabc')
b.insert(-1, ord('e'))
- assert b == bytearray('dabec')
+ assert b == bytearray(b'dabec')
- b.insert(6, 'f')
- assert b == bytearray('dabecf')
+ b.insert(6, ord('f'))
+ assert b == bytearray(b'dabecf')
- b.insert(1, 'g')
- assert b == bytearray('dgabecf')
+ b.insert(1, ord('g'))
+ assert b == bytearray(b'dgabecf')
- b.insert(-12, 'h')
- assert b == bytearray('hdgabecf')
+ b.insert(-12, ord('h'))
+ assert b == bytearray(b'hdgabecf')
- raises(ValueError, b.insert, 1, 'go')
- raises(TypeError, b.insert, 'g', 'o')
+ raises(TypeError, b.insert, 1, 'g')
+ raises(TypeError, b.insert, 1, b'g')
+ raises(TypeError, b.insert, b'g', b'o')
def test_pop(self):
- b = bytearray('world')
+ b = bytearray(b'world')
assert b.pop() == ord('d')
assert b.pop(0) == ord('w')
assert b.pop(-2) == ord('r')
raises(IndexError, b.pop, 10)
raises(OverflowError, bytearray().pop)
- assert bytearray('\xff').pop() == 0xff
+ assert bytearray(b'\xff').pop() == 0xff
def test_remove(self):
class Indexable:
def __index__(self):
return ord('e')
- b = bytearray('hello')
+ b = bytearray(b'hello')
b.remove(ord('l'))
- assert b == 'helo'
+ assert b == b'helo'
b.remove(ord('l'))
- assert b == 'heo'
+ assert b == b'heo'
raises(ValueError, b.remove, ord('l'))
raises(ValueError, b.remove, 400)
raises(TypeError, b.remove, u'e')
@@ -286,49 +282,49 @@
# remove first and last
b.remove(ord('o'))
b.remove(ord('h'))
- assert b == 'e'
+ assert b == b'e'
raises(TypeError, b.remove, u'e')
b.remove(Indexable())
- assert b == ''
+ assert b == b''
def test_reverse(self):
- b = bytearray('hello')
+ b = bytearray(b'hello')
b.reverse()
- assert b == bytearray('olleh')
+ assert b == bytearray(b'olleh')
def test_delitem(self):
- b = bytearray('abc')
+ b = bytearray(b'abc')
del b[1]
- assert b == bytearray('ac')
+ assert b == bytearray(b'ac')
del b[1:1]
- assert b == bytearray('ac')
+ assert b == bytearray(b'ac')
del b[:]
assert b == bytearray()
- b = bytearray('fooble')
+ b = bytearray(b'fooble')
del b[::2]
- assert b == bytearray('obe')
+ assert b == bytearray(b'obe')
def test_iadd(self):
- b = bytearray('abc')
- b += 'def'
- assert b == 'abcdef'
+ b = bytearray(b'abc')
+ b += b'def'
+ assert b == b'abcdef'
assert isinstance(b, bytearray)
raises(TypeError, b.__iadd__, u"")
def test_add(self):
- b1 = bytearray("abc")
- b2 = bytearray("def")
+ b1 = bytearray(b"abc")
+ b2 = bytearray(b"def")
def check(a, b, expected):
result = a + b
assert result == expected
assert isinstance(result, bytearray)
- check(b1, b2, "abcdef")
- check(b1, "def", "abcdef")
- check("def", b1, "defabc")
- check(b1, memoryview("def"), "abcdef")
+ check(b1, b2, b"abcdef")
+ check(b1, b"def", b"abcdef")
+ check(b"def", b1, b"defabc")
+ check(b1, memoryview(b"def"), b"abcdef")
raises(TypeError, lambda: b1 + u"def")
raises(TypeError, lambda: u"abc" + b2)
@@ -340,9 +336,8 @@
b = bytearray([0x1a, 0x2b, 0x30])
assert bytearray.fromhex('1a2B30') == b
- assert bytearray.fromhex(u'1a2B30') == b
- assert bytearray.fromhex(u' 1A 2B 30 ') == b
- assert bytearray.fromhex(u'0000') == '\0\0'
+ assert bytearray.fromhex(' 1A 2B 30 ') == b
+ assert bytearray.fromhex('0000') == b'\0\0'
raises(ValueError, bytearray.fromhex, u'a')
raises(ValueError, bytearray.fromhex, u'A')
@@ -350,92 +345,92 @@
raises(ValueError, bytearray.fromhex, u'1a b cd')
raises(ValueError, bytearray.fromhex, u'\x00')
raises(ValueError, bytearray.fromhex, u'12 \x00 34')
- raises(UnicodeEncodeError, bytearray.fromhex, u'\u1234')
+ raises(ValueError, bytearray.fromhex, u'\u1234')
def test_extend(self):
- b = bytearray('abc')
- b.extend(bytearray('def'))
- b.extend('ghi')
- assert b == 'abcdefghi'
- b.extend(buffer('jkl'))
- assert b == 'abcdefghijkl'
+ b = bytearray(b'abc')
+ b.extend(bytearray(b'def'))
+ b.extend(b'ghi')
+ assert b == b'abcdefghi'
+ b.extend(buffer(b'jkl'))
+ assert b == b'abcdefghijkl'
- b = bytearray('world')
+ b = bytearray(b'world')
b.extend([ord(c) for c in 'hello'])
- assert b == bytearray('worldhello')
+ assert b == bytearray(b'worldhello')
- b = bytearray('world')
- b.extend(list('hello'))
- assert b == bytearray('worldhello')
+ b = bytearray(b'world')
+ b.extend(list(b'hello'))
+ assert b == bytearray(b'worldhello')
- b = bytearray('world')
- b.extend(c for c in 'hello')
- assert b == bytearray('worldhello')
+ b = bytearray(b'world')
+ b.extend(c for c in b'hello')
+ assert b == bytearray(b'worldhello')
- raises(ValueError, b.extend, ['fish'])
+ raises(TypeError, b.extend, [b'fish'])
raises(ValueError, b.extend, [256])
raises(TypeError, b.extend, object())
raises(TypeError, b.extend, [object()])
- raises(TypeError, b.extend, u"unicode")
+ raises(TypeError, b.extend, "unicode")
def test_setslice(self):
- b = bytearray('hello')
+ b = bytearray(b'hello')
b[:] = [ord(c) for c in 'world']
- assert b == bytearray('world')
+ assert b == bytearray(b'world')
- b = bytearray('hello world')
- b[::2] = 'bogoff'
- assert b == bytearray('beolg ooflf')
+ b = bytearray(b'hello world')
+ b[::2] = b'bogoff'
+ assert b == bytearray(b'beolg ooflf')
def set_wrong_size():
- b[::2] = 'foo'
+ b[::2] = b'foo'
raises(ValueError, set_wrong_size)
def test_delitem_slice(self):
- b = bytearray('abcdefghi')
+ b = bytearray(b'abcdefghi')
del b[5:8]
- assert b == 'abcdei'
+ assert b == b'abcdei'
del b[:3]
- assert b == 'dei'
+ assert b == b'dei'
- b = bytearray('hello world')
+ b = bytearray(b'hello world')
del b[::2]
- assert b == bytearray('el ol')
+ assert b == bytearray(b'el ol')
def test_setitem(self):
- b = bytearray('abcdefghi')
- b[1] = 'B'
- assert b == 'aBcdefghi'
+ b = bytearray(b'abcdefghi')
+ b[1] = ord('B')
+ assert b == b'aBcdefghi'
def test_setitem_slice(self):
- b = bytearray('abcdefghi')
- b[0:3] = 'ABC'
- assert b == 'ABCdefghi'
- b[3:3] = '...'
- assert b == 'ABC...defghi'
- b[3:6] = '()'
- assert b == 'ABC()defghi'
- b[6:6] = '<<'
- assert b == 'ABC()d<<efghi'
+ b = bytearray(b'abcdefghi')
+ b[0:3] = b'ABC'
+ assert b == b'ABCdefghi'
+ b[3:3] = b'...'
+ assert b == b'ABC...defghi'
+ b[3:6] = b'()'
+ assert b == b'ABC()defghi'
+ b[6:6] = b'<<'
+ assert b == b'ABC()d<<efghi'
def test_buffer(self):
- b = bytearray('abcdefghi')
+ b = bytearray(b'abcdefghi')
buf = buffer(b)
- assert buf[2] == 'c'
- buf[3] = 'D'
- assert b == 'abcDefghi'
- buf[4:6] = 'EF'
- assert b == 'abcDEFghi'
+ assert buf[2] == b'c'
+ buf[3] = b'D'
+ assert b == b'abcDefghi'
+ buf[4:6] = b'EF'
+ assert b == b'abcDEFghi'
def test_decode(self):
- b = bytearray('abcdefghi')
+ b = bytearray(b'abcdefghi')
u = b.decode('utf-8')
- assert isinstance(u, unicode)
+ assert isinstance(u, str)
assert u == u'abcdefghi'
def test_int(self):
- assert int(bytearray('-1234')) == -1234
+ assert int(bytearray(b'-1234')) == -1234
def test_reduce(self):
- assert bytearray('caf\xe9').__reduce__() == (
+ assert bytearray(b'caf\xe9').__reduce__() == (
bytearray, (u'caf\xe9', 'latin-1'), None)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit