Mercurial > cpython-lru_cache-weakref
diff Python/exceptions.c @ 30139:aa5cb8e3b9ea 2.3
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.
| author | Walter Dörwald <walter@livinglogic.de> |
|---|---|
| date | Tue, 12 Aug 2003 17:38:22 +0000 |
| parents | b38cebc76d82 |
| children | e373630bfeab |
line wrap: on
line diff
--- a/Python/exceptions.c Tue Aug 12 00:01:16 2003 +0000 +++ b/Python/exceptions.c Tue Aug 12 17:38:22 2003 +0000 @@ -1251,10 +1251,18 @@ 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 @@ 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 @@ 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) );
