https://github.com/python/cpython/commit/c863349f983673bdc9c94c9980e8e6122d36d48a
commit: c863349f983673bdc9c94c9980e8e6122d36d48a
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2025-09-22T22:49:25+02:00
summary:
gh-139156: Use PyBytesWriter in the UTF-7 encoder (#139248)
Replace PyBytes_FromStringAndSize() and _PyBytes_Resize() with the
PyBytesWriter API.
files:
M Objects/unicodeobject.c
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 934faf236cf3c1..2928f20fb4d480 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -4897,33 +4897,27 @@ _PyUnicode_EncodeUTF7(PyObject *str,
int base64WhiteSpace,
const char *errors)
{
- int kind;
- const void *data;
- Py_ssize_t len;
- PyObject *v;
- int inShift = 0;
- Py_ssize_t i;
- unsigned int base64bits = 0;
- unsigned long base64buffer = 0;
- char * out;
- const char * start;
-
- kind = PyUnicode_KIND(str);
- data = PyUnicode_DATA(str);
- len = PyUnicode_GET_LENGTH(str);
-
- if (len == 0)
+ Py_ssize_t len = PyUnicode_GET_LENGTH(str);
+ if (len == 0) {
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
+ }
+ int kind = PyUnicode_KIND(str);
+ const void *data = PyUnicode_DATA(str);
/* It might be possible to tighten this worst case */
- if (len > PY_SSIZE_T_MAX / 8)
+ if (len > PY_SSIZE_T_MAX / 8) {
return PyErr_NoMemory();
- v = PyBytes_FromStringAndSize(NULL, len * 8);
- if (v == NULL)
+ }
+ PyBytesWriter *writer = PyBytesWriter_Create(len * 8);
+ if (writer == NULL) {
return NULL;
+ }
- start = out = PyBytes_AS_STRING(v);
- for (i = 0; i < len; ++i) {
+ int inShift = 0;
+ unsigned int base64bits = 0;
+ unsigned long base64buffer = 0;
+ char *out = PyBytesWriter_GetData(writer);
+ for (Py_ssize_t i = 0; i < len; ++i) {
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
if (inShift) {
@@ -4986,9 +4980,7 @@ _PyUnicode_EncodeUTF7(PyObject *str,
*out++= TO_BASE64(base64buffer << (6-base64bits) );
if (inShift)
*out++ = '-';
- if (_PyBytes_Resize(&v, out - start) < 0)
- return NULL;
- return v;
+ return PyBytesWriter_FinishWithPointer(writer, out);
}
#undef IS_BASE64
_______________________________________________
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]