Skip to content

Commit 82d0bcb

Browse files
lysnikolaouvstinner
authored andcommitted
Address more feedback; assert return value and raise ValueError
1 parent ee4b707 commit 82d0bcb

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

Doc/c-api/unicode.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ These APIs can be used for fast direct character conversions:
313313
able to hold as many characters needed for *ch* to be lower cased, and
314314
return the number of characters stored. Passing a ``NULL`` buffer returns
315315
the buffer size needed. If at some point a buffer overflow is detected,
316-
an :exc:`OverflowError` is raised and ``-1`` is returned.
316+
an :exc:`ValueError` is raised and ``-1`` is returned.
317317
318318
.. versionadded:: next
319319
@@ -324,7 +324,7 @@ These APIs can be used for fast direct character conversions:
324324
able to hold as many characters needed for *ch* to be upper cased, and
325325
return the number of characters stored. Passing a ``NULL`` buffer returns
326326
the buffer size needed. If at some point a buffer overflow is detected,
327-
an :exc:`OverflowError` is raised and ``-1`` is returned.
327+
an :exc:`ValueError` is raised and ``-1`` is returned.
328328
329329
.. versionadded:: next
330330
@@ -335,7 +335,7 @@ These APIs can be used for fast direct character conversions:
335335
able to hold as many characters needed for *ch* to be title cased, and
336336
return the number of characters stored. Passing a ``NULL`` buffer returns
337337
the buffer size needed. If at some point a buffer overflow is detected,
338-
an :exc:`OverflowError` is raised and ``-1`` is returned.
338+
an :exc:`ValueError` is raised and ``-1`` is returned.
339339
340340
.. versionadded:: next
341341
@@ -346,7 +346,7 @@ These APIs can be used for fast direct character conversions:
346346
able to hold as many characters needed for *ch* to be foldcased, and
347347
return the number of characters stored. Passing a ``NULL`` buffer returns
348348
the buffer size needed. If at some point a buffer overflow is detected,
349-
an :exc:`OverflowError` is raised and ``-1`` is returned.
349+
an :exc:`ValueError` is raised and ``-1`` is returned.
350350
351351
.. versionadded:: next
352352

Objects/unicodectype.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ int PyUnicode_ToLower(Py_UCS4 ch, Py_UCS4 *res, int size)
209209
for (i = 0; i < n; i++) {
210210
if (res != NULL) {
211211
if (i >= size) {
212-
PyErr_SetString(PyExc_OverflowError, "output buffer is too small");
212+
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
213213
return -1;
214214
}
215215
res[i] = _PyUnicode_ExtendedCase[index + i];
@@ -220,7 +220,7 @@ int PyUnicode_ToLower(Py_UCS4 ch, Py_UCS4 *res, int size)
220220

221221
if (res != NULL) {
222222
if (0 >= size) {
223-
PyErr_SetString(PyExc_OverflowError, "output buffer is too small");
223+
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
224224
return -1;
225225
}
226226
res[0] = ch + ctype->lower;
@@ -239,7 +239,7 @@ int PyUnicode_ToTitle(Py_UCS4 ch, Py_UCS4 *res, int size)
239239
for (i = 0; i < n; i++) {
240240
if (res != NULL) {
241241
if (i >= size) {
242-
PyErr_SetString(PyExc_OverflowError, "output buffer is too small");
242+
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
243243
return -1;
244244
}
245245
res[i] = _PyUnicode_ExtendedCase[index + i];
@@ -249,7 +249,7 @@ int PyUnicode_ToTitle(Py_UCS4 ch, Py_UCS4 *res, int size)
249249
}
250250
if (res != NULL) {
251251
if (0 >= size) {
252-
PyErr_SetString(PyExc_OverflowError, "output buffer is too small");
252+
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
253253
return -1;
254254
}
255255
res[0] = ch + ctype->title;
@@ -268,7 +268,7 @@ int PyUnicode_ToUpper(Py_UCS4 ch, Py_UCS4 *res, int size)
268268
for (i = 0; i < n; i++) {
269269
if (res != NULL) {
270270
if (i >= size) {
271-
PyErr_SetString(PyExc_OverflowError, "output buffer is too small");
271+
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
272272
return -1;
273273
}
274274
res[i] = _PyUnicode_ExtendedCase[index + i];
@@ -278,7 +278,7 @@ int PyUnicode_ToUpper(Py_UCS4 ch, Py_UCS4 *res, int size)
278278
}
279279
if (res != NULL) {
280280
if (0 >= size) {
281-
PyErr_SetString(PyExc_OverflowError, "output buffer is too small");
281+
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
282282
return -1;
283283
}
284284
res[0] = ch + ctype->upper;
@@ -297,7 +297,7 @@ int PyUnicode_ToFolded(Py_UCS4 ch, Py_UCS4 *res, int size)
297297
for (i = 0; i < n; i++) {
298298
if (res != NULL) {
299299
if (i >= size) {
300-
PyErr_SetString(PyExc_OverflowError, "output buffer is too small");
300+
PyErr_SetString(PyExc_ValueError, "output buffer is too small");
301301
return -1;
302302
}
303303
res[i] = _PyUnicode_ExtendedCase[index + i];

Objects/unicodeobject.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10014,14 +10014,16 @@ do_capitalize(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UC
1001410014
Py_UCS4 c, mapped[3];
1001510015

1001610016
c = PyUnicode_READ(kind, data, 0);
10017-
n_res = PyUnicode_ToTitle(c, mapped, 3);
10017+
n_res = PyUnicode_ToTitle(c, mapped, Py_ARRAY_LENGTH(mapped));
10018+
assert(n_res >= 1);
1001810019
for (j = 0; j < n_res; j++) {
1001910020
*maxchar = Py_MAX(*maxchar, mapped[j]);
1002010021
res[k++] = mapped[j];
1002110022
}
1002210023
for (i = 1; i < length; i++) {
1002310024
c = PyUnicode_READ(kind, data, i);
10024-
n_res = lower_ucs4(kind, data, length, i, c, mapped, 3);
10025+
n_res = lower_ucs4(kind, data, length, i, c, mapped, Py_ARRAY_LENGTH(mapped));
10026+
assert(n_res >= 1);
1002510027
for (j = 0; j < n_res; j++) {
1002610028
*maxchar = Py_MAX(*maxchar, mapped[j]);
1002710029
res[k++] = mapped[j];
@@ -10038,15 +10040,16 @@ do_swapcase(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4
1003810040
Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3];
1003910041
int n_res, j;
1004010042
if (Py_UNICODE_ISUPPER(c)) {
10041-
n_res = lower_ucs4(kind, data, length, i, c, mapped, 3);
10043+
n_res = lower_ucs4(kind, data, length, i, c, mapped, Py_ARRAY_LENGTH(mapped));
1004210044
}
1004310045
else if (Py_UNICODE_ISLOWER(c)) {
10044-
n_res = PyUnicode_ToUpper(c, mapped, 3);
10046+
n_res = PyUnicode_ToUpper(c, mapped, Py_ARRAY_LENGTH(mapped));
1004510047
}
1004610048
else {
1004710049
n_res = 1;
1004810050
mapped[0] = c;
1004910051
}
10052+
assert(n_res >= 1);
1005010053
for (j = 0; j < n_res; j++) {
1005110054
*maxchar = Py_MAX(*maxchar, mapped[j]);
1005210055
res[k++] = mapped[j];
@@ -10065,9 +10068,10 @@ do_upper_or_lower(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res,
1006510068
Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3];
1006610069
int n_res, j;
1006710070
if (lower)
10068-
n_res = lower_ucs4(kind, data, length, i, c, mapped, 3);
10071+
n_res = lower_ucs4(kind, data, length, i, c, mapped, Py_ARRAY_LENGTH(mapped));
1006910072
else
10070-
n_res = PyUnicode_ToUpper(c, mapped, 3);
10073+
n_res = PyUnicode_ToUpper(c, mapped, Py_ARRAY_LENGTH(mapped));
10074+
assert(n_res >= 1);
1007110075
for (j = 0; j < n_res; j++) {
1007210076
*maxchar = Py_MAX(*maxchar, mapped[j]);
1007310077
res[k++] = mapped[j];
@@ -10096,7 +10100,8 @@ do_casefold(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4
1009610100
for (i = 0; i < length; i++) {
1009710101
Py_UCS4 c = PyUnicode_READ(kind, data, i);
1009810102
Py_UCS4 mapped[3];
10099-
int j, n_res = PyUnicode_ToFolded(c, mapped, 3);
10103+
int j, n_res = PyUnicode_ToFolded(c, mapped, Py_ARRAY_LENGTH(mapped));
10104+
assert(n_res >= 1);
1010010105
for (j = 0; j < n_res; j++) {
1010110106
*maxchar = Py_MAX(*maxchar, mapped[j]);
1010210107
res[k++] = mapped[j];
@@ -10118,10 +10123,10 @@ do_title(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *m
1011810123
int n_res, j;
1011910124

1012010125
if (previous_is_cased)
10121-
n_res = lower_ucs4(kind, data, length, i, c, mapped, 3);
10126+
n_res = lower_ucs4(kind, data, length, i, c, mapped, Py_ARRAY_LENGTH(mapped));
1012210127
else
10123-
n_res = PyUnicode_ToTitle(c, mapped, 3);
10124-
10128+
n_res = PyUnicode_ToTitle(c, mapped, Py_ARRAY_LENGTH(mapped));
10129+
assert(n_res >= 1);
1012510130
for (j = 0; j < n_res; j++) {
1012610131
*maxchar = Py_MAX(*maxchar, mapped[j]);
1012710132
res[k++] = mapped[j];

0 commit comments

Comments
 (0)