Skip to content

Commit b89d4f5

Browse files
pganssleAlexey Izbyshev
andcommitted
Refactor sanitize_isoformat_str
Rather than splitting the string at position 10 and re-joining it with PyUnicode_Format, this copies the original unicode object and overwrites the separator character. Co-Authored-By: Alexey Izbyshev <[email protected]>
1 parent a0246a0 commit b89d4f5

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

Modules/_datetimemodule.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4864,21 +4864,14 @@ _sanitize_isoformat_str(PyObject *dtstr, int *needs_decref) {
48644864
return dtstr;
48654865
}
48664866

4867-
PyObject *left = PyUnicode_Substring(dtstr, 0, 10);
4868-
if (left == NULL) {
4869-
return NULL;
4870-
}
4871-
4872-
PyObject *right = PyUnicode_Substring(dtstr, 11, len);
4873-
if (right == NULL) {
4874-
Py_DECREF(left);
4867+
PyObject *str_out = PyUnicode_New(len, PyUnicode_MAX_CHAR_VALUE(dtstr));
4868+
if (str_out == NULL) {
48754869
return NULL;
48764870
}
48774871

4878-
PyObject *str_out = PyUnicode_FromFormat("%UT%U", left, right);
4879-
Py_DECREF(left);
4880-
Py_DECREF(right);
4881-
if (str_out == NULL) {
4872+
if (PyUnicode_CopyCharacters(str_out, 0, dtstr, 0, len) == -1 ||
4873+
PyUnicode_WriteChar(str_out, 10, (Py_UCS4)'T')) {
4874+
Py_DECREF(str_out);
48824875
return NULL;
48834876
}
48844877

0 commit comments

Comments
 (0)