Skip to content

Commit 5e5ecbc

Browse files
committed
remove magic constants
1 parent 3eff7b8 commit 5e5ecbc

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Python/codecs.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,14 @@ PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
908908
return NULL;
909909
}
910910

911-
if (slen > PY_SSIZE_T_MAX / 10) {
912-
end = start + PY_SSIZE_T_MAX / 10;
911+
// The number of characters that each character 'ch' contributes
912+
// in the result is 1 + 1 + k, where k >= min{t >= 1 | 16^t > ch}
913+
// and will be formatted as "\\" + ('U'|'u'|'x') + HEXDIGITS,
914+
// where the number of hexdigits is either 2, 4, or 8 (not 6).
915+
// Since the Unicode range is below 10^7, we choose k = 8 whence
916+
// each "block" requires at most 1 + 1 + 8 characters.
917+
if (slen > PY_SSIZE_T_MAX / (1 + 1 + 8)) {
918+
end = start + PY_SSIZE_T_MAX / (1 + 1 + 8);
913919
end = Py_MIN(end, objlen);
914920
slen = Py_MAX(0, end - start);
915921
}
@@ -919,13 +925,13 @@ PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
919925
/* object is guaranteed to be "ready" */
920926
Py_UCS4 c = PyUnicode_READ_CHAR(obj, i);
921927
if (c >= 0x10000) {
922-
ressize += 10;
928+
ressize += 1 + 1 + 8;
923929
}
924930
else if (c >= 0x100) {
925-
ressize += 6;
931+
ressize += 1 + 1 + 4;
926932
}
927933
else {
928-
ressize += 4;
934+
ressize += 1 + 1 + 2;
929935
}
930936
}
931937
PyObject *res = PyUnicode_New(ressize, 127);

0 commit comments

Comments
 (0)