Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Revert consumed parameter
  • Loading branch information
vstinner committed Jun 11, 2024
commit 89fd69ab3eaa1a24648572c65bc129b28e6187a2
19 changes: 14 additions & 5 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ unicode_decode_utf8(const char *s, Py_ssize_t size,
static int
unicode_decode_utf8_writer(_PyUnicodeWriter *writer,
const char *s, Py_ssize_t size,
_Py_error_handler error_handler, const char *errors);
_Py_error_handler error_handler, const char *errors,
Py_ssize_t *consumed);
#ifdef Py_DEBUG
static inline int unicode_is_finalizing(void);
static int unicode_is_singleton(PyObject *unicode);
Expand Down Expand Up @@ -2401,7 +2402,7 @@ unicode_fromformat_write_utf8(_PyUnicodeWriter *writer, const char *str,

if (width < 0) {
return unicode_decode_utf8_writer(writer, str, length,
_Py_ERROR_REPLACE, "replace");
_Py_ERROR_REPLACE, "replace", NULL);
}

PyObject *unicode = PyUnicode_DecodeUTF8Stateful(str, length,
Expand Down Expand Up @@ -2905,7 +2906,8 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
}

if (unicode_decode_utf8_writer(&writer, f, len,
_Py_ERROR_STRICT, "strict") < 0) {
_Py_ERROR_STRICT, "strict",
NULL) < 0) {
PyObject *exc = PyErr_GetRaisedException();
PyErr_SetString(PyExc_ValueError,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why raise ValueError explicitly? If you want a ValueError for compatibility, UnicodeDecode is a subclass of ValueError, so this is a backward compatible change. Other functions which take const char * do not raise ValueError explicitly.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message helps debugging such issue: it points directly to the format string.

"PyUnicode_FromFormatV() expects a valid UTF-8-encoded "
Expand Down Expand Up @@ -4927,9 +4929,13 @@ unicode_decode_utf8(const char *s, Py_ssize_t size,
static int
unicode_decode_utf8_writer(_PyUnicodeWriter *writer,
const char *s, Py_ssize_t size,
_Py_error_handler error_handler, const char *errors)
_Py_error_handler error_handler, const char *errors,
Py_ssize_t *consumed)
{
if (size == 0) {
if (consumed) {
*consumed = 0;
}
return 0;
}

Expand All @@ -4947,14 +4953,17 @@ unicode_decode_utf8_writer(_PyUnicodeWriter *writer,
writer->pos += decoded;

if (decoded == size) {
if (consumed) {
*consumed = size;
}
return 0;
}
s += decoded;
size -= decoded;
}

return unicode_decode_utf8_impl(writer, starts, s, end,
error_handler, errors, NULL);
error_handler, errors, consumed);
}


Expand Down