| 1 |
|
| 2 |
import pytest |
| 3 |
from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w |
| 4 |
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 5 |
from pypy.module.cpyext.unicodeobject import ( |
| 6 |
Py_UNICODE, PyUnicodeObject, new_empty_unicode) |
| 7 |
from pypy.module.cpyext.api import (PyObjectP, PyObject, |
| 8 |
Py_CLEANUP_SUPPORTED, INTP_real) |
| 9 |
from pypy.module.cpyext.pyobject import decref, from_ref |
| 10 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 11 |
import sys, py |
| 12 |
from pypy.module.cpyext.unicodeobject import * |
| 13 |
|
| 14 |
class AppTestUnicodeObject(AppTestCpythonExtensionBase): |
| 15 |
def test_unicodeobject_basic(self): |
| 16 |
module = self.import_extension('foo', [ |
| 17 |
("get_hello1", "METH_NOARGS", |
| 18 |
""" |
| 19 |
return PyUnicode_FromStringAndSize( |
| 20 |
"Hello world<should not be included>", 11); |
| 21 |
"""), |
| 22 |
("test_GetSize", "METH_NOARGS", |
| 23 |
""" |
| 24 |
PyObject* s = PyUnicode_FromString("Hello world"); |
| 25 |
int result = 0; |
| 26 |
|
| 27 |
_Py_COMP_DIAG_PUSH |
| 28 |
_Py_COMP_DIAG_IGNORE_DEPR_DECLS |
| 29 |
if(PyUnicode_GetSize(s) != 11) { |
| 30 |
result = -PyUnicode_GetSize(s); |
| 31 |
} |
| 32 |
_Py_COMP_DIAG_POP |
| 33 |
if(s->ob_type->tp_basicsize != sizeof(PyUnicodeObject)) |
| 34 |
result = s->ob_type->tp_basicsize; |
| 35 |
Py_DECREF(s); |
| 36 |
return PyLong_FromLong(result); |
| 37 |
"""), |
| 38 |
("test_GetLength", "METH_NOARGS", |
| 39 |
""" |
| 40 |
PyObject* s = PyUnicode_FromString("Hello world"); |
| 41 |
int result = 0; |
| 42 |
|
| 43 |
if(PyUnicode_GetLength(s) != 11) { |
| 44 |
result = -PyUnicode_GetLength(s); |
| 45 |
} |
| 46 |
Py_DECREF(s); |
| 47 |
return PyLong_FromLong(result); |
| 48 |
"""), |
| 49 |
("test_GetSize_exception", "METH_NOARGS", |
| 50 |
""" |
| 51 |
PyObject* f = PyFloat_FromDouble(1.0); |
| 52 |
_Py_COMP_DIAG_PUSH |
| 53 |
_Py_COMP_DIAG_IGNORE_DEPR_DECLS |
| 54 |
PyUnicode_GetSize(f); |
| 55 |
_Py_COMP_DIAG_POP |
| 56 |
|
| 57 |
Py_DECREF(f); |
| 58 |
return NULL; |
| 59 |
"""), |
| 60 |
("test_is_unicode", "METH_VARARGS", |
| 61 |
""" |
| 62 |
return PyBool_FromLong(PyUnicode_Check(PyTuple_GetItem(args, 0))); |
| 63 |
"""), |
| 64 |
("test_append", "METH_VARARGS", |
| 65 |
""" |
| 66 |
PyObject *ret = PyTuple_GetItem(args, 0); |
| 67 |
PyUnicode_Append(&ret, PyTuple_GetItem(args, 1)); |
| 68 |
return ret; |
| 69 |
"""), |
| 70 |
("test_appenddel", "METH_VARARGS", |
| 71 |
""" |
| 72 |
PyObject *s = PyUnicode_FromString("Hello world"); |
| 73 |
PyObject *ret = PyTuple_GetItem(args, 0); |
| 74 |
PyUnicode_AppendAndDel(&ret, s); |
| 75 |
// No need to decref s, it is done for us. |
| 76 |
return ret; |
| 77 |
"""), |
| 78 |
("test_TONUMERIC", "METH_O", |
| 79 |
""" |
| 80 |
Py_UCS4 *ucs4 = PyUnicode_AsUCS4Copy(args); |
| 81 |
if (ucs4 == NULL) return NULL; |
| 82 |
double result = _PyUnicode_ToNumeric(ucs4[0]); |
| 83 |
return PyFloat_FromDouble(result); |
| 84 |
"""), |
| 85 |
]) |
| 86 |
assert module.get_hello1() == u'Hello world' |
| 87 |
assert module.test_GetSize() == 0 |
| 88 |
raises(TypeError, module.test_GetSize_exception) |
| 89 |
|
| 90 |
|
| 91 |
assert module.test_GetLength() == 0 |
| 92 |
|
| 93 |
assert module.test_is_unicode(u"") |
| 94 |
assert not module.test_is_unicode(()) |
| 95 |
a = u"a" |
| 96 |
assert module.test_append(a, u"b") == u"ab" |
| 97 |
assert module.test_appenddel(a) == a + u"Hello world" |
| 98 |
assert module.test_TONUMERIC('6') == 6.0 |
| 99 |
assert module.test_TONUMERIC('A') == -1.0 |
| 100 |
|
| 101 |
def test_strlen(self): |
| 102 |
module = self.import_extension('foo', [ |
| 103 |
('strlen', "METH_O", |
| 104 |
""" |
| 105 |
PyObject* s = PyObject_Str(args); |
| 106 |
return PyLong_FromLong(PyUnicode_GetLength(s)); |
| 107 |
""")]) |
| 108 |
assert module.strlen(True) == 4 |
| 109 |
assert module.strlen('a' * 40) == 40 |
| 110 |
|
| 111 |
def test_intern_inplace(self): |
| 112 |
module = self.import_extension('foo', [ |
| 113 |
("test_intern_inplace", "METH_O", |
| 114 |
''' |
| 115 |
PyObject *s = args; |
| 116 |
Py_INCREF(s); |
| 117 |
PyUnicode_InternInPlace(&s); |
| 118 |
return s; |
| 119 |
''' |
| 120 |
)]) |
| 121 |
|
| 122 |
assert module.test_intern_inplace('s') == 's' |
| 123 |
|
| 124 |
|
| 125 |
def test_unicode_buffer_init(self): |
| 126 |
module = self.import_extension('foo', [ |
| 127 |
("getunicode", "METH_NOARGS", |
| 128 |
""" |
| 129 |
PyObject *s, *t; |
| 130 |
Py_UNICODE* c; |
| 131 |
|
| 132 |
s = PyUnicode_FromUnicode(NULL, 4); |
| 133 |
if (s == NULL) |
| 134 |
return NULL; |
| 135 |
t = PyUnicode_FromUnicode(NULL, 3); |
| 136 |
if (t == NULL) |
| 137 |
return NULL; |
| 138 |
Py_DECREF(t); |
| 139 |
_Py_COMP_DIAG_PUSH |
| 140 |
_Py_COMP_DIAG_IGNORE_DEPR_DECLS |
| 141 |
c = PyUnicode_AsUnicode(s); |
| 142 |
_Py_COMP_DIAG_POP |
| 143 |
c[0] = 'a'; |
| 144 |
c[1] = 0xe9; |
| 145 |
c[2] = 0x00; |
| 146 |
c[3] = 'c'; |
| 147 |
return s; |
| 148 |
"""), |
| 149 |
]) |
| 150 |
s = module.getunicode() |
| 151 |
assert len(s) == 4 |
| 152 |
assert s == u'a\xe9\x00c' |
| 153 |
|
| 154 |
def test_default_encoded_string(self): |
| 155 |
import sys |
| 156 |
module = self.import_extension('foo', [ |
| 157 |
("test_default_encoded_string", "METH_O", |
| 158 |
''' |
| 159 |
PyObject* result = PyUnicode_AsEncodedString(args, NULL, "replace"); |
| 160 |
Py_INCREF(result); |
| 161 |
return result; |
| 162 |
''' |
| 163 |
), |
| 164 |
]) |
| 165 |
res = module.test_default_encoded_string(u"xyz") |
| 166 |
assert res == b'xyz' |
| 167 |
res = module.test_default_encoded_string(u"caf\xe9") |
| 168 |
assert res == u"caf\xe9".encode(sys.getdefaultencoding(), 'replace') |
| 169 |
|
| 170 |
def test_unicode_macros(self): |
| 171 |
"""The PyUnicode_* macros cast, and calls expecting that build.""" |
| 172 |
module = self.import_extension('foo', [ |
| 173 |
("test_macro_invocations", "METH_NOARGS", |
| 174 |
""" |
| 175 |
PyObject* o = PyUnicode_FromString(""); |
| 176 |
PyUnicodeObject* u = (PyUnicodeObject*)o; |
| 177 |
|
| 178 |
_Py_COMP_DIAG_PUSH |
| 179 |
_Py_COMP_DIAG_IGNORE_DEPR_DECLS |
| 180 |
PyUnicode_GET_SIZE(u); |
| 181 |
PyUnicode_GET_SIZE(o); |
| 182 |
|
| 183 |
PyUnicode_GET_DATA_SIZE(u); |
| 184 |
PyUnicode_GET_DATA_SIZE(o); |
| 185 |
|
| 186 |
PyUnicode_AS_UNICODE(o); |
| 187 |
PyUnicode_AS_UNICODE(u); |
| 188 |
_Py_COMP_DIAG_POP |
| 189 |
return o; |
| 190 |
"""), |
| 191 |
("test_ISWHITESPACE", "METH_O", |
| 192 |
""" |
| 193 |
Py_UCS4 *ucs4 = PyUnicode_AsUCS4Copy(args); |
| 194 |
if (ucs4 == NULL) return NULL; |
| 195 |
int result = _PyUnicode_IsWhitespace(ucs4[0]); |
| 196 |
return PyLong_FromLong(result); |
| 197 |
"""), |
| 198 |
("test_ISLINEBREAK", "METH_O", |
| 199 |
""" |
| 200 |
Py_UCS4 *ucs4 = PyUnicode_AsUCS4Copy(args); |
| 201 |
if (ucs4 == NULL) return NULL; |
| 202 |
int result = Py_UNICODE_ISLINEBREAK(ucs4[0]); |
| 203 |
return PyLong_FromLong(result); |
| 204 |
"""), |
| 205 |
]) |
| 206 |
assert module.test_macro_invocations() == u'' |
| 207 |
for char in [0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x1c, 0x1d, 0x1e, 0x1f, |
| 208 |
0x20, 0x85, 0xa0, 0x1680, 0x2000, 0x2001, 0x2002, |
| 209 |
0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, |
| 210 |
0x2009, 0x200a, |
| 211 |
|
| 212 |
0x2028, 0x2029, 0x202f, 0x205f, 0x3000]: |
| 213 |
assert module.test_ISWHITESPACE(chr(char)) == 1 |
| 214 |
assert module.test_ISWHITESPACE('a') == 0 |
| 215 |
for char in [0x0a, 0x0d, 0x1c, 0x1d, 0x1e, 0x85, 0x2028, 0x2029]: |
| 216 |
assert module.test_ISLINEBREAK(chr(char)) |
| 217 |
|
| 218 |
def test_format_v(self): |
| 219 |
module = self.import_extension('foo', [ |
| 220 |
("test_unicode_format_v", "METH_VARARGS", |
| 221 |
''' |
| 222 |
return helper("bla %d ble %s\\n", |
| 223 |
PyLong_AsLong(PyTuple_GetItem(args, 0)), |
| 224 |
_PyUnicode_AsString(PyTuple_GetItem(args, 1))); |
| 225 |
''' |
| 226 |
) |
| 227 |
], prologue=''' |
| 228 |
PyObject* helper(char* fmt, ...) |
| 229 |
{ |
| 230 |
va_list va; |
| 231 |
PyObject* res; |
| 232 |
va_start(va, fmt); |
| 233 |
res = PyUnicode_FromFormatV(fmt, va); |
| 234 |
va_end(va); |
| 235 |
return res; |
| 236 |
} |
| 237 |
''') |
| 238 |
res = module.test_unicode_format_v(1, "xyz") |
| 239 |
assert res == "bla 1 ble xyz\n" |
| 240 |
|
| 241 |
def test_format1(self): |
| 242 |
module = self.import_extension('foo', [ |
| 243 |
("test_unicode_format", "METH_VARARGS", |
| 244 |
''' |
| 245 |
return PyUnicode_FromFormat("bla %d ble %s\\n", |
| 246 |
PyLong_AsLong(PyTuple_GetItem(args, 0)), |
| 247 |
_PyUnicode_AsString(PyTuple_GetItem(args, 1))); |
| 248 |
''' |
| 249 |
) |
| 250 |
]) |
| 251 |
res = module.test_unicode_format(1, "xyz") |
| 252 |
assert res == "bla 1 ble xyz\n" |
| 253 |
|
| 254 |
def test_format_obj(self): |
| 255 |
module = self.import_extension('foo', [ |
| 256 |
("format_obj", "METH_VARARGS", |
| 257 |
""" |
| 258 |
char *fmt = PyUnicode_AsUTF8(PyTuple_GetItem(args, 0)); |
| 259 |
return PyUnicode_FromFormat(fmt, PyTuple_GetItem(args, 1)); |
| 260 |
"""), |
| 261 |
("format_d_str", "METH_VARARGS", |
| 262 |
""" |
| 263 |
char *fmt = PyUnicode_AsUTF8(PyTuple_GetItem(args, 0)); |
| 264 |
if (PyObject_Size(args) != 3) { |
| 265 |
PyErr_SetString(PyExc_RuntimeError, "bad args"); |
| 266 |
return NULL; |
| 267 |
} |
| 268 |
long d = PyLong_AsLong(PyTuple_GetItem(args, 1)); |
| 269 |
char *s2 = PyUnicode_AsUTF8(PyTuple_GetItem(args, 2)); |
| 270 |
return PyUnicode_FromFormat(fmt, d, s2); |
| 271 |
"""), |
| 272 |
("format_str_str", "METH_VARARGS", |
| 273 |
""" |
| 274 |
char *fmt = PyUnicode_AsUTF8(PyTuple_GetItem(args, 0)); |
| 275 |
if (PyObject_Size(args) != 3) { |
| 276 |
PyErr_SetString(PyExc_RuntimeError, "bad args"); |
| 277 |
return NULL; |
| 278 |
} |
| 279 |
char *s1 = PyUnicode_AsUTF8(PyTuple_GetItem(args, 1)); |
| 280 |
char *s2 = PyUnicode_AsUTF8(PyTuple_GetItem(args, 2)); |
| 281 |
return PyUnicode_FromFormat(fmt, s1, s2); |
| 282 |
"""), |
| 283 |
("format_parsing", "METH_NOARGS", |
| 284 |
""" |
| 285 |
/* From getargs.c */ |
| 286 |
char *fmt = "%.150s%s takes %s %d argument%s (%ld given)"; |
| 287 |
return PyUnicode_FromFormat(fmt, "add", "()", "exactly", 2, "s", 1L); |
| 288 |
"""), |
| 289 |
("format_ll_str", "METH_VARARGS", |
| 290 |
""" |
| 291 |
char *fmt = PyUnicode_AsUTF8(PyTuple_GetItem(args, 0)); |
| 292 |
if (PyObject_Size(args) != 3) { |
| 293 |
PyErr_SetString(PyExc_RuntimeError, "bad args"); |
| 294 |
return NULL; |
| 295 |
} |
| 296 |
long long d = PyLong_AsLongLong(PyTuple_GetItem(args, 1)); |
| 297 |
char *s2 = PyUnicode_AsUTF8(PyTuple_GetItem(args, 2)); |
| 298 |
return PyUnicode_FromFormat(fmt, d, s2); |
| 299 |
"""), |
| 300 |
]) |
| 301 |
assert module.format_obj("formatting 100R '%.100R'", 1.0) == "formatting 100R '1.0'" |
| 302 |
assert module.format_d_str("id:%d, name:%s", 12, "abc") == "id:12, name:abc" |
| 303 |
ret = module.format_str_str("%.200s%s takes no arguments", "abc", "def") |
| 304 |
assert ret == "abcdef takes no arguments" |
| 305 |
ret = module.format_parsing() |
| 306 |
print(ret) |
| 307 |
assert ret == "add() takes exactly 2 arguments (1 given)"; |
| 308 |
|
| 309 |
s = module.format_ll_str("id:%lli %s", 12, "abc") |
| 310 |
assert s == "id:12 abc" |
| 311 |
|
| 312 |
def test_fromkind(self): |
| 313 |
module = self.import_extension('foo', [ |
| 314 |
('from_ucs1', 'METH_O', |
| 315 |
""" |
| 316 |
char* p; |
| 317 |
Py_ssize_t size; |
| 318 |
if (PyBytes_AsStringAndSize(args, &p, &size) < 0) |
| 319 |
return NULL; |
| 320 |
return PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND, p, size); |
| 321 |
"""), |
| 322 |
('from_ucs2', 'METH_O', |
| 323 |
""" |
| 324 |
char* p; |
| 325 |
Py_ssize_t size; |
| 326 |
if (PyBytes_AsStringAndSize(args, &p, &size) < 0) |
| 327 |
return NULL; |
| 328 |
return PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, p, size/2); |
| 329 |
"""), |
| 330 |
('from_ucs4', 'METH_O', |
| 331 |
""" |
| 332 |
char* p; |
| 333 |
Py_ssize_t size; |
| 334 |
if (PyBytes_AsStringAndSize(args, &p, &size) < 0) |
| 335 |
return NULL; |
| 336 |
return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, p, size/4); |
| 337 |
""")]) |
| 338 |
res = module.from_ucs1(b'spam') |
| 339 |
assert res == 'spam' |
| 340 |
s = "späm" |
| 341 |
b = s.encode('utf-16')[2:] |
| 342 |
s2 = module.from_ucs2(b) |
| 343 |
assert module.from_ucs2(b) == s |
| 344 |
s = "x\N{PILE OF POO}x" |
| 345 |
b = s.encode('utf-32')[4:] |
| 346 |
assert module.from_ucs4(b) == s |
| 347 |
|
| 348 |
b = b'\x00\xd8\x00\x00' |
| 349 |
for func, ret in zip([module.from_ucs4, module.from_ucs2], |
| 350 |
[['0xd800'], ['0xd800', '0x0']]): |
| 351 |
s = func(b) |
| 352 |
assert isinstance(s, str) |
| 353 |
h = [hex(ord(x)) for x in s] |
| 354 |
assert h == ret, '%s, %s' %(h, ret) |
| 355 |
|
| 356 |
def test_substring(self): |
| 357 |
module = self.import_extension('foo', [ |
| 358 |
("slice_start", "METH_VARARGS", |
| 359 |
''' |
| 360 |
PyObject* text; |
| 361 |
Py_ssize_t start, length; |
| 362 |
if (!PyArg_ParseTuple(args, "On", &text, &start)) |
| 363 |
return NULL; |
| 364 |
if (PyUnicode_READY(text) == -1) return NULL; |
| 365 |
if (!PyUnicode_1BYTE_DATA(text)) { |
| 366 |
// Don't segfault, just fail the test. |
| 367 |
Py_RETURN_NONE; |
| 368 |
} |
| 369 |
length = PyUnicode_GET_LENGTH(text); |
| 370 |
if (start > length) return PyLong_FromSsize_t(start); |
| 371 |
return PyUnicode_FromKindAndData(PyUnicode_KIND(text), |
| 372 |
PyUnicode_1BYTE_DATA(text) + start*PyUnicode_KIND(text), |
| 373 |
length-start); |
| 374 |
''')]) |
| 375 |
s = u'aАbБcСdД' |
| 376 |
assert module.slice_start(s, 2) == 'bБcСdД' |
| 377 |
|
| 378 |
s = u'xx\U0001F4A9' |
| 379 |
assert module.slice_start(s, 2) == u'\U0001F4A9' |
| 380 |
|
| 381 |
def test_aswidecharstring(self): |
| 382 |
module = self.import_extension('foo', [ |
| 383 |
("aswidecharstring", "METH_O", |
| 384 |
''' |
| 385 |
PyObject *result; |
| 386 |
Py_ssize_t size; |
| 387 |
wchar_t *buffer; |
| 388 |
|
| 389 |
buffer = PyUnicode_AsWideCharString(args, &size); |
| 390 |
if (buffer == NULL) |
| 391 |
return NULL; |
| 392 |
|
| 393 |
result = PyUnicode_FromWideChar(buffer, size + 1); |
| 394 |
PyMem_Free(buffer); |
| 395 |
if (result == NULL) |
| 396 |
return NULL; |
| 397 |
return Py_BuildValue("(Nn)", result, size); |
| 398 |
''')]) |
| 399 |
res = module.aswidecharstring("Caf\xe9") |
| 400 |
assert res == ("Caf\xe9\0", 4) |
| 401 |
|
| 402 |
def test_fromwidechar(self): |
| 403 |
module = self.import_extension('foo', [ |
| 404 |
("truncate", "METH_O", |
| 405 |
''' |
| 406 |
wchar_t buffer[5] = { 0 }; |
| 407 |
|
| 408 |
if (PyUnicode_AsWideChar(args, buffer, 4) == -1) |
| 409 |
return NULL; |
| 410 |
|
| 411 |
return PyUnicode_FromWideChar(buffer, -1); |
| 412 |
''')]) |
| 413 |
assert module.truncate("Caf\xe9") == "Caf\xe9" |
| 414 |
assert module.truncate("abcde") == "abcd" |
| 415 |
assert module.truncate("ab") == "ab" |
| 416 |
|
| 417 |
def test_CompareWithASCIIString(self): |
| 418 |
module = self.import_extension('foo', [ |
| 419 |
("compare", "METH_VARARGS", |
| 420 |
''' |
| 421 |
PyObject *uni; |
| 422 |
const char* s; |
| 423 |
int res; |
| 424 |
|
| 425 |
if (!PyArg_ParseTuple(args, "Uy", &uni, &s)) |
| 426 |
return NULL; |
| 427 |
|
| 428 |
res = PyUnicode_CompareWithASCIIString(uni, s); |
| 429 |
return PyLong_FromLong(res); |
| 430 |
''')]) |
| 431 |
assert module.compare("abc", b"abc") == 0 |
| 432 |
assert module.compare("abd", b"abc") == 1 |
| 433 |
assert module.compare("abb", b"abc") == -1 |
| 434 |
assert module.compare("caf\xe9", b"caf\xe9") == 0 |
| 435 |
assert module.compare("abc", b"ab") == 1 |
| 436 |
assert module.compare("ab\0", b"ab") == 1 |
| 437 |
assert module.compare("ab", b"abc") == -1 |
| 438 |
assert module.compare("", b"abc") == -1 |
| 439 |
assert module.compare("abc", b"") == 1 |
| 440 |
|
| 441 |
def test_As_AndSize(self): |
| 442 |
module = self.import_extension('foo', [ |
| 443 |
("utf8", "METH_O", |
| 444 |
""" |
| 445 |
Py_ssize_t size; |
| 446 |
char *utf8 = PyUnicode_AsUTF8AndSize(args, &size); |
| 447 |
return PyBytes_FromStringAndSize(utf8, size); |
| 448 |
"""), |
| 449 |
("unicode", "METH_O", |
| 450 |
""" |
| 451 |
Py_ssize_t size; |
| 452 |
wchar_t *buf = PyUnicode_AsUnicodeAndSize(args, &size); |
| 453 |
return PyUnicode_FromUnicode(buf, size); |
| 454 |
"""), |
| 455 |
]) |
| 456 |
assert module.utf8('xyz') == b'xyz' |
| 457 |
assert module.utf8('café') == 'café'.encode('utf-8') |
| 458 |
assert module.unicode('np') == 'np' |
| 459 |
assert module.unicode('café') == u'café' |
| 460 |
|
| 461 |
def test_ready(self): |
| 462 |
module = self.import_extension('foo', [ |
| 463 |
("unsafe_len", "METH_O", |
| 464 |
""" |
| 465 |
Py_ssize_t size = PyUnicode_GET_LENGTH(args); |
| 466 |
return PyLong_FromSsize_t(size); |
| 467 |
""")]) |
| 468 |
assert module.unsafe_len(u"abc") == 3 |
| 469 |
assert module.unsafe_len(u"café") == 4 |
| 470 |
assert module.unsafe_len(u'aАbБcСdД') == 8 |
| 471 |
assert module.unsafe_len(u"café\U0001F4A9") == 5 |
| 472 |
|
| 473 |
def test_FromObject(self): |
| 474 |
module = self.import_extension('foo', [ |
| 475 |
("from_object", "METH_O", |
| 476 |
""" |
| 477 |
return PyUnicode_FromObject(args); |
| 478 |
""")]) |
| 479 |
class my_str(str): pass |
| 480 |
assert module.from_object('abc') == 'abc' |
| 481 |
res = module.from_object(my_str('abc')) |
| 482 |
assert type(res) is str |
| 483 |
assert res == 'abc' |
| 484 |
raises(TypeError, module.from_object, b'abc') |
| 485 |
raises(TypeError, module.from_object, 42) |
| 486 |
|
| 487 |
def test_widechar(self): |
| 488 |
module = self.import_extension('foo', [ |
| 489 |
("make_wide", "METH_NOARGS", |
| 490 |
""" |
| 491 |
#if defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4) |
| 492 |
const wchar_t wtext[2] = {(wchar_t)0x10ABCDu}; |
| 493 |
size_t wtextlen = 1; |
| 494 |
#else |
| 495 |
const wchar_t wtext[3] = {(wchar_t)0xDBEAu, (wchar_t)0xDFCDu}; |
| 496 |
size_t wtextlen = 2; |
| 497 |
#endif |
| 498 |
return PyUnicode_FromWideChar(wtext, wtextlen); |
| 499 |
"""), |
| 500 |
("make_utf8", "METH_NOARGS", |
| 501 |
""" |
| 502 |
return PyUnicode_FromString("\\xf4\\x8a\\xaf\\x8d"); |
| 503 |
""")]) |
| 504 |
wide = module.make_wide() |
| 505 |
utf8 = module.make_utf8() |
| 506 |
print(repr(wide), repr(utf8)) |
| 507 |
assert wide == utf8 |
| 508 |
|
| 509 |
def test_invalid(self): |
| 510 |
m = self.import_module('_widechar') |
| 511 |
if m.get_sizeof_wchar() != 4: |
| 512 |
|
| 513 |
return |
| 514 |
exc = raises((SystemError, ValueError), m.test_widechar) |
| 515 |
assert 'not in range [U+0000; U+10ffff]' in str(exc.value), str(exc.value) |
| 516 |
|
| 517 |
def test_AsUTFNString(self): |
| 518 |
module = self.import_extension('foo', [ |
| 519 |
("asutf8", "METH_O", "return PyUnicode_AsUTF8String(args);"), |
| 520 |
("asutf16", "METH_O", "return PyUnicode_AsUTF16String(args);"), |
| 521 |
("asutf32", "METH_O", "return PyUnicode_AsUTF32String(args);"), |
| 522 |
]) |
| 523 |
u = u'sp\x09m\u1234\U00012345' |
| 524 |
s = module.asutf8(u) |
| 525 |
assert s == u.encode('utf-8') |
| 526 |
s = module.asutf16(u) |
| 527 |
assert s == u.encode('utf-16') |
| 528 |
s = module.asutf32(u) |
| 529 |
assert s == u.encode('utf-32') |
| 530 |
|
| 531 |
def test_lower_cython(self): |
| 532 |
|
| 533 |
import time |
| 534 |
module = self.import_extension('foo', [ |
| 535 |
("lower", "METH_O", |
| 536 |
""" |
| 537 |
PyObject *p, *res, *tup; |
| 538 |
p = PyObject_GetAttrString(args, "lower"); |
| 539 |
if (p == NULL) { |
| 540 |
return NULL; |
| 541 |
} |
| 542 |
tup = PyTuple_New(0); |
| 543 |
Py_INCREF(tup); |
| 544 |
res = PyObject_Call(p, tup, NULL); |
| 545 |
Py_DECREF(tup); |
| 546 |
return res; |
| 547 |
""")]) |
| 548 |
assert module.lower('ABC') == 'abc' |
| 549 |
try: |
| 550 |
time.tzset() |
| 551 |
except AttributeError: |
| 552 |
|
| 553 |
pass |
| 554 |
tz1 = time.tzname[1] |
| 555 |
assert module.lower(tz1) == tz1.lower() |
| 556 |
|
| 557 |
def test_contains(self): |
| 558 |
import sys |
| 559 |
module = self.import_extension('foo', [ |
| 560 |
("contains", "METH_VARARGS", |
| 561 |
""" |
| 562 |
PyObject *arg1 = PyTuple_GetItem(args, 0); |
| 563 |
PyObject *arg2 = PyTuple_GetItem(args, 1); |
| 564 |
int ret = PyUnicode_Contains(arg1, arg2); |
| 565 |
if (ret < 0) { |
| 566 |
return NULL; |
| 567 |
} |
| 568 |
return PyLong_FromLong(ret); |
| 569 |
""")]) |
| 570 |
s = u"abcabcabc" |
| 571 |
assert module.contains(s, u"a") == 1 |
| 572 |
assert module.contains(s, u"e") == 0 |
| 573 |
try: |
| 574 |
module.contains(s, 1) |
| 575 |
except TypeError: |
| 576 |
pass |
| 577 |
else: |
| 578 |
assert False |
| 579 |
try: |
| 580 |
module.contains(1, u"a") |
| 581 |
except TypeError: |
| 582 |
pass |
| 583 |
else: |
| 584 |
assert False |
| 585 |
if sys.version_info < (3, 0): |
| 586 |
assert module.contains(b'abcdef', b'e') == 1 |
| 587 |
else: |
| 588 |
try: |
| 589 |
module.contains(b'abcdef', b'e') |
| 590 |
except TypeError: |
| 591 |
pass |
| 592 |
else: |
| 593 |
assert False |
| 594 |
|
| 595 |
def test_itemsize(self): |
| 596 |
module = self.import_extension('foo', [ |
| 597 |
("itemsize", "METH_NOARGS", |
| 598 |
""" |
| 599 |
PyObject* o = PyUnicode_FromString(""); |
| 600 |
int ret = o->ob_type->tp_itemsize; |
| 601 |
if (ret < 0) { |
| 602 |
return NULL; |
| 603 |
} |
| 604 |
return PyLong_FromLong(ret); |
| 605 |
""")]) |
| 606 |
itemsize = module.itemsize() |
| 607 |
assert itemsize == 0 |
| 608 |
|
| 609 |
def test_UnicodeNew(self): |
| 610 |
module = self.import_extension('unicodenew', [ |
| 611 |
("make", "METH_VARARGS", |
| 612 |
""" |
| 613 |
long length = PyLong_AsLong(PyTuple_GetItem(args, 0)); |
| 614 |
long unichr = PyLong_AsLong(PyTuple_GetItem(args, 1)); |
| 615 |
|
| 616 |
PyObject *retval = PyUnicode_New(length, (Py_UCS4)unichr); |
| 617 |
if (unichr <= 255) { |
| 618 |
Py_UCS1 *retbuf = PyUnicode_1BYTE_DATA(retval); |
| 619 |
for (long i = 0; i < length; i++) |
| 620 |
retbuf[i] = unichr; |
| 621 |
} |
| 622 |
else if (unichr <= 65535) { |
| 623 |
Py_UCS2 *retbuf = PyUnicode_2BYTE_DATA(retval); |
| 624 |
for (long i = 0; i < length; i++) |
| 625 |
retbuf[i] = unichr; |
| 626 |
} |
| 627 |
else { |
| 628 |
Py_UCS4 *retbuf = PyUnicode_4BYTE_DATA(retval); |
| 629 |
for (long i = 0; i < length; i++) |
| 630 |
retbuf[i] = unichr; |
| 631 |
} |
| 632 |
return retval; |
| 633 |
"""), |
| 634 |
]) |
| 635 |
assert module.make(0, 32) == u'' |
| 636 |
assert module.make(1, 32) == u' ' |
| 637 |
assert module.make(5, 255) == u'\xff' * 5 |
| 638 |
assert module.make(3, 0x1234) == u'\u1234' * 3 |
| 639 |
assert module.make(7, 0x12345) == u'\U00012345' * 7 |
| 640 |
|
| 641 |
def test_char_ops(self): |
| 642 |
module = self.import_extension('char_ops', [ |
| 643 |
("readchar", "METH_VARARGS", |
| 644 |
""" |
| 645 |
PyObject *obj = PyTuple_GetItem(args, 0); |
| 646 |
Py_ssize_t indx = PyLong_AsLong(PyTuple_GetItem(args, 1)); |
| 647 |
Py_UCS4 chr = PyUnicode_ReadChar(obj, indx); |
| 648 |
if (chr == (Py_UCS4)-1 || chr == (Py_UCS4)-2) { |
| 649 |
return NULL; |
| 650 |
} |
| 651 |
return PyLong_FromLong(chr); |
| 652 |
"""), |
| 653 |
("writechar", "METH_VARARGS", |
| 654 |
""" |
| 655 |
char *str; |
| 656 |
Py_ssize_t indx; |
| 657 |
int ch; |
| 658 |
if (!PyArg_ParseTuple(args, "sni", &str, &indx, &ch)) { |
| 659 |
return NULL; |
| 660 |
} |
| 661 |
if (ch > (int)0xffff || ch < 0) { |
| 662 |
PyErr_SetString(PyExc_OverflowError, "ch is out of bounds"); |
| 663 |
return NULL; |
| 664 |
} |
| 665 |
PyObject * newstr = PyUnicode_FromString(str); |
| 666 |
int ret = PyUnicode_WriteChar(newstr, indx, ch); |
| 667 |
if (ret < 0) { |
| 668 |
Py_DECREF(newstr); |
| 669 |
return NULL; |
| 670 |
} |
| 671 |
return newstr; |
| 672 |
"""), |
| 673 |
("findchar", "METH_VARARGS", |
| 674 |
""" |
| 675 |
PyObject *uni; |
| 676 |
int ch; |
| 677 |
Py_ssize_t start, end, ret; |
| 678 |
int direction; |
| 679 |
if (!PyArg_ParseTuple(args, "Oinni", &uni, &ch, &start, &end, &direction)) { |
| 680 |
return NULL; |
| 681 |
} |
| 682 |
if (ch > (int)0xffff || ch < 0) { |
| 683 |
PyErr_SetString(PyExc_OverflowError, "ch is out of bounds"); |
| 684 |
return NULL; |
| 685 |
} |
| 686 |
ret = PyUnicode_FindChar(uni, (Py_UCS4)ch, start, end, direction); |
| 687 |
if (ret == -2) return NULL; |
| 688 |
return PyLong_FromLong(ret); |
| 689 |
"""), |
| 690 |
]) |
| 691 |
s = 'abcdef' |
| 692 |
assert module.readchar(s, 3) == ord(s[3]) |
| 693 |
try: |
| 694 |
newstr = module.writechar(s, 3, ord('z')) |
| 695 |
assert newstr[3] == 'z' |
| 696 |
assert newstr[0] == 'a' |
| 697 |
except SystemError: |
| 698 |
|
| 699 |
pass |
| 700 |
indx = module.findchar(s, ord('z'), 0, -1, 0) |
| 701 |
assert indx == -1 |
| 702 |
indx = module.findchar(s, ord('d'), 0, -1, 0) |
| 703 |
assert indx == 3 |
| 704 |
|
| 705 |
def test_totuple(self): |
| 706 |
module = self.import_extension('foo', [ |
| 707 |
("to_tuple", "METH_O", |
| 708 |
""" |
| 709 |
int i, len = PyUnicode_GET_LENGTH(args); |
| 710 |
enum PyUnicode_Kind kind = PyUnicode_KIND(args); |
| 711 |
PyObject *retval = PyTuple_New(len); |
| 712 |
uint8_t * c = NULL; |
| 713 |
uint16_t * k = NULL; |
| 714 |
uint32_t * u = NULL; |
| 715 |
|
| 716 |
switch (kind) { |
| 717 |
case PyUnicode_1BYTE_KIND: |
| 718 |
c = PyUnicode_DATA(args); |
| 719 |
for(i=0; i<len; i++) |
| 720 |
PyTuple_SetItem(retval, i, PyLong_FromLong(c[i])); |
| 721 |
break; |
| 722 |
case PyUnicode_2BYTE_KIND: |
| 723 |
k = PyUnicode_DATA(args); |
| 724 |
for(i=0; i<len; i++) |
| 725 |
PyTuple_SetItem(retval, i, PyLong_FromLong(k[i])); |
| 726 |
break; |
| 727 |
case PyUnicode_4BYTE_KIND: |
| 728 |
u = PyUnicode_DATA(args); |
| 729 |
for(i=0; i<len; i++) |
| 730 |
PyTuple_SetItem(retval, i, PyLong_FromLong(u[i])); |
| 731 |
break; |
| 732 |
default: |
| 733 |
Py_DECREF(retval); |
| 734 |
PyErr_SetString(PyExc_RuntimeError, "unknown kind"); |
| 735 |
return NULL; |
| 736 |
break; |
| 737 |
} |
| 738 |
return retval; |
| 739 |
"""), |
| 740 |
]) |
| 741 |
for s in [u'000\x80', u'abc', u'späm', u'abcdefghij' *5 + 'z']: |
| 742 |
print(module.to_tuple(s), tuple([ord(x) for x in s])) |
| 743 |
assert module.to_tuple(s) == tuple([ord(x) for x in s]) |
| 744 |
|
| 745 |
def test_COMPACT(self): |
| 746 |
module = self.import_extension('foo', [ |
| 747 |
("is_compact_ascii", "METH_O", |
| 748 |
""" |
| 749 |
int ret = PyUnicode_IS_COMPACT_ASCII(args); |
| 750 |
return PyLong_FromLong(ret); |
| 751 |
"""), |
| 752 |
("get_compact_data", "METH_O", |
| 753 |
""" |
| 754 |
char * val = _PyUnicode_COMPACT_DATA(args); |
| 755 |
int len = PyUnicode_GET_LENGTH(args); |
| 756 |
return PyUnicode_FromStringAndSize(val, len); |
| 757 |
"""), |
| 758 |
]) |
| 759 |
assert module.is_compact_ascii('abc') |
| 760 |
assert not module.is_compact_ascii(u'000\x80') |
| 761 |
assert module.get_compact_data('abc') == 'abc' |
| 762 |
|
| 763 |
def test_subclass(self): |
| 764 |
module = self.import_extension('gcc', [ |
| 765 |
('is_ascii', "METH_O", |
| 766 |
''' |
| 767 |
if (!PyUnicode_Check(args)) { |
| 768 |
Py_RETURN_FALSE; |
| 769 |
} |
| 770 |
if (PyUnicode_IS_ASCII(args)) { |
| 771 |
Py_RETURN_TRUE; |
| 772 |
} |
| 773 |
Py_RETURN_FALSE; |
| 774 |
'''), |
| 775 |
('is_compact', "METH_O", |
| 776 |
''' |
| 777 |
if (!PyUnicode_Check(args)) { |
| 778 |
Py_RETURN_FALSE; |
| 779 |
} |
| 780 |
if (PyUnicode_IS_COMPACT(args)) { |
| 781 |
Py_RETURN_TRUE; |
| 782 |
} |
| 783 |
Py_RETURN_FALSE; |
| 784 |
'''), |
| 785 |
], prologue=""" |
| 786 |
#include <Python.h> |
| 787 |
PyTypeObject PyUnicodeSubtype = { |
| 788 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 789 |
"foo.unicode_", /* tp_name*/ |
| 790 |
sizeof(PyUnicodeObject), /* tp_basicsize*/ |
| 791 |
0 /* tp_itemsize */ |
| 792 |
}; |
| 793 |
|
| 794 |
""", more_init = ''' |
| 795 |
PyUnicodeSubtype.tp_alloc = NULL; |
| 796 |
PyUnicodeSubtype.tp_free = NULL; |
| 797 |
|
| 798 |
PyUnicodeSubtype.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE; |
| 799 |
PyUnicodeSubtype.tp_itemsize = sizeof(char); |
| 800 |
PyUnicodeSubtype.tp_base = &PyUnicode_Type; |
| 801 |
if (PyType_Ready(&PyUnicodeSubtype) < 0) INITERROR; |
| 802 |
PyModule_AddObject(mod, "subtype", |
| 803 |
(PyObject *)&PyUnicodeSubtype); |
| 804 |
''') |
| 805 |
|
| 806 |
a = module.subtype('abc') |
| 807 |
assert module.is_ascii(a) is True |
| 808 |
assert module.is_compact(a) is False |
| 809 |
|
| 810 |
def test_fsconverter(self): |
| 811 |
module = self.import_extension("foo", [ |
| 812 |
('fsdecoder', "METH_O", |
| 813 |
""" |
| 814 |
PyObject *result = Py_None; |
| 815 |
int ret; |
| 816 |
Py_INCREF(result); |
| 817 |
Py_INCREF(result); |
| 818 |
if (args == Py_None) { |
| 819 |
ret = PyUnicode_FSDecoder(NULL, &result); |
| 820 |
if (ret != 1) |
| 821 |
return PyLong_FromLong(ret); |
| 822 |
} |
| 823 |
else |
| 824 |
ret = PyUnicode_FSDecoder(args, &result); |
| 825 |
if (ret == 0) |
| 826 |
return NULL; |
| 827 |
if (ret == 1) |
| 828 |
return Py_None; |
| 829 |
if (ret != Py_CLEANUP_SUPPORTED) |
| 830 |
return NULL; |
| 831 |
return result; |
| 832 |
"""), |
| 833 |
('fsconverter', "METH_O", |
| 834 |
""" |
| 835 |
PyObject *result = Py_None; |
| 836 |
int ret; |
| 837 |
Py_INCREF(result); |
| 838 |
Py_INCREF(result); |
| 839 |
if (args == Py_None) { |
| 840 |
ret = PyUnicode_FSDecoder(NULL, &result); |
| 841 |
if (ret != 1) |
| 842 |
return PyLong_FromLong(ret); |
| 843 |
} |
| 844 |
else |
| 845 |
ret = PyUnicode_FSConverter(args, &result); |
| 846 |
if (ret == 0) |
| 847 |
return NULL; |
| 848 |
if (ret == 1) |
| 849 |
return Py_None; |
| 850 |
if (ret != Py_CLEANUP_SUPPORTED) |
| 851 |
return NULL; |
| 852 |
return result; |
| 853 |
""")]) |
| 854 |
|
| 855 |
assert module.fsdecoder(None) is None |
| 856 |
assert module.fsconverter(None) is None |
| 857 |
|
| 858 |
assert module.fsdecoder(b"test") == "test" |
| 859 |
assert module.fsconverter(b"test") == b"test" |
| 860 |
|
| 861 |
|
| 862 |
assert module.fsdecoder("test") == "test" |
| 863 |
assert module.fsconverter("test") == b"test" |
| 864 |
|
| 865 |
|
| 866 |
class pathlike_str(object): |
| 867 |
def __fspath__(self): |
| 868 |
return "test" |
| 869 |
|
| 870 |
inpt = pathlike_str() |
| 871 |
assert module.fsdecoder(inpt) == "test" |
| 872 |
assert module.fsconverter(inpt) == b"test" |
| 873 |
|
| 874 |
|
| 875 |
with raises(TypeError): |
| 876 |
module.fsconverter(42) |
| 877 |
|
| 878 |
def test_cython_unicode_tests(self): |
| 879 |
module = self.import_extension("foo", [ |
| 880 |
('encoderaw', "METH_O", |
| 881 |
""" |
| 882 |
return PyUnicode_AsRawUnicodeEscapeString(args); |
| 883 |
"""), |
| 884 |
('decoderaw', "METH_O", |
| 885 |
""" |
| 886 |
char* as_c_string; |
| 887 |
Py_ssize_t size; |
| 888 |
if (PyBytes_AsStringAndSize(args, &as_c_string, &size) < 0) { |
| 889 |
return NULL; |
| 890 |
} |
| 891 |
return PyUnicode_DecodeRawUnicodeEscape(as_c_string, size, NULL); |
| 892 |
"""), |
| 893 |
]) |
| 894 |
|
| 895 |
x = u'\U00100000' |
| 896 |
y = module.decoderaw(module.encoderaw(x)) |
| 897 |
assert y == x |
| 898 |
|
| 899 |
x = b'\U00100000' |
| 900 |
y = module.encoderaw(module.decoderaw(x)) |
| 901 |
assert y == x |
| 902 |
|
| 903 |
x = b'\U00010000' |
| 904 |
y = module.encoderaw(module.decoderaw(x)) |
| 905 |
assert y == x |
| 906 |
|
| 907 |
try: |
| 908 |
module.decoderaw(br'\U11111111') |
| 909 |
except UnicodeDecodeError as e: |
| 910 |
assert e.start == 0 |
| 911 |
assert e.end == 10 |
| 912 |
else: |
| 913 |
assert False, "Should have raised UnicodeDecodeError" |
| 914 |
|
| 915 |
|
| 916 |
class TestUnicode(BaseApiTest): |
| 917 |
def test_unicodeobject(self, space): |
| 918 |
encoding = rffi.charp2str(PyUnicode_GetDefaultEncoding(space, )) |
| 919 |
w_default_encoding = space.call_function( |
| 920 |
space.sys.get('getdefaultencoding') |
| 921 |
) |
| 922 |
assert encoding == space.unwrap(w_default_encoding) |
| 923 |
|
| 924 |
def test_AS(self, space): |
| 925 |
word = space.wrap(u'spam') |
| 926 |
array = rffi.cast(rffi.CWCHARP, PyUnicode_AsUnicodeAndSize(space, word, None)) |
| 927 |
array2 = PyUnicode_AsUnicodeAndSize(space, word, None) |
| 928 |
for (i, char) in enumerate(space.utf8_w(word)): |
| 929 |
assert array[i] == char |
| 930 |
assert array2[i] == char |
| 931 |
with raises_w(space, TypeError): |
| 932 |
PyUnicode_AsUnicodeAndSize(space, space.newbytes('spam'), None) |
| 933 |
|
| 934 |
utf_8 = rffi.str2charp('utf-8') |
| 935 |
encoded = PyUnicode_AsEncodedString(space, space.wrap(u'späm'), |
| 936 |
utf_8, None) |
| 937 |
assert space.unwrap(encoded) == 'sp\xc3\xa4m' |
| 938 |
encoded_obj = PyUnicode_AsEncodedObject(space, space.wrap(u'späm'), |
| 939 |
utf_8, None) |
| 940 |
assert space.eq_w(encoded, encoded_obj) |
| 941 |
one = space.newint(1) |
| 942 |
with raises_w(space, TypeError): |
| 943 |
PyUnicode_AsEncodedString( |
| 944 |
space, space.newtuple([one, one, one]), None, None) |
| 945 |
with raises_w(space, TypeError): |
| 946 |
PyUnicode_AsEncodedString(space, space.newbytes(''), None, None) |
| 947 |
ascii = rffi.str2charp('ascii') |
| 948 |
replace = rffi.str2charp('replace') |
| 949 |
encoded = PyUnicode_AsEncodedString(space, space.wrap(u'späm'), |
| 950 |
ascii, replace) |
| 951 |
assert space.unwrap(encoded) == 'sp?m' |
| 952 |
rffi.free_charp(utf_8) |
| 953 |
rffi.free_charp(replace) |
| 954 |
rffi.free_charp(ascii) |
| 955 |
|
| 956 |
buf = rffi.unicode2wcharp(u"12345") |
| 957 |
PyUnicode_AsWideChar(space, space.wrap(u'longword'), buf, 5) |
| 958 |
assert rffi.wcharp2unicode(buf) == 'longw' |
| 959 |
PyUnicode_AsWideChar(space, space.wrap(u'a'), buf, 5) |
| 960 |
assert rffi.wcharp2unicode(buf) == 'a' |
| 961 |
rffi.free_wcharp(buf) |
| 962 |
|
| 963 |
def test_fromstring(self, space): |
| 964 |
s = rffi.str2charp(u'sp\x09m'.encode("utf-8")) |
| 965 |
w_res = PyUnicode_FromString(space, s) |
| 966 |
assert space.utf8_w(w_res) == u'sp\x09m'.encode("utf-8") |
| 967 |
|
| 968 |
res = PyUnicode_FromStringAndSize(space, s, 4) |
| 969 |
w_res = from_ref(space, res) |
| 970 |
decref(space, res) |
| 971 |
assert space.utf8_w(w_res) == u'sp\x09m'.encode("utf-8") |
| 972 |
rffi.free_charp(s) |
| 973 |
|
| 974 |
def test_internfromstring(self, space): |
| 975 |
with rffi.scoped_str2charp('foo') as s: |
| 976 |
w_res = PyUnicode_InternFromString(space, s) |
| 977 |
assert space.unwrap(w_res) == u'foo' |
| 978 |
w_res2 = PyUnicode_InternFromString(space, s) |
| 979 |
assert w_res is w_res2 |
| 980 |
|
| 981 |
def test_unicode_resize(self, space): |
| 982 |
py_uni = new_empty_unicode(space, 10) |
| 983 |
ar = lltype.malloc(PyObjectP.TO, 1, flavor='raw') |
| 984 |
buf = get_wbuffer(py_uni) |
| 985 |
buf[0] = u'a' |
| 986 |
buf[1] = u'b' |
| 987 |
buf[2] = u'c' |
| 988 |
ar[0] = rffi.cast(PyObject, py_uni) |
| 989 |
PyUnicode_Resize(space, ar, 3) |
| 990 |
py_uni = rffi.cast(PyUnicodeObject, ar[0]) |
| 991 |
assert get_wsize(py_uni) == 3 |
| 992 |
assert get_wbuffer(py_uni)[1] == u'b' |
| 993 |
assert get_wbuffer(py_uni)[3] == u'\x00' |
| 994 |
|
| 995 |
ar[0] = rffi.cast(PyObject, py_uni) |
| 996 |
PyUnicode_Resize(space, ar, 10) |
| 997 |
py_uni = rffi.cast(PyUnicodeObject, ar[0]) |
| 998 |
assert get_wsize(py_uni) == 10 |
| 999 |
assert get_wbuffer(py_uni)[1] == 'b' |
| 1000 |
assert get_wbuffer(py_uni)[10] == '\x00' |
| 1001 |
decref(space, ar[0]) |
| 1002 |
lltype.free(ar, flavor='raw') |
| 1003 |
|
| 1004 |
def test_AsUTF8String(self, space): |
| 1005 |
w_u = space.wrap(u'sp\x09m\u1234') |
| 1006 |
w_res = PyUnicode_AsUTF8String(space, w_u) |
| 1007 |
assert space.type(w_res) is space.w_bytes |
| 1008 |
assert space.unwrap(w_res) == 'sp\tm\xe1\x88\xb4' |
| 1009 |
|
| 1010 |
def test_AsUTF16String(self, space): |
| 1011 |
u = u'sp\x09m\u1234\U00012345' |
| 1012 |
w_u = space.wrap(u) |
| 1013 |
w_res = PyUnicode_AsUTF16String(space, w_u) |
| 1014 |
assert space.type(w_res) is space.w_bytes |
| 1015 |
assert space.unwrap(w_res) == u.encode('utf-16') |
| 1016 |
|
| 1017 |
def test_AsUTF32String(self, space): |
| 1018 |
u = u'sp\x09m\u1234\U00012345' |
| 1019 |
w_u = space.wrap(u) |
| 1020 |
w_res = PyUnicode_AsUTF32String(space, w_u) |
| 1021 |
assert space.type(w_res) is space.w_bytes |
| 1022 |
assert space.unwrap(w_res) == u.encode('utf-32') |
| 1023 |
|
| 1024 |
def test_decode_utf8(self, space): |
| 1025 |
u = rffi.str2charp(u'sp\x134m'.encode("utf-8")) |
| 1026 |
w_u = PyUnicode_DecodeUTF8(space, u, 5, None) |
| 1027 |
assert space.type(w_u) is space.w_unicode |
| 1028 |
assert space.utf8_w(w_u) == u'sp\x134m'.encode("utf-8") |
| 1029 |
|
| 1030 |
w_u = PyUnicode_DecodeUTF8(space, u, 2, None) |
| 1031 |
assert space.type(w_u) is space.w_unicode |
| 1032 |
assert space.utf8_w(w_u) == 'sp' |
| 1033 |
rffi.free_charp(u) |
| 1034 |
|
| 1035 |
def test_encode_utf8(self, space): |
| 1036 |
u = rffi.unicode2wcharp(u'sp\x09m') |
| 1037 |
w_b = PyUnicode_EncodeUTF8(space, u, 4, None) |
| 1038 |
assert space.type(w_b) is space.w_bytes |
| 1039 |
assert space.bytes_w(w_b) == u'sp\x09m'.encode('utf-8') |
| 1040 |
rffi.free_wcharp(u) |
| 1041 |
|
| 1042 |
def test_encode_decimal(self, space): |
| 1043 |
with rffi.scoped_unicode2wcharp(u' (12, 35 ABC)') as u: |
| 1044 |
with rffi.scoped_alloc_buffer(20) as buf: |
| 1045 |
res = PyUnicode_EncodeDecimal(space, u, 13, buf.raw, None) |
| 1046 |
s = rffi.charp2str(buf.raw) |
| 1047 |
assert res == 0 |
| 1048 |
assert s == ' (12, 35 ABC)' |
| 1049 |
|
| 1050 |
with rffi.scoped_unicode2wcharp(u' (12, \u1234\u1235)') as u: |
| 1051 |
with rffi.scoped_alloc_buffer(20) as buf: |
| 1052 |
with pytest.raises(OperationError): |
| 1053 |
PyUnicode_EncodeDecimal(space, u, 9, buf.raw, None) |
| 1054 |
|
| 1055 |
with rffi.scoped_unicode2wcharp(u' (12, \u1234\u1235)') as u: |
| 1056 |
with rffi.scoped_alloc_buffer(20) as buf: |
| 1057 |
with rffi.scoped_str2charp("replace") as errors: |
| 1058 |
res = PyUnicode_EncodeDecimal(space, u, 9, buf.raw, |
| 1059 |
errors) |
| 1060 |
s = rffi.charp2str(buf.raw) |
| 1061 |
assert res == 0 |
| 1062 |
assert s == " (12, ??)" |
| 1063 |
|
| 1064 |
with rffi.scoped_unicode2wcharp(u'12\u1234') as u: |
| 1065 |
with rffi.scoped_alloc_buffer(20) as buf: |
| 1066 |
with rffi.scoped_str2charp("xmlcharrefreplace") as errors: |
| 1067 |
res = PyUnicode_EncodeDecimal(space, u, 3, buf.raw, |
| 1068 |
errors) |
| 1069 |
s = rffi.charp2str(buf.raw) |
| 1070 |
assert res == 0 |
| 1071 |
assert s == "12ሴ" |
| 1072 |
|
| 1073 |
def test_encode_fsdefault(self, space): |
| 1074 |
w_u = space.wrap(u'späm') |
| 1075 |
try: |
| 1076 |
w_s = PyUnicode_EncodeFSDefault(space, w_u) |
| 1077 |
except (OperationError, UnicodeEncodeError): |
| 1078 |
py.test.skip("Requires a unicode-aware fsencoding") |
| 1079 |
with rffi.scoped_str2charp(space.bytes_w(w_s)) as encoded: |
| 1080 |
w_decoded = PyUnicode_DecodeFSDefaultAndSize(space, encoded, space.len_w(w_s)) |
| 1081 |
assert space.eq_w(w_decoded, w_u) |
| 1082 |
w_decoded = PyUnicode_DecodeFSDefault(space, encoded) |
| 1083 |
assert space.eq_w(w_decoded, w_u) |
| 1084 |
|
| 1085 |
|
| 1086 |
def test_locale(self, space): |
| 1087 |
|
| 1088 |
with rffi.scoped_str2charp('test') as test: |
| 1089 |
with rffi.scoped_str2charp('strict') as errors: |
| 1090 |
w_ret = PyUnicode_DecodeLocale(space, test, errors) |
| 1091 |
assert space.utf8_w(w_ret) == 'test' |
| 1092 |
with rffi.scoped_str2charp('surrogateescape') as errors: |
| 1093 |
w_ret = PyUnicode_DecodeLocale(space, test, errors) |
| 1094 |
assert space.utf8_w(w_ret) == 'test' |
| 1095 |
|
| 1096 |
|
| 1097 |
w_input = space.newtext("test", 4) |
| 1098 |
with rffi.scoped_str2charp('strict') as errors: |
| 1099 |
w_ret = PyUnicode_EncodeLocale(space, w_input, errors) |
| 1100 |
assert space.bytes_w(w_ret) == b'test' |
| 1101 |
with rffi.scoped_str2charp(None) as errors: |
| 1102 |
w_ret = PyUnicode_EncodeLocale(space, w_input, errors) |
| 1103 |
assert space.bytes_w(w_ret) == b'test' |
| 1104 |
with rffi.scoped_str2charp('surrogateescape') as errors: |
| 1105 |
w_ret = PyUnicode_EncodeLocale(space, w_input, errors) |
| 1106 |
assert space.bytes_w(w_ret) == b'test' |
| 1107 |
|
| 1108 |
with rffi.scoped_str2charp('something else') as errors: |
| 1109 |
with pytest.raises(OperationError): |
| 1110 |
PyUnicode_EncodeLocale(space, w_input, errors) |
| 1111 |
|
| 1112 |
|
| 1113 |
def test_IS(self, space): |
| 1114 |
from pypy.module.cpyext.unicodeobject import (_PyUnicode_IsAlpha, |
| 1115 |
_PyUnicode_IsDecimalDigit, _PyUnicode_IsDigit, _PyUnicode_IsNumeric, |
| 1116 |
_PyUnicode_IsLowercase, _PyUnicode_IsUppercase, |
| 1117 |
_PyUnicode_IsTitlecase) |
| 1118 |
assert not _PyUnicode_IsAlpha(space, ord('0')) |
| 1119 |
assert _PyUnicode_IsAlpha(space, ord('a')) |
| 1120 |
assert not _PyUnicode_IsAlpha(space, ord('+')) |
| 1121 |
|
| 1122 |
assert _PyUnicode_IsDecimalDigit(space, ord(u'\u0660')) |
| 1123 |
assert not _PyUnicode_IsDecimalDigit(space, ord('a')) |
| 1124 |
assert _PyUnicode_IsDigit(space, ord('9')) |
| 1125 |
assert not _PyUnicode_IsDigit(space, ord('@')) |
| 1126 |
assert _PyUnicode_IsNumeric(space, ord('9')) |
| 1127 |
assert not _PyUnicode_IsNumeric(space, ord('@')) |
| 1128 |
|
| 1129 |
assert _PyUnicode_IsLowercase(space, ord('\xdf')) |
| 1130 |
assert _PyUnicode_IsUppercase(space, ord('\xde')) |
| 1131 |
assert _PyUnicode_IsLowercase(space, ord('a')) |
| 1132 |
assert not _PyUnicode_IsUppercase(space, ord('a')) |
| 1133 |
assert not _PyUnicode_IsTitlecase(space, ord('\xce')) |
| 1134 |
assert _PyUnicode_IsTitlecase(space, |
| 1135 |
ord(u'\N{LATIN CAPITAL LETTER L WITH SMALL LETTER J}')) |
| 1136 |
|
| 1137 |
def test_TOLOWER(self, space): |
| 1138 |
from pypy.module.cpyext.unicodeobject import _PyUnicode_ToLowercase |
| 1139 |
assert _PyUnicode_ToLowercase(space, ord(u'�') == ord(u'�')) |
| 1140 |
assert _PyUnicode_ToLowercase(space, ord(u'�') == ord(u'�')) |
| 1141 |
|
| 1142 |
def test_TOUPPER(self, space): |
| 1143 |
from pypy.module.cpyext.unicodeobject import _PyUnicode_ToUppercase |
| 1144 |
assert _PyUnicode_ToUppercase(space, ord(u'�') == ord(u'�')) |
| 1145 |
assert _PyUnicode_ToUppercase(space, ord(u'�') == ord(u'�')) |
| 1146 |
|
| 1147 |
def test_TOTITLE(self, space): |
| 1148 |
from pypy.module.cpyext.unicodeobject import _PyUnicode_ToTitlecase |
| 1149 |
assert _PyUnicode_ToTitlecase(space, ord('/') == ord('/')) |
| 1150 |
|
| 1151 |
def test_TODECIMAL(self, space): |
| 1152 |
from pypy.module.cpyext.unicodeobject import _PyUnicode_ToDecimalDigit |
| 1153 |
assert _PyUnicode_ToDecimalDigit(space, ord('6')) == 6 |
| 1154 |
assert _PyUnicode_ToDecimalDigit(space, ord('A')) == -1 |
| 1155 |
|
| 1156 |
def test_TODIGIT(self, space): |
| 1157 |
from pypy.module.cpyext.unicodeobject import _PyUnicode_ToDigit |
| 1158 |
assert _PyUnicode_ToDigit(space, ord('6')) == 6 |
| 1159 |
assert _PyUnicode_ToDigit(space, ord('A')) == -1 |
| 1160 |
|
| 1161 |
def test_transform_decimal(self, space): |
| 1162 |
def transform_decimal(s): |
| 1163 |
with rffi.scoped_unicode2wcharp(s) as u: |
| 1164 |
return space.unwrap( |
| 1165 |
PyUnicode_TransformDecimalToASCII(space, u, len(s))) |
| 1166 |
assert isinstance(transform_decimal(u'123'), unicode) |
| 1167 |
assert transform_decimal(u'123') == u'123' |
| 1168 |
assert transform_decimal(u'\u0663.\u0661\u0664') == u'3.14' |
| 1169 |
assert transform_decimal(u"\N{EM SPACE}3.14\N{EN SPACE}") == ( |
| 1170 |
u"\N{EM SPACE}3.14\N{EN SPACE}") |
| 1171 |
assert transform_decimal(u'123\u20ac') == u'123\u20ac' |
| 1172 |
|
| 1173 |
def test_fromobject(self, space): |
| 1174 |
w_u = space.wrap(u'a') |
| 1175 |
assert PyUnicode_FromObject(space, w_u) is w_u |
| 1176 |
with raises_w(space, TypeError): |
| 1177 |
PyUnicode_FromObject(space, space.newbytes('test')) |
| 1178 |
with raises_w(space, TypeError): |
| 1179 |
PyUnicode_FromObject(space, space.newint(42)) |
| 1180 |
|
| 1181 |
|
| 1182 |
def test_decode(self, space): |
| 1183 |
b_text = rffi.str2charp('caf\x82xx') |
| 1184 |
b_encoding = rffi.str2charp('cp437') |
| 1185 |
b_errors = rffi.str2charp('strict') |
| 1186 |
assert space.text_w(PyUnicode_Decode( |
| 1187 |
space, b_text, 4, b_encoding, b_errors)).decode('utf8') == u'caf\xe9' |
| 1188 |
assert (space.text_w( |
| 1189 |
PyUnicode_Decode(space, b_text, 4, b_encoding, None)) == |
| 1190 |
u'caf\xe9'.encode("utf-8")) |
| 1191 |
|
| 1192 |
w_text = PyUnicode_FromEncodedObject(space, space.newbytes("test"), b_encoding, None) |
| 1193 |
assert space.isinstance_w(w_text, space.w_unicode) |
| 1194 |
assert space.utf8_w(w_text) == "test" |
| 1195 |
|
| 1196 |
with raises_w(space, TypeError): |
| 1197 |
PyUnicode_FromEncodedObject(space, space.wrap(u"test"), |
| 1198 |
b_encoding, None) |
| 1199 |
with raises_w(space, TypeError): |
| 1200 |
PyUnicode_FromEncodedObject(space, space.wrap(1), b_encoding, None) |
| 1201 |
|
| 1202 |
rffi.free_charp(b_text) |
| 1203 |
rffi.free_charp(b_encoding) |
| 1204 |
|
| 1205 |
def test_decode_null_encoding(self, space): |
| 1206 |
null_charp = lltype.nullptr(rffi.CCHARP.TO) |
| 1207 |
u_text = u'abcdefg' |
| 1208 |
s_text = space.bytes_w(PyUnicode_AsEncodedString(space, space.wrap(u_text), null_charp, null_charp)) |
| 1209 |
b_text = rffi.str2charp(s_text) |
| 1210 |
assert (space.utf8_w(PyUnicode_Decode( |
| 1211 |
space, b_text, len(s_text), null_charp, null_charp)) == |
| 1212 |
u_text.encode("utf-8")) |
| 1213 |
with raises_w(space, TypeError): |
| 1214 |
PyUnicode_FromEncodedObject( |
| 1215 |
space, space.wrap(u_text), null_charp, None) |
| 1216 |
assert space.text_w(PyUnicode_FromEncodedObject( |
| 1217 |
space, space.newbytes(s_text), null_charp, None)) == u_text |
| 1218 |
rffi.free_charp(b_text) |
| 1219 |
|
| 1220 |
def test_mbcs(self, space): |
| 1221 |
if sys.platform != 'win32': |
| 1222 |
py.test.skip("mcbs encoding only exists on Windows") |
| 1223 |
unichars = u"abc" |
| 1224 |
wbuf = rffi.unicode2wcharp(unichars) |
| 1225 |
try: |
| 1226 |
w_bytes = PyUnicode_EncodeMBCS(space, wbuf, 3, None) |
| 1227 |
finally: |
| 1228 |
rffi.free_wcharp(wbuf) |
| 1229 |
assert space.type(w_bytes) is space.w_bytes |
| 1230 |
assert space.utf8_w(w_bytes) == "abc" |
| 1231 |
|
| 1232 |
def test_codepage(self, space): |
| 1233 |
if sys.platform != 'win32': |
| 1234 |
py.test.skip("codepage encoding only exists on Windows") |
| 1235 |
chars = "abc" |
| 1236 |
w_obj = space.newtext(chars) |
| 1237 |
w_bytes = PyUnicode_EncodeCodePage(space, 932, w_obj, None) |
| 1238 |
assert space.type(w_bytes) is space.w_bytes |
| 1239 |
assert space.utf8_w(w_bytes) == "abc" |
| 1240 |
|
| 1241 |
def test_escape(self, space): |
| 1242 |
def test(ustr): |
| 1243 |
w_ustr = space.wrap(ustr.decode('Unicode-Escape')) |
| 1244 |
result = PyUnicode_AsUnicodeEscapeString(space, w_ustr) |
| 1245 |
assert space.eq_w(space.newbytes(ustr), result) |
| 1246 |
|
| 1247 |
test('\\u674f\\u7f8e') |
| 1248 |
test('\\u0105\\u0107\\u017c\\u017a') |
| 1249 |
test('El Ni\\xf1o') |
| 1250 |
|
| 1251 |
def test_ascii(self, space): |
| 1252 |
ustr = "abcdef" |
| 1253 |
w_ustr = space.wrap(ustr.decode("ascii")) |
| 1254 |
result = PyUnicode_AsASCIIString(space, w_ustr) |
| 1255 |
assert space.eq_w(space.newbytes(ustr), result) |
| 1256 |
with raises_w(space, UnicodeEncodeError): |
| 1257 |
PyUnicode_AsASCIIString(space, space.wrap(u"abcd\xe9f")) |
| 1258 |
|
| 1259 |
def test_decode_utf16(self, space): |
| 1260 |
def test(encoded, endian, realendian=None): |
| 1261 |
encoded_charp = rffi.str2charp(encoded) |
| 1262 |
strict_charp = rffi.str2charp("strict") |
| 1263 |
if endian is not None: |
| 1264 |
if endian < 0: |
| 1265 |
value = -1 |
| 1266 |
elif endian > 0: |
| 1267 |
value = 1 |
| 1268 |
else: |
| 1269 |
value = 0 |
| 1270 |
pendian = lltype.malloc(INTP_real.TO, 1, flavor='raw') |
| 1271 |
pendian[0] = rffi.cast(rffi.INT_real, value) |
| 1272 |
else: |
| 1273 |
pendian = None |
| 1274 |
|
| 1275 |
w_ustr = PyUnicode_DecodeUTF16(space, encoded_charp, len(encoded), strict_charp, pendian) |
| 1276 |
assert space.eq_w(space.call_method(w_ustr, 'encode', space.wrap('ascii')), |
| 1277 |
space.newbytes("abcd")) |
| 1278 |
|
| 1279 |
rffi.free_charp(encoded_charp) |
| 1280 |
rffi.free_charp(strict_charp) |
| 1281 |
if pendian: |
| 1282 |
if realendian is not None: |
| 1283 |
assert rffi.cast(rffi.INT_real, realendian) == pendian[0] |
| 1284 |
lltype.free(pendian, flavor='raw') |
| 1285 |
|
| 1286 |
test("\x61\x00\x62\x00\x63\x00\x64\x00", -1) |
| 1287 |
if sys.byteorder == 'big': |
| 1288 |
test("\x00\x61\x00\x62\x00\x63\x00\x64", None) |
| 1289 |
else: |
| 1290 |
test("\x61\x00\x62\x00\x63\x00\x64\x00", None) |
| 1291 |
test("\x00\x61\x00\x62\x00\x63\x00\x64", 1) |
| 1292 |
test("\xFE\xFF\x00\x61\x00\x62\x00\x63\x00\x64", 0, 1) |
| 1293 |
test("\xFF\xFE\x61\x00\x62\x00\x63\x00\x64\x00", 0, -1) |
| 1294 |
|
| 1295 |
def test_decode_utf32(self, space): |
| 1296 |
def test(encoded, endian, realendian=None): |
| 1297 |
encoded_charp = rffi.str2charp(encoded) |
| 1298 |
strict_charp = rffi.str2charp("strict") |
| 1299 |
if endian is not None: |
| 1300 |
if endian < 0: |
| 1301 |
value = -1 |
| 1302 |
elif endian > 0: |
| 1303 |
value = 1 |
| 1304 |
else: |
| 1305 |
value = 0 |
| 1306 |
pendian = lltype.malloc(INTP_real.TO, 1, flavor='raw') |
| 1307 |
pendian[0] = rffi.cast(rffi.INT_real, value) |
| 1308 |
else: |
| 1309 |
pendian = None |
| 1310 |
|
| 1311 |
w_ustr = PyUnicode_DecodeUTF32(space, encoded_charp, len(encoded), |
| 1312 |
strict_charp, pendian) |
| 1313 |
assert space.eq_w(space.call_method(w_ustr, 'encode', space.wrap('ascii')), |
| 1314 |
space.newbytes("ab")) |
| 1315 |
|
| 1316 |
rffi.free_charp(encoded_charp) |
| 1317 |
rffi.free_charp(strict_charp) |
| 1318 |
if pendian: |
| 1319 |
if realendian is not None: |
| 1320 |
assert rffi.cast(rffi.INT, realendian) == pendian[0] |
| 1321 |
lltype.free(pendian, flavor='raw') |
| 1322 |
|
| 1323 |
test("\x61\x00\x00\x00\x62\x00\x00\x00", -1) |
| 1324 |
|
| 1325 |
if sys.byteorder == 'big': |
| 1326 |
test("\x00\x00\x00\x61\x00\x00\x00\x62", None) |
| 1327 |
else: |
| 1328 |
test("\x61\x00\x00\x00\x62\x00\x00\x00", None) |
| 1329 |
|
| 1330 |
test("\x00\x00\x00\x61\x00\x00\x00\x62", 1) |
| 1331 |
|
| 1332 |
test("\x00\x00\xFE\xFF\x00\x00\x00\x61\x00\x00\x00\x62", 0, 1) |
| 1333 |
test("\xFF\xFE\x00\x00\x61\x00\x00\x00\x62\x00\x00\x00", 0, -1) |
| 1334 |
|
| 1335 |
def test_compare(self, space): |
| 1336 |
assert PyUnicode_Compare(space, space.wrap('a'), space.wrap('b')) == -1 |
| 1337 |
|
| 1338 |
def test_concat(self, space): |
| 1339 |
w_res = PyUnicode_Concat(space, space.wrap(u'a'), space.wrap(u'b')) |
| 1340 |
assert space.utf8_w(w_res) == 'ab' |
| 1341 |
|
| 1342 |
def test_copy(self, space): |
| 1343 |
w_x = space.wrap(u"abcd\u0660") |
| 1344 |
count1 = space.int_w(space.len(w_x)) |
| 1345 |
target_chunk = lltype.malloc(rffi.CWCHARP.TO, count1, flavor='raw') |
| 1346 |
|
| 1347 |
x_chunk = PyUnicode_AsUnicodeAndSize(space, w_x, None) |
| 1348 |
Py_UNICODE_COPY(space, target_chunk, x_chunk, 4) |
| 1349 |
w_y = space.wrap(rffi.wcharpsize2unicode(target_chunk, 4)) |
| 1350 |
|
| 1351 |
assert space.eq_w(w_y, space.wrap(u"abcd")) |
| 1352 |
|
| 1353 |
size = get_wsize(as_pyobj(space, w_x)) |
| 1354 |
Py_UNICODE_COPY(space, target_chunk, x_chunk, size) |
| 1355 |
w_y = space.wrap(rffi.wcharpsize2unicode(target_chunk, size)) |
| 1356 |
|
| 1357 |
assert space.eq_w(w_y, w_x) |
| 1358 |
|
| 1359 |
lltype.free(target_chunk, flavor='raw') |
| 1360 |
|
| 1361 |
def test_ascii_codec(self, space): |
| 1362 |
s = 'abcdefg' |
| 1363 |
data = rffi.str2charp(s) |
| 1364 |
NULL = lltype.nullptr(rffi.CCHARP.TO) |
| 1365 |
w_u = PyUnicode_DecodeASCII(space, data, len(s), NULL) |
| 1366 |
assert space.eq_w(w_u, space.wrap(u"abcdefg")) |
| 1367 |
rffi.free_charp(data) |
| 1368 |
|
| 1369 |
s = 'abcd\xFF' |
| 1370 |
data = rffi.str2charp(s) |
| 1371 |
with raises_w(space, UnicodeDecodeError): |
| 1372 |
PyUnicode_DecodeASCII(space, data, len(s), NULL) |
| 1373 |
rffi.free_charp(data) |
| 1374 |
|
| 1375 |
uni = u'abcdefg' |
| 1376 |
data = rffi.unicode2wcharp(uni) |
| 1377 |
w_s = PyUnicode_EncodeASCII(space, data, len(uni), NULL) |
| 1378 |
assert space.eq_w(space.newbytes("abcdefg"), w_s) |
| 1379 |
rffi.free_wcharp(data) |
| 1380 |
|
| 1381 |
u = u'�bcd�fg' |
| 1382 |
data = rffi.unicode2wcharp(u) |
| 1383 |
with raises_w(space, UnicodeEncodeError): |
| 1384 |
PyUnicode_EncodeASCII(space, data, len(u), NULL) |
| 1385 |
rffi.free_wcharp(data) |
| 1386 |
|
| 1387 |
def test_latin1(self, space): |
| 1388 |
s = 'abcdefg' |
| 1389 |
data = rffi.str2charp(s) |
| 1390 |
w_u = PyUnicode_DecodeLatin1(space, data, len(s), |
| 1391 |
lltype.nullptr(rffi.CCHARP.TO)) |
| 1392 |
assert space.eq_w(w_u, space.wrap(u"abcdefg")) |
| 1393 |
rffi.free_charp(data) |
| 1394 |
|
| 1395 |
uni = u'abcdefg' |
| 1396 |
data = rffi.unicode2wcharp(uni) |
| 1397 |
w_s = PyUnicode_EncodeLatin1(space, data, len(uni), |
| 1398 |
lltype.nullptr(rffi.CCHARP.TO)) |
| 1399 |
assert space.eq_w(space.newbytes("abcdefg"), w_s) |
| 1400 |
rffi.free_wcharp(data) |
| 1401 |
|
| 1402 |
ustr = "abcdef" |
| 1403 |
w_ustr = space.wrap(ustr.decode("ascii")) |
| 1404 |
result = PyUnicode_AsLatin1String(space, w_ustr) |
| 1405 |
assert space.eq_w(space.newbytes(ustr), result) |
| 1406 |
|
| 1407 |
def test_format(self, space): |
| 1408 |
w_format = space.wrap(u'hi %s') |
| 1409 |
w_args = space.wrap((u'test',)) |
| 1410 |
w_formated = PyUnicode_Format(space, w_format, w_args) |
| 1411 |
assert (space.utf8_w(w_formated) == |
| 1412 |
space.utf8_w(space.mod(w_format, w_args))) |
| 1413 |
|
| 1414 |
def test_join(self, space): |
| 1415 |
w_sep = space.wrap(u'<sep>') |
| 1416 |
w_seq = space.wrap([u'a', u'b']) |
| 1417 |
w_joined = PyUnicode_Join(space, w_sep, w_seq) |
| 1418 |
assert space.utf8_w(w_joined) == u'a<sep>b'.encode("utf-8") |
| 1419 |
|
| 1420 |
def test_fromordinal(self, space): |
| 1421 |
w_char = PyUnicode_FromOrdinal(space, 65) |
| 1422 |
assert space.utf8_w(w_char) == u'A' |
| 1423 |
w_char = PyUnicode_FromOrdinal(space, 0) |
| 1424 |
assert space.utf8_w(w_char) == u'\0' |
| 1425 |
w_char = PyUnicode_FromOrdinal(space, 0xFFFF) |
| 1426 |
assert space.utf8_w(w_char) == u'\uFFFF'.encode('utf-8') |
| 1427 |
|
| 1428 |
def test_replace(self, space): |
| 1429 |
w_str = space.wrap(u"abababab") |
| 1430 |
w_substr = space.wrap(u"a") |
| 1431 |
w_replstr = space.wrap(u"z") |
| 1432 |
assert u"zbzbabab" == space.utf8_w( |
| 1433 |
PyUnicode_Replace(space, w_str, w_substr, w_replstr, 2)) |
| 1434 |
assert u"zbzbzbzb" == space.utf8_w( |
| 1435 |
PyUnicode_Replace(space, w_str, w_substr, w_replstr, -1)) |
| 1436 |
|
| 1437 |
def test_tailmatch(self, space): |
| 1438 |
w_str = space.wrap(u"abcdef") |
| 1439 |
|
| 1440 |
assert PyUnicode_Tailmatch(space, w_str, space.wrap("cde"), 2, 9, -1) == 1 |
| 1441 |
assert PyUnicode_Tailmatch(space, w_str, space.wrap("cde"), 2, 4, -1) == 0 |
| 1442 |
assert PyUnicode_Tailmatch(space, w_str, space.wrap("cde"), 1, 6, -1) == 0 |
| 1443 |
assert PyUnicode_Tailmatch(space, w_str, space.wrap("cdf"), 2, 6, -1) == 0 |
| 1444 |
|
| 1445 |
assert PyUnicode_Tailmatch(space, w_str, space.wrap("cde"), 1, 5, 1) == 1 |
| 1446 |
assert PyUnicode_Tailmatch(space, w_str, space.wrap("cde"), 3, 5, 1) == 0 |
| 1447 |
assert PyUnicode_Tailmatch(space, w_str, space.wrap("cde"), 1, 6, 1) == 0 |
| 1448 |
assert PyUnicode_Tailmatch(space, w_str, space.wrap("bde"), 1, 5, 1) == 0 |
| 1449 |
|
| 1450 |
with raises_w(space, TypeError): |
| 1451 |
PyUnicode_Tailmatch(space, w_str, space.wrap(3), 2, 10, 1) |
| 1452 |
with raises_w(space, TypeError): |
| 1453 |
PyUnicode_Tailmatch( |
| 1454 |
space, space.wrap(3), space.wrap("abc"), 2, 10, 1) |
| 1455 |
|
| 1456 |
def test_count(self, space): |
| 1457 |
w_str = space.wrap(u"abcabdab") |
| 1458 |
assert PyUnicode_Count(space, w_str, space.wrap(u"ab"), 0, -1) == 2 |
| 1459 |
assert PyUnicode_Count(space, w_str, space.wrap(u"ab"), 0, 2) == 1 |
| 1460 |
assert PyUnicode_Count(space, w_str, space.wrap(u"ab"), -5, 30) == 2 |
| 1461 |
|
| 1462 |
def test_find(self, space): |
| 1463 |
w_str = space.wrap(u"abcabcd") |
| 1464 |
assert PyUnicode_Find(space, w_str, space.wrap(u"c"), 0, 7, 1) == 2 |
| 1465 |
assert PyUnicode_Find(space, w_str, space.wrap(u"c"), 3, 7, 1) == 5 |
| 1466 |
assert PyUnicode_Find(space, w_str, space.wrap(u"c"), 0, 7, -1) == 5 |
| 1467 |
assert PyUnicode_Find(space, w_str, space.wrap(u"c"), 3, 7, -1) == 5 |
| 1468 |
assert PyUnicode_Find(space, w_str, space.wrap(u"c"), 0, 4, -1) == 2 |
| 1469 |
assert PyUnicode_Find(space, w_str, space.wrap(u"z"), 0, 4, -1) == -1 |
| 1470 |
|
| 1471 |
def test_split(self, space): |
| 1472 |
w_str = space.wrap(u"a\nb\nc\nd") |
| 1473 |
assert "['a', 'b', 'c', 'd']" == space.unwrap(space.repr( |
| 1474 |
PyUnicode_Split(space, w_str, space.wrap('\n'), -1))) |
| 1475 |
assert r"['a', 'b', 'c\nd']" == space.unwrap(space.repr( |
| 1476 |
PyUnicode_Split(space, w_str, space.wrap('\n'), 2))) |
| 1477 |
assert r"['a', 'b', 'c d']" == space.unwrap(space.repr( |
| 1478 |
PyUnicode_Split(space, space.wrap(u'a\nb c d'), None, 2))) |
| 1479 |
assert "['a', 'b', 'c', 'd']" == space.unwrap(space.repr( |
| 1480 |
PyUnicode_Splitlines(space, w_str, 0))) |
| 1481 |
assert r"['a\n', 'b\n', 'c\n', 'd']" == space.unwrap(space.repr( |
| 1482 |
PyUnicode_Splitlines(space, w_str, 1))) |
| 1483 |
|
| 1484 |
def test_substring_api(self, space): |
| 1485 |
w_str = space.wrap(u"abcd") |
| 1486 |
assert space.unwrap(PyUnicode_Substring(space, w_str, 1, 3)) == u"bc" |
| 1487 |
assert space.unwrap(PyUnicode_Substring(space, w_str, 0, 4)) == u"abcd" |
| 1488 |
assert space.unwrap(PyUnicode_Substring(space, w_str, 0, 9)) == u"abcd" |
| 1489 |
assert space.unwrap(PyUnicode_Substring(space, w_str, 1, 4)) == u"bcd" |
| 1490 |
assert space.unwrap(PyUnicode_Substring(space, w_str, 2, 2)) == u"" |
| 1491 |
assert space.unwrap(PyUnicode_Substring(space, w_str, 5, 4)) == u"" |
| 1492 |
assert space.unwrap(PyUnicode_Substring(space, w_str, 5, 3)) == u"" |
| 1493 |
assert space.unwrap(PyUnicode_Substring(space, w_str, 4, 3)) == u"" |
| 1494 |
|
| 1495 |
def test_Ready(self, space): |
| 1496 |
def as_py_uni(val): |
| 1497 |
py_obj = new_empty_unicode(space, len(val)) |
| 1498 |
w_obj = space.wrap(val) |
| 1499 |
|
| 1500 |
unicode_attach(space, py_obj, w_obj) |
| 1501 |
return py_obj |
| 1502 |
|
| 1503 |
py_str = as_py_uni(u'abc') |
| 1504 |
assert get_kind(py_str) == 1 |
| 1505 |
assert get_ascii(py_str) == 1 |
| 1506 |
|
| 1507 |
py_str = as_py_uni(u'café') 1 |
| 1508 |
assert get_kind(py_str) == 1 |
| 1509 |
assert get_ascii(py_str) == 0 |
| 1510 |
|
| 1511 |
py_str = as_py_uni(u'Росси # UCS2 |
| 1512 |
assert get_kind(py_str) == 2 |
| 1513 |
assert get_ascii(py_str) == 0 |
| 1514 |
|
| 1515 |
py_str = as_py_uni(u'***\U0001f4a9***') |
| 1516 |
assert get_kind(py_str) == 4 |
| 1517 |
assert get_ascii(py_str) == 0 |
| 1518 |
|
| 1519 |
def test_as_ucs4(self, space): |
| 1520 |
w_x = space.wrap(u"ab\u0660") |
| 1521 |
count1 = space.int_w(space.len(w_x)) |
| 1522 |
x_chunk = PyUnicode_AsUCS4Copy(space, w_x) |
| 1523 |
assert x_chunk[0] == ord('a') |
| 1524 |
assert x_chunk[1] == ord('b') |
| 1525 |
assert x_chunk[2] == 0x0660 |
| 1526 |
assert x_chunk[3] == 0 |
| 1527 |
Py_UCS4 = lltype.typeOf(x_chunk).TO.OF |
| 1528 |
lltype.free(x_chunk, flavor='raw', track_allocation=False) |
| 1529 |
|
| 1530 |
target_chunk = lltype.malloc(rffi.CArray(Py_UCS4), 4, flavor='raw') |
| 1531 |
target_chunk[3] = rffi.cast(Py_UCS4, 99999) |
| 1532 |
x_chunk = PyUnicode_AsUCS4(space, w_x, target_chunk, 3, 0) |
| 1533 |
assert x_chunk == target_chunk |
| 1534 |
assert x_chunk[0] == ord('a') |
| 1535 |
assert x_chunk[1] == ord('b') |
| 1536 |
assert x_chunk[2] == 0x0660 |
| 1537 |
assert x_chunk[3] == 99999 |
| 1538 |
|
| 1539 |
x_chunk[2] = rffi.cast(Py_UCS4, 77777) |
| 1540 |
x_chunk = PyUnicode_AsUCS4(space, w_x, target_chunk, 4, 1) |
| 1541 |
assert x_chunk == target_chunk |
| 1542 |
assert x_chunk[0] == ord('a') |
| 1543 |
assert x_chunk[1] == ord('b') |
| 1544 |
assert x_chunk[2] == 0x0660 |
| 1545 |
assert x_chunk[3] == 0 |
| 1546 |
lltype.free(target_chunk, flavor='raw') |
| 1547 |
|
| 1548 |
def test_wide_as_ucs4(self, space): |
| 1549 |
w_x = space.wrap(u'\U00100900') |
| 1550 |
x_chunk = PyUnicode_AsUCS4Copy(space, w_x) |
| 1551 |
assert x_chunk[0] == 0x00100900 |
| 1552 |
Py_UCS4 = lltype.typeOf(x_chunk).TO.OF |
| 1553 |
lltype.free(x_chunk, flavor='raw', track_allocation=False) |
| 1554 |
|
| 1555 |
target_chunk = lltype.malloc(rffi.CArray(Py_UCS4), 1, flavor='raw') |
| 1556 |
x_chunk = PyUnicode_AsUCS4(space, w_x, target_chunk, 1, 0) |
| 1557 |
assert x_chunk == target_chunk |
| 1558 |
assert x_chunk[0] == 0x00100900 |
| 1559 |
lltype.free(target_chunk, flavor='raw') |