https://github.com/python/cpython/commit/2cd6d4bc5b3804068ba5c93fefb72601d86b1d5e
commit: 2cd6d4bc5b3804068ba5c93fefb72601d86b1d5e
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-09-13T01:14:17Z
summary:

gh-156939: Update UTF-8 encoder for PyBytesWriter changes (#157383)

Don't modify directly the writer size, call
PyBytesWriter_GrowAndUpdatePointer() instead.

files:
M Objects/stringlib/codecs.h

diff --git a/Objects/stringlib/codecs.h b/Objects/stringlib/codecs.h
index 6ea7ef1c9a92bf4..aa3de5e34eaaae0 100644
--- a/Objects/stringlib/codecs.h
+++ b/Objects/stringlib/codecs.h
@@ -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)
@@ -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)
@@ -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;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to