Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r49978:d5cb1a5800c3
Date: 2011-11-29 22:04 +0100
http://bitbucket.org/pypy/pypy/changeset/d5cb1a5800c3/
Log: (chronitis) String tests should test (bytes) strings. Also make sure
that the fillchar character in bytes.rjust() cannot be a unicode
string.
diff --git a/pypy/interpreter/gateway.py b/pypy/interpreter/gateway.py
--- a/pypy/interpreter/gateway.py
+++ b/pypy/interpreter/gateway.py
@@ -832,7 +832,7 @@
defs_w.append(None)
else:
spec = unwrap_spec[i]
- if spec in ['bufferstr']:
+ if isinstance(val, str) and spec not in [str]:
defs_w.append(space.wrapbytes(val))
else:
defs_w.append(space.wrap(val))
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
@@ -389,7 +389,7 @@
def str_rjust__String_ANY_ANY(space, w_self, w_arg, w_fillchar):
u_arg = space.int_w(w_arg)
u_self = w_self._value
- fillchar = space.str_w(w_fillchar)
+ fillchar = space.bytes_w(w_fillchar)
if len(fillchar) != 1:
raise OperationError(space.w_TypeError,
space.wrap("rjust() argument 2 must be a single character"))
@@ -405,7 +405,7 @@
def str_ljust__String_ANY_ANY(space, w_self, w_arg, w_fillchar):
u_self = w_self._value
u_arg = space.int_w(w_arg)
- fillchar = space.str_w(w_fillchar)
+ fillchar = space.bytes_w(w_fillchar)
if len(fillchar) != 1:
raise OperationError(space.w_TypeError,
space.wrap("ljust() argument 2 must be a single character"))
@@ -614,7 +614,7 @@
def str_center__String_ANY_ANY(space, w_self, w_arg, w_fillchar):
u_self = w_self._value
u_arg = space.int_w(w_arg)
- fillchar = space.str_w(w_fillchar)
+ fillchar = space.bytes_w(w_fillchar)
if len(fillchar) != 1:
raise OperationError(space.w_TypeError,
space.wrap("center() argument 2 must be a single character"))
@@ -966,7 +966,7 @@
space.wrap("translation table must be 256 characters long"))
string = w_string._value
- deletechars = space.str_w(w_deletechars)
+ deletechars = space.bytes_w(w_deletechars)
if len(deletechars) == 0:
buf = StringBuilder(len(string))
for char in string:
diff --git a/pypy/objspace/std/strjoinobject.py
b/pypy/objspace/std/strjoinobject.py
--- a/pypy/objspace/std/strjoinobject.py
+++ b/pypy/objspace/std/strjoinobject.py
@@ -29,7 +29,7 @@
def unwrap(w_self, space):
return w_self.force()
- str_w = unwrap
+ bytes_w = unwrap
registerimplementation(W_StringJoinObject)
@@ -51,7 +51,7 @@
def add__StringJoin_String(space, w_self, w_other):
if len(w_self.joined_strs) > w_self.until:
w_self.force(True)
- other = space.str_w(w_other)
+ other = space.bytes_w(w_other)
w_self.joined_strs.append(other)
return W_StringJoinObject(w_self.joined_strs)
diff --git a/pypy/objspace/std/test/test_strbufobject.py
b/pypy/objspace/std/test/test_strbufobject.py
--- a/pypy/objspace/std/test/test_strbufobject.py
+++ b/pypy/objspace/std/test/test_strbufobject.py
@@ -12,36 +12,36 @@
import __pypy__
# cannot do "Hello, " + "World!" because cpy2.5 optimises this
# away on AST level
- s = "Hello, ".__add__("World!")
- assert type(s) is str
+ s = b"Hello, ".__add__(b"World!")
+ assert type(s) is bytes
assert 'W_StringBufferObject' in __pypy__.internal_repr(s)
def test_add_twice(self):
- x = "a".__add__("b")
- y = x + "c"
- c = x + "d"
- assert y == "abc"
- assert c == "abd"
+ x = b"a".__add__(b"b")
+ y = x + b"c"
+ c = x + b"d"
+ assert y == b"abc"
+ assert c == b"abd"
def test_add(self):
import __pypy__
- all = ""
+ all = b""
for i in range(20):
- all += str(i)
+ all += str(i).encode()
assert 'W_StringBufferObject' in __pypy__.internal_repr(all)
- assert all == "012345678910111213141516171819"
+ assert all == b"012345678910111213141516171819"
def test_hash(self):
import __pypy__
def join(s): return s[:len(s) // 2] + s[len(s) // 2:]
- t = 'a' * 101
+ t = b'a' * 101
s = join(t)
assert 'W_StringBufferObject' in __pypy__.internal_repr(s)
assert hash(s) == hash(t)
def test_len(self):
- s = "a".__add__("b")
- r = "c".__add__("d")
+ s = b"a".__add__(b"b")
+ r = b"c".__add__(b"d")
t = s + r
assert len(s) == 2
assert len(r) == 2
@@ -49,30 +49,30 @@
def test_add_strbuf(self):
# make three strbuf objects
- s = 'a'.__add__('b')
- t = 'x'.__add__('c')
- u = 'y'.__add__('d')
+ s = b'a'.__add__(b'b')
+ t = b'x'.__add__(b'c')
+ u = b'y'.__add__(b'd')
# add two different strbufs to the same string
v = s + t
w = s + u
# check that insanity hasn't resulted.
- assert v == "abxc"
- assert w == "abyd"
+ assert v == b"abxc"
+ assert w == b"abyd"
def test_more_adding_fun(self):
- s = 'a'.__add__('b') # s is a strbuf now
- t = s + 'c'
- u = s + 'd'
- v = s + 'e'
- assert v == 'abe'
- assert u == 'abd'
- assert t == 'abc'
+ s = b'a'.__add__(b'b') # s is a strbuf now
+ t = s + b'c'
+ u = s + b'd'
+ v = s + b'e'
+ assert v == b'abe'
+ assert u == b'abd'
+ assert t == b'abc'
def test_buh_even_more(self):
- a = 'a'.__add__('b')
- b = a + 'c'
- c = '0'.__add__('1')
+ a = b'a'.__add__(b'b')
+ b = a + b'c'
+ c = b'0'.__add__(b'1')
x = c + a
- assert x == '01ab'
+ assert x == b'01ab'
diff --git a/pypy/objspace/std/test/test_stringobject.py
b/pypy/objspace/std/test/test_stringobject.py
--- a/pypy/objspace/std/test/test_stringobject.py
+++ b/pypy/objspace/std/test/test_stringobject.py
@@ -199,8 +199,9 @@
assert b'abc'.rjust(6) == b' abc'
assert b'abc'.rjust(3) == b'abc'
assert b'abc'.rjust(2) == b'abc'
- assert b'abc'.rjust(5, '*') == b'**abc' # Python 2.4
- raises(TypeError, b'abc'.rjust, 5, 'xx')
+ assert b'abc'.rjust(5, b'*') == b'**abc' # Python 2.4
+ raises(TypeError, b'abc'.rjust, 5, '*')
+ raises(TypeError, b'abc'.rjust, 5, b'xx')
def test_ljust(self):
s = b"abc"
@@ -212,8 +213,9 @@
assert b'abc'.ljust(6) == b'abc '
assert b'abc'.ljust(3) == b'abc'
assert b'abc'.ljust(2) == b'abc'
- assert b'abc'.ljust(5, '*') == b'abc**' # Python 2.4
- raises(TypeError, b'abc'.ljust, 6, '')
+ assert b'abc'.ljust(5, b'*') == b'abc**' # Python 2.4
+ raises(TypeError, b'abc'.ljust, 5, '*')
+ raises(TypeError, b'abc'.ljust, 6, b'')
def test_replace(self):
assert b'one!two!three!'.replace(b'!', b'@', 1) == b'one@two!three!'
@@ -279,8 +281,8 @@
assert b'abc'.center(6) == b' abc '
assert b'abc'.center(3) == b'abc'
assert b'abc'.center(2) == b'abc'
- assert b'abc'.center(5, '*') == b'*abc*' # Python 2.4
- raises(TypeError, b'abc'.center, 4, 'cba')
+ assert b'abc'.center(5, b'*') == b'*abc*' # Python 2.4
+ raises(TypeError, b'abc'.center, 4, b'cba')
assert b' abc'.center(7) == b' abc '
def test_count(self):
@@ -597,19 +599,20 @@
return tbl
table = maketrans(b'abc', b'xyz')
- assert b'xyzxyz' == b'xyzabcdef'.translate(table, 'def')
- assert b'xyzxyz' == b'xyzabcdef'.translate(memoryview(table), 'def')
+ assert b'xyzxyz' == b'xyzabcdef'.translate(table, b'def')
+ assert b'xyzxyz' == b'xyzabcdef'.translate(memoryview(table), b'def')
table = maketrans(b'a', b'A')
assert b'Abc' == b'abc'.translate(table)
assert b'xyz' == b'xyz'.translate(table)
- assert b'yz' == b'xyz'.translate(table, 'x')
+ assert b'yz' == b'xyz'.translate(table, b'x')
+ raises(TypeError, b'xyz'.translate, table, 'x')
- raises(ValueError, b'xyz'.translate, b'too short', 'strip')
+ raises(ValueError, b'xyz'.translate, b'too short', b'strip')
raises(ValueError, b'xyz'.translate, b'too short')
raises(ValueError, b'xyz'.translate, b'too long'*33)
- assert b'yz' == b'xyz'.translate(None, 'x') # 2.6
+ assert b'yz' == b'xyz'.translate(None, b'x') # 2.6
def test_iter(self):
l=[]
diff --git a/pypy/objspace/std/test/test_strjoinobject.py
b/pypy/objspace/std/test/test_strjoinobject.py
--- a/pypy/objspace/std/test/test_strjoinobject.py
+++ b/pypy/objspace/std/test/test_strjoinobject.py
@@ -12,21 +12,21 @@
import __pypy__
# cannot do "Hello, " + "World!" because cpy2.5 optimises this
# away on AST level (no idea why it doesn't this one)
- s = "Hello, ".__add__("World!")
- assert type(s) is str
+ s = b"Hello, ".__add__(b"World!")
+ assert type(s) is bytes
assert 'W_StringJoinObject' in __pypy__.internal_repr(s)
def test_add_twice(self):
- x = "a" + ""
- y = x + "b"
- c = x + "b"
- assert c == "ab"
+ x = b"a" + b""
+ y = x + b"b"
+ c = x + b"b"
+ assert c == b"ab"
def test_add(self):
import __pypy__
- all = ""
+ all = b""
for i in range(20):
- all += str(i)
+ all += str(i).encode()
assert 'W_StringJoinObject' in __pypy__.internal_repr(all)
def test_hash(self):
@@ -35,21 +35,21 @@
# (but don't go checking CPython's special case -1)
# disabled: assert hash('') == 0 --- different special case
def join(s): return s[:len(s) // 2] + s[len(s) // 2:]
- s = join('a' * 101)
+ s = join(b'a' * 101)
assert 'W_StringJoinObject' in __pypy__.internal_repr(s)
assert hash(s) & 0x7fffffff == 0x7e0bce58
def test_len(self):
- s = "a" + "b"
- r = "c" + "d"
+ s = b"a" + b"b"
+ r = b"c" + b"d"
t = s + r
assert len(s) == 2
def test_add_strjoin_strjoin(self):
# make three strjoin objects
- s = 'a' + 'b'
- t = 'c' + 'd'
- u = 'e' + 'f'
+ s = b'a' + b'b'
+ t = b'c' + b'd'
+ u = b'e' + b'f'
# add two different strjoins to the same string
v = s + t
@@ -59,16 +59,16 @@
assert len(v) == len(w) == 4
def test_more_adding_fun(self):
- s = 'a' + 'b' # s is a strjoin now
- t = s + 'c' # this calls s.force() which sets s.until to 1
- u = s + 'd'
- v = s + 'e'
- assert v == 'abe' # meaning u is abcd
+ s = b'a' + b'b' # s is a strjoin now
+ t = s + b'c' # this calls s.force() which sets s.until to 1
+ u = s + b'd'
+ v = s + b'e'
+ assert v == b'abe' # meaning u is abcd
def test_buh_even_more(self):
- a = 'a' + 'b'
- b = a + 'c'
- c = '0' + '1'
+ a = b'a' + b'b'
+ b = a + b'c'
+ c = b'0' + b'1'
x = c + a
- assert x == '01ab'
+ assert x == b'01ab'
diff --git a/pypy/objspace/std/test/test_strsliceobject.py
b/pypy/objspace/std/test/test_strsliceobject.py
--- a/pypy/objspace/std/test/test_strsliceobject.py
+++ b/pypy/objspace/std/test/test_strsliceobject.py
@@ -17,88 +17,88 @@
def test_basic(self):
import __pypy__
def slice(s): return (s*3)[len(s):-len(s)]
- s = slice('0123456789' * 20)
+ s = slice(b'0123456789' * 20)
assert len(s) == 200
assert self.not_forced(s)
- assert s[5] == '5'
- assert s[-2] == '8'
- assert s[3:7] == '3456'
+ assert s[5] == b'5'
+ assert s[-2] == b'8'
+ assert s[3:7] == b'3456'
assert 'W_StringSliceObject' in __pypy__.internal_repr(s)
# when the slice is too short, don't use the slice string object
- assert 'W_StringObject' in __pypy__.internal_repr("abcdefgh"[3:7])
+ assert 'W_StringObject' in __pypy__.internal_repr(b"abcdefgh"[3:7])
s2 = s.upper()
assert not self.not_forced(s)
def test_find(self):
import __pypy__
def slice(s): return (s*3)[len(s):-len(s)]
- s = slice('abcdefghiabc' + "X" * 100)
+ s = slice(b'abcdefghiabc' + b"X" * 100)
assert 'W_StringSliceObject' in __pypy__.internal_repr(s)
- assert slice('abcdefghiabc' + 'X' * 100) == 'abcdefghiabc' + 'X' * 100
- res = s.find('abc')
+ assert slice(b'abcdefghiabc' + b'X' * 100) == b'abcdefghiabc' + b'X' *
100
+ res = s.find(b'abc')
assert res == 0
- assert s.find('abc', 1) == 9
- assert s.find('def', 4) == -1
+ assert s.find(b'abc', 1) == 9
+ assert s.find(b'def', 4) == -1
def test_index(self):
import __pypy__, sys
m = sys.maxint
def slice(s): return (s*3)[len(s):-len(s)]
- s = slice('abcdefghiabc' * 20)
+ s = slice(b'abcdefghiabc' * 20)
assert 'W_StringSliceObject' in __pypy__.internal_repr(s)
- assert s.index('') == 0
- assert s.index('def') == 3
- assert s.index('abc') == 0
- assert s.index('abc', 1) == 9
- assert s.index('def', -4*m, 4*m) == 3
- raises(ValueError, s.index, 'hib')
- raises(ValueError, slice('abcdefghiab' + "X" * 100).index, 'abc', 1)
- raises(ValueError, slice('abcdefghi' + "X" * 20).index, 'ghi', 8)
- raises(ValueError, slice('abcdefghi' + "X" * 20).index, 'ghi', -1)
- raises(TypeError, slice('abcdefghijklmn' * 20).index, 'abc', 0, 0.0)
- raises(TypeError, slice('abcdefghijklmn' * 20).index, 'abc', -10.0, 30)
+ assert s.index(b'') == 0
+ assert s.index(b'def') == 3
+ assert s.index(b'abc') == 0
+ assert s.index(b'abc', 1) == 9
+ assert s.index(b'def', -4*m, 4*m) == 3
+ raises(ValueError, s.index, b'hib')
+ raises(ValueError, slice(b'abcdefghiab' + b"X" * 100).index, b'abc', 1)
+ raises(ValueError, slice(b'abcdefghi' + b"X" * 20).index, b'ghi', 8)
+ raises(ValueError, slice(b'abcdefghi' + b"X" * 20).index, b'ghi', -1)
+ raises(TypeError, slice(b'abcdefghijklmn' * 20).index, b'abc', 0, 0.0)
+ raises(TypeError, slice(b'abcdefghijklmn' * 20).index, b'abc', -10.0,
30)
def test_rfind(self):
import __pypy__
def slice(s): return (s*3)[len(s):-len(s)]
- s = slice('abcdefghiabc' + "X" * 100)
+ s = slice(b'abcdefghiabc' + b"X" * 100)
assert 'W_StringSliceObject' in __pypy__.internal_repr(s)
- assert s.rfind('abc') == 9
- assert s.rfind('') == 112
- assert s.rfind('abcd') == 0
- assert s.rfind('abcz') == -1
+ assert s.rfind(b'abc') == 9
+ assert s.rfind(b'') == 112
+ assert s.rfind(b'abcd') == 0
+ assert s.rfind(b'abcz') == -1
def test_rindex(self):
import __pypy__
from sys import maxint
def slice(s): return (s*3)[len(s):-len(s)]
- s = slice("X" * 100 + 'abcdefghiabc')
+ s = slice(b"X" * 100 + b'abcdefghiabc')
assert 'W_StringSliceObject' in __pypy__.internal_repr(s)
- assert s.rindex('') == 112
- assert s.rindex('def') == 103
- assert s.rindex('abc') == 109
- assert s.rindex('abc', 0, -1) == 100
- assert s.rindex('abc', -4*maxint, 4*maxint) == 109
- raises(ValueError, slice('abcdefghiabc' * 20).rindex, 'hib')
- raises(ValueError, slice('defghiabc' + "X" * 100).rindex, 'def', 1)
- raises(ValueError, slice('defghiabc' + "X" * 100).rindex, 'abc', 0,
-101)
- raises(ValueError, slice('abcdefghi' + "X" * 100).rindex, 'ghi', 0, 8)
- raises(ValueError, slice('abcdefghi' + "X" * 100).rindex, 'ghi', 0,
-101)
- raises(TypeError, slice('abcdefghijklmn' + "X" * 100).rindex,
- 'abc', 0, 0.0)
- raises(TypeError, slice('abcdefghijklmn' + "X" * 100).rindex,
- 'abc', -10.0, 30)
+ assert s.rindex(b'') == 112
+ assert s.rindex(b'def') == 103
+ assert s.rindex(b'abc') == 109
+ assert s.rindex(b'abc', 0, -1) == 100
+ assert s.rindex(b'abc', -4*maxint, 4*maxint) == 109
+ raises(ValueError, slice(b'abcdefghiabc' * 20).rindex, b'hib')
+ raises(ValueError, slice(b'defghiabc' + b"X" * 100).rindex, b'def', 1)
+ raises(ValueError, slice(b'defghiabc' + b"X" * 100).rindex, b'abc', 0,
-101)
+ raises(ValueError, slice(b'abcdefghi' + b"X" * 100).rindex, b'ghi', 0,
8)
+ raises(ValueError, slice(b'abcdefghi' + b"X" * 100).rindex, b'ghi', 0,
-101)
+ raises(TypeError, slice(b'abcdefghijklmn' + b"X" * 100).rindex,
+ b'abc', 0, 0.0)
+ raises(TypeError, slice(b'abcdefghijklmn' + b"X" * 100).rindex,
+ b'abc', -10.0, 30)
def test_contains(self):
import __pypy__
def slice(s): return (s*3)[len(s):-len(s)]
- s = slice("abc" + "X" * 100)
+ s = slice(b"abc" + b"X" * 100)
assert 'W_StringSliceObject' in __pypy__.internal_repr(s)
- assert '' in s
- assert 'a' in s
- assert 'ab' in s
- assert not 'd' in s
- raises(TypeError, slice('a' * 100).__contains__, 1)
+ assert b'' in s
+ assert b'a' in s
+ assert b'ab' in s
+ assert not b'd' in s
+ raises(TypeError, slice(b'a' * 100).__contains__, 1)
def test_hash(self):
import __pypy__
@@ -106,43 +106,43 @@
# (but don't go checking CPython's special case -1)
# disabled: assert hash('') == 0 --- different special case
def slice(s): return (s*3)[len(s):-len(s)]
- s = slice('a' * 101)
+ s = slice(b'a' * 101)
assert 'W_StringSliceObject' in __pypy__.internal_repr(s)
assert hash(s) & 0x7fffffff == 0x7e0bce58
def test_split_produces_strslices(self):
import __pypy__
- l = ("X" * 100 + "," + "Y" * 100).split(",")
+ l = (b"X" * 100 + b"," + b"Y" * 100).split(b",")
assert "W_StringSliceObject" in __pypy__.internal_repr(l[0])
assert "W_StringSliceObject" in __pypy__.internal_repr(l[1])
def test_strip_produces_strslices(self):
import __pypy__
- s = ("abc" + "X" * 100 + "," + "Y" * 100 + "abc").strip("abc")
+ s = (b"abc" + b"X" * 100 + b"," + b"Y" * 100 + b"abc").strip(b"abc")
assert "W_StringSliceObject" in __pypy__.internal_repr(s)
def test_splitlines_produces_strslices(self):
import __pypy__
- l = ("X" * 100 + "\n" + "Y" * 100).splitlines()
+ l = (b"X" * 100 + b"\n" + b"Y" * 100).splitlines()
assert "W_StringSliceObject" in __pypy__.internal_repr(l[0])
assert "W_StringSliceObject" in __pypy__.internal_repr(l[1])
def test_count_does_not_force(self):
def slice(s): return (s*3)[len(s):-len(s)]
- s = slice("X" * 100 + "\n" + "Y" * 100)
- assert s.count("X") == 100
- assert s.count("Y") == 100
+ s = slice(b"X" * 100 + b"\n" + b"Y" * 100)
+ assert s.count(b"X") == 100
+ assert s.count(b"Y") == 100
assert self.not_forced(s)
def test_extended_slice(self):
import __pypy__
def slice1(s): return (s*3)[len(s):-len(s)]
- s = slice1('0123456789' * 20)
+ s = slice1(b'0123456789' * 20)
assert len(s) == 200
assert self.not_forced(s)
t = s[::-1]
- assert t == '9876543210' * 20
+ assert t == b'9876543210' * 20
assert not self.not_forced(t)
u = s[slice(10, 20)]
assert self.not_forced(u)
- assert u == '0123456789'
+ assert u == b'0123456789'
diff --git a/pypy/objspace/std/unicodetype.py b/pypy/objspace/std/unicodetype.py
--- a/pypy/objspace/std/unicodetype.py
+++ b/pypy/objspace/std/unicodetype.py
@@ -35,7 +35,7 @@
doc='S.capitalize() -> unicode\n\nReturn a'
' capitalized version of S, i.e. make the first'
' character\nhave upper case.')
-unicode_center = SMM('center', 3, defaults=(' ',),
+unicode_center = SMM('center', 3, defaults=(u' ',),
doc='S.center(width[, fillchar]) -> unicode\n\nReturn'
' S centered in a Unicode string of length width.'
' Padding is\ndone using the specified fill'
@@ -112,7 +112,7 @@
' which is the concatenation of the strings in'
' the\nsequence. The separator between elements'
' is S.')
-unicode_ljust = SMM('ljust', 3, defaults=(' ',),
+unicode_ljust = SMM('ljust', 3, defaults=(u' ',),
doc='S.ljust(width[, fillchar]) -> int\n\nReturn S'
' left justified in a Unicode string of length'
' width. Padding is\ndone using the specified'
@@ -120,7 +120,7 @@
unicode_lower = SMM('lower', 1,
doc='S.lower() -> unicode\n\nReturn a copy of the'
' string S converted to lowercase.')
-unicode_rjust = SMM('rjust', 3, defaults=(' ',),
+unicode_rjust = SMM('rjust', 3, defaults=(u' ',),
doc='S.rjust(width[, fillchar]) -> unicode\n\nReturn'
' S right justified in a Unicode string of length'
' width. Padding is\ndone using the specified'
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit