Patchwork [2,of,5] util: refactor getstackframes

login
register
mail settings
Submitter timeless@mozdev.org
Date March 11, 2016, 5:43 p.m.
Message ID <35389631c894ac1bc19c.1457718234@waste.org>
Download mbox | patch
Permalink /patch/13801/
State Accepted
Headers show

Comments

timeless@mozdev.org - March 11, 2016, 5:43 p.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1457716924 0
#      Fri Mar 11 17:22:04 2016 +0000
# Node ID 35389631c894ac1bc19cccba78d0f550e8777df5
# Parent  b3b25012e74bb276782957f5126ef16833cf14e2
util: refactor getstackframes

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -2550,6 +2550,28 @@ 
             results.append(hook(*args))
         return results
 
+def getstackframes(skip=0, line=' %-*s in %s\n', fileline='%s:%s'):
+    '''Yields lines for a nicely formatted stacktrace.
+    Skips the 'skip' last entries.
+    Each file+linenumber is formatted according to fileline.
+    Each line is formatted according to line.
+    If line is None, it yields:
+      length of longest filepath+line number,
+      filepath+linenumber,
+      function
+
+    Not be used in production code but very convenient while developing.
+    '''
+    entries = [(fileline % (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:
+            if line is None:
+                yield (fnmax, fnln, func)
+            else:
+                yield line % (fnmax, fnln, func)
+
 def debugstacktrace(msg='stacktrace', skip=0, f=sys.stderr, otherf=sys.stdout):
     '''Writes a message to f (stderr) with a nicely formatted stacktrace.
     Skips the 'skip' last entries. By default it will flush stdout first.
@@ -2559,12 +2581,8 @@ 
     if otherf:
         otherf.flush()
     f.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:
-            f.write(' %-*s in %s\n' % (fnmax, fnln, func))
+    for line in getstackframes(skip + 1):
+        f.write(line)
     f.flush()
 
 class dirs(object):