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