summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWalter Dörwald <walter@livinglogic.de>2007-06-05 13:29:29 (GMT)
committerWalter Dörwald <walter@livinglogic.de>2007-06-05 13:29:29 (GMT)
commit787b03ba4b023663074a76b473b143d0a2998c3b (patch)
tree561023956097402434a2ca82d97348dd97a08720
parentcf47af4d713f4993ee9326f83dc79197896c1b9a (diff)
downloadcpython-787b03ba4b023663074a76b473b143d0a2998c3b.zip
cpython-787b03ba4b023663074a76b473b143d0a2998c3b.tar.gz
cpython-787b03ba4b023663074a76b473b143d0a2998c3b.tar.bz2
PyUnicode_FromFormat() does support %02x, so use it
for formatting the unicode decoding/encoding/translating exception messages.
-rw-r--r--Objects/exceptions.c31
1 files changed, 14 insertions, 17 deletions
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index 6cb234b..2fb58e2 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -1341,17 +1341,17 @@ UnicodeEncodeError_str(PyObject *self)
if (end==start+1) {
int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
- char badchar_str[20];
+ const char *fmt;
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)
- 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
- 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(
- "'%U' codec can't encode character u'\\%s' in position %zd: %U",
+ fmt,
((PyUnicodeErrorObject *)self)->encoding,
- badchar_str,
+ badchar,
start,
((PyUnicodeErrorObject *)self)->reason
);
@@ -1416,12 +1416,9 @@ UnicodeDecodeError_str(PyObject *self)
return NULL;
if (end==start+1) {
- /* FromFormat does not support %02x, so format that separately */
- char byte[4];
- PyOS_snprintf(byte, sizeof(byte), "%02x",
- ((int)PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[start])&0xff);
+ int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[start]&0xff);
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,
byte,
start,
@@ -1513,16 +1510,16 @@ UnicodeTranslateError_str(PyObject *self)
if (end==start+1) {
int badchar = (int)PyUnicode_AS_UNICODE(((PyUnicodeErrorObject *)self)->object)[start];
- char badchar_str[20];
+ const char *fmt;
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)
- PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
+ fmt = "can't translate character u'\\u%04x' in position %zd: %U";
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(
- "can't translate character u'\\%s' in position %zd: %U",
- badchar_str,
+ fmt,
+ badchar,
start,
((PyUnicodeErrorObject *)self)->reason
);