mirror of
https://github.com/python/cpython.git
synced 2025-12-04 16:43:27 +00:00
PyUnicode_FromFormat() does support %02x, so use it
for formatting the unicode decoding/encoding/translating exception messages.
This commit is contained in:
parent
cf47af4d71
commit
787b03ba4b
1 changed files with 14 additions and 17 deletions
|
|
@ -1341,17 +1341,17 @@ UnicodeEncodeError_str(PyObject *self)
|
||||||
|
|
||||||
if (end==start+1) {
|
if (end==start+1) {
|
||||||
int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
|
int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
|
||||||
char badchar_str[20];
|
const char *fmt;
|
||||||
if (badchar <= 0xff)
|
if (badchar <= 0xff)
|
||||||
PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
|
fmt = "'%U' codec can't encode character u'\\x%02x' in position %zd: %U";
|
||||||
else if (badchar <= 0xffff)
|
else if (badchar <= 0xffff)
|
||||||
PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
|
fmt = "'%U' codec can't encode character u'\\u%04x' in position %zd: %U";
|
||||||
else
|
else
|
||||||
PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
|
fmt = "'%U' codec can't encode character u'\\U%08x' in position %zd: %U";
|
||||||
return PyUnicode_FromFormat(
|
return PyUnicode_FromFormat(
|
||||||
"'%U' codec can't encode character u'\\%s' in position %zd: %U",
|
fmt,
|
||||||
((PyUnicodeErrorObject *)self)->encoding,
|
((PyUnicodeErrorObject *)self)->encoding,
|
||||||
badchar_str,
|
badchar,
|
||||||
start,
|
start,
|
||||||
((PyUnicodeErrorObject *)self)->reason
|
((PyUnicodeErrorObject *)self)->reason
|
||||||
);
|
);
|
||||||
|
|
@ -1416,12 +1416,9 @@ UnicodeDecodeError_str(PyObject *self)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (end==start+1) {
|
if (end==start+1) {
|
||||||
/* FromFormat does not support %02x, so format that separately */
|
int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[start]&0xff);
|
||||||
char byte[4];
|
|
||||||
PyOS_snprintf(byte, sizeof(byte), "%02x",
|
|
||||||
((int)PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[start])&0xff);
|
|
||||||
return PyUnicode_FromFormat(
|
return PyUnicode_FromFormat(
|
||||||
"'%U' codec can't decode byte 0x%s in position %zd: %U",
|
"'%U' codec can't decode byte 0x%02x in position %zd: %U",
|
||||||
((PyUnicodeErrorObject *)self)->encoding,
|
((PyUnicodeErrorObject *)self)->encoding,
|
||||||
byte,
|
byte,
|
||||||
start,
|
start,
|
||||||
|
|
@ -1513,16 +1510,16 @@ UnicodeTranslateError_str(PyObject *self)
|
||||||
|
|
||||||
if (end==start+1) {
|
if (end==start+1) {
|
||||||
int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
|
int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
|
||||||
char badchar_str[20];
|
const char *fmt;
|
||||||
if (badchar <= 0xff)
|
if (badchar <= 0xff)
|
||||||
PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
|
fmt = "can't translate character u'\\x%02x' in position %zd: %U";
|
||||||
else if (badchar <= 0xffff)
|
else if (badchar <= 0xffff)
|
||||||
PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
|
fmt = "can't translate character u'\\u%04x' in position %zd: %U";
|
||||||
else
|
else
|
||||||
PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
|
fmt = "can't translate character u'\\U%08x' in position %zd: %U";
|
||||||
return PyUnicode_FromFormat(
|
return PyUnicode_FromFormat(
|
||||||
"can't translate character u'\\%s' in position %zd: %U",
|
fmt,
|
||||||
badchar_str,
|
badchar,
|
||||||
start,
|
start,
|
||||||
((PyUnicodeErrorObject *)self)->reason
|
((PyUnicodeErrorObject *)self)->reason
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue