]> granicus.if.org Git - python/commitdiff
Merged revisions 80777 via svnmerge from
authorVictor Stinner <victor.stinner@haypocalc.com>
Wed, 5 May 2010 12:45:31 +0000 (12:45 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Wed, 5 May 2010 12:45:31 +0000 (12:45 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r80777 | victor.stinner | 2010-05-05 14:40:49 +0200 (mer., 05 mai 2010) | 3 lines

  Issue #8313: traceback.format_exception_only() encodes unicode message to
  ASCII with backslashreplace error handler if str(value) failed
........

Lib/test/test_traceback.py
Lib/traceback.py
Misc/NEWS

index 1c067d776c05fbb1d6a97078d1b73856cb4c6f75..6ef70a61ce486508be431a1dfff1e56910536f7d 100644 (file)
@@ -168,6 +168,15 @@ def test():
         err = traceback.format_exception_only(None, None)
         self.assertEqual(err, ['None\n'])
 
+    def test_unicode(self):
+        err = AssertionError('\xff')
+        lines = traceback.format_exception_only(type(err), err)
+        self.assertEqual(lines, ['AssertionError: \xff\n'])
+
+        err = AssertionError(u'\xe9')
+        lines = traceback.format_exception_only(type(err), err)
+        self.assertEqual(lines, ['AssertionError: \\xe9\n'])
+
 
 class TracebackFormatTests(unittest.TestCase):
 
index ed0f256c77eea559e5a0133b5ae07386818cebd8..2a77d2cfe85cbd31d42212dc2fbb60eacd9e079d 100644 (file)
@@ -212,8 +212,14 @@ def _format_final_exc_line(etype, value):
 def _some_str(value):
     try:
         return str(value)
-    except:
-        return '<unprintable %s object>' % type(value).__name__
+    except Exception:
+        pass
+    try:
+        value = unicode(value)
+        return value.encode("ascii", "backslashreplace")
+    except Exception:
+        pass
+    return '<unprintable %s object>' % type(value).__name__
 
 
 def print_exc(limit=None, file=None):
index 8ab0508bcd3f1b88679c2a8b09149b063ce3e2fc..5d7bd4fa7699eb7c6060d346d36f0076a7269b3f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -33,6 +33,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #8313: traceback.format_exception_only() encodes unicode message to
+  ASCII with backslashreplace error handler if str(value) failed
+
 - Issue #8567: Fix precedence of signals in Decimal module: when a
   Decimal operation raises multiple signals and more than one of those
   signals is trapped, the specification determines the order in which