forked from dmanchon/pydump
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pydump.py
202 lines (172 loc) · 6.45 KB
/
pydump.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""
The MIT License (MIT)
Copyright (C) 2012 Eli Finer <eli.finer@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import os
import sys
import pdb
import gzip
import pickle
import linecache
import inspect
import dill
import builtins as __builtin__
__version__ = "1.1.1"
DUMP_VERSION = 1
def save_dump(filename, tb=None):
"""
Saves a Python traceback in a pickled file. This function will usually be called from
an except block to allow post-mortem debugging of a failed process.
The saved file can be loaded with load_dump which creates a fake traceback
object that can be passed to any reasonable Python debugger.
The simplest way to do that is to run:
$ pydump.py my_dump_file.dump
"""
if not tb:
tb = sys.exc_info()[2]
fake_tb = FakeTraceback(tb)
_remove_builtins(fake_tb)
dump = {
'traceback':fake_tb,
'files':_get_traceback_files(fake_tb),
'dump_version' : DUMP_VERSION
}
with gzip.open(filename, 'wb') as f:
dill.dump(dump, f)
def load_dump(filename):
# ugly hack to handle running non-install pydump
if 'pydump.pydump' not in sys.modules:
sys.modules['pydump.pydump'] = sys.modules[__name__]
with gzip.open(filename, 'rb') as f:
try:
return dill.load(f)
except IOError:
with open(filename, 'rb') as f:
return dill.load(f)
def debug_dump(dump_filename, post_mortem_func=pdb.post_mortem):
dump = load_dump(dump_filename)
_cache_files(dump['files'])
tb = dump['traceback']
_inject_builtins(tb)
_old_checkcache = linecache.checkcache
_old_isframe = inspect.isframe
_old_iscode = inspect.iscode
_old_istraceback = inspect.istraceback
_old_isclass = inspect.isclass
linecache.checkcache = lambda filename=None: None
inspect.isframe = lambda o: _old_isframe(o) or isinstance(o, FakeFrame)
inspect.iscode = lambda o: _old_iscode(o) or isinstance(o, FakeCode)
inspect.istraceback = lambda o: _old_istraceback(o) or isinstance(o, FakeTraceback)
inspect.isclass = lambda o: _old_isclass(o) or isinstance(o, FakeClass)
post_mortem_func(tb)
inspect.isframe = _old_isframe
inspect.iscode = _old_iscode
inspect.istraceback = _old_istraceback
inspect.isclass = _old_isclass
linecache.checkcache = _old_checkcache
class FakeClass(object):
def __init__(self, repr, vars):
self.__repr = repr
self.__dict__.update(vars)
def __repr__(self):
return self.__repr
class FakeCode(object):
def __init__(self, code):
self.co_filename = os.path.abspath(code.co_filename)
self.co_name = code.co_name
self.co_argcount = code.co_argcount
self.co_consts = tuple(
FakeCode(c) if hasattr(c, 'co_filename') else c
for c in code.co_consts
)
self.co_firstlineno = code.co_firstlineno
self.co_lnotab = code.co_lnotab
self.co_varnames = code.co_varnames
self.co_flags = code.co_flags
class FakeFrame():
def __init__(self, frame):
self.f_code = FakeCode(frame.f_code)
self.f_locals = _convert_dict(frame.f_locals)
self.f_globals = _convert_dict(frame.f_globals)
self.f_lineno = frame.f_lineno
self.f_back = FakeFrame(frame.f_back) if frame.f_back else None
if 'self' in self.f_locals:
self.f_locals['self'] = _convert_obj(frame.f_locals['self'])
class FakeTraceback(object):
def __init__(self, traceback):
self.tb_frame = FakeFrame(traceback.tb_frame)
self.tb_lineno = traceback.tb_lineno
self.tb_next = FakeTraceback(traceback.tb_next) if traceback.tb_next else None
self.tb_lasti = 0
def _remove_builtins(fake_tb):
traceback = fake_tb
while traceback:
frame = traceback.tb_frame
while frame:
frame.f_globals = dict(
(k,v) for k,v in frame.f_globals.items()
if k not in dir(__builtin__)
)
frame = frame.f_back
traceback = traceback.tb_next
def _inject_builtins(fake_tb):
traceback = fake_tb
while traceback:
frame = traceback.tb_frame
while frame:
frame.f_globals.update(__builtin__.__dict__)
frame = frame.f_back
traceback = traceback.tb_next
def _get_traceback_files(traceback):
files = {}
while traceback:
frame = traceback.tb_frame
while frame:
filename = os.path.abspath(frame.f_code.co_filename)
if filename not in files:
try:
files[filename] = open(filename).read()
except IOError:
files[filename] = "couldn't locate '%s' during dump" % frame.f_code.co_filename
frame = frame.f_back
traceback = traceback.tb_next
return files
def _safe_repr(v):
try:
return repr(v)
except Exception as e:
return "repr error: " + str(e)
def _convert_obj(obj):
try:
return FakeClass(_safe_repr(obj), _convert_dict(obj.__dict__))
except:
return _convert(obj)
def _convert_dict(v):
return dict((_convert(k), _convert(i)) for (k, i) in v.items())
def _convert_seq(v):
return (_convert(i) for i in v)
def _convert(v):
try:
dill.dumps(v)
return v
except:
return _safe_repr(v)
def _cache_files(files):
for name, data in files.items():
lines = [line+'\n' for line in data.splitlines()]
linecache.cache[name] = (len(data), None, lines, name)