Last active
August 29, 2015 14:03
-
-
Save muzhig/6bda9b7d0022921b6047 to your computer and use it in GitHub Desktop.
iter_stack (with locals of each frame)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def iter_stack(tb, limit=None): | |
"""modified version of traceback.extract_stack. it yields same tuples, but with frame.f_locals appended.""" | |
if limit is None: | |
if hasattr(sys, 'tracebacklimit'): | |
limit = sys.tracebacklimit | |
n = 0 | |
while tb is not None and (limit is None or n < limit): | |
f = tb.tb_frame | |
lineno = tb.tb_lineno | |
co = f.f_code | |
filename = co.co_filename | |
name = co.co_name | |
linecache.checkcache(filename) | |
line = linecache.getline(filename, lineno, f.f_globals) | |
line = line.strip() if line else None | |
n += 1 | |
yield (filename, lineno, name, line, f.f_locals) | |
tb = tb.tb_next |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment