Skip to content

Commit b731f07

Browse files
committed
gh-139156: Use PyBytesWriter in PyUnicode_AsRawUnicodeEscapeString()
Replace PyBytes_FromStringAndSize() and _PyBytes_Resize() with the PyBytesWriter API.
1 parent c863349 commit b731f07

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

Objects/unicodeobject.c

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7155,41 +7155,34 @@ PyUnicode_DecodeRawUnicodeEscape(const char *s,
71557155
PyObject *
71567156
PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
71577157
{
7158-
PyObject *repr;
7159-
char *p;
7160-
Py_ssize_t expandsize, pos;
7161-
int kind;
7162-
const void *data;
7163-
Py_ssize_t len;
7164-
71657158
if (!PyUnicode_Check(unicode)) {
71667159
PyErr_BadArgument();
71677160
return NULL;
71687161
}
7169-
kind = PyUnicode_KIND(unicode);
7170-
data = PyUnicode_DATA(unicode);
7171-
len = PyUnicode_GET_LENGTH(unicode);
7162+
int kind = PyUnicode_KIND(unicode);
7163+
const void *data = PyUnicode_DATA(unicode);
7164+
Py_ssize_t len = PyUnicode_GET_LENGTH(unicode);
7165+
if (len == 0) {
7166+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
7167+
}
71727168
if (kind == PyUnicode_1BYTE_KIND) {
71737169
return PyBytes_FromStringAndSize(data, len);
71747170
}
71757171

71767172
/* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6
71777173
bytes, and 1 byte characters 4. */
7178-
expandsize = kind * 2 + 2;
7179-
7174+
Py_ssize_t expandsize = kind * 2 + 2;
71807175
if (len > PY_SSIZE_T_MAX / expandsize) {
71817176
return PyErr_NoMemory();
71827177
}
7183-
repr = PyBytes_FromStringAndSize(NULL, expandsize * len);
7184-
if (repr == NULL) {
7178+
7179+
PyBytesWriter *writer = PyBytesWriter_Create(expandsize * len);
7180+
if (writer == NULL) {
71857181
return NULL;
71867182
}
7187-
if (len == 0) {
7188-
return repr;
7189-
}
7183+
char *p = PyBytesWriter_GetData(writer);
71907184

7191-
p = PyBytes_AS_STRING(repr);
7192-
for (pos = 0; pos < len; pos++) {
7185+
for (Py_ssize_t pos = 0; pos < len; pos++) {
71937186
Py_UCS4 ch = PyUnicode_READ(kind, data, pos);
71947187

71957188
/* U+0000-U+00ff range: Copy 8-bit characters as-is */
@@ -7221,11 +7214,7 @@ PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
72217214
}
72227215
}
72237216

7224-
assert(p > PyBytes_AS_STRING(repr));
7225-
if (_PyBytes_Resize(&repr, p - PyBytes_AS_STRING(repr)) < 0) {
7226-
return NULL;
7227-
}
7228-
return repr;
7217+
return PyBytesWriter_FinishWithPointer(writer, p);
72297218
}
72307219

72317220
/* --- Latin-1 Codec ------------------------------------------------------ */

0 commit comments

Comments
 (0)