Skip to content

Instantly share code, notes, and snippets.

@muzhig
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muzhig/6bda9b7d0022921b6047 to your computer and use it in GitHub Desktop.
Save muzhig/6bda9b7d0022921b6047 to your computer and use it in GitHub Desktop.
iter_stack (with locals of each frame)
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