Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions Objects/stringlib/codecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,6 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
break;

case _Py_ERROR_BACKSLASHREPLACE:
/* subtract preallocated bytes */
writer->size -= max_char_size * (endpos - startpos);
p = backslashreplace(writer, p,
unicode, startpos, endpos);
if (p == NULL)
Expand All @@ -360,8 +358,6 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
break;

case _Py_ERROR_XMLCHARREFREPLACE:
/* subtract preallocated bytes */
writer->size -= max_char_size * (endpos - startpos);
p = xmlcharrefreplace(writer, p,
unicode, startpos, endpos);
if (p == NULL)
Expand Down Expand Up @@ -400,10 +396,15 @@ STRINGLIB(utf8_encoder)(PyObject *unicode,
}
}
else {
/* subtract preallocated bytes */
writer->size -= max_char_size * (newpos - startpos);
/* Only overallocate the buffer if it's not the last write */
writer->overallocate = (newpos < size);

/* subtract preallocated bytes */
Py_ssize_t prealloc = max_char_size * (newpos - startpos);
p = PyBytesWriter_GrowAndUpdatePointer(writer, -prealloc, p);
if (p == NULL) {
goto error;
}
}

const char *rep_str;
Expand Down
Loading