]> granicus.if.org Git - python/commitdiff
Enhance message for UnicodeEncodeError and UnicodeTranslateError.
authorWalter Dörwald <walter@livinglogic.de>
Tue, 12 Aug 2003 17:32:43 +0000 (17:32 +0000)
committerWalter Dörwald <walter@livinglogic.de>
Tue, 12 Aug 2003 17:32:43 +0000 (17:32 +0000)
If there is only one bad character it will now be printed in a
form that is a valid Python string.

Lib/test/test_codeccallbacks.py
Python/exceptions.c

index 134d86cc0bbc63e5e9e4cb0f3d5d39178db35acf..4552462680c7bde7ae5040b93ee034f2982071bc 100644 (file)
@@ -258,7 +258,7 @@ class CodecCallbackTest(unittest.TestCase):
         self.check_exceptionobjectargs(
             UnicodeEncodeError,
             ["ascii", u"g\xfcrk", 1, 2, "ouch"],
-            "'ascii' codec can't encode character '\ufc' in position 1: ouch"
+            "'ascii' codec can't encode character '\\xfc' in position 1: ouch"
         )
         self.check_exceptionobjectargs(
             UnicodeEncodeError,
@@ -268,8 +268,24 @@ class CodecCallbackTest(unittest.TestCase):
         self.check_exceptionobjectargs(
             UnicodeEncodeError,
             ["ascii", u"\xfcx", 0, 1, "ouch"],
-            "'ascii' codec can't encode character '\ufc' in position 0: ouch"
+            "'ascii' codec can't encode character '\\xfc' in position 0: ouch"
         )
+        self.check_exceptionobjectargs(
+            UnicodeEncodeError,
+            ["ascii", u"\u0100x", 0, 1, "ouch"],
+            "'ascii' codec can't encode character '\\u0100' in position 0: ouch"
+        )
+        self.check_exceptionobjectargs(
+            UnicodeEncodeError,
+            ["ascii", u"\uffffx", 0, 1, "ouch"],
+            "'ascii' codec can't encode character '\\uffff' in position 0: ouch"
+        )
+        if sys.maxunicode > 0xffff:
+            self.check_exceptionobjectargs(
+                UnicodeEncodeError,
+                ["ascii", u"\U00010000x", 0, 1, "ouch"],
+                "'ascii' codec can't encode character '\\U00010000' in position 0: ouch"
+            )
 
     def test_unicodedecodeerror(self):
         self.check_exceptionobjectargs(
@@ -287,8 +303,24 @@ class CodecCallbackTest(unittest.TestCase):
         self.check_exceptionobjectargs(
             UnicodeTranslateError,
             [u"g\xfcrk", 1, 2, "ouch"],
-            "can't translate character '\\ufc' in position 1: ouch"
+            "can't translate character '\\xfc' in position 1: ouch"
         )
+        self.check_exceptionobjectargs(
+            UnicodeTranslateError,
+            [u"g\u0100rk", 1, 2, "ouch"],
+            "can't translate character '\\u0100' in position 1: ouch"
+        )
+        self.check_exceptionobjectargs(
+            UnicodeTranslateError,
+            [u"g\uffffrk", 1, 2, "ouch"],
+            "can't translate character '\\uffff' in position 1: ouch"
+        )
+        if sys.maxunicode > 0xffff:
+            self.check_exceptionobjectargs(
+                UnicodeTranslateError,
+                [u"g\U00010000rk", 1, 2, "ouch"],
+                "can't translate character '\\U00010000' in position 1: ouch"
+            )
         self.check_exceptionobjectargs(
             UnicodeTranslateError,
             [u"g\xfcrk", 1, 3, "ouch"],
index f8e330ff53a0c844fd84633c0366a8015295fa21..d49b364e85aa691f108539b6eaed2411a27f2e66 100644 (file)
@@ -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 '\\x%02x' in position %d: %.400s";
+       else if (badchar <= 0xffff)
+          format = "'%.400s' codec can't encode character '\\u%04x' in position %d: %.400s";
+       else
+          format = "'%.400s' codec can't encode character '\\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 '\\x%02x' in position %d: %.400s";
+       else if (badchar <= 0xffff)
+          format = "can't translate character '\\u%04x' in position %d: %.400s";
+       else
+          format = "can't translate character '\\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)
        );