提交 357723ed authored 作者: Olivier Breuleux's avatar Olivier Breuleux

added op traceback support to PerformLinker

上级 cb772e00
...@@ -130,7 +130,7 @@ class _test_PerformLinker(unittest.TestCase): ...@@ -130,7 +130,7 @@ class _test_PerformLinker(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
......
...@@ -13,40 +13,17 @@ def thunk_hook(type, value, trace): ...@@ -13,40 +13,17 @@ def thunk_hook(type, value, trace):
if len(value.args) > 0 and hasattr(value[0], '__thunk_trace__'): if len(value.args) > 0 and hasattr(value[0], '__thunk_trace__'):
# such a hack :( # such a hack :(
trace2 = value[0].__thunk_trace__ #.exc_info trace2 = value[0].__thunk_trace__ #.exc_info
print>>sys.stderr, "Definition in: " if trace2 is None:
for line in traceback.format_list(trace2): print>>sys.stderr, "Could not find where this Op was defined."
print>>sys.stderr, line, print>>sys.stderr, " * You might have instantiated this Op directly instead of using a constructor."
print>>sys.stderr, " * The Op you constructed might have been optimized. Try turning off optimizations."
elif trace2:
print>>sys.stderr, "Definition in: "
for line in traceback.format_list(trace2):
print>>sys.stderr, line,
__excepthook(type, value, trace) __excepthook(type, value, trace)
sys.excepthook = thunk_hook sys.excepthook = thunk_hook
class Thunk:
def __init__(self):
self.results = None
self.is_valid = False
self.exc_info = ()
self.inputs = []
self.outputs = []
def call_thunk(self):
raise AbstractFunctionError
def exc_print(self, f = sys.stderr):
if self.is_valid:
return
type, value, trace = self.exc_info
for line in traceback.format_list(trace):
print>>f, line,
print>>f, traceback.format_exception_only(type, value)
def call_thunk_and_raise(self):
self.call_thunk()
if not self.is_valid:
type, value, trace = self.exc_info
raise self.type, self.value
def __call__(self, *inputs):
raise AbstractFunctionError
class Linker: class Linker:
...@@ -124,13 +101,22 @@ class PerformLinker(Linker): ...@@ -124,13 +101,22 @@ class PerformLinker(Linker):
order = env.toposort() order = env.toposort()
thunks = [op.perform for op in order] thunks = [op.perform for op in order]
def f(): def f():
for thunk in thunks: try:
thunk() for thunk, op in zip(thunks, order):
return f, env.inputs, env.outputs thunk()
except:
try:
trace = op.trace
except AttributeError:
trace = ()
exc_type, exc_value, exc_trace = sys.exc_info()
class X:pass
__x = X()
__x.__thunk_trace__ = trace
__x.__str__ = lambda: str(exc_value) + " (in op: " + str(op) + ")"
raise exc_type, __x, exc_trace
# self.thunk = f return f, env.inputs, env.outputs
# self.order = order
# self.thunks = thunks
class ProfilePerformLinker(Linker): class ProfilePerformLinker(Linker):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论