##// END OF EJS Templates
Handle string exceptions.
Gael Varoquaux -
Show More
@@ -1,80 +1,85 b''
1 1 # encoding: utf-8
2 2
3 3 """Object to manage sys.excepthook()."""
4 4
5 5 __docformat__ = "restructuredtext en"
6 6
7 7 #-------------------------------------------------------------------------------
8 8 # Copyright (C) 2008 The IPython Development Team
9 9 #
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #-------------------------------------------------------------------------------
13 13
14 14 #-------------------------------------------------------------------------------
15 15 # Imports
16 16 #-------------------------------------------------------------------------------
17 17 import sys
18 from traceback import format_list
18 19
19 20 class TracebackTrap(object):
20 21 """ Object to trap and format tracebacks.
21 22 """
22 23
23 24 def __init__(self, formatters=None):
24 25 # A list of formatters to apply.
25 26 if formatters is None:
26 27 formatters = []
27 28 self.formatters = formatters
28 29
29 30 # All of the traceback information provided to sys.excepthook().
30 31 self.args = None
31 32
32 33 # The previous hook before we replace it.
33 34 self.old_hook = None
34 35
35 36
36 37 def hook(self, *args):
37 38 """ This method actually implements the hook.
38 39 """
39 40 self.args = args
40 41
41 42 def set(self):
42 43 """ Set the hook.
43 44 """
44 45
45 46 if sys.excepthook is not self.hook:
46 47 self.old_hook = sys.excepthook
47 48 sys.excepthook = self.hook
48 49
49 50 def unset(self):
50 51 """ Unset the hook.
51 52 """
52 53
53 54 sys.excepthook = self.old_hook
54 55
55 56 def clear(self):
56 57 """ Remove the stored traceback.
57 58 """
58 59
59 60 self.args = None
60 61
61 62 def add_to_message(self, message):
62 63 """ Add the formatted display of the traceback to the message dictionary
63 64 being returned from the interpreter to its listeners.
64 65
65 66 Parameters
66 67 ----------
67 68 message : dict
68 69 """
69 70
70 71 # If there was no traceback, then don't add anything.
71 72 if self.args is None:
72 73 return
73 74
74 75 # Go through the list of formatters and let them add their formatting.
75 76 traceback = {}
76 for formatter in self.formatters:
77 traceback[formatter.identifier] = formatter(*self.args)
78
77 try:
78 for formatter in self.formatters:
79 traceback[formatter.identifier] = formatter(*self.args)
80 except:
81 # This works always, including with string exceptions.
82 traceback['fallback'] = repr(self.args)
83
79 84 message['traceback'] = traceback
80 85
General Comments 0
You need to be logged in to leave comments. Login now