| 1 |
|
| 2 |
|
| 3 |
from rpython.rtyper.lltypesystem.lltype import malloc |
| 4 |
from rpython.rtyper.lltypesystem.rstr import LLHelpers, UNICODE |
| 5 |
from rpython.rtyper.test.tool import BaseRtypingTest |
| 6 |
from rpython.rtyper.test.test_rstr import AbstractTestRstr |
| 7 |
import py |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class TestRUnicode(AbstractTestRstr, BaseRtypingTest): |
| 12 |
const = unicode |
| 13 |
constchar = unichr |
| 14 |
|
| 15 |
def test_unicode_explicit_conv(self): |
| 16 |
def f(x): |
| 17 |
return unicode(x) |
| 18 |
|
| 19 |
for v in ['x', u'x']: |
| 20 |
res = self.interpret(f, [v]) |
| 21 |
assert self.ll_to_unicode(res) == v |
| 22 |
|
| 23 |
def f(x): |
| 24 |
if x > 1: |
| 25 |
y = const('yxx') |
| 26 |
else: |
| 27 |
y = const('xx') |
| 28 |
return unicode(y) |
| 29 |
|
| 30 |
const = str |
| 31 |
assert self.ll_to_unicode(self.interpret(f, [1])) == f(1) |
| 32 |
|
| 33 |
def f(x): |
| 34 |
if x > 1: |
| 35 |
y = const('yxx') |
| 36 |
else: |
| 37 |
y = const('xx') |
| 38 |
return unicode(y) |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
const = unicode |
| 43 |
assert self.ll_to_unicode(self.interpret(f, [1])) == f(1) |
| 44 |
|
| 45 |
def test_str_unicode_const(self): |
| 46 |
def f(): |
| 47 |
return str(u'xxx') |
| 48 |
|
| 49 |
assert self.ll_to_string(self.interpret(f, [])) == 'xxx' |
| 50 |
|
| 51 |
def test_unicode_of_unicode(self): |
| 52 |
def f(x): |
| 53 |
return len(unicode(unichr(x) * 3)) |
| 54 |
assert self.interpret(f, [ord('a')]) == 3 |
| 55 |
assert self.interpret(f, [128]) == 3 |
| 56 |
assert self.interpret(f, [1000]) == 3 |
| 57 |
|
| 58 |
def test_unicode_of_unichar(self): |
| 59 |
def f(x): |
| 60 |
return len(unicode(unichr(x))) |
| 61 |
assert self.interpret(f, [ord('a')]) == 1 |
| 62 |
assert self.interpret(f, [128]) == 1 |
| 63 |
assert self.interpret(f, [1000]) == 1 |
| 64 |
|
| 65 |
def test_conversion_errors(self): |
| 66 |
py.test.skip("do we want this test to pass?") |
| 67 |
def f(x): |
| 68 |
if x: |
| 69 |
string = '\x80\x81' |
| 70 |
uni = u'\x80\x81' |
| 71 |
else: |
| 72 |
string = '\x82\x83' |
| 73 |
uni = u'\x83\x84\x84' |
| 74 |
try: |
| 75 |
str(uni) |
| 76 |
except UnicodeEncodeError: |
| 77 |
pass |
| 78 |
else: |
| 79 |
return -1 |
| 80 |
try: |
| 81 |
unicode(string) |
| 82 |
except UnicodeDecodeError: |
| 83 |
return len(string) + len(uni) |
| 84 |
else: |
| 85 |
return -2 |
| 86 |
assert f(True) == 4 |
| 87 |
assert f(False) == 5 |
| 88 |
res = self.interpret(f, [True]) |
| 89 |
assert res == 4 |
| 90 |
|
| 91 |
|
| 92 |
def test_str_unicode_nonconst(self): |
| 93 |
def f(x): |
| 94 |
y = u'xxx' + unichr(x) |
| 95 |
return str(y) |
| 96 |
|
| 97 |
assert self.ll_to_string(self.interpret(f, [38])) == f(38) |
| 98 |
self.interpret_raises(UnicodeEncodeError, f, [1234]) |
| 99 |
|
| 100 |
def test_unicode_encode(self): |
| 101 |
def f(n): |
| 102 |
x = u'xxx' + unichr(n) |
| 103 |
y = u'àèì' + unichr(n) |
| 104 |
z = u'美' + unichr(n) |
| 105 |
return x.encode('ascii') + y.encode('latin-1') + z.encode('utf-8') |
| 106 |
|
| 107 |
assert self.ll_to_string(self.interpret(f, [38])) == f(38) |
| 108 |
|
| 109 |
def g(n): |
| 110 |
x = u'\ud800' + unichr(n) |
| 111 |
return x.encode('utf-8') |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
assert self.ll_to_string(self.interpret(g, [38])) == g(38) |
| 120 |
|
| 121 |
def test_utf_8_encoding_annotation(self): |
| 122 |
from rpython.rlib.runicode import unicode_encode_utf_8 |
| 123 |
def errorhandler(errors, encoding, msg, u, |
| 124 |
startingpos, endingpos): |
| 125 |
raise UnicodeEncodeError(encoding, u, startingpos, endingpos, msg) |
| 126 |
def f(n): |
| 127 |
x = u'àèì' + unichr(n) |
| 128 |
if x: |
| 129 |
y = u'ìòé' |
| 130 |
else: |
| 131 |
y = u'òìàà' |
| 132 |
|
| 133 |
y = unicode_encode_utf_8(y, len(y), 'strict', errorhandler) |
| 134 |
return x.encode('utf-8') + y |
| 135 |
|
| 136 |
assert self.ll_to_string(self.interpret(f, [38])) == f(38) |
| 137 |
|
| 138 |
def test_unicode_encode_error(self): |
| 139 |
def f(x, which): |
| 140 |
if which: |
| 141 |
y = u'xxx' |
| 142 |
try: |
| 143 |
x = (y + unichr(x)).encode('ascii') |
| 144 |
return len(x) |
| 145 |
except UnicodeEncodeError: |
| 146 |
return -1 |
| 147 |
else: |
| 148 |
y = u'xxx' |
| 149 |
try: |
| 150 |
x = (y + unichr(x)).encode('latin-1') |
| 151 |
return len(x) |
| 152 |
except UnicodeEncodeError: |
| 153 |
return -1 |
| 154 |
|
| 155 |
assert self.interpret(f, [38, True]) == f(38, True) |
| 156 |
assert self.interpret(f, [138, True]) == f(138, True) |
| 157 |
assert self.interpret(f, [38, False]) == f(38, False) |
| 158 |
assert self.interpret(f, [138, False]) == f(138, False) |
| 159 |
assert self.interpret(f, [300, False]) == f(300, False) |
| 160 |
|
| 161 |
def test_unicode_decode(self): |
| 162 |
strings = ['xxx', u'àèì'.encode('latin-1'), u'美'.encode('utf-8')] |
| 163 |
def f(n): |
| 164 |
x = strings[n] |
| 165 |
y = strings[n+1] |
| 166 |
z = strings[n+2] |
| 167 |
return x.decode('ascii') + y.decode('latin-1') + z.decode('utf-8') |
| 168 |
|
| 169 |
assert self.ll_to_string(self.interpret(f, [0])) == f(0) |
| 170 |
|
| 171 |
def test_unicode_decode_final(self): |
| 172 |
strings = ['\xc3', ''] |
| 173 |
def f(n): |
| 174 |
try: |
| 175 |
strings[n].decode('utf-8') |
| 176 |
except UnicodeDecodeError: |
| 177 |
return True |
| 178 |
return False |
| 179 |
|
| 180 |
assert f(0) |
| 181 |
assert self.interpret(f, [0]) |
| 182 |
|
| 183 |
def test_utf_8_decoding_annotation(self): |
| 184 |
from rpython.rlib.runicode import str_decode_utf_8 |
| 185 |
def errorhandler(errors, encoding, msg, s, |
| 186 |
startingpos, endingpos): |
| 187 |
raise UnicodeDecodeError(encoding, s, startingpos, endingpos, msg) |
| 188 |
|
| 189 |
strings = [u'àèì'.encode('utf-8'), u'ìòéà'.encode('utf-8')] |
| 190 |
def f(n): |
| 191 |
x = strings[n] |
| 192 |
if n: |
| 193 |
errors = 'strict' |
| 194 |
else: |
| 195 |
errors = 'foo' |
| 196 |
|
| 197 |
y, _ = str_decode_utf_8(x, len(x), errors, errorhandler=errorhandler) |
| 198 |
return x.decode('utf-8') + y |
| 199 |
|
| 200 |
assert self.ll_to_string(self.interpret(f, [1])) == f(1) |
| 201 |
|
| 202 |
def test_unicode_decode_error(self): |
| 203 |
def f(x): |
| 204 |
y = 'xxx' |
| 205 |
try: |
| 206 |
x = (y + chr(x)).decode('ascii') |
| 207 |
return len(x) |
| 208 |
except UnicodeDecodeError: |
| 209 |
return -1 |
| 210 |
|
| 211 |
assert self.interpret(f, [38]) == f(38) |
| 212 |
assert self.interpret(f, [138]) == f(138) |
| 213 |
|
| 214 |
|
| 215 |
def test_unichar_const(self): |
| 216 |
def fn(c): |
| 217 |
return c |
| 218 |
assert self.interpret(fn, [u'\u03b1']) == u'\u03b1' |
| 219 |
|
| 220 |
def test_unichar_eq(self): |
| 221 |
def fn(c1, c2): |
| 222 |
return c1 == c2 |
| 223 |
assert self.interpret(fn, [u'\u03b1', u'\u03b1']) == True |
| 224 |
assert self.interpret(fn, [u'\u03b1', u'\u03b2']) == False |
| 225 |
|
| 226 |
def test_unichar_ord(self): |
| 227 |
def fn(c): |
| 228 |
return ord(c) |
| 229 |
assert self.interpret(fn, [u'\u03b1']) == ord(u'\u03b1') |
| 230 |
|
| 231 |
def test_unichar_hash(self): |
| 232 |
def fn(c): |
| 233 |
d = {c: 42} |
| 234 |
return d[c] |
| 235 |
assert self.interpret(fn, [u'\u03b1']) == 42 |
| 236 |
|
| 237 |
def test_strformat_unicode_arg(self): |
| 238 |
const = self.const |
| 239 |
def percentS(s, i): |
| 240 |
s = [s, None][i] |
| 241 |
return const("before %s after") % (s,) |
| 242 |
|
| 243 |
res = self.interpret(percentS, [const(u'à'), 0]) |
| 244 |
assert self.ll_to_string(res) == const(u'before à after') |
| 245 |
|
| 246 |
res = self.interpret(percentS, [const(u'à'), 1]) |
| 247 |
assert self.ll_to_string(res) == const(u'before None after') |
| 248 |
|
| 249 |
|
| 250 |
def test_strformat_unicode_and_str(self): |
| 251 |
|
| 252 |
|
| 253 |
const = self.const |
| 254 |
def percentS(ch): |
| 255 |
x = "%s" % (ch + "bc") |
| 256 |
y = u"%s" % (unichr(ord(ch)) + u"bc") |
| 257 |
return len(x) + len(y) |
| 258 |
|
| 259 |
res = self.interpret(percentS, ["a"]) |
| 260 |
assert res == 6 |
| 261 |
|
| 262 |
def unsupported(self): |
| 263 |
py.test.skip("not supported") |
| 264 |
|
| 265 |
test_char_isxxx = unsupported |
| 266 |
test_isdigit = unsupported |
| 267 |
test_str_isalpha = unsupported |
| 268 |
test_str_isalnum = unsupported |
| 269 |
test_upper = unsupported |
| 270 |
test_lower = unsupported |
| 271 |
test_splitlines = unsupported |
| 272 |
test_int = unsupported |
| 273 |
test_int_valueerror = unsupported |
| 274 |
test_float = unsupported |
| 275 |
test_hlstr = unsupported |
| 276 |
test_strip_multiple_chars = unsupported |
| 277 |
|
| 278 |
def test_hash_via_type(self): |
| 279 |
from rpython.rlib.objectmodel import compute_hash |
| 280 |
|
| 281 |
def f(n): |
| 282 |
s = malloc(UNICODE, n) |
| 283 |
s.hash = 0 |
| 284 |
for i in range(n): |
| 285 |
s.chars[i] = unichr(ord('A') + i) |
| 286 |
return s.gethash() - compute_hash(u'ABCDE') |
| 287 |
|
| 288 |
res = self.interpret(f, [5]) |
| 289 |
assert res == 0 |
| 290 |
|
| 291 |
def test_unicode_char_comparison(self): |
| 292 |
const = u'abcdef' |
| 293 |
def f(n): |
| 294 |
return const[n] >= u'c' |
| 295 |
|
| 296 |
res = self.interpret(f, [1]) |
| 297 |
assert res == False |
| 298 |
res = self.interpret(f, [2]) |
| 299 |
assert res == True |
| 300 |
|
| 301 |
def test_strip_no_arg(self): |
| 302 |
|
| 303 |
def f(): |
| 304 |
return u'abcdef'.strip() |
| 305 |
|
| 306 |
e = py.test.raises(Exception, self.interpret, f, []) |
| 307 |
assert "unicode.strip() with no arg is not RPython" in str(e.value) |