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

Mads Kiilerich mads at kiilerich.com
Sun Nov 17 11:53:57 CST 2013


# HG changeset patch
# User Mads Kiilerich <madski at 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
...

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))
+


More information about the Mercurial-devel mailing list