##// END OF EJS Templates
util: add debugstacktrace depth limit...
Mads Kiilerich -
r31315:78ac7061 default
parent child Browse files
Show More
@@ -2841,9 +2841,9 class hooks(object):
2841 2841 results.append(hook(*args))
2842 2842 return results
2843 2843
2844 def getstackframes(skip=0, line=' %-*s in %s\n', fileline='%s:%s'):
2844 def getstackframes(skip=0, line=' %-*s in %s\n', fileline='%s:%s', depth=0):
2845 2845 '''Yields lines for a nicely formatted stacktrace.
2846 Skips the 'skip' last entries.
2846 Skips the 'skip' last entries, then return the last 'depth' entries.
2847 2847 Each file+linenumber is formatted according to fileline.
2848 2848 Each line is formatted according to line.
2849 2849 If line is None, it yields:
@@ -2854,7 +2854,8 def getstackframes(skip=0, line=' %-*s i
2854 2854 Not be used in production code but very convenient while developing.
2855 2855 '''
2856 2856 entries = [(fileline % (fn, ln), func)
2857 for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1]]
2857 for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1]
2858 ][-depth:]
2858 2859 if entries:
2859 2860 fnmax = max(len(entry[0]) for entry in entries)
2860 2861 for fnln, func in entries:
@@ -2863,16 +2864,18 def getstackframes(skip=0, line=' %-*s i
2863 2864 else:
2864 2865 yield line % (fnmax, fnln, func)
2865 2866
2866 def debugstacktrace(msg='stacktrace', skip=0, f=stderr, otherf=stdout):
2867 def debugstacktrace(msg='stacktrace', skip=0,
2868 f=stderr, otherf=stdout, depth=0):
2867 2869 '''Writes a message to f (stderr) with a nicely formatted stacktrace.
2868 Skips the 'skip' last entries. By default it will flush stdout first.
2870 Skips the 'skip' entries closest to the call, then show 'depth' entries.
2871 By default it will flush stdout first.
2869 2872 It can be used everywhere and intentionally does not require an ui object.
2870 2873 Not be used in production code but very convenient while developing.
2871 2874 '''
2872 2875 if otherf:
2873 2876 otherf.flush()
2874 2877 f.write('%s at:\n' % msg.rstrip())
2875 for line in getstackframes(skip + 1):
2878 for line in getstackframes(skip + 1, depth=depth):
2876 2879 f.write(line)
2877 2880 f.flush()
2878 2881
@@ -122,7 +122,7 Test internal debugstacktrace command
122 122 > dst('hello from g\\n', skip=1)
123 123 > h()
124 124 > def h():
125 > dst('hi ...\\nfrom h hidden in g', 1)
125 > dst('hi ...\\nfrom h hidden in g', 1, depth=2)
126 126 > f()
127 127 > EOF
128 128 $ python debugstacktrace.py
@@ -134,6 +134,5 Test internal debugstacktrace command
134 134 debugstacktrace.py:4 in f
135 135 hi ...
136 136 from h hidden in g at:
137 debugstacktrace.py:10 in * (glob)
138 137 debugstacktrace.py:4 in f
139 138 debugstacktrace.py:7 in g
General Comments 0
You need to be logged in to leave comments. Login now