diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 9c00e22ea24bd0..c1e1dd34745fa3 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -6440,6 +6440,8 @@ _PyUnicode_EncodeUTF16(PyObject *str, #endif if (kind == PyUnicode_1BYTE_KIND) { + // gh-139156: Don't use PyBytesWriter API here since it has an overhead + // on short strings PyObject *v = PyBytes_FromStringAndSize(NULL, nsize * 2); if (v == NULL) { return NULL; @@ -8857,11 +8859,15 @@ charmapencode_output(Py_UCS4 c, PyObject *mapping, if (Py_IS_TYPE(mapping, &EncodingMapType)) { int res = encoding_map_lookup(c, mapping); Py_ssize_t requiredsize = *outpos+1; - if (res == -1) + if (res == -1) { return enc_FAILED; - if (outsize adjust input position */ + ++inpos; + continue; + +enc_FAILED: if (charmap_encoding_error(unicode, &inpos, mapping, &exc, &error_handler, &error_handler_obj, errors, writer, &respos)) { goto onError; } + outstart = _PyBytesWriter_GetData(writer); + outsize = _PyBytesWriter_GetSize(writer); } - else { - /* done with this character => adjust input position */ - ++inpos; + } + else { + while (inpos adjust input position */ + ++inpos; + } } }