Patchwork [1,of,3,fyi] ui: introduce warnstack for dumping a stack trace without crashing

login
register
mail settings
Submitter Mads Kiilerich
Date Nov. 17, 2013, 5:53 p.m.
Message ID <41d9e7b97f3ced21c066.1384710837@localhost.localdomain>
Download mbox | patch
Permalink /patch/3023/
State Superseded
Headers show

Comments

Mads Kiilerich - Nov. 17, 2013, 5:53 p.m.
# HG changeset patch
# User Mads Kiilerich <madski@unity3d.com>
# Date 1384710551 18000
#      Sun Nov 17 12:49:11 2013 -0500
# Branch stable
# Node ID 41d9e7b97f3ced21c0665253a6495165f58a2730
# Parent  cb466830826a5fb0eecd4428922c2618d6a1b8af
ui: introduce warnstack for dumping a stack trace without crashing

This is often very handy when hacking/debugging.

Calling ui.warnstack('hey') will give
  hey at:
   ./hg:38                                     in <module>
   /home/user/hgsrc/mercurial/dispatch.py:28   in run
   /home/user/hgsrc/mercurial/dispatch.py:65   in dispatch
   /home/user/hgsrc/mercurial/dispatch.py:88   in _runcatch
   /home/user/hgsrc/mercurial/dispatch.py:740  in _dispatch
   /home/user/hgsrc/mercurial/dispatch.py:514  in runcommand
   /home/user/hgsrc/mercurial/dispatch.py:830  in _runcommand
   /home/user/hgsrc/mercurial/dispatch.py:801  in checkargs
   /home/user/hgsrc/mercurial/dispatch.py:737  in <lambda>
   /home/user/hgsrc/mercurial/util.py:472      in check
...
Matt Mackall - Jan. 10, 2014, 8:07 p.m.
On Sun, 2013-11-17 at 12:53 -0500, Mads Kiilerich wrote:
> # HG changeset patch
> # User Mads Kiilerich <madski@unity3d.com>
> # Date 1384710551 18000
> #      Sun Nov 17 12:49:11 2013 -0500
> # Branch stable
> # Node ID 41d9e7b97f3ced21c0665253a6495165f58a2730
> # Parent  cb466830826a5fb0eecd4428922c2618d6a1b8af
> ui: introduce warnstack for dumping a stack trace without crashing

I'd probably accept this if it moved to util.

Patch

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -813,3 +813,16 @@  class ui(object):
         ui.write(ui.label(s, 'label')).
         '''
         return msg
+
+def warnstack(msg='warnstack', skip=0):
+    '''issue warning with the message and the current stack, skipping the
+    skip last entries. not be used in production code but very convenient
+    while developing.'''
+    sys.stderr.write('%s at:\n' % msg)
+    entries = [('%s:%s' % (fn, ln), func)
+        for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1]]
+    if entries:
+        fnmax = max(len(entry[0]) for entry in entries)
+        for fnln, func in entries:
+            sys.stderr.write(' %-*s in %s\n' % (fnmax, fnln, func))
+