Skip to content

Commit

Permalink
pythongh-125196: Use PyUnicodeWriter in symtable.c
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Oct 9, 2024
1 parent eb18574 commit 26a81a7
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -3128,25 +3128,23 @@ _Py_Mangle(PyObject *privateobj, PyObject *ident)
return NULL;
}

Py_UCS4 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) {
maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
}

PyObject *result = PyUnicode_New(1 + nlen + plen, maxchar);
if (!result) {
PyUnicodeWriter *writer = PyUnicodeWriter_Create(1 + nlen + plen);
if (!writer) {
return NULL;
}
/* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
Py_DECREF(result);
return NULL;
if (PyUnicodeWriter_WriteChar(writer, '_') < 0) {
goto error;
}
if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
Py_DECREF(result);
return NULL;
if (PyUnicodeWriter_WriteSubstring(writer, privateobj, ipriv, plen) < 0) {
goto error;
}
assert(_PyUnicode_CheckConsistency(result, 1));
return result;
if (PyUnicodeWriter_WriteStr(writer, ident) < 0) {
goto error;
}
return PyUnicodeWriter_Finish(writer);

error:
PyUnicodeWriter_Discard(writer);
return NULL;
}

0 comments on commit 26a81a7

Please sign in to comment.