| 1 |
|
| 2 |
import py, os |
| 3 |
try: |
| 4 |
from hypothesis import given, strategies, settings, example |
| 5 |
HAS_HYPOTHESIS = True |
| 6 |
except ImportError: |
| 7 |
HAS_HYPOTHESIS = False |
| 8 |
|
| 9 |
from rpython.rlib import rutf8 |
| 10 |
from pypy.interpreter.error import OperationError |
| 11 |
from pypy.objspace.std.unicodeobject import unicodedb |
| 12 |
|
| 13 |
if HAS_HYPOTHESIS: |
| 14 |
@strategies.composite |
| 15 |
def random_split_input(draw): |
| 16 |
def make_spaces(): |
| 17 |
spaces = draw(strategies.text(strategies.characters(whitelist_categories=['Zs']), min_size=1)) |
| 18 |
|
| 19 |
spaces = u"".join(c for c in spaces if unicodedb.isspace(ord(c))) |
| 20 |
if not spaces: |
| 21 |
spaces = u" " |
| 22 |
return spaces |
| 23 |
|
| 24 |
length = draw(strategies.integers(min_value=0, max_value=20)) |
| 25 |
res_list = [] |
| 26 |
all_list = [] |
| 27 |
for i in range(length): |
| 28 |
all_list.append(make_spaces()) |
| 29 |
next_non_space = draw(strategies.text(min_size=1)) |
| 30 |
next_non_space = u"".join(c for c in next_non_space if not unicodedb.isspace(ord(c))) |
| 31 |
if next_non_space: |
| 32 |
all_list.append(next_non_space) |
| 33 |
res_list.append(next_non_space) |
| 34 |
if draw(strategies.booleans()): |
| 35 |
all_list.append(make_spaces()) |
| 36 |
return u"".join(all_list), res_list |
| 37 |
|
| 38 |
|
| 39 |
class TestUnicodeObject: |
| 40 |
spaceconfig = dict(usemodules=('unicodedata',)) |
| 41 |
|
| 42 |
def test_unicode_to_decimal_w(self, space): |
| 43 |
from pypy.objspace.std.unicodeobject import unicode_to_decimal_w |
| 44 |
w_s = space.wrap(u"\N{EM SPACE}-3\N{EN SPACE}") |
| 45 |
s2 = unicode_to_decimal_w(space, w_s) |
| 46 |
assert s2 == " -3 " |
| 47 |
|
| 48 |
@py.test.mark.skipif("not config.option.runappdirect and sys.maxunicode == 0xffff") |
| 49 |
def test_unicode_to_decimal_w_wide(self, space): |
| 50 |
from pypy.objspace.std.unicodeobject import unicode_to_decimal_w |
| 51 |
w_s = space.wrap(u'\U0001D7CF\U0001D7CE') 𝟎 |
| 52 |
s2 = unicode_to_decimal_w(space, w_s) |
| 53 |
assert s2 == "10" |
| 54 |
|
| 55 |
def test_listview_ascii(self): |
| 56 |
w_str = self.space.newutf8('abcd', 4) |
| 57 |
assert self.space.listview_ascii(w_str) == list("abcd") |
| 58 |
|
| 59 |
def test_new_shortcut(self): |
| 60 |
space = self.space |
| 61 |
w_uni = self.space.newutf8('abcd', 4) |
| 62 |
w_new = space.call_method( |
| 63 |
space.w_unicode, "__new__", space.w_unicode, w_uni) |
| 64 |
assert w_new is w_uni |
| 65 |
|
| 66 |
def test_fast_iter(self): |
| 67 |
space = self.space |
| 68 |
w_uni = space.newutf8(u"aä".encode("utf-8"), 2) |
| 69 |
old_index_storage = w_uni._index_storage |
| 70 |
w_iter = space.iter(w_uni) |
| 71 |
w_char1 = w_iter.descr_next(space) |
| 72 |
w_char2 = w_iter.descr_next(space) |
| 73 |
py.test.raises(OperationError, w_iter.descr_next, space) |
| 74 |
assert w_uni._index_storage is old_index_storage |
| 75 |
assert space.eq_w(w_char1, w_uni._getitem_result(space, 0)) |
| 76 |
assert space.eq_w(w_char2, w_uni._getitem_result(space, 1)) |
| 77 |
|
| 78 |
|
| 79 |
if HAS_HYPOTHESIS: |
| 80 |
@given(u=strategies.text(), |
| 81 |
start=strategies.integers(min_value=0, max_value=10), |
| 82 |
len1=strategies.integers(min_value=-1, max_value=10)) |
| 83 |
def test_hypo_index_find(self, u, start, len1): |
| 84 |
space = self.space |
| 85 |
if start + len1 < 0: |
| 86 |
return |
| 87 |
v = u[start : start + len1] |
| 88 |
w_u = space.wrap(u) |
| 89 |
w_v = space.wrap(v) |
| 90 |
expected = u.find(v, start, start + len1) |
| 91 |
try: |
| 92 |
w_index = space.call_method(w_u, 'index', w_v, |
| 93 |
space.newint(start), |
| 94 |
space.newint(start + len1)) |
| 95 |
except OperationError as e: |
| 96 |
if not e.match(space, space.w_ValueError): |
| 97 |
raise |
| 98 |
assert expected == -1 |
| 99 |
else: |
| 100 |
assert space.int_w(w_index) == expected >= 0 |
| 101 |
|
| 102 |
w_index = space.call_method(w_u, 'find', w_v, |
| 103 |
space.newint(start), |
| 104 |
space.newint(start + len1)) |
| 105 |
assert space.int_w(w_index) == expected |
| 106 |
|
| 107 |
rexpected = u.rfind(v, start, start + len1) |
| 108 |
try: |
| 109 |
w_index = space.call_method(w_u, 'rindex', w_v, |
| 110 |
space.newint(start), |
| 111 |
space.newint(start + len1)) |
| 112 |
except OperationError as e: |
| 113 |
if not e.match(space, space.w_ValueError): |
| 114 |
raise |
| 115 |
assert rexpected == -1 |
| 116 |
else: |
| 117 |
assert space.int_w(w_index) == rexpected >= 0 |
| 118 |
|
| 119 |
w_index = space.call_method(w_u, 'rfind', w_v, |
| 120 |
space.newint(start), |
| 121 |
space.newint(start + len1)) |
| 122 |
assert space.int_w(w_index) == rexpected |
| 123 |
|
| 124 |
@given(random_split_input()) |
| 125 |
def test_hypo_split(self, inp): |
| 126 |
space = self.space |
| 127 |
input, expected = inp |
| 128 |
w_u = space.newtext(input.encode('utf8')) |
| 129 |
for methname in ('split', 'rsplit'): |
| 130 |
w_l = space.call_method(w_u, methname) |
| 131 |
l_w = space.unpackiterable(w_l) |
| 132 |
assert len(l_w) == len(expected) |
| 133 |
for i, w_elt in enumerate(l_w): |
| 134 |
assert space.text_w(w_elt) == expected[i].encode('utf8') |
| 135 |
for maxsplit in range(len(expected)): |
| 136 |
w_l = space.call_method(w_u, 'split', space.w_None, space.newint(maxsplit)) |
| 137 |
l_w = space.unpackiterable(w_l) |
| 138 |
assert len(l_w) == maxsplit + 1 |
| 139 |
for i, w_elt in enumerate(l_w[:-1]): |
| 140 |
assert space.text_w(w_elt) == expected[i].encode('utf8') |
| 141 |
assert input.encode('utf8').endswith(space.text_w(l_w[-1])) |
| 142 |
|
| 143 |
|
| 144 |
def test_getitem_constant_index_jit(self): |
| 145 |
|
| 146 |
space = self.space |
| 147 |
u = u"äöabc" |
| 148 |
w_u = self.space.wrap(u) |
| 149 |
for i in range(-len(u), len(u)): |
| 150 |
assert w_u._getitem_result_constant_index_jit(space, i)._utf8 == u[i].encode("utf-8") |
| 151 |
with py.test.raises(OperationError): |
| 152 |
w_u._getitem_result_constant_index_jit(space, len(u)) |
| 153 |
with py.test.raises(OperationError): |
| 154 |
w_u._getitem_result_constant_index_jit(space, -len(u) - 1) |
| 155 |
|
| 156 |
def test_getslice_constant_index_jit(self): |
| 157 |
space = self.space |
| 158 |
u = u"äöabcéééß" |
| 159 |
w_u = self.space.wrap(u) |
| 160 |
for start in range(0, 4): |
| 161 |
for end in range(start, len(u)): |
| 162 |
assert w_u._unicode_sliced_constant_index_jit(space, start, end)._utf8 == u[start: end].encode("utf-8") |
| 163 |
|
| 164 |
def test_lower_upper_ascii(self): |
| 165 |
from pypy.module.unicodedata.interp_ucd import unicodedb |
| 166 |
|
| 167 |
|
| 168 |
for ch in range(128): |
| 169 |
unilower, = unicodedb.tolower_full(ch) |
| 170 |
assert chr(unilower) == chr(ch).lower() |
| 171 |
uniupper, = unicodedb.toupper_full(ch) |
| 172 |
assert chr(uniupper) == chr(ch).upper() |
| 173 |
|
| 174 |
def test_latin1_ascii_encode_shortcut_ascii(self, monkeypatch): |
| 175 |
from rpython.rlib import rutf8 |
| 176 |
from pypy.objspace.std.unicodeobject import encode_object |
| 177 |
monkeypatch.setattr(rutf8, "check_ascii", None) |
| 178 |
w_b = encode_object(self.space, self.space.newutf8("abc", 3), "latin-1", "strict") |
| 179 |
assert self.space.bytes_w(w_b) == "abc" |
| 180 |
w_b = encode_object(self.space, self.space.newutf8("abc", 3), "ascii", "strict") |
| 181 |
assert self.space.bytes_w(w_b) == "abc" |
| 182 |
|
| 183 |
def test_utf8_ascii_encode_shortcut_ascii(self, monkeypatch): |
| 184 |
from rpython.rlib import rutf8 |
| 185 |
from pypy.objspace.std.unicodeobject import encode_object |
| 186 |
monkeypatch.setattr(rutf8, "check_utf8", None) |
| 187 |
for enc in ["utf-8", "UTF-8", "utf8"]: |
| 188 |
w_b = encode_object(self.space, self.space.newutf8("abc", 3), enc, "strict") |
| 189 |
assert self.space.bytes_w(w_b) == "abc" |
| 190 |
|
| 191 |
def test_split_shortcut_ascii(self, monkeypatch): |
| 192 |
from rpython.rlib import rutf8 |
| 193 |
monkeypatch.setattr(rutf8, "isspace", None) |
| 194 |
w_s = self.space.newutf8("a b c", 5) |
| 195 |
w_l = w_s.descr_split(self.space) |
| 196 |
assert self.space.len_w(w_l) == 3 |
| 197 |
|
| 198 |
|
| 199 |
class AppTestUnicodeStringStdOnly: |
| 200 |
def test_compares(self): |
| 201 |
assert type('a') != type(b'a') |
| 202 |
assert 'a' != b'a' |
| 203 |
assert b'a' != 'a' |
| 204 |
assert not ('a' == 5) |
| 205 |
assert 'a' != 5 |
| 206 |
raises(TypeError, "'a' < 5") |
| 207 |
raises(TypeError, "'a' < bytearray(b'a')") |
| 208 |
|
| 209 |
|
| 210 |
class AppTestUnicodeString: |
| 211 |
spaceconfig = dict(usemodules=('unicodedata',)) |
| 212 |
|
| 213 |
with open(os.path.join(os.path.dirname(__file__), 'startswith.py')) as f: |
| 214 |
exec 'def test_startswith_endswith_external(self): """%s"""\n' % ( |
| 215 |
f.read(),) |
| 216 |
|
| 217 |
def test_codecs_errors(self): |
| 218 |
|
| 219 |
raises(UnicodeError, 'Andr\202 x'.encode, 'ascii') |
| 220 |
raises(UnicodeError, 'Andr\202 x'.encode, 'ascii','strict') |
| 221 |
assert 'Andr\202 x'.encode('ascii','ignore') == b"Andr x" |
| 222 |
assert 'Andr\202 x'.encode('ascii','replace') == b"Andr? x" |
| 223 |
|
| 224 |
|
| 225 |
raises(UnicodeError, str, b'Andr\202 x', 'ascii') |
| 226 |
raises(UnicodeError, str, b'Andr\202 x', 'ascii','strict') |
| 227 |
assert str(b'Andr\202 x','ascii','ignore') == "Andr x" |
| 228 |
assert str(b'Andr\202 x','ascii','replace') == 'Andr\uFFFD x' |
| 229 |
|
| 230 |
|
| 231 |
assert b"\\N{foo}xx".decode("unicode-escape", "ignore") == "xx" |
| 232 |
|
| 233 |
|
| 234 |
raises(UnicodeError, b"\\".decode, "unicode-escape") |
| 235 |
|
| 236 |
raises(UnicodeError, b"\xc2".decode, "utf-8") |
| 237 |
assert b'\xe1\x80'.decode('utf-8', 'replace') == "\ufffd" |
| 238 |
|
| 239 |
@py.test.mark.skipif("not config.option.runappdirect and sys.maxunicode == 0xffff") |
| 240 |
def test_isprintable_wide(self): |
| 241 |
assert '\U0001F46F'.isprintable() |
| 242 |
assert not '\U000E0020'.isprintable() |
| 243 |
assert u'\ud800'.capitalize() == u'\ud800' |
| 244 |
assert u'xx\ud800'.capitalize() == u'Xx\ud800' |
| 245 |
|
| 246 |
|