Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-119182: Use public PyUnicodeWriter in wrap_strftime()
Replace the private _PyUnicodeWriter API with the public
PyUnicodeWriter API.
  • Loading branch information
vstinner committed Jan 22, 2025
commit 908e84692cc0b13f9624893e2c76ef40ef5fe113
23 changes: 12 additions & 11 deletions 23 Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1849,9 +1849,10 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
* is expensive, don't unless they're actually used.
*/

_PyUnicodeWriter writer;
_PyUnicodeWriter_Init(&writer);
writer.overallocate = 1;
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
if (writer == NULL) {
goto Error;
}

Py_ssize_t flen = PyUnicode_GET_LENGTH(format);
Py_ssize_t i = 0;
Expand Down Expand Up @@ -1955,11 +1956,11 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
if (ch == 'C') {
n -= 2;
}
if (_PyUnicodeWriter_WriteSubstring(&writer, format, start, end) < 0) {
if (PyUnicodeWriter_WriteSubstring(writer, format, start, end) < 0) {
goto Error;
}
start = i;
if (_PyUnicodeWriter_WriteASCIIString(&writer, buf, n) < 0) {
if (PyUnicodeWriter_WriteUTF8(writer, buf, n) < 0) {
goto Error;
}
continue;
Expand All @@ -1971,25 +1972,25 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
}
assert(replacement != NULL);
assert(PyUnicode_Check(replacement));
if (_PyUnicodeWriter_WriteSubstring(&writer, format, start, end) < 0) {
if (PyUnicodeWriter_WriteSubstring(writer, format, start, end) < 0) {
goto Error;
}
start = i;
if (_PyUnicodeWriter_WriteStr(&writer, replacement) < 0) {
if (PyUnicodeWriter_WriteStr(writer, replacement) < 0) {
goto Error;
}
} /* end while() */

PyObject *newformat;
if (start == 0) {
_PyUnicodeWriter_Dealloc(&writer);
PyUnicodeWriter_Discard(writer);
newformat = Py_NewRef(format);
}
else {
if (_PyUnicodeWriter_WriteSubstring(&writer, format, start, flen) < 0) {
if (PyUnicodeWriter_WriteSubstring(writer, format, start, flen) < 0) {
goto Error;
}
newformat = _PyUnicodeWriter_Finish(&writer);
newformat = PyUnicodeWriter_Finish(writer);
if (newformat == NULL) {
goto Done;
}
Expand All @@ -2007,7 +2008,7 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
return result;

Error:
_PyUnicodeWriter_Dealloc(&writer);
PyUnicodeWriter_Discard(writer);
goto Done;
}

Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.