Skip to content

Commit 327a257

Browse files
authored
gh-119182: Use public PyUnicodeWriter in wrap_strftime() (#129206)
Replace the private _PyUnicodeWriter API with the public PyUnicodeWriter API.
1 parent 719c9dd commit 327a257

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

Modules/_datetimemodule.c

+12-11
Original file line numberDiff line numberDiff line change
@@ -1849,9 +1849,10 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
18491849
* is expensive, don't unless they're actually used.
18501850
*/
18511851

1852-
_PyUnicodeWriter writer;
1853-
_PyUnicodeWriter_Init(&writer);
1854-
writer.overallocate = 1;
1852+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
1853+
if (writer == NULL) {
1854+
goto Error;
1855+
}
18551856

18561857
Py_ssize_t flen = PyUnicode_GET_LENGTH(format);
18571858
Py_ssize_t i = 0;
@@ -1955,11 +1956,11 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
19551956
if (ch == 'C') {
19561957
n -= 2;
19571958
}
1958-
if (_PyUnicodeWriter_WriteSubstring(&writer, format, start, end) < 0) {
1959+
if (PyUnicodeWriter_WriteSubstring(writer, format, start, end) < 0) {
19591960
goto Error;
19601961
}
19611962
start = i;
1962-
if (_PyUnicodeWriter_WriteASCIIString(&writer, buf, n) < 0) {
1963+
if (PyUnicodeWriter_WriteUTF8(writer, buf, n) < 0) {
19631964
goto Error;
19641965
}
19651966
continue;
@@ -1971,25 +1972,25 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
19711972
}
19721973
assert(replacement != NULL);
19731974
assert(PyUnicode_Check(replacement));
1974-
if (_PyUnicodeWriter_WriteSubstring(&writer, format, start, end) < 0) {
1975+
if (PyUnicodeWriter_WriteSubstring(writer, format, start, end) < 0) {
19751976
goto Error;
19761977
}
19771978
start = i;
1978-
if (_PyUnicodeWriter_WriteStr(&writer, replacement) < 0) {
1979+
if (PyUnicodeWriter_WriteStr(writer, replacement) < 0) {
19791980
goto Error;
19801981
}
19811982
} /* end while() */
19821983

19831984
PyObject *newformat;
19841985
if (start == 0) {
1985-
_PyUnicodeWriter_Dealloc(&writer);
1986+
PyUnicodeWriter_Discard(writer);
19861987
newformat = Py_NewRef(format);
19871988
}
19881989
else {
1989-
if (_PyUnicodeWriter_WriteSubstring(&writer, format, start, flen) < 0) {
1990+
if (PyUnicodeWriter_WriteSubstring(writer, format, start, flen) < 0) {
19901991
goto Error;
19911992
}
1992-
newformat = _PyUnicodeWriter_Finish(&writer);
1993+
newformat = PyUnicodeWriter_Finish(writer);
19931994
if (newformat == NULL) {
19941995
goto Done;
19951996
}
@@ -2007,7 +2008,7 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
20072008
return result;
20082009

20092010
Error:
2010-
_PyUnicodeWriter_Dealloc(&writer);
2011+
PyUnicodeWriter_Discard(writer);
20112012
goto Done;
20122013
}
20132014

0 commit comments

Comments
 (0)