summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-11-23 13:13:00 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-11-23 13:13:00 (GMT)
commit99250d5c630015b179493cfef97ec5a5517d1833 (patch)
tree25379bdd4bc9fded2d1718fe65f230c08ffe3060 /Objects
parentd04d8474dfde5ed584a51a43d3d177376a98fb58 (diff)
downloadcpython-99250d5c630015b179493cfef97ec5a5517d1833.zip
cpython-99250d5c630015b179493cfef97ec5a5517d1833.tar.gz
cpython-99250d5c630015b179493cfef97ec5a5517d1833.tar.bz2
Issue #28774: Simplified encoding a str result of an error handler in ASCII
and Latin1 encoders.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/unicodeobject.c38
1 files changed, 12 insertions, 26 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 2bf48b7..1c2257e 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -6814,33 +6814,19 @@ unicode_encode_ucs1(PyObject *unicode,
if (PyUnicode_READY(rep) < 0)
goto onError;
- if (PyUnicode_IS_ASCII(rep)) {
- /* Fast path: all characters are smaller than limit */
- assert(limit >= 128);
- assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND);
- str = _PyBytesWriter_WriteBytes(&writer, str,
- PyUnicode_DATA(rep),
- PyUnicode_GET_LENGTH(rep));
- }
- else {
- Py_ssize_t repsize = PyUnicode_GET_LENGTH(rep);
-
- str = _PyBytesWriter_Prepare(&writer, str, repsize);
- if (str == NULL)
- goto onError;
-
- /* check if there is anything unencodable in the
- replacement and copy it to the output */
- for (i = 0; repsize-->0; ++i, ++str) {
- ch = PyUnicode_READ_CHAR(rep, i);
- if (ch >= limit) {
- raise_encode_exception(&exc, encoding, unicode,
- collstart, collend, reason);
- goto onError;
- }
- *str = (char)ch;
- }
+ if (limit == 256 ?
+ PyUnicode_KIND(rep) != PyUnicode_1BYTE_KIND :
+ !PyUnicode_IS_ASCII(rep))
+ {
+ /* Not all characters are smaller than limit */
+ raise_encode_exception(&exc, encoding, unicode,
+ collstart, collend, reason);
+ goto onError;
}
+ assert(PyUnicode_KIND(rep) == PyUnicode_1BYTE_KIND);
+ str = _PyBytesWriter_WriteBytes(&writer, str,
+ PyUnicode_DATA(rep),
+ PyUnicode_GET_LENGTH(rep));
}
pos = newpos;
Py_CLEAR(rep);