Skip to content

Commit cfb5171

Browse files
fangererlukasstadler
authored andcommitted
Use PyTruffleUnicode_FromUCS for _PyUnicode_FromUCS1/2/4
1 parent 66383d7 commit cfb5171

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

graalpython/com.oracle.graal.python.cext/src/unicodeobject.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -369,21 +369,17 @@ PyObject * PyUnicode_FromWideChar(const wchar_t *u, Py_ssize_t size) {
369369
}
370370

371371
static PyObject* _PyUnicode_FromUCS1(const Py_UCS1* u, Py_ssize_t size) {
372-
Py_UCS1* buffer = (Py_UCS1*) malloc(size * PyUnicode_1BYTE_KIND);
373-
memcpy(buffer, u, size * PyUnicode_1BYTE_KIND);
374-
return GraalPyTruffleUnicode_New(polyglot_from_Py_UCS1_array(buffer, size), PyUnicode_1BYTE_KIND, 0);
372+
return GraalPyTruffleUnicode_FromUCS(polyglot_from_i8_array((int8_t *)u, size), size, PyUnicode_1BYTE_KIND);
375373
}
376374

377375
static PyObject* _PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size) {
378-
Py_UCS2* buffer = (Py_UCS2*) malloc(size * PyUnicode_2BYTE_KIND);
379-
memcpy(buffer, u, size * PyUnicode_2BYTE_KIND);
380-
return GraalPyTruffleUnicode_New(polyglot_from_Py_UCS2_array(buffer, size), PyUnicode_2BYTE_KIND, 0);
376+
const Py_ssize_t byte_size = size * PyUnicode_2BYTE_KIND;
377+
return GraalPyTruffleUnicode_FromUCS(polyglot_from_i8_array((int8_t *)u, byte_size), byte_size, PyUnicode_2BYTE_KIND);
381378
}
382379

383380
static PyObject* _PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size) {
384-
Py_UCS4* buffer = (Py_UCS4*) malloc(size * PyUnicode_4BYTE_KIND);
385-
memcpy(buffer, u, size * PyUnicode_4BYTE_KIND);
386-
return GraalPyTruffleUnicode_New(polyglot_from_Py_UCS4_array(buffer, size), PyUnicode_4BYTE_KIND, 0);
381+
const Py_ssize_t byte_size = size * PyUnicode_4BYTE_KIND;
382+
return GraalPyTruffleUnicode_FromUCS(polyglot_from_i8_array((int8_t *)u, byte_size), byte_size, PyUnicode_4BYTE_KIND);
387383
}
388384

389385
// taken from CPython "Python/Objects/unicodeobject.c"

0 commit comments

Comments
 (0)