Skip to content

Commit e3e3868

Browse files
committed
Add test for PyUnicode_New
1 parent e53ae3e commit e3e3868

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_unicode.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,4 +544,37 @@ def compile_module(self, name):
544544
cmpfunc=unhandled_error_compare
545545
)
546546

547+
test_PyUnicode_New = CPyExtFunction(
548+
lambda args: args[3],
549+
lambda: (
550+
(134818, bytearray([0xA2, 0x0E, 0x02, 0x00]), 1, "𠺢"),
551+
(134988, bytearray([0xA2, 0x0E, 0x02, 0x00, 0x4C, 0x0F, 0x02, 0x00]), 2, "𠺢𠽌"),
552+
(8240, bytearray([0x30, 0x20]), 1, "‰"),
553+
(8252, bytearray([0x30, 0x20, 0x3C, 0x20]), 2, "‰‼"),
554+
(127, bytearray([0x61, 0x62, 0x63, 0x64]), 4, "abcd"),
555+
(127, bytearray([0x61, 0x62, 0x63, 0x64]), 2, "ab"),
556+
),
557+
code='''PyObject* wrap_PyUnicode_New(Py_ssize_t maxchar, Py_buffer buffer, Py_ssize_t nchars, PyObject* dummy) {
558+
PyObject* obj = PyUnicode_New(nchars, (Py_UCS4) maxchar);
559+
void* data = PyUnicode_DATA(obj);
560+
size_t char_size;
561+
if (maxchar < 256) {
562+
char_size = 1;
563+
} else if (maxchar < 65536) {
564+
char_size = 2;
565+
} else {
566+
char_size = 4;
567+
}
568+
memcpy(data, buffer.buf, nchars * char_size);
569+
PyUnicode_READY(obj);
570+
return obj;
571+
}
572+
''',
573+
resultspec="O",
574+
argspec='ny*nO',
575+
arguments=["Py_ssize_t maxchar", "Py_buffer buffer", "Py_ssize_t nchars", "PyObject* dummy"],
576+
callfunction="wrap_PyUnicode_New",
577+
cmpfunc=unhandled_error_compare
578+
)
579+
547580

0 commit comments

Comments
 (0)