]> granicus.if.org Git - python/commitdiff
Patch #860326: traceback.format_exception_only() now prepends the
authorGeorg Brandl <georg@python.org>
Wed, 12 Apr 2006 21:14:09 +0000 (21:14 +0000)
committerGeorg Brandl <georg@python.org>
Wed, 12 Apr 2006 21:14:09 +0000 (21:14 +0000)
exception's module name to non-builtin exceptions, like the interpreter
itself does.

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

index 22c04567a7232f2fa1901e14d0cabf150a901517..26ab7dc111896708edde51fce9acc8fc2cdfbf68 100644 (file)
@@ -5,6 +5,9 @@ from test.test_support import run_unittest, is_jython
 
 import traceback
 
+class TbError(Exception):
+    pass
+
 class TracebackCases(unittest.TestCase):
     # For now, a very minimal set of tests.  I want to be sure that
     # formatting of SyntaxErrors works based on changes for 2.1.
@@ -103,6 +106,24 @@ def test():
             import sys
             sys.exc_traceback.__members__
 
+    def raise_tberror(self):
+        raise TbError
+
+    def raise_typeerror(self):
+        raise TypeError
+
+    def test_modulename(self):
+        # Bug 860326: format_exception_only should prepend module name
+        # to exceptions not in "exceptions", like PyErr_Print does.
+        err = self.get_exception_format(self.raise_tberror, TbError)
+        self.assertEquals(len(err), 1)
+        self.assert_(err[0] == '__main__.TbError\n' or
+                     err[0] == 'test.test_traceback.TbError\n')
+
+        err = self.get_exception_format(self.raise_typeerror, TypeError)
+        self.assertEquals(err[0], 'TypeError\n')
+
+
 def test_main():
     run_unittest(TracebackCases)
 
index 454eb1b7de6400ad7f8fe5fc57f84180f4e4b0d8..56a87dc4e28c8ada6c67818fdaf7be4bfd6a40c1 100644 (file)
@@ -158,8 +158,12 @@ def format_exception_only(etype, value):
     """
     list = []
     if (type(etype) == types.ClassType
-        or (isinstance(etype, type) and issubclass(etype, Exception))):
+        or (isinstance(etype, type) and issubclass(etype, BaseException))):
         stype = etype.__name__
+        if not hasattr(etype, '__module__'):
+            stype = '<unknown>.' + stype
+        elif etype.__module__ != 'exceptions':
+            stype = etype.__module__ + '.' + stype
     else:
         stype = etype
     if value is None:
index 1e242d056c1e7af8a1535da1654e1f908006091a..5ee249f03f48102159bc31a8181c63092c775d32 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -49,6 +49,10 @@ Extension Modules
 Library
 -------
 
+- Patch #860326: traceback.format_exception_only() now prepends the
+  exception's module name to non-builtin exceptions, like the interpreter
+  itself does.
+
 - SimpleXMLRPCServer relied on the fcntl module, which is unavailable on
   Windows. Bug #1469163.