Skip to content

Commit 9c0e1f8

Browse files
Issue python#28379: Added sanity checks and tests for PyUnicode_CopyCharacters().
Patch by Xiang Zhang.
1 parent 48b1c3f commit 9c0e1f8

5 files changed

Lines changed: 94 additions & 7 deletions

File tree

‎Doc/c-api/unicode.rst‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,13 +578,16 @@ APIs:
578578
.. versionadded:: 3.3
579579
580580
581-
.. c:function:: int PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, \
582-
PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many)
581+
.. c:function:: Py_ssize_t PyUnicode_CopyCharacters(PyObject *to, \
582+
Py_ssize_t to_start, \
583+
PyObject *from, \
584+
Py_ssize_t from_start, \
585+
Py_ssize_t how_many)
583586
584587
Copy characters from one Unicode object into another. This function performs
585588
character conversion when necessary and falls back to :c:func:`memcpy` if
586589
possible. Returns ``-1`` and sets an exception on error, otherwise returns
587-
``0``.
590+
the number of copied characters.
588591
589592
.. versionadded:: 3.3
590593

‎Lib/test/test_unicode.py‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
66
7-
"""#"
7+
"""
88
import _string
99
import codecs
1010
import itertools
@@ -2704,6 +2704,49 @@ def test_asucs4(self):
27042704
self.assertEqual(unicode_asucs4(s, len(s), 1), s+'\0')
27052705
self.assertEqual(unicode_asucs4(s, len(s), 0), s+'\uffff')
27062706

2707+
# Test PyUnicode_CopyCharacters()
2708+
@support.cpython_only
2709+
def test_copycharacters(self):
2710+
from _testcapi import unicode_copycharacters
2711+
2712+
strings = [
2713+
'abcde', '\xa1\xa2\xa3\xa4\xa5',
2714+
'\u4f60\u597d\u4e16\u754c\uff01',
2715+
'\U0001f600\U0001f601\U0001f602\U0001f603\U0001f604'
2716+
]
2717+
2718+
for idx, from_ in enumerate(strings):
2719+
# wide -> narrow: exceed maxchar limitation
2720+
for to in strings[:idx]:
2721+
self.assertRaises(
2722+
SystemError,
2723+
unicode_copycharacters, to, 0, from_, 0, 5
2724+
)
2725+
# same kind
2726+
for from_start in range(5):
2727+
self.assertEqual(
2728+
unicode_copycharacters(from_, 0, from_, from_start, 5),
2729+
(from_[from_start:from_start+5].ljust(5, '\0'),
2730+
5-from_start)
2731+
)
2732+
for to_start in range(5):
2733+
self.assertEqual(
2734+
unicode_copycharacters(from_, to_start, from_, to_start, 5),
2735+
(from_[to_start:to_start+5].rjust(5, '\0'),
2736+
5-to_start)
2737+
)
2738+
# narrow -> wide
2739+
# Tests omitted since this creates invalid strings.
2740+
2741+
s = strings[0]
2742+
self.assertRaises(IndexError, unicode_copycharacters, s, 6, s, 0, 5)
2743+
self.assertRaises(IndexError, unicode_copycharacters, s, -1, s, 0, 5)
2744+
self.assertRaises(IndexError, unicode_copycharacters, s, 0, s, 6, 5)
2745+
self.assertRaises(IndexError, unicode_copycharacters, s, 0, s, -1, 5)
2746+
self.assertRaises(SystemError, unicode_copycharacters, s, 1, s, 0, 5)
2747+
self.assertRaises(SystemError, unicode_copycharacters, s, 0, s, 0, -1)
2748+
self.assertRaises(SystemError, unicode_copycharacters, s, 0, b'', 0, 0)
2749+
27072750
@support.cpython_only
27082751
def test_encode_decimal(self):
27092752
from _testcapi import unicode_encodedecimal

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #28379: Added sanity checks and tests for PyUnicode_CopyCharacters().
14+
Patch by Xiang Zhang.
15+
1316
- Issue #28376: The type of long range iterator is now registered as Iterator.
1417
Patch by Oren Milman.
1518

‎Modules/_testcapimodule.c‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,39 @@ unicode_asucs4(PyObject *self, PyObject *args)
18591859
return result;
18601860
}
18611861

1862+
static PyObject *
1863+
unicode_copycharacters(PyObject *self, PyObject *args)
1864+
{
1865+
PyObject *from, *to, *to_copy;
1866+
Py_ssize_t from_start, to_start, how_many, copied;
1867+
1868+
if (!PyArg_ParseTuple(args, "UnOnn:unicode_copycharacters", &to, &to_start,
1869+
&from, &from_start, &how_many)) {
1870+
return NULL;
1871+
}
1872+
1873+
if (PyUnicode_READY(to) < 0) {
1874+
return NULL;
1875+
}
1876+
1877+
if (!(to_copy = PyUnicode_New(PyUnicode_GET_LENGTH(to),
1878+
PyUnicode_MAX_CHAR_VALUE(to)))) {
1879+
return NULL;
1880+
}
1881+
if (PyUnicode_Fill(to_copy, 0, PyUnicode_GET_LENGTH(to_copy), 0U) < 0) {
1882+
Py_DECREF(to_copy);
1883+
return NULL;
1884+
}
1885+
1886+
if ((copied = PyUnicode_CopyCharacters(to_copy, to_start, from,
1887+
from_start, how_many)) < 0) {
1888+
Py_DECREF(to_copy);
1889+
return NULL;
1890+
}
1891+
1892+
return Py_BuildValue("(Nn)", to_copy, copied);
1893+
}
1894+
18621895
static PyObject *
18631896
unicode_encodedecimal(PyObject *self, PyObject *args)
18641897
{
@@ -3915,6 +3948,7 @@ static PyMethodDef TestMethods[] = {
39153948
{"unicode_aswidechar", unicode_aswidechar, METH_VARARGS},
39163949
{"unicode_aswidecharstring",unicode_aswidecharstring, METH_VARARGS},
39173950
{"unicode_asucs4", unicode_asucs4, METH_VARARGS},
3951+
{"unicode_copycharacters", unicode_copycharacters, METH_VARARGS},
39183952
{"unicode_encodedecimal", unicode_encodedecimal, METH_VARARGS},
39193953
{"unicode_transformdecimaltoascii", unicode_transformdecimaltoascii, METH_VARARGS},
39203954
{"unicode_legacy_string", unicode_legacy_string, METH_VARARGS},

‎Objects/unicodeobject.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,15 +1366,19 @@ PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start,
13661366
if (PyUnicode_READY(to) == -1)
13671367
return -1;
13681368

1369-
if (from_start < 0) {
1369+
if ((size_t)from_start > (size_t)PyUnicode_GET_LENGTH(from)) {
13701370
PyErr_SetString(PyExc_IndexError, "string index out of range");
13711371
return -1;
13721372
}
1373-
if (to_start < 0) {
1373+
if ((size_t)to_start > (size_t)PyUnicode_GET_LENGTH(to)) {
13741374
PyErr_SetString(PyExc_IndexError, "string index out of range");
13751375
return -1;
13761376
}
1377-
how_many = Py_MIN(PyUnicode_GET_LENGTH(from), how_many);
1377+
if (how_many < 0) {
1378+
PyErr_SetString(PyExc_SystemError, "how_many cannot be negative");
1379+
return -1;
1380+
}
1381+
how_many = Py_MIN(PyUnicode_GET_LENGTH(from)-from_start, how_many);
13781382
if (to_start + how_many > PyUnicode_GET_LENGTH(to)) {
13791383
PyErr_Format(PyExc_SystemError,
13801384
"Cannot write %zi characters at %zi "

0 commit comments

Comments
 (0)