diff options
author | Walter Dörwald <walter@livinglogic.de> | 2003-08-12 17:38:22 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2003-08-12 17:38:22 (GMT) |
commit | 0098d8643341de44c731b398cf92c2e533cfb0e6 (patch) | |
tree | 3d0053789790c5f3b79be33ef44626a0a46a8e9b /Python/exceptions.c | |
parent | 6c8980aecdbf7f435d1688a3b9d58ad13e2f06f2 (diff) | |
download | cpython-0098d8643341de44c731b398cf92c2e533cfb0e6.zip cpython-0098d8643341de44c731b398cf92c2e533cfb0e6.tar.gz cpython-0098d8643341de44c731b398cf92c2e533cfb0e6.tar.bz2 |
Backport checkins:
* Enhance message for UnicodeEncodeError and UnicodeTranslateError.
If there is only one bad character it will now be printed in a
form that is a valid Python string.
* Add a unicode prefix to the characters in the UnicodeEncodeError
and UnicodeTranslateError message.
Diffstat (limited to 'Python/exceptions.c')
-rw-r--r-- | Python/exceptions.c | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/Python/exceptions.c b/Python/exceptions.c index f8e330f..da78f2b 100644 --- a/Python/exceptions.c +++ b/Python/exceptions.c @@ -1251,10 +1251,18 @@ UnicodeEncodeError__str__(PyObject *self, PyObject *arg) goto error; if (end==start+1) { + int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start]; + char *format; + if (badchar <= 0xff) + format = "'%.400s' codec can't encode character u'\\x%02x' in position %d: %.400s"; + else if (badchar <= 0xffff) + format = "'%.400s' codec can't encode character u'\\u%04x' in position %d: %.400s"; + else + format = "'%.400s' codec can't encode character u'\\U%08x' in position %d: %.400s"; PyOS_snprintf(buffer, sizeof(buffer), - "'%.400s' codec can't encode character '\\u%x' in position %d: %.400s", + format, PyString_AS_STRING(encodingObj), - (int)PyUnicode_AS_UNICODE(objectObj)[start], + badchar, start, PyString_AS_STRING(reasonObj) ); @@ -1329,7 +1337,7 @@ UnicodeDecodeError__str__(PyObject *self, PyObject *arg) if (end==start+1) { PyOS_snprintf(buffer, sizeof(buffer), - "'%.400s' codec can't decode byte 0x%x in position %d: %.400s", + "'%.400s' codec can't decode byte 0x%02x in position %d: %.400s", PyString_AS_STRING(encodingObj), ((int)PyString_AS_STRING(objectObj)[start])&0xff, start, @@ -1438,9 +1446,17 @@ UnicodeTranslateError__str__(PyObject *self, PyObject *arg) goto error; if (end==start+1) { + int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start]; + char *format; + if (badchar <= 0xff) + format = "can't translate character u'\\x%02x' in position %d: %.400s"; + else if (badchar <= 0xffff) + format = "can't translate character u'\\u%04x' in position %d: %.400s"; + else + format = "can't translate character u'\\U%08x' in position %d: %.400s"; PyOS_snprintf(buffer, sizeof(buffer), - "can't translate character '\\u%x' in position %d: %.400s", - (int)PyUnicode_AS_UNICODE(objectObj)[start], + format, + badchar, start, PyString_AS_STRING(reasonObj) ); |