bpo-34481: Fix surrogate-handling in strftime by pganssle · Pull Request #8983 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
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
49 changes: 37 additions & 12 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add tests for proper handling of non-UTF-8-encodable strings in
:mod:`datetime` classes. Patch by Alexey Izbyshev.
60 changes: 53 additions & 7 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1475,8 +1475,23 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
assert(PyUnicode_Check(format));
/* Convert the input format to a C string and size */
pin = PyUnicode_AsUTF8AndSize(format, &flen);
if (!pin)
return NULL;
PyObject* format_esc = NULL;
if (pin == NULL) {
PyErr_Clear();

format_esc = PyUnicode_AsEncodedString(format, "utf-8", "surrogatepass");
if (format_esc == NULL) {
return NULL;
}
char *pin_tmp;
if (PyBytes_AsStringAndSize(format_esc, &pin_tmp, &flen)) {
Py_DECREF(format_esc);
return NULL;
}

pin = pin_tmp;
}
int contains_surrogates = (format_esc != NULL);

/* Scan the input format, looking for %z/%Z/%f escapes, building
* a new format. Since computing the replacements for those codes
Expand Down Expand Up @@ -1537,15 +1552,33 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
if (Zreplacement == NULL) {
Zreplacement = make_Zreplacement(object,
tzinfoarg);
if (Zreplacement == NULL)
if (Zreplacement == NULL) {
goto Done;
}
}
assert(Zreplacement != NULL);
assert(PyUnicode_Check(Zreplacement));
ptoappend = PyUnicode_AsUTF8AndSize(Zreplacement,
&ntoappend);
if (ptoappend == NULL)
goto Done;
if (ptoappend == NULL) {
PyErr_Clear();

PyObject *Zreplacement_old = Zreplacement;
Zreplacement = PyUnicode_AsEncodedString(
Zreplacement, "utf-8", "surrogatepass"
);
Py_DECREF(Zreplacement_old);
if (Zreplacement == NULL) {
goto Done;
}

char *p_tmp;
if (PyBytes_AsStringAndSize(Zreplacement, &p_tmp, &ntoappend)) {
goto Done;
}
ptoappend = p_tmp;
contains_surrogates = 1;
}
}
else if (ch == 'f') {
/* format microseconds */
Expand Down Expand Up @@ -1596,7 +1629,13 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,

if (time == NULL)
goto Done;
format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt));
if (!contains_surrogates) {
format = PyUnicode_FromString(PyBytes_AS_STRING(newfmt));
} else {
format = PyUnicode_Decode(PyBytes_AS_STRING(newfmt),
PyBytes_GET_SIZE(newfmt),
"utf-8", "surrogatepass");
}
if (format != NULL) {
result = _PyObject_CallMethodIdObjArgs(time, &PyId_strftime,
format, timetuple, NULL);
Expand All @@ -1605,6 +1644,8 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
Py_DECREF(time);
}
Done:

Py_XDECREF(format_esc);
Py_XDECREF(freplacement);
Py_XDECREF(zreplacement);
Py_XDECREF(Zreplacement);
Expand Down Expand Up @@ -4898,7 +4939,12 @@ datetime_fromisoformat(PyObject* cls, PyObject *dtstr) {
const char * dt_ptr = PyUnicode_AsUTF8AndSize(dtstr, &len);

if (dt_ptr == NULL) {
goto invalid_string_error;
if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) {
// Encoding errors are invalid string errors at this point
goto invalid_string_error;
} else {
goto error;
}
}

const char *p = dt_ptr;
Expand Down
99 changes: 96 additions & 3 deletions Modules/timemodule.c