Skip to content

Commit

Permalink
allow reuse of debug code
Browse files Browse the repository at this point in the history
  • Loading branch information
elifiner committed Dec 25, 2012
1 parent f604328 commit 0beca4d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
18 changes: 3 additions & 15 deletions pydump
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
#!/usr/bin/env python
import pdb
import sys
import pydump
import linecache
from optparse import OptionParser

def cache_files(files):
for name, data in files.iteritems():
lines = [line+'\n' for line in data.splitlines()]
linecache.cache[name] = (len(data), None, lines, name)

def patch_linecache_checkcache():
linecache.checkcache = lambda filename=None: None

if __name__ == '__main__':

parser = OptionParser(usage="%prog <filename.dump> [options]", description="pydump v%s: post-mortem debugging for Python programs" % pydump.__version__)
Expand All @@ -24,11 +17,6 @@ if __name__ == '__main__':
parser.print_help()
sys.exit(1)

dump = pydump.load_dump(args[0])
cache_files(dump['files'])
patch_linecache_checkcache()
tb = dump['traceback']

if not options.debuggers:
options.debuggers = ["pdb"]

Expand All @@ -41,7 +29,7 @@ if __name__ == '__main__':
else:
print >>sys.stderr, "Starting %s..." % debugger
if debugger == "pudb":
dbg.post_mortem((None, None, tb))
pydump.debug_dump(args[0], lambda tb: dbg.post_mortem((None, None, tb)))
else:
dbg.post_mortem(tb)
pydump.debug_dump(args[0])
break
17 changes: 16 additions & 1 deletion pydump.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@

import os
import sys
import pdb
import pickle
import linecache

__version__ = "1.0.1"
__all__ = ['save_dump', 'load_dump', '__version__']
__all__ = ['save_dump', 'load_dump', 'debug_dump', '__version__']

DUMP_VERSION = 1

Expand Down Expand Up @@ -59,6 +60,15 @@ def load_dump(filename):
sys.modules['pydump.pydump'] = sys.modules[__name__]
return pickle.load(open(filename, 'rb'))

def debug_dump(dump_filename, post_mortem_func=pdb.post_mortem):
dump = load_dump(dump_filename)
_cache_files(dump['files'])
tb = dump['traceback']
_old_checkcache = linecache.checkcache
linecache.checkcache = lambda filename=None: None
post_mortem_func(tb)
linecache.checkcache = _old_checkcache

class FakeClass(object):
def __init__(self, repr, vars):
self.__repr = repr
Expand Down Expand Up @@ -125,3 +135,8 @@ def _safe_repr(v):

def _flat_dict(d):
return dict(zip(d.keys(), [_safe_repr(v) for v in d.values()]))

def _cache_files(files):
for name, data in files.iteritems():
lines = [line+'\n' for line in data.splitlines()]
linecache.cache[name] = (len(data), None, lines, name)

0 comments on commit 0beca4d

Please sign in to comment.