Skip to content

Commit 145856d

Browse files
committed
Better remote exception formatting
1 parent ebb3b3e commit 145856d

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

pymc3/parallel_sampling.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import multiprocessing
22
import multiprocessing.sharedctypes
3-
import sys
43
import ctypes
54
import time
65
import logging
76
from collections import namedtuple
7+
import traceback
88

99
import six
1010
import numpy as np
@@ -13,6 +13,32 @@
1313

1414
logger = logging.getLogger('pymc3')
1515

16+
17+
# Taken from https://hg.python.org/cpython/rev/c4f92b597074
18+
class RemoteTraceback(Exception):
19+
def __init__(self, tb):
20+
self.tb = tb
21+
22+
def __str__(self):
23+
return self.tb
24+
25+
26+
class ExceptionWithTraceback:
27+
def __init__(self, exc, tb):
28+
tb = traceback.format_exception(type(exc), exc, tb)
29+
tb = ''.join(tb)
30+
self.exc = exc
31+
self.tb = '\n"""\n%s"""' % tb
32+
33+
def __reduce__(self):
34+
return rebuild_exc, (self.exc, self.tb)
35+
36+
37+
def rebuild_exc(exc, tb):
38+
exc.__cause__ = RemoteTraceback(tb)
39+
return exc
40+
41+
1642
# Messages
1743
# ('writing_done', is_last, sample_idx, tuning, stats)
1844
# ('error', *exception_info)
@@ -47,9 +73,9 @@ def run(self):
4773
self._start_loop()
4874
except KeyboardInterrupt:
4975
pass
50-
except BaseException:
51-
exc_info = sys.exc_info()
52-
self._msg_pipe.send(('error', exc_info[:2]))
76+
except BaseException as e:
77+
e = ExceptionWithTraceback(e, e.__traceback__)
78+
self._msg_pipe.send(('error', e))
5379
finally:
5480
self._msg_pipe.close()
5581

@@ -193,7 +219,7 @@ def recv_draw(processes, timeout=3600):
193219
msg = ready[0].recv()
194220

195221
if msg[0] == 'error':
196-
old = msg[1][1]#.with_traceback(msg[1][2])
222+
old = msg[1]
197223
six.raise_from(RuntimeError('Chain %s failed.' % proc.chain), old)
198224
elif msg[0] == 'writing_done':
199225
proc._readable = True

0 commit comments

Comments
 (0)