-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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 all 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 |
---|---|---|
|
@@ -2896,28 +2896,27 @@ 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", | ||
NULL) < 0) { | ||
PyObject *exc = PyErr_GetRaisedException(); | ||
PyErr_SetString(PyExc_ValueError, | ||
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 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); | ||
|
Uh oh!
There was an error while loading. Please reload this page.