[PATCH 2 of 4 STABLE V3] ui: add support for fully printing chained exception stacks in ui.traceback()

Matt Harbison matt_harbison at yahoo.com
Thu Feb 14 21:57:52 CST 2013


# HG changeset patch
# User Matt Harbison <matt_harbison at yahoo.com>
# Date 1360437334 18000
# Branch stable
# Node ID 18b83d035077297b4da66bbd177743533870b061
# Parent  620ec1695340dc622212356dee673807d6a853f0
ui: add support for fully printing chained exception stacks in ui.traceback()

Currently, only SubrepoAbort has a cause chained to it.

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -686,11 +686,23 @@
         only to call in exception handler. returns true if traceback
         printed.'''
         if self.tracebackflag:
-            if exc:
+            if exc is None:
+                exc = sys.exc_info()
+            cause = getattr(exc[1], 'cause', None)
+
+            if cause is not None:
+                causetb = traceback.format_tb(cause[2])
+                exctb = traceback.format_tb(exc[2])
+                exconly = traceback.format_exception_only(cause[0], cause[1])
+
+                # exclude frame where 'exc' was chained and rethrown from exctb
+                self.write_err('Traceback (most recent call last):\n',
+                               ''.join(exctb[:-1]),
+                               ''.join(causetb),
+                               ''.join(exconly))
+            else:
                 traceback.print_exception(exc[0], exc[1], exc[2],
                                           file=self.ferr)
-            else:
-                traceback.print_exc(file=self.ferr)
         return self.tracebackflag
 
     def geteditor(self):
diff --git a/tests/test-subrepo.t b/tests/test-subrepo.t
--- a/tests/test-subrepo.t
+++ b/tests/test-subrepo.t
@@ -648,6 +648,11 @@
   abort: default path for subrepository not found (in subrepo sub/repo) (glob)
   [255]
 
+Ensure a full traceback, not just the SubrepoAbort part
+
+  $ hg -R issue1852b update --traceback 2>&1 | grep 'raise util\.Abort'
+      raise util.Abort(_("default path for subrepository not found"))
+
 Pull -u now doesn't help
 
   $ hg -R issue1852b pull -u issue1852a


More information about the Mercurial-devel mailing list