[Python-checkins] cpython (merge 3.3 -> default): Issue #16491: IDLE now prints chained exception tracebacks.

serhiy.storchaka python-checkins at python.org
Wed Jan 9 11:29:28 CET 2013


http://hg.python.org/cpython/rev/0793d68a0eba
changeset:   81338:0793d68a0eba
parent:      81335:f65eae38f71e
parent:      81337:3feead75c7a5
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Wed Jan 09 12:26:54 2013 +0200
summary:
  Issue #16491: IDLE now prints chained exception tracebacks.

files:
  Lib/idlelib/run.py |  37 +++++++++++++++++++++++++--------
  Misc/NEWS          |   2 +
  2 files changed, 30 insertions(+), 9 deletions(-)


diff --git a/Lib/idlelib/run.py b/Lib/idlelib/run.py
--- a/Lib/idlelib/run.py
+++ b/Lib/idlelib/run.py
@@ -171,15 +171,34 @@
     efile = sys.stderr
     typ, val, tb = excinfo = sys.exc_info()
     sys.last_type, sys.last_value, sys.last_traceback = excinfo
-    tbe = traceback.extract_tb(tb)
-    print('Traceback (most recent call last):', file=efile)
-    exclude = ("run.py", "rpc.py", "threading.py", "queue.py",
-               "RemoteDebugger.py", "bdb.py")
-    cleanup_traceback(tbe, exclude)
-    traceback.print_list(tbe, file=efile)
-    lines = traceback.format_exception_only(typ, val)
-    for line in lines:
-        print(line, end='', file=efile)
+    seen = set()
+
+    def print_exc(typ, exc, tb):
+        seen.add(exc)
+        context = exc.__context__
+        cause = exc.__cause__
+        if cause is not None and cause not in seen:
+            print_exc(type(cause), cause, cause.__traceback__)
+            print("\nThe above exception was the direct cause "
+                  "of the following exception:\n", file=efile)
+        elif (context is not None and
+              not exc.__suppress_context__ and
+              context not in seen):
+            print_exc(type(context), context, context.__traceback__)
+            print("\nDuring handling of the above exception, "
+                  "another exception occurred:\n", file=efile)
+        if tb:
+            tbe = traceback.extract_tb(tb)
+            print('Traceback (most recent call last):', file=efile)
+            exclude = ("run.py", "rpc.py", "threading.py", "queue.py",
+                       "RemoteDebugger.py", "bdb.py")
+            cleanup_traceback(tbe, exclude)
+            traceback.print_list(tbe, file=efile)
+        lines = traceback.format_exception_only(typ, exc)
+        for line in lines:
+            print(line, end='', file=efile)
+
+    print_exc(typ, val, tb)
 
 def cleanup_traceback(tb, exclude):
     "Remove excluded traces from beginning/end of tb; get cached lines"
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -209,6 +209,8 @@
 Library
 -------
 
+- Issue #16491: IDLE now prints chained exception tracebacks.
+
 - fcntl: add F_DUPFD_CLOEXEC constant, available on Linux 2.6.24+.
 
 - Issue #15972: Fix error messages when os functions expecting a file name or

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list