gh-119182: Use public PyUnicodeWriter in wrap_strftime() (#129206)

Replace the private _PyUnicodeWriter API with the public
PyUnicodeWriter API.
This commit is contained in:
Victor Stinner 2025-01-23 01:18:26 +01:00 committed by GitHub
parent 719c9dd72f
commit 327a257e6a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1849,9 +1849,10 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
* is expensive, don't unless they're actually used. * is expensive, don't unless they're actually used.
*/ */
_PyUnicodeWriter writer; PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
_PyUnicodeWriter_Init(&writer); if (writer == NULL) {
writer.overallocate = 1; goto Error;
}
Py_ssize_t flen = PyUnicode_GET_LENGTH(format); Py_ssize_t flen = PyUnicode_GET_LENGTH(format);
Py_ssize_t i = 0; Py_ssize_t i = 0;
@ -1955,11 +1956,11 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
if (ch == 'C') { if (ch == 'C') {
n -= 2; n -= 2;
} }
if (_PyUnicodeWriter_WriteSubstring(&writer, format, start, end) < 0) { if (PyUnicodeWriter_WriteSubstring(writer, format, start, end) < 0) {
goto Error; goto Error;
} }
start = i; start = i;
if (_PyUnicodeWriter_WriteASCIIString(&writer, buf, n) < 0) { if (PyUnicodeWriter_WriteUTF8(writer, buf, n) < 0) {
goto Error; goto Error;
} }
continue; continue;
@ -1971,25 +1972,25 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
} }
assert(replacement != NULL); assert(replacement != NULL);
assert(PyUnicode_Check(replacement)); assert(PyUnicode_Check(replacement));
if (_PyUnicodeWriter_WriteSubstring(&writer, format, start, end) < 0) { if (PyUnicodeWriter_WriteSubstring(writer, format, start, end) < 0) {
goto Error; goto Error;
} }
start = i; start = i;
if (_PyUnicodeWriter_WriteStr(&writer, replacement) < 0) { if (PyUnicodeWriter_WriteStr(writer, replacement) < 0) {
goto Error; goto Error;
} }
} /* end while() */ } /* end while() */
PyObject *newformat; PyObject *newformat;
if (start == 0) { if (start == 0) {
_PyUnicodeWriter_Dealloc(&writer); PyUnicodeWriter_Discard(writer);
newformat = Py_NewRef(format); newformat = Py_NewRef(format);
} }
else { else {
if (_PyUnicodeWriter_WriteSubstring(&writer, format, start, flen) < 0) { if (PyUnicodeWriter_WriteSubstring(writer, format, start, flen) < 0) {
goto Error; goto Error;
} }
newformat = _PyUnicodeWriter_Finish(&writer); newformat = PyUnicodeWriter_Finish(writer);
if (newformat == NULL) { if (newformat == NULL) {
goto Done; goto Done;
} }
@ -2007,7 +2008,7 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
return result; return result;
Error: Error:
_PyUnicodeWriter_Dealloc(&writer); PyUnicodeWriter_Discard(writer);
goto Done; goto Done;
} }