From 1149ab7c02fee09ac2c15d65e8e8813f71cfec24 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 23 Jan 2025 00:49:50 +0100 Subject: [PATCH] gh-119182: Use public PyUnicodeWriter in time_strftime() Replace the private _PyUnicodeWriter API with the public PyUnicodeWriter API. --- Modules/timemodule.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 340011fc08b551..5d0cd52a93a2d3 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -913,9 +913,10 @@ time_strftime(PyObject *module, PyObject *args) PyErr_NoMemory(); return NULL; } - _PyUnicodeWriter writer; - _PyUnicodeWriter_Init(&writer); - writer.overallocate = 1; + PyUnicodeWriter *writer = PyUnicodeWriter_Create(0); + if (writer == NULL) { + goto error; + } Py_ssize_t i = 0; while (i < format_size) { fmtlen = 0; @@ -933,7 +934,7 @@ time_strftime(PyObject *module, PyObject *args) if (unicode == NULL) { goto error; } - if (_PyUnicodeWriter_WriteStr(&writer, unicode) < 0) { + if (PyUnicodeWriter_WriteStr(writer, unicode) < 0) { Py_DECREF(unicode); goto error; } @@ -947,18 +948,18 @@ time_strftime(PyObject *module, PyObject *args) break; } } - if (_PyUnicodeWriter_WriteSubstring(&writer, format_arg, start, i) < 0) { + if (PyUnicodeWriter_WriteSubstring(writer, format_arg, start, i) < 0) { goto error; } } PyMem_Free(outbuf); PyMem_Free(format); - return _PyUnicodeWriter_Finish(&writer); + return PyUnicodeWriter_Finish(writer); error: PyMem_Free(outbuf); PyMem_Free(format); - _PyUnicodeWriter_Dealloc(&writer); + PyUnicodeWriter_Discard(writer); return NULL; }