[Python-checkins] r80777 - in python/trunk: Lib/test/test_traceback.py Lib/traceback.py Misc/NEWS

victor.stinner python-checkins at python.org
Wed May 5 14:40:49 CEST 2010


Author: victor.stinner
Date: Wed May  5 14:40:49 2010
New Revision: 80777

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


Modified:
   python/trunk/Lib/test/test_traceback.py
   python/trunk/Lib/traceback.py
   python/trunk/Misc/NEWS

Modified: python/trunk/Lib/test/test_traceback.py
==============================================================================
--- python/trunk/Lib/test/test_traceback.py	(original)
+++ python/trunk/Lib/test/test_traceback.py	Wed May  5 14:40:49 2010
@@ -159,6 +159,15 @@
         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):
 

Modified: python/trunk/Lib/traceback.py
==============================================================================
--- python/trunk/Lib/traceback.py	(original)
+++ python/trunk/Lib/traceback.py	Wed May  5 14:40:49 2010
@@ -211,8 +211,14 @@
 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):

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Wed May  5 14:40:49 2010
@@ -39,6 +39,9 @@
 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


More information about the Python-checkins mailing list