-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-119182: Decode PyUnicode_FromFormat() format string from UTF-8 #120248
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
Changes from 6 commits
3d5bca4
6a87915
e830944
242e6cb
d04269f
94da5e7
89fd69a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| :c:func:`PyUnicode_FromFormat` now decodes the format string from UTF-8, | ||
| instead of ASCII. Patch by Victor Stinner. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -205,8 +205,7 @@ 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_ssize_t *consumed); | ||
|
vstinner marked this conversation as resolved.
|
||
| _Py_error_handler error_handler, const char *errors); | ||
| #ifdef Py_DEBUG | ||
| static inline int unicode_is_finalizing(void); | ||
| static int unicode_is_singleton(PyObject *unicode); | ||
|
|
@@ -2402,7 +2401,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", NULL); | ||
| _Py_ERROR_REPLACE, "replace"); | ||
| } | ||
|
|
||
| PyObject *unicode = PyUnicode_DecodeUTF8Stateful(str, length, | ||
|
|
@@ -2896,28 +2895,26 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) | |
| const char *p; | ||
| Py_ssize_t len; | ||
|
|
||
| p = f; | ||
| do | ||
| { | ||
| if ((unsigned char)*p > 127) { | ||
| PyErr_Format(PyExc_ValueError, | ||
| "PyUnicode_FromFormatV() expects an ASCII-encoded format " | ||
| "string, got a non-ASCII byte: 0x%02x", | ||
| (unsigned char)*p); | ||
| goto fail; | ||
| } | ||
| p++; | ||
| p = strchr(f, '%'); | ||
| if (p != NULL) { | ||
| len = p - f; | ||
| } | ||
| while (*p != '\0' && *p != '%'); | ||
| len = p - f; | ||
|
|
||
| if (*p == '\0') | ||
| else { | ||
| len = strlen(f); | ||
| writer.overallocate = 0; | ||
| } | ||
|
|
||
| if (_PyUnicodeWriter_WriteASCIIString(&writer, f, len) < 0) | ||
| if (unicode_decode_utf8_writer(&writer, f, len, | ||
| _Py_ERROR_STRICT, "strict") < 0) { | ||
| PyObject *exc = PyErr_GetRaisedException(); | ||
| PyErr_SetString(PyExc_ValueError, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 " | ||
| "format string, got an invalid UTF-8 string"); | ||
| _PyErr_ChainExceptions1(exc); | ||
| goto fail; | ||
| } | ||
|
|
||
| f = p; | ||
| f += len; | ||
| } | ||
| } | ||
| va_end(vargs2); | ||
|
|
@@ -4930,13 +4927,9 @@ 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_ssize_t *consumed) | ||
| _Py_error_handler error_handler, const char *errors) | ||
| { | ||
| if (size == 0) { | ||
| if (consumed) { | ||
| *consumed = 0; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
|
|
@@ -4954,17 +4947,14 @@ 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, consumed); | ||
| error_handler, errors, NULL); | ||
| } | ||
|
|
||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.