Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 99250d5

Browse files
Issue #28774: Simplified encoding a str result of an error handler in ASCII
and Latin1 encoders.
1 parent d04d847 commit 99250d5

1 file changed

Lines changed: 12 additions & 26 deletions

File tree

Objects/unicodeobject.c

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6814,33 +6814,19 @@ unicode_encode_ucs1(PyObject *unicode,
68146814
if (PyUnicode_READY(rep) < 0)
68156815
goto onError;
68166816

6817-
if (PyUnicode_IS_ASCII(rep)) {
6818-
/* Fast path: all characters are smaller than limit */
6819-
assert(limit >= 128);
6820-
assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND);
6821-
str = _PyBytesWriter_WriteBytes(&writer, str,
6822-
PyUnicode_DATA(rep),
6823-
PyUnicode_GET_LENGTH(rep));
6824-
}
6825-
else {
6826-
Py_ssize_t repsize = PyUnicode_GET_LENGTH(rep);
6827-
6828-
str = _PyBytesWriter_Prepare(&writer, str, repsize);
6829-
if (str == NULL)
6830-
goto onError;
6831-
6832-
/* check if there is anything unencodable in the
6833-
replacement and copy it to the output */
6834-
for (i = 0; repsize-->0; ++i, ++str) {
6835-
ch = PyUnicode_READ_CHAR(rep, i);
6836-
if (ch >= limit) {
6837-
raise_encode_exception(&exc, encoding, unicode,
6838-
collstart, collend, reason);
6839-
goto onError;
6840-
}
6841-
*str = (char)ch;
6842-
}
6817+
if (limit == 256 ?
6818+
PyUnicode_KIND(rep) != PyUnicode_1BYTE_KIND :
6819+
!PyUnicode_IS_ASCII(rep))
6820+
{
6821+
/* Not all characters are smaller than limit */
6822+
raise_encode_exception(&exc, encoding, unicode,
6823+
collstart, collend, reason);
6824+
goto onError;
68436825
}
6826+
assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND);
6827+
str = _PyBytesWriter_WriteBytes(&writer, str,
6828+
PyUnicode_DATA(rep),
6829+
PyUnicode_GET_LENGTH(rep));
68446830
}
68456831
pos = newpos;
68466832
Py_CLEAR(rep);

0 commit comments

Comments
 (0)