Skip to content

Commit f17aa0c

Browse files
committed
Address more feedback; assert return value and raise ValueError
1 parent fbbf841 commit f17aa0c

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
@@ -10057,14 +10057,16 @@ do_capitalize(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UC
1005710057
Py_UCS4 c, mapped[3];
1005810058

1005910059
c = PyUnicode_READ(kind, data, 0);
10060-
n_res = PyUnicode_ToTitle(c, mapped, 3);
10060+
n_res = PyUnicode_ToTitle(c, mapped, Py_ARRAY_LENGTH(mapped));
10061+
assert(n_res >= 1);
1006110062
for (j = 0; j < n_res; j++) {
1006210063
*maxchar = Py_MAX(*maxchar, mapped[j]);
1006310064
res[k++] = mapped[j];
1006410065
}
1006510066
for (i = 1; i < length; i++) {
1006610067
c = PyUnicode_READ(kind, data, i);
10067-
n_res = lower_ucs4(kind, data, length, i, c, mapped, 3);
10068+
n_res = lower_ucs4(kind, data, length, i, c, mapped, Py_ARRAY_LENGTH(mapped));
10069+
assert(n_res >= 1);
1006810070
for (j = 0; j < n_res; j++) {
1006910071
*maxchar = Py_MAX(*maxchar, mapped[j]);
1007010072
res[k++] = mapped[j];
@@ -10081,15 +10083,16 @@ do_swapcase(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4
1008110083
Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3];
1008210084
int n_res, j;
1008310085
if (Py_UNICODE_ISUPPER(c)) {
10084-
n_res = lower_ucs4(kind, data, length, i, c, mapped, 3);
10086+
n_res = lower_ucs4(kind, data, length, i, c, mapped, Py_ARRAY_LENGTH(mapped));
1008510087
}
1008610088
else if (Py_UNICODE_ISLOWER(c)) {
10087-
n_res = PyUnicode_ToUpper(c, mapped, 3);
10089+
n_res = PyUnicode_ToUpper(c, mapped, Py_ARRAY_LENGTH(mapped));
1008810090
}
1008910091
else {
1009010092
n_res = 1;
1009110093
mapped[0] = c;
1009210094
}
10095+
assert(n_res >= 1);
1009310096
for (j = 0; j < n_res; j++) {
1009410097
*maxchar = Py_MAX(*maxchar, mapped[j]);
1009510098
res[k++] = mapped[j];
@@ -10108,9 +10111,10 @@ do_upper_or_lower(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res,
1010810111
Py_UCS4 c = PyUnicode_READ(kind, data, i), mapped[3];
1010910112
int n_res, j;
1011010113
if (lower)
10111-
n_res = lower_ucs4(kind, data, length, i, c, mapped, 3);
10114+
n_res = lower_ucs4(kind, data, length, i, c, mapped, Py_ARRAY_LENGTH(mapped));
1011210115
else
10113-
n_res = PyUnicode_ToUpper(c, mapped, 3);
10116+
n_res = PyUnicode_ToUpper(c, mapped, Py_ARRAY_LENGTH(mapped));
10117+
assert(n_res >= 1);
1011410118
for (j = 0; j < n_res; j++) {
1011510119
*maxchar = Py_MAX(*maxchar, mapped[j]);
1011610120
res[k++] = mapped[j];
@@ -10139,7 +10143,8 @@ do_casefold(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4
1013910143
for (i = 0; i < length; i++) {
1014010144
Py_UCS4 c = PyUnicode_READ(kind, data, i);
1014110145
Py_UCS4 mapped[3];
10142-
int j, n_res = PyUnicode_ToFolded(c, mapped, 3);
10146+
int j, n_res = PyUnicode_ToFolded(c, mapped, Py_ARRAY_LENGTH(mapped));
10147+
assert(n_res >= 1);
1014310148
for (j = 0; j < n_res; j++) {
1014410149
*maxchar = Py_MAX(*maxchar, mapped[j]);
1014510150
res[k++] = mapped[j];
@@ -10161,10 +10166,10 @@ do_title(int kind, const void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *m
1016110166
int n_res, j;
1016210167

1016310168
if (previous_is_cased)
10164-
n_res = lower_ucs4(kind, data, length, i, c, mapped, 3);
10169+
n_res = lower_ucs4(kind, data, length, i, c, mapped, Py_ARRAY_LENGTH(mapped));
1016510170
else
10166-
n_res = PyUnicode_ToTitle(c, mapped, 3);
10167-
10171+
n_res = PyUnicode_ToTitle(c, mapped, Py_ARRAY_LENGTH(mapped));
10172+
assert(n_res >= 1);
1016810173
for (j = 0; j < n_res; j++) {
1016910174
*maxchar = Py_MAX(*maxchar, mapped[j]);
1017010175
res[k++] = mapped[j];

0 commit comments

Comments
 (0)