Patchwork [2,of,3] ui: add support for fully printing chained exception tracebacks

login
register
mail settings
Submitter Matt Harbison
Date Feb. 7, 2013, 5:42 a.m.
Message ID <2c31ee8efad0e21d79d7.1360215763@Envy>
Download mbox | patch
Permalink /patch/821/
State Superseded
Headers show

Comments

Matt Harbison - Feb. 7, 2013, 5:42 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1360211366 18000
# Branch stable
# Node ID 2c31ee8efad0e21d79d7caba2e306e918aa7798f
# Parent  8c093ee06ae1b39997e8793f215a638ee198ce8e
ui: add support for fully printing chained exception tracebacks

Currently, only SubrepoAbort has a cause chained to it.
Bryan O'Sullivan - Feb. 8, 2013, 11:39 a.m.
On Wed, Feb 6, 2013 at 9:42 PM, Matt Harbison <matt_harbison@yahoo.com>wrote:

> +            cause=getattr(exc[1], 'cause', None)
> +
> +            if cause is not None:
> +                tbc=traceback.format_tb(cause[2])
> +                tbe=traceback.format_tb(exc[2])
> +                feo=traceback.format_exception_only(cause[0], cause[1])
>

Please fix up the spacing in these lines.

Also, "tbc", "tbe", and "feo" are excessively cryptic variable names. Could
you please find something a bit more descriptive.

Patch

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:
+                tbc=traceback.format_tb(cause[2])
+                tbe=traceback.format_tb(exc[2])
+                feo=traceback.format_exception_only(cause[0], cause[1])
+
+                # exclude frame where 'exc' was chained and rethrown from tbe
+                self.write_err('Traceback (most recent call last):\n',
+                               ''.join(tbe[:-1]),
+                               ''.join(tbc),
+                               ''.join(feo))
+            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
@@ -644,7 +644,43 @@ 
   adding file changes
   added 2 changesets with 3 changes to 2 files
   (run 'hg update' to get a working copy)
-  $ hg -R issue1852b update
+  $ hg -R issue1852b update --traceback
+  Traceback (most recent call last):
+    File "*/mercurial/dispatch.py", line *, in _runcatch (glob)
+      return _dispatch(req)
+    File "*/mercurial/dispatch.py", line *, in _dispatch (glob)
+      cmdpats, cmdoptions)
+    File "*/mercurial/dispatch.py", line *, in runcommand (glob)
+      ret = _runcommand(ui, options, cmd, d)
+    File "*/mercurial/dispatch.py", line *, in _runcommand (glob)
+      return checkargs()
+    File "*/mercurial/dispatch.py", line *, in checkargs (glob)
+      return cmdfunc()
+    File "*/mercurial/dispatch.py", line *, in <lambda> (glob)
+      d = lambda: util.checksignature(func)(ui, *args, **cmdoptions)
+    File "*/mercurial/util.py", line *, in check (glob)
+      return func(*args, **kwargs)
+    File "*/mercurial/commands.py", line *, in update (glob)
+      ret = hg.update(repo, rev)
+    File "*/mercurial/hg.py", line *, in update (glob)
+      stats = updaterepo(repo, node, False)
+    File "*/mercurial/hg.py", line *, in updaterepo (glob)
+      return mergemod.update(repo, node, False, overwrite, None)
+    File "*/mercurial/merge.py", line *, in update (glob)
+      stats = applyupdates(repo, actions, wc, p2, pa, overwrite)
+    File "*/mercurial/merge.py", line *, in applyupdates (glob)
+      subrepo.submerge(repo, wctx, mctx, wctx, overwrite)
+    File "*/mercurial/subrepo.py", line *, in submerge (glob)
+      mctx.sub(s).get(r)
+    File "*/mercurial/subrepo.py", line *, in decoratedmethod (glob)
+      res = func(self, *args, **kargs)
+    File "*/mercurial/subrepo.py", line *, in get (glob)
+      self._get(state)
+    File "*/mercurial/subrepo.py", line *, in _get (glob)
+      srcurl = _abssource(self._repo)
+    File "*/mercurial/subrepo.py", line *, in _abssource (glob)
+      raise util.Abort(_("default path for subrepository not found"))
+  Abort: default path for subrepository not found
   abort: default path for subrepository not found (in subrepo sub/repo) (glob)
   [255]