Show More
The requested changes are too big and content was truncated. Show full diff
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
@@ -1,1489 +1,1491 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | Verbose and colourful traceback formatting. |
|
3 | Verbose and colourful traceback formatting. | |
4 |
|
4 | |||
5 | **ColorTB** |
|
5 | **ColorTB** | |
6 |
|
6 | |||
7 | I've always found it a bit hard to visually parse tracebacks in Python. The |
|
7 | I've always found it a bit hard to visually parse tracebacks in Python. The | |
8 | ColorTB class is a solution to that problem. It colors the different parts of a |
|
8 | ColorTB class is a solution to that problem. It colors the different parts of a | |
9 | traceback in a manner similar to what you would expect from a syntax-highlighting |
|
9 | traceback in a manner similar to what you would expect from a syntax-highlighting | |
10 | text editor. |
|
10 | text editor. | |
11 |
|
11 | |||
12 | Installation instructions for ColorTB:: |
|
12 | Installation instructions for ColorTB:: | |
13 |
|
13 | |||
14 | import sys,ultratb |
|
14 | import sys,ultratb | |
15 | sys.excepthook = ultratb.ColorTB() |
|
15 | sys.excepthook = ultratb.ColorTB() | |
16 |
|
16 | |||
17 | **VerboseTB** |
|
17 | **VerboseTB** | |
18 |
|
18 | |||
19 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds |
|
19 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds | |
20 | of useful info when a traceback occurs. Ping originally had it spit out HTML |
|
20 | of useful info when a traceback occurs. Ping originally had it spit out HTML | |
21 | and intended it for CGI programmers, but why should they have all the fun? I |
|
21 | and intended it for CGI programmers, but why should they have all the fun? I | |
22 | altered it to spit out colored text to the terminal. It's a bit overwhelming, |
|
22 | altered it to spit out colored text to the terminal. It's a bit overwhelming, | |
23 | but kind of neat, and maybe useful for long-running programs that you believe |
|
23 | but kind of neat, and maybe useful for long-running programs that you believe | |
24 | are bug-free. If a crash *does* occur in that type of program you want details. |
|
24 | are bug-free. If a crash *does* occur in that type of program you want details. | |
25 | Give it a shot--you'll love it or you'll hate it. |
|
25 | Give it a shot--you'll love it or you'll hate it. | |
26 |
|
26 | |||
27 | .. note:: |
|
27 | .. note:: | |
28 |
|
28 | |||
29 | The Verbose mode prints the variables currently visible where the exception |
|
29 | The Verbose mode prints the variables currently visible where the exception | |
30 | happened (shortening their strings if too long). This can potentially be |
|
30 | happened (shortening their strings if too long). This can potentially be | |
31 | very slow, if you happen to have a huge data structure whose string |
|
31 | very slow, if you happen to have a huge data structure whose string | |
32 | representation is complex to compute. Your computer may appear to freeze for |
|
32 | representation is complex to compute. Your computer may appear to freeze for | |
33 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback |
|
33 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback | |
34 | with Ctrl-C (maybe hitting it more than once). |
|
34 | with Ctrl-C (maybe hitting it more than once). | |
35 |
|
35 | |||
36 | If you encounter this kind of situation often, you may want to use the |
|
36 | If you encounter this kind of situation often, you may want to use the | |
37 | Verbose_novars mode instead of the regular Verbose, which avoids formatting |
|
37 | Verbose_novars mode instead of the regular Verbose, which avoids formatting | |
38 | variables (but otherwise includes the information and context given by |
|
38 | variables (but otherwise includes the information and context given by | |
39 | Verbose). |
|
39 | Verbose). | |
40 |
|
40 | |||
41 | .. note:: |
|
41 | .. note:: | |
42 |
|
42 | |||
43 | The verbose mode print all variables in the stack, which means it can |
|
43 | The verbose mode print all variables in the stack, which means it can | |
44 | potentially leak sensitive information like access keys, or unencryted |
|
44 | potentially leak sensitive information like access keys, or unencryted | |
45 | password. |
|
45 | password. | |
46 |
|
46 | |||
47 | Installation instructions for VerboseTB:: |
|
47 | Installation instructions for VerboseTB:: | |
48 |
|
48 | |||
49 | import sys,ultratb |
|
49 | import sys,ultratb | |
50 | sys.excepthook = ultratb.VerboseTB() |
|
50 | sys.excepthook = ultratb.VerboseTB() | |
51 |
|
51 | |||
52 | Note: Much of the code in this module was lifted verbatim from the standard |
|
52 | Note: Much of the code in this module was lifted verbatim from the standard | |
53 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. |
|
53 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. | |
54 |
|
54 | |||
55 | Color schemes |
|
55 | Color schemes | |
56 | ------------- |
|
56 | ------------- | |
57 |
|
57 | |||
58 | The colors are defined in the class TBTools through the use of the |
|
58 | The colors are defined in the class TBTools through the use of the | |
59 | ColorSchemeTable class. Currently the following exist: |
|
59 | ColorSchemeTable class. Currently the following exist: | |
60 |
|
60 | |||
61 | - NoColor: allows all of this module to be used in any terminal (the color |
|
61 | - NoColor: allows all of this module to be used in any terminal (the color | |
62 | escapes are just dummy blank strings). |
|
62 | escapes are just dummy blank strings). | |
63 |
|
63 | |||
64 | - Linux: is meant to look good in a terminal like the Linux console (black |
|
64 | - Linux: is meant to look good in a terminal like the Linux console (black | |
65 | or very dark background). |
|
65 | or very dark background). | |
66 |
|
66 | |||
67 | - LightBG: similar to Linux but swaps dark/light colors to be more readable |
|
67 | - LightBG: similar to Linux but swaps dark/light colors to be more readable | |
68 | in light background terminals. |
|
68 | in light background terminals. | |
69 |
|
69 | |||
70 | - Neutral: a neutral color scheme that should be readable on both light and |
|
70 | - Neutral: a neutral color scheme that should be readable on both light and | |
71 | dark background |
|
71 | dark background | |
72 |
|
72 | |||
73 | You can implement other color schemes easily, the syntax is fairly |
|
73 | You can implement other color schemes easily, the syntax is fairly | |
74 | self-explanatory. Please send back new schemes you develop to the author for |
|
74 | self-explanatory. Please send back new schemes you develop to the author for | |
75 | possible inclusion in future releases. |
|
75 | possible inclusion in future releases. | |
76 |
|
76 | |||
77 | Inheritance diagram: |
|
77 | Inheritance diagram: | |
78 |
|
78 | |||
79 | .. inheritance-diagram:: IPython.core.ultratb |
|
79 | .. inheritance-diagram:: IPython.core.ultratb | |
80 | :parts: 3 |
|
80 | :parts: 3 | |
81 | """ |
|
81 | """ | |
82 |
|
82 | |||
83 | #***************************************************************************** |
|
83 | #***************************************************************************** | |
84 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> |
|
84 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> | |
85 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> |
|
85 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> | |
86 | # |
|
86 | # | |
87 | # Distributed under the terms of the BSD License. The full license is in |
|
87 | # Distributed under the terms of the BSD License. The full license is in | |
88 | # the file COPYING, distributed as part of this software. |
|
88 | # the file COPYING, distributed as part of this software. | |
89 | #***************************************************************************** |
|
89 | #***************************************************************************** | |
90 |
|
90 | |||
91 | from __future__ import absolute_import |
|
91 | from __future__ import absolute_import | |
92 | from __future__ import unicode_literals |
|
92 | from __future__ import unicode_literals | |
93 | from __future__ import print_function |
|
93 | from __future__ import print_function | |
94 |
|
94 | |||
95 | import dis |
|
95 | import dis | |
96 | import inspect |
|
96 | import inspect | |
97 | import keyword |
|
97 | import keyword | |
98 | import linecache |
|
98 | import linecache | |
99 | import os |
|
99 | import os | |
100 | import pydoc |
|
100 | import pydoc | |
101 | import re |
|
101 | import re | |
102 | import sys |
|
102 | import sys | |
103 | import time |
|
103 | import time | |
104 | import tokenize |
|
104 | import tokenize | |
105 | import traceback |
|
105 | import traceback | |
106 | import types |
|
106 | import types | |
107 |
|
107 | |||
108 | try: # Python 2 |
|
108 | try: # Python 2 | |
109 | generate_tokens = tokenize.generate_tokens |
|
109 | generate_tokens = tokenize.generate_tokens | |
110 | except AttributeError: # Python 3 |
|
110 | except AttributeError: # Python 3 | |
111 | generate_tokens = tokenize.tokenize |
|
111 | generate_tokens = tokenize.tokenize | |
112 |
|
112 | |||
113 | # For purposes of monkeypatching inspect to fix a bug in it. |
|
113 | # For purposes of monkeypatching inspect to fix a bug in it. | |
114 | from inspect import getsourcefile, getfile, getmodule, \ |
|
114 | from inspect import getsourcefile, getfile, getmodule, \ | |
115 | ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode |
|
115 | ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode | |
116 |
|
116 | |||
117 | # IPython's own modules |
|
117 | # IPython's own modules | |
118 | from IPython import get_ipython |
|
118 | from IPython import get_ipython | |
119 | from IPython.core import debugger |
|
119 | from IPython.core import debugger | |
120 | from IPython.core.display_trap import DisplayTrap |
|
120 | from IPython.core.display_trap import DisplayTrap | |
121 | from IPython.core.excolors import exception_colors |
|
121 | from IPython.core.excolors import exception_colors | |
122 | from IPython.utils import PyColorize |
|
122 | from IPython.utils import PyColorize | |
123 | from IPython.utils import openpy |
|
123 | from IPython.utils import openpy | |
124 | from IPython.utils import path as util_path |
|
124 | from IPython.utils import path as util_path | |
125 | from IPython.utils import py3compat |
|
125 | from IPython.utils import py3compat | |
126 | from IPython.utils import ulinecache |
|
126 | from IPython.utils import ulinecache | |
127 | from IPython.utils.data import uniq_stable |
|
127 | from IPython.utils.data import uniq_stable | |
128 | from IPython.utils.terminal import get_terminal_size |
|
128 | from IPython.utils.terminal import get_terminal_size | |
129 | from logging import info, error |
|
129 | from logging import info, error | |
130 |
|
130 | |||
131 | import IPython.utils.colorable as colorable |
|
131 | import IPython.utils.colorable as colorable | |
132 |
|
132 | |||
133 | # Globals |
|
133 | # Globals | |
134 | # amount of space to put line numbers before verbose tracebacks |
|
134 | # amount of space to put line numbers before verbose tracebacks | |
135 | INDENT_SIZE = 8 |
|
135 | INDENT_SIZE = 8 | |
136 |
|
136 | |||
137 | # Default color scheme. This is used, for example, by the traceback |
|
137 | # Default color scheme. This is used, for example, by the traceback | |
138 | # formatter. When running in an actual IPython instance, the user's rc.colors |
|
138 | # formatter. When running in an actual IPython instance, the user's rc.colors | |
139 | # value is used, but having a module global makes this functionality available |
|
139 | # value is used, but having a module global makes this functionality available | |
140 | # to users of ultratb who are NOT running inside ipython. |
|
140 | # to users of ultratb who are NOT running inside ipython. | |
141 | DEFAULT_SCHEME = 'NoColor' |
|
141 | DEFAULT_SCHEME = 'NoColor' | |
142 |
|
142 | |||
143 | # --------------------------------------------------------------------------- |
|
143 | # --------------------------------------------------------------------------- | |
144 | # Code begins |
|
144 | # Code begins | |
145 |
|
145 | |||
146 | # Utility functions |
|
146 | # Utility functions | |
147 | def inspect_error(): |
|
147 | def inspect_error(): | |
148 | """Print a message about internal inspect errors. |
|
148 | """Print a message about internal inspect errors. | |
149 |
|
149 | |||
150 | These are unfortunately quite common.""" |
|
150 | These are unfortunately quite common.""" | |
151 |
|
151 | |||
152 | error('Internal Python error in the inspect module.\n' |
|
152 | error('Internal Python error in the inspect module.\n' | |
153 | 'Below is the traceback from this internal error.\n') |
|
153 | 'Below is the traceback from this internal error.\n') | |
154 |
|
154 | |||
155 |
|
155 | |||
156 | # This function is a monkeypatch we apply to the Python inspect module. We have |
|
156 | # This function is a monkeypatch we apply to the Python inspect module. We have | |
157 | # now found when it's needed (see discussion on issue gh-1456), and we have a |
|
157 | # now found when it's needed (see discussion on issue gh-1456), and we have a | |
158 | # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if |
|
158 | # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if | |
159 | # the monkeypatch is not applied. TK, Aug 2012. |
|
159 | # the monkeypatch is not applied. TK, Aug 2012. | |
160 | def findsource(object): |
|
160 | def findsource(object): | |
161 | """Return the entire source file and starting line number for an object. |
|
161 | """Return the entire source file and starting line number for an object. | |
162 |
|
162 | |||
163 | The argument may be a module, class, method, function, traceback, frame, |
|
163 | The argument may be a module, class, method, function, traceback, frame, | |
164 | or code object. The source code is returned as a list of all the lines |
|
164 | or code object. The source code is returned as a list of all the lines | |
165 | in the file and the line number indexes a line in that list. An IOError |
|
165 | in the file and the line number indexes a line in that list. An IOError | |
166 | is raised if the source code cannot be retrieved. |
|
166 | is raised if the source code cannot be retrieved. | |
167 |
|
167 | |||
168 | FIXED version with which we monkeypatch the stdlib to work around a bug.""" |
|
168 | FIXED version with which we monkeypatch the stdlib to work around a bug.""" | |
169 |
|
169 | |||
170 | file = getsourcefile(object) or getfile(object) |
|
170 | file = getsourcefile(object) or getfile(object) | |
171 | # If the object is a frame, then trying to get the globals dict from its |
|
171 | # If the object is a frame, then trying to get the globals dict from its | |
172 | # module won't work. Instead, the frame object itself has the globals |
|
172 | # module won't work. Instead, the frame object itself has the globals | |
173 | # dictionary. |
|
173 | # dictionary. | |
174 | globals_dict = None |
|
174 | globals_dict = None | |
175 | if inspect.isframe(object): |
|
175 | if inspect.isframe(object): | |
176 | # XXX: can this ever be false? |
|
176 | # XXX: can this ever be false? | |
177 | globals_dict = object.f_globals |
|
177 | globals_dict = object.f_globals | |
178 | else: |
|
178 | else: | |
179 | module = getmodule(object, file) |
|
179 | module = getmodule(object, file) | |
180 | if module: |
|
180 | if module: | |
181 | globals_dict = module.__dict__ |
|
181 | globals_dict = module.__dict__ | |
182 | lines = linecache.getlines(file, globals_dict) |
|
182 | lines = linecache.getlines(file, globals_dict) | |
183 | if not lines: |
|
183 | if not lines: | |
184 | raise IOError('could not get source code') |
|
184 | raise IOError('could not get source code') | |
185 |
|
185 | |||
186 | if ismodule(object): |
|
186 | if ismodule(object): | |
187 | return lines, 0 |
|
187 | return lines, 0 | |
188 |
|
188 | |||
189 | if isclass(object): |
|
189 | if isclass(object): | |
190 | name = object.__name__ |
|
190 | name = object.__name__ | |
191 | pat = re.compile(r'^(\s*)class\s*' + name + r'\b') |
|
191 | pat = re.compile(r'^(\s*)class\s*' + name + r'\b') | |
192 | # make some effort to find the best matching class definition: |
|
192 | # make some effort to find the best matching class definition: | |
193 | # use the one with the least indentation, which is the one |
|
193 | # use the one with the least indentation, which is the one | |
194 | # that's most probably not inside a function definition. |
|
194 | # that's most probably not inside a function definition. | |
195 | candidates = [] |
|
195 | candidates = [] | |
196 | for i in range(len(lines)): |
|
196 | for i in range(len(lines)): | |
197 | match = pat.match(lines[i]) |
|
197 | match = pat.match(lines[i]) | |
198 | if match: |
|
198 | if match: | |
199 | # if it's at toplevel, it's already the best one |
|
199 | # if it's at toplevel, it's already the best one | |
200 | if lines[i][0] == 'c': |
|
200 | if lines[i][0] == 'c': | |
201 | return lines, i |
|
201 | return lines, i | |
202 | # else add whitespace to candidate list |
|
202 | # else add whitespace to candidate list | |
203 | candidates.append((match.group(1), i)) |
|
203 | candidates.append((match.group(1), i)) | |
204 | if candidates: |
|
204 | if candidates: | |
205 | # this will sort by whitespace, and by line number, |
|
205 | # this will sort by whitespace, and by line number, | |
206 | # less whitespace first |
|
206 | # less whitespace first | |
207 | candidates.sort() |
|
207 | candidates.sort() | |
208 | return lines, candidates[0][1] |
|
208 | return lines, candidates[0][1] | |
209 | else: |
|
209 | else: | |
210 | raise IOError('could not find class definition') |
|
210 | raise IOError('could not find class definition') | |
211 |
|
211 | |||
212 | if ismethod(object): |
|
212 | if ismethod(object): | |
213 | object = object.__func__ |
|
213 | object = object.__func__ | |
214 | if isfunction(object): |
|
214 | if isfunction(object): | |
215 | object = object.__code__ |
|
215 | object = object.__code__ | |
216 | if istraceback(object): |
|
216 | if istraceback(object): | |
217 | object = object.tb_frame |
|
217 | object = object.tb_frame | |
218 | if isframe(object): |
|
218 | if isframe(object): | |
219 | object = object.f_code |
|
219 | object = object.f_code | |
220 | if iscode(object): |
|
220 | if iscode(object): | |
221 | if not hasattr(object, 'co_firstlineno'): |
|
221 | if not hasattr(object, 'co_firstlineno'): | |
222 | raise IOError('could not find function definition') |
|
222 | raise IOError('could not find function definition') | |
223 | pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') |
|
223 | pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') | |
224 | pmatch = pat.match |
|
224 | pmatch = pat.match | |
225 | # fperez - fix: sometimes, co_firstlineno can give a number larger than |
|
225 | # fperez - fix: sometimes, co_firstlineno can give a number larger than | |
226 | # the length of lines, which causes an error. Safeguard against that. |
|
226 | # the length of lines, which causes an error. Safeguard against that. | |
227 | lnum = min(object.co_firstlineno, len(lines)) - 1 |
|
227 | lnum = min(object.co_firstlineno, len(lines)) - 1 | |
228 | while lnum > 0: |
|
228 | while lnum > 0: | |
229 | if pmatch(lines[lnum]): |
|
229 | if pmatch(lines[lnum]): | |
230 | break |
|
230 | break | |
231 | lnum -= 1 |
|
231 | lnum -= 1 | |
232 |
|
232 | |||
233 | return lines, lnum |
|
233 | return lines, lnum | |
234 | raise IOError('could not find code object') |
|
234 | raise IOError('could not find code object') | |
235 |
|
235 | |||
236 |
|
236 | |||
237 | # This is a patched version of inspect.getargs that applies the (unmerged) |
|
237 | # This is a patched version of inspect.getargs that applies the (unmerged) | |
238 | # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes |
|
238 | # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes | |
239 | # https://github.com/ipython/ipython/issues/8205 and |
|
239 | # https://github.com/ipython/ipython/issues/8205 and | |
240 | # https://github.com/ipython/ipython/issues/8293 |
|
240 | # https://github.com/ipython/ipython/issues/8293 | |
241 | def getargs(co): |
|
241 | def getargs(co): | |
242 | """Get information about the arguments accepted by a code object. |
|
242 | """Get information about the arguments accepted by a code object. | |
243 |
|
243 | |||
244 | Three things are returned: (args, varargs, varkw), where 'args' is |
|
244 | Three things are returned: (args, varargs, varkw), where 'args' is | |
245 | a list of argument names (possibly containing nested lists), and |
|
245 | a list of argument names (possibly containing nested lists), and | |
246 | 'varargs' and 'varkw' are the names of the * and ** arguments or None.""" |
|
246 | 'varargs' and 'varkw' are the names of the * and ** arguments or None.""" | |
247 | if not iscode(co): |
|
247 | if not iscode(co): | |
248 | raise TypeError('{!r} is not a code object'.format(co)) |
|
248 | raise TypeError('{!r} is not a code object'.format(co)) | |
249 |
|
249 | |||
250 | nargs = co.co_argcount |
|
250 | nargs = co.co_argcount | |
251 | names = co.co_varnames |
|
251 | names = co.co_varnames | |
252 | args = list(names[:nargs]) |
|
252 | args = list(names[:nargs]) | |
253 | step = 0 |
|
253 | step = 0 | |
254 |
|
254 | |||
255 | # The following acrobatics are for anonymous (tuple) arguments. |
|
255 | # The following acrobatics are for anonymous (tuple) arguments. | |
256 | for i in range(nargs): |
|
256 | for i in range(nargs): | |
257 | if args[i][:1] in ('', '.'): |
|
257 | if args[i][:1] in ('', '.'): | |
258 | stack, remain, count = [], [], [] |
|
258 | stack, remain, count = [], [], [] | |
259 | while step < len(co.co_code): |
|
259 | while step < len(co.co_code): | |
260 | op = ord(co.co_code[step]) |
|
260 | op = ord(co.co_code[step]) | |
261 | step = step + 1 |
|
261 | step = step + 1 | |
262 | if op >= dis.HAVE_ARGUMENT: |
|
262 | if op >= dis.HAVE_ARGUMENT: | |
263 | opname = dis.opname[op] |
|
263 | opname = dis.opname[op] | |
264 | value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256 |
|
264 | value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256 | |
265 | step = step + 2 |
|
265 | step = step + 2 | |
266 | if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'): |
|
266 | if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'): | |
267 | remain.append(value) |
|
267 | remain.append(value) | |
268 | count.append(value) |
|
268 | count.append(value) | |
269 | elif opname in ('STORE_FAST', 'STORE_DEREF'): |
|
269 | elif opname in ('STORE_FAST', 'STORE_DEREF'): | |
270 | if op in dis.haslocal: |
|
270 | if op in dis.haslocal: | |
271 | stack.append(co.co_varnames[value]) |
|
271 | stack.append(co.co_varnames[value]) | |
272 | elif op in dis.hasfree: |
|
272 | elif op in dis.hasfree: | |
273 | stack.append((co.co_cellvars + co.co_freevars)[value]) |
|
273 | stack.append((co.co_cellvars + co.co_freevars)[value]) | |
274 | # Special case for sublists of length 1: def foo((bar)) |
|
274 | # Special case for sublists of length 1: def foo((bar)) | |
275 | # doesn't generate the UNPACK_TUPLE bytecode, so if |
|
275 | # doesn't generate the UNPACK_TUPLE bytecode, so if | |
276 | # `remain` is empty here, we have such a sublist. |
|
276 | # `remain` is empty here, we have such a sublist. | |
277 | if not remain: |
|
277 | if not remain: | |
278 | stack[0] = [stack[0]] |
|
278 | stack[0] = [stack[0]] | |
279 | break |
|
279 | break | |
280 | else: |
|
280 | else: | |
281 | remain[-1] = remain[-1] - 1 |
|
281 | remain[-1] = remain[-1] - 1 | |
282 | while remain[-1] == 0: |
|
282 | while remain[-1] == 0: | |
283 | remain.pop() |
|
283 | remain.pop() | |
284 | size = count.pop() |
|
284 | size = count.pop() | |
285 | stack[-size:] = [stack[-size:]] |
|
285 | stack[-size:] = [stack[-size:]] | |
286 | if not remain: |
|
286 | if not remain: | |
287 | break |
|
287 | break | |
288 | remain[-1] = remain[-1] - 1 |
|
288 | remain[-1] = remain[-1] - 1 | |
289 | if not remain: |
|
289 | if not remain: | |
290 | break |
|
290 | break | |
291 | args[i] = stack[0] |
|
291 | args[i] = stack[0] | |
292 |
|
292 | |||
293 | varargs = None |
|
293 | varargs = None | |
294 | if co.co_flags & inspect.CO_VARARGS: |
|
294 | if co.co_flags & inspect.CO_VARARGS: | |
295 | varargs = co.co_varnames[nargs] |
|
295 | varargs = co.co_varnames[nargs] | |
296 | nargs = nargs + 1 |
|
296 | nargs = nargs + 1 | |
297 | varkw = None |
|
297 | varkw = None | |
298 | if co.co_flags & inspect.CO_VARKEYWORDS: |
|
298 | if co.co_flags & inspect.CO_VARKEYWORDS: | |
299 | varkw = co.co_varnames[nargs] |
|
299 | varkw = co.co_varnames[nargs] | |
300 | return inspect.Arguments(args, varargs, varkw) |
|
300 | return inspect.Arguments(args, varargs, varkw) | |
301 |
|
301 | |||
302 |
|
302 | |||
303 | # Monkeypatch inspect to apply our bugfix. |
|
303 | # Monkeypatch inspect to apply our bugfix. | |
304 | def with_patch_inspect(f): |
|
304 | def with_patch_inspect(f): | |
305 | """decorator for monkeypatching inspect.findsource""" |
|
305 | """decorator for monkeypatching inspect.findsource""" | |
306 |
|
306 | |||
307 | def wrapped(*args, **kwargs): |
|
307 | def wrapped(*args, **kwargs): | |
308 | save_findsource = inspect.findsource |
|
308 | save_findsource = inspect.findsource | |
309 | save_getargs = inspect.getargs |
|
309 | save_getargs = inspect.getargs | |
310 | inspect.findsource = findsource |
|
310 | inspect.findsource = findsource | |
311 | inspect.getargs = getargs |
|
311 | inspect.getargs = getargs | |
312 | try: |
|
312 | try: | |
313 | return f(*args, **kwargs) |
|
313 | return f(*args, **kwargs) | |
314 | finally: |
|
314 | finally: | |
315 | inspect.findsource = save_findsource |
|
315 | inspect.findsource = save_findsource | |
316 | inspect.getargs = save_getargs |
|
316 | inspect.getargs = save_getargs | |
317 |
|
317 | |||
318 | return wrapped |
|
318 | return wrapped | |
319 |
|
319 | |||
320 |
|
320 | |||
321 | if py3compat.PY3: |
|
321 | if py3compat.PY3: | |
322 | fixed_getargvalues = inspect.getargvalues |
|
322 | fixed_getargvalues = inspect.getargvalues | |
323 | else: |
|
323 | else: | |
324 | # Fixes for https://github.com/ipython/ipython/issues/8293 |
|
324 | # Fixes for https://github.com/ipython/ipython/issues/8293 | |
325 | # and https://github.com/ipython/ipython/issues/8205. |
|
325 | # and https://github.com/ipython/ipython/issues/8205. | |
326 | # The relevant bug is caused by failure to correctly handle anonymous tuple |
|
326 | # The relevant bug is caused by failure to correctly handle anonymous tuple | |
327 | # unpacking, which only exists in Python 2. |
|
327 | # unpacking, which only exists in Python 2. | |
328 | fixed_getargvalues = with_patch_inspect(inspect.getargvalues) |
|
328 | fixed_getargvalues = with_patch_inspect(inspect.getargvalues) | |
329 |
|
329 | |||
330 |
|
330 | |||
331 | def fix_frame_records_filenames(records): |
|
331 | def fix_frame_records_filenames(records): | |
332 | """Try to fix the filenames in each record from inspect.getinnerframes(). |
|
332 | """Try to fix the filenames in each record from inspect.getinnerframes(). | |
333 |
|
333 | |||
334 | Particularly, modules loaded from within zip files have useless filenames |
|
334 | Particularly, modules loaded from within zip files have useless filenames | |
335 | attached to their code object, and inspect.getinnerframes() just uses it. |
|
335 | attached to their code object, and inspect.getinnerframes() just uses it. | |
336 | """ |
|
336 | """ | |
337 | fixed_records = [] |
|
337 | fixed_records = [] | |
338 | for frame, filename, line_no, func_name, lines, index in records: |
|
338 | for frame, filename, line_no, func_name, lines, index in records: | |
339 | # Look inside the frame's globals dictionary for __file__, |
|
339 | # Look inside the frame's globals dictionary for __file__, | |
340 | # which should be better. However, keep Cython filenames since |
|
340 | # which should be better. However, keep Cython filenames since | |
341 | # we prefer the source filenames over the compiled .so file. |
|
341 | # we prefer the source filenames over the compiled .so file. | |
342 | filename = py3compat.cast_unicode_py2(filename, "utf-8") |
|
342 | filename = py3compat.cast_unicode_py2(filename, "utf-8") | |
343 | if not filename.endswith(('.pyx', '.pxd', '.pxi')): |
|
343 | if not filename.endswith(('.pyx', '.pxd', '.pxi')): | |
344 | better_fn = frame.f_globals.get('__file__', None) |
|
344 | better_fn = frame.f_globals.get('__file__', None) | |
345 | if isinstance(better_fn, str): |
|
345 | if isinstance(better_fn, str): | |
346 | # Check the type just in case someone did something weird with |
|
346 | # Check the type just in case someone did something weird with | |
347 | # __file__. It might also be None if the error occurred during |
|
347 | # __file__. It might also be None if the error occurred during | |
348 | # import. |
|
348 | # import. | |
349 | filename = better_fn |
|
349 | filename = better_fn | |
350 | fixed_records.append((frame, filename, line_no, func_name, lines, index)) |
|
350 | fixed_records.append((frame, filename, line_no, func_name, lines, index)) | |
351 | return fixed_records |
|
351 | return fixed_records | |
352 |
|
352 | |||
353 |
|
353 | |||
354 | @with_patch_inspect |
|
354 | @with_patch_inspect | |
355 | def _fixed_getinnerframes(etb, context=1, tb_offset=0): |
|
355 | def _fixed_getinnerframes(etb, context=1, tb_offset=0): | |
356 | LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 |
|
356 | LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 | |
357 |
|
357 | |||
358 | records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) |
|
358 | records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) | |
359 | # If the error is at the console, don't build any context, since it would |
|
359 | # If the error is at the console, don't build any context, since it would | |
360 | # otherwise produce 5 blank lines printed out (there is no file at the |
|
360 | # otherwise produce 5 blank lines printed out (there is no file at the | |
361 | # console) |
|
361 | # console) | |
362 | rec_check = records[tb_offset:] |
|
362 | rec_check = records[tb_offset:] | |
363 | try: |
|
363 | try: | |
364 | rname = rec_check[0][1] |
|
364 | rname = rec_check[0][1] | |
365 | if rname == '<ipython console>' or rname.endswith('<string>'): |
|
365 | if rname == '<ipython console>' or rname.endswith('<string>'): | |
366 | return rec_check |
|
366 | return rec_check | |
367 | except IndexError: |
|
367 | except IndexError: | |
368 | pass |
|
368 | pass | |
369 |
|
369 | |||
370 | aux = traceback.extract_tb(etb) |
|
370 | aux = traceback.extract_tb(etb) | |
371 | assert len(records) == len(aux) |
|
371 | assert len(records) == len(aux) | |
372 | for i, (file, lnum, _, _) in zip(range(len(records)), aux): |
|
372 | for i, (file, lnum, _, _) in zip(range(len(records)), aux): | |
373 | maybeStart = lnum - 1 - context // 2 |
|
373 | maybeStart = lnum - 1 - context // 2 | |
374 | start = max(maybeStart, 0) |
|
374 | start = max(maybeStart, 0) | |
375 | end = start + context |
|
375 | end = start + context | |
376 | lines = ulinecache.getlines(file)[start:end] |
|
376 | lines = ulinecache.getlines(file)[start:end] | |
377 | buf = list(records[i]) |
|
377 | buf = list(records[i]) | |
378 | buf[LNUM_POS] = lnum |
|
378 | buf[LNUM_POS] = lnum | |
379 | buf[INDEX_POS] = lnum - 1 - start |
|
379 | buf[INDEX_POS] = lnum - 1 - start | |
380 | buf[LINES_POS] = lines |
|
380 | buf[LINES_POS] = lines | |
381 | records[i] = tuple(buf) |
|
381 | records[i] = tuple(buf) | |
382 | return records[tb_offset:] |
|
382 | return records[tb_offset:] | |
383 |
|
383 | |||
384 | # Helper function -- largely belongs to VerboseTB, but we need the same |
|
384 | # Helper function -- largely belongs to VerboseTB, but we need the same | |
385 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they |
|
385 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they | |
386 | # can be recognized properly by ipython.el's py-traceback-line-re |
|
386 | # can be recognized properly by ipython.el's py-traceback-line-re | |
387 | # (SyntaxErrors have to be treated specially because they have no traceback) |
|
387 | # (SyntaxErrors have to be treated specially because they have no traceback) | |
388 |
|
388 | |||
389 |
|
389 | |||
390 |
def _format_traceback_lines(lnum, index, lines, Colors, lvals=None, |
|
390 | def _format_traceback_lines(lnum, index, lines, Colors, lvals=None, _line_format=(lambda x,_:x,None)): | |
391 | numbers_width = INDENT_SIZE - 1 |
|
391 | numbers_width = INDENT_SIZE - 1 | |
392 | res = [] |
|
392 | res = [] | |
393 | i = lnum - index |
|
393 | i = lnum - index | |
394 |
|
394 | |||
395 | _line_format = PyColorize.Parser(style=scheme).format2 |
|
|||
396 |
|
||||
397 | for line in lines: |
|
395 | for line in lines: | |
398 | line = py3compat.cast_unicode(line) |
|
396 | line = py3compat.cast_unicode(line) | |
399 |
|
397 | |||
400 | new_line, err = _line_format(line, 'str') |
|
398 | new_line, err = _line_format(line, 'str') | |
401 | if not err: line = new_line |
|
399 | if not err: line = new_line | |
402 |
|
400 | |||
403 | if i == lnum: |
|
401 | if i == lnum: | |
404 | # This is the line with the error |
|
402 | # This is the line with the error | |
405 | pad = numbers_width - len(str(i)) |
|
403 | pad = numbers_width - len(str(i)) | |
406 | num = '%s%s' % (debugger.make_arrow(pad), str(lnum)) |
|
404 | num = '%s%s' % (debugger.make_arrow(pad), str(lnum)) | |
407 | line = '%s%s%s %s%s' % (Colors.linenoEm, num, |
|
405 | line = '%s%s%s %s%s' % (Colors.linenoEm, num, | |
408 | Colors.line, line, Colors.Normal) |
|
406 | Colors.line, line, Colors.Normal) | |
409 | else: |
|
407 | else: | |
410 | num = '%*s' % (numbers_width, i) |
|
408 | num = '%*s' % (numbers_width, i) | |
411 | line = '%s%s%s %s' % (Colors.lineno, num, |
|
409 | line = '%s%s%s %s' % (Colors.lineno, num, | |
412 | Colors.Normal, line) |
|
410 | Colors.Normal, line) | |
413 |
|
411 | |||
414 | res.append(line) |
|
412 | res.append(line) | |
415 | if lvals and i == lnum: |
|
413 | if lvals and i == lnum: | |
416 | res.append(lvals + '\n') |
|
414 | res.append(lvals + '\n') | |
417 | i = i + 1 |
|
415 | i = i + 1 | |
418 | return res |
|
416 | return res | |
419 |
|
417 | |||
420 | def is_recursion_error(etype, value, records): |
|
418 | def is_recursion_error(etype, value, records): | |
421 | try: |
|
419 | try: | |
422 | # RecursionError is new in Python 3.5 |
|
420 | # RecursionError is new in Python 3.5 | |
423 | recursion_error_type = RecursionError |
|
421 | recursion_error_type = RecursionError | |
424 | except NameError: |
|
422 | except NameError: | |
425 | recursion_error_type = RuntimeError |
|
423 | recursion_error_type = RuntimeError | |
426 |
|
424 | |||
427 | # The default recursion limit is 1000, but some of that will be taken up |
|
425 | # The default recursion limit is 1000, but some of that will be taken up | |
428 | # by stack frames in IPython itself. >500 frames probably indicates |
|
426 | # by stack frames in IPython itself. >500 frames probably indicates | |
429 | # a recursion error. |
|
427 | # a recursion error. | |
430 | return (etype is recursion_error_type) \ |
|
428 | return (etype is recursion_error_type) \ | |
431 | and "recursion" in str(value).lower() \ |
|
429 | and "recursion" in str(value).lower() \ | |
432 | and len(records) > 500 |
|
430 | and len(records) > 500 | |
433 |
|
431 | |||
434 | def find_recursion(etype, value, records): |
|
432 | def find_recursion(etype, value, records): | |
435 | """Identify the repeating stack frames from a RecursionError traceback |
|
433 | """Identify the repeating stack frames from a RecursionError traceback | |
436 |
|
434 | |||
437 | 'records' is a list as returned by VerboseTB.get_records() |
|
435 | 'records' is a list as returned by VerboseTB.get_records() | |
438 |
|
436 | |||
439 | Returns (last_unique, repeat_length) |
|
437 | Returns (last_unique, repeat_length) | |
440 | """ |
|
438 | """ | |
441 | # This involves a bit of guesswork - we want to show enough of the traceback |
|
439 | # This involves a bit of guesswork - we want to show enough of the traceback | |
442 | # to indicate where the recursion is occurring. We guess that the innermost |
|
440 | # to indicate where the recursion is occurring. We guess that the innermost | |
443 | # quarter of the traceback (250 frames by default) is repeats, and find the |
|
441 | # quarter of the traceback (250 frames by default) is repeats, and find the | |
444 | # first frame (from in to out) that looks different. |
|
442 | # first frame (from in to out) that looks different. | |
445 | if not is_recursion_error(etype, value, records): |
|
443 | if not is_recursion_error(etype, value, records): | |
446 | return len(records), 0 |
|
444 | return len(records), 0 | |
447 |
|
445 | |||
448 | # Select filename, lineno, func_name to track frames with |
|
446 | # Select filename, lineno, func_name to track frames with | |
449 | records = [r[1:4] for r in records] |
|
447 | records = [r[1:4] for r in records] | |
450 | inner_frames = records[-(len(records)//4):] |
|
448 | inner_frames = records[-(len(records)//4):] | |
451 | frames_repeated = set(inner_frames) |
|
449 | frames_repeated = set(inner_frames) | |
452 |
|
450 | |||
453 | last_seen_at = {} |
|
451 | last_seen_at = {} | |
454 | longest_repeat = 0 |
|
452 | longest_repeat = 0 | |
455 | i = len(records) |
|
453 | i = len(records) | |
456 | for frame in reversed(records): |
|
454 | for frame in reversed(records): | |
457 | i -= 1 |
|
455 | i -= 1 | |
458 | if frame not in frames_repeated: |
|
456 | if frame not in frames_repeated: | |
459 | last_unique = i |
|
457 | last_unique = i | |
460 | break |
|
458 | break | |
461 |
|
459 | |||
462 | if frame in last_seen_at: |
|
460 | if frame in last_seen_at: | |
463 | distance = last_seen_at[frame] - i |
|
461 | distance = last_seen_at[frame] - i | |
464 | longest_repeat = max(longest_repeat, distance) |
|
462 | longest_repeat = max(longest_repeat, distance) | |
465 |
|
463 | |||
466 | last_seen_at[frame] = i |
|
464 | last_seen_at[frame] = i | |
467 | else: |
|
465 | else: | |
468 | last_unique = 0 # The whole traceback was recursion |
|
466 | last_unique = 0 # The whole traceback was recursion | |
469 |
|
467 | |||
470 | return last_unique, longest_repeat |
|
468 | return last_unique, longest_repeat | |
471 |
|
469 | |||
472 | #--------------------------------------------------------------------------- |
|
470 | #--------------------------------------------------------------------------- | |
473 | # Module classes |
|
471 | # Module classes | |
474 | class TBTools(colorable.Colorable): |
|
472 | class TBTools(colorable.Colorable): | |
475 | """Basic tools used by all traceback printer classes.""" |
|
473 | """Basic tools used by all traceback printer classes.""" | |
476 |
|
474 | |||
477 | # Number of frames to skip when reporting tracebacks |
|
475 | # Number of frames to skip when reporting tracebacks | |
478 | tb_offset = 0 |
|
476 | tb_offset = 0 | |
479 |
|
477 | |||
480 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None): |
|
478 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None): | |
481 | # Whether to call the interactive pdb debugger after printing |
|
479 | # Whether to call the interactive pdb debugger after printing | |
482 | # tracebacks or not |
|
480 | # tracebacks or not | |
483 | super(TBTools, self).__init__(parent=parent, config=config) |
|
481 | super(TBTools, self).__init__(parent=parent, config=config) | |
484 | self.call_pdb = call_pdb |
|
482 | self.call_pdb = call_pdb | |
485 |
|
483 | |||
486 | # Output stream to write to. Note that we store the original value in |
|
484 | # Output stream to write to. Note that we store the original value in | |
487 | # a private attribute and then make the public ostream a property, so |
|
485 | # a private attribute and then make the public ostream a property, so | |
488 | # that we can delay accessing sys.stdout until runtime. The way |
|
486 | # that we can delay accessing sys.stdout until runtime. The way | |
489 | # things are written now, the sys.stdout object is dynamically managed |
|
487 | # things are written now, the sys.stdout object is dynamically managed | |
490 | # so a reference to it should NEVER be stored statically. This |
|
488 | # so a reference to it should NEVER be stored statically. This | |
491 | # property approach confines this detail to a single location, and all |
|
489 | # property approach confines this detail to a single location, and all | |
492 | # subclasses can simply access self.ostream for writing. |
|
490 | # subclasses can simply access self.ostream for writing. | |
493 | self._ostream = ostream |
|
491 | self._ostream = ostream | |
494 |
|
492 | |||
495 | # Create color table |
|
493 | # Create color table | |
496 | self.color_scheme_table = exception_colors() |
|
494 | self.color_scheme_table = exception_colors() | |
497 |
|
495 | |||
498 | self.set_colors(color_scheme) |
|
496 | self.set_colors(color_scheme) | |
499 | self.old_scheme = color_scheme # save initial value for toggles |
|
497 | self.old_scheme = color_scheme # save initial value for toggles | |
500 |
|
498 | |||
501 | if call_pdb: |
|
499 | if call_pdb: | |
502 | self.pdb = debugger.Pdb() |
|
500 | self.pdb = debugger.Pdb() | |
503 | else: |
|
501 | else: | |
504 | self.pdb = None |
|
502 | self.pdb = None | |
505 |
|
503 | |||
506 | def _get_ostream(self): |
|
504 | def _get_ostream(self): | |
507 | """Output stream that exceptions are written to. |
|
505 | """Output stream that exceptions are written to. | |
508 |
|
506 | |||
509 | Valid values are: |
|
507 | Valid values are: | |
510 |
|
508 | |||
511 | - None: the default, which means that IPython will dynamically resolve |
|
509 | - None: the default, which means that IPython will dynamically resolve | |
512 | to sys.stdout. This ensures compatibility with most tools, including |
|
510 | to sys.stdout. This ensures compatibility with most tools, including | |
513 | Windows (where plain stdout doesn't recognize ANSI escapes). |
|
511 | Windows (where plain stdout doesn't recognize ANSI escapes). | |
514 |
|
512 | |||
515 | - Any object with 'write' and 'flush' attributes. |
|
513 | - Any object with 'write' and 'flush' attributes. | |
516 | """ |
|
514 | """ | |
517 | return sys.stdout if self._ostream is None else self._ostream |
|
515 | return sys.stdout if self._ostream is None else self._ostream | |
518 |
|
516 | |||
519 | def _set_ostream(self, val): |
|
517 | def _set_ostream(self, val): | |
520 | assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush')) |
|
518 | assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush')) | |
521 | self._ostream = val |
|
519 | self._ostream = val | |
522 |
|
520 | |||
523 | ostream = property(_get_ostream, _set_ostream) |
|
521 | ostream = property(_get_ostream, _set_ostream) | |
524 |
|
522 | |||
525 | def set_colors(self, *args, **kw): |
|
523 | def set_colors(self, *args, **kw): | |
526 | """Shorthand access to the color table scheme selector method.""" |
|
524 | """Shorthand access to the color table scheme selector method.""" | |
527 |
|
525 | |||
528 | # Set own color table |
|
526 | # Set own color table | |
529 | self.color_scheme_table.set_active_scheme(*args, **kw) |
|
527 | self.color_scheme_table.set_active_scheme(*args, **kw) | |
530 | # for convenience, set Colors to the active scheme |
|
528 | # for convenience, set Colors to the active scheme | |
531 | self.Colors = self.color_scheme_table.active_colors |
|
529 | self.Colors = self.color_scheme_table.active_colors | |
532 | # Also set colors of debugger |
|
530 | # Also set colors of debugger | |
533 | if hasattr(self, 'pdb') and self.pdb is not None: |
|
531 | if hasattr(self, 'pdb') and self.pdb is not None: | |
534 | self.pdb.set_colors(*args, **kw) |
|
532 | self.pdb.set_colors(*args, **kw) | |
535 |
|
533 | |||
536 | def color_toggle(self): |
|
534 | def color_toggle(self): | |
537 | """Toggle between the currently active color scheme and NoColor.""" |
|
535 | """Toggle between the currently active color scheme and NoColor.""" | |
538 |
|
536 | |||
539 | if self.color_scheme_table.active_scheme_name == 'NoColor': |
|
537 | if self.color_scheme_table.active_scheme_name == 'NoColor': | |
540 | self.color_scheme_table.set_active_scheme(self.old_scheme) |
|
538 | self.color_scheme_table.set_active_scheme(self.old_scheme) | |
541 | self.Colors = self.color_scheme_table.active_colors |
|
539 | self.Colors = self.color_scheme_table.active_colors | |
542 | else: |
|
540 | else: | |
543 | self.old_scheme = self.color_scheme_table.active_scheme_name |
|
541 | self.old_scheme = self.color_scheme_table.active_scheme_name | |
544 | self.color_scheme_table.set_active_scheme('NoColor') |
|
542 | self.color_scheme_table.set_active_scheme('NoColor') | |
545 | self.Colors = self.color_scheme_table.active_colors |
|
543 | self.Colors = self.color_scheme_table.active_colors | |
546 |
|
544 | |||
547 | def stb2text(self, stb): |
|
545 | def stb2text(self, stb): | |
548 | """Convert a structured traceback (a list) to a string.""" |
|
546 | """Convert a structured traceback (a list) to a string.""" | |
549 | return '\n'.join(stb) |
|
547 | return '\n'.join(stb) | |
550 |
|
548 | |||
551 | def text(self, etype, value, tb, tb_offset=None, context=5): |
|
549 | def text(self, etype, value, tb, tb_offset=None, context=5): | |
552 | """Return formatted traceback. |
|
550 | """Return formatted traceback. | |
553 |
|
551 | |||
554 | Subclasses may override this if they add extra arguments. |
|
552 | Subclasses may override this if they add extra arguments. | |
555 | """ |
|
553 | """ | |
556 | tb_list = self.structured_traceback(etype, value, tb, |
|
554 | tb_list = self.structured_traceback(etype, value, tb, | |
557 | tb_offset, context) |
|
555 | tb_offset, context) | |
558 | return self.stb2text(tb_list) |
|
556 | return self.stb2text(tb_list) | |
559 |
|
557 | |||
560 | def structured_traceback(self, etype, evalue, tb, tb_offset=None, |
|
558 | def structured_traceback(self, etype, evalue, tb, tb_offset=None, | |
561 | context=5, mode=None): |
|
559 | context=5, mode=None): | |
562 | """Return a list of traceback frames. |
|
560 | """Return a list of traceback frames. | |
563 |
|
561 | |||
564 | Must be implemented by each class. |
|
562 | Must be implemented by each class. | |
565 | """ |
|
563 | """ | |
566 | raise NotImplementedError() |
|
564 | raise NotImplementedError() | |
567 |
|
565 | |||
568 |
|
566 | |||
569 | #--------------------------------------------------------------------------- |
|
567 | #--------------------------------------------------------------------------- | |
570 | class ListTB(TBTools): |
|
568 | class ListTB(TBTools): | |
571 | """Print traceback information from a traceback list, with optional color. |
|
569 | """Print traceback information from a traceback list, with optional color. | |
572 |
|
570 | |||
573 | Calling requires 3 arguments: (etype, evalue, elist) |
|
571 | Calling requires 3 arguments: (etype, evalue, elist) | |
574 | as would be obtained by:: |
|
572 | as would be obtained by:: | |
575 |
|
573 | |||
576 | etype, evalue, tb = sys.exc_info() |
|
574 | etype, evalue, tb = sys.exc_info() | |
577 | if tb: |
|
575 | if tb: | |
578 | elist = traceback.extract_tb(tb) |
|
576 | elist = traceback.extract_tb(tb) | |
579 | else: |
|
577 | else: | |
580 | elist = None |
|
578 | elist = None | |
581 |
|
579 | |||
582 | It can thus be used by programs which need to process the traceback before |
|
580 | It can thus be used by programs which need to process the traceback before | |
583 | printing (such as console replacements based on the code module from the |
|
581 | printing (such as console replacements based on the code module from the | |
584 | standard library). |
|
582 | standard library). | |
585 |
|
583 | |||
586 | Because they are meant to be called without a full traceback (only a |
|
584 | Because they are meant to be called without a full traceback (only a | |
587 | list), instances of this class can't call the interactive pdb debugger.""" |
|
585 | list), instances of this class can't call the interactive pdb debugger.""" | |
588 |
|
586 | |||
589 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None): |
|
587 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None): | |
590 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, |
|
588 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, | |
591 | ostream=ostream, parent=parent) |
|
589 | ostream=ostream, parent=parent,config=config) | |
592 |
|
590 | |||
593 | def __call__(self, etype, value, elist): |
|
591 | def __call__(self, etype, value, elist): | |
594 | self.ostream.flush() |
|
592 | self.ostream.flush() | |
595 | self.ostream.write(self.text(etype, value, elist)) |
|
593 | self.ostream.write(self.text(etype, value, elist)) | |
596 | self.ostream.write('\n') |
|
594 | self.ostream.write('\n') | |
597 |
|
595 | |||
598 | def structured_traceback(self, etype, value, elist, tb_offset=None, |
|
596 | def structured_traceback(self, etype, value, elist, tb_offset=None, | |
599 | context=5): |
|
597 | context=5): | |
600 | """Return a color formatted string with the traceback info. |
|
598 | """Return a color formatted string with the traceback info. | |
601 |
|
599 | |||
602 | Parameters |
|
600 | Parameters | |
603 | ---------- |
|
601 | ---------- | |
604 | etype : exception type |
|
602 | etype : exception type | |
605 | Type of the exception raised. |
|
603 | Type of the exception raised. | |
606 |
|
604 | |||
607 | value : object |
|
605 | value : object | |
608 | Data stored in the exception |
|
606 | Data stored in the exception | |
609 |
|
607 | |||
610 | elist : list |
|
608 | elist : list | |
611 | List of frames, see class docstring for details. |
|
609 | List of frames, see class docstring for details. | |
612 |
|
610 | |||
613 | tb_offset : int, optional |
|
611 | tb_offset : int, optional | |
614 | Number of frames in the traceback to skip. If not given, the |
|
612 | Number of frames in the traceback to skip. If not given, the | |
615 | instance value is used (set in constructor). |
|
613 | instance value is used (set in constructor). | |
616 |
|
614 | |||
617 | context : int, optional |
|
615 | context : int, optional | |
618 | Number of lines of context information to print. |
|
616 | Number of lines of context information to print. | |
619 |
|
617 | |||
620 | Returns |
|
618 | Returns | |
621 | ------- |
|
619 | ------- | |
622 | String with formatted exception. |
|
620 | String with formatted exception. | |
623 | """ |
|
621 | """ | |
624 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
622 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
625 | Colors = self.Colors |
|
623 | Colors = self.Colors | |
626 | out_list = [] |
|
624 | out_list = [] | |
627 | if elist: |
|
625 | if elist: | |
628 |
|
626 | |||
629 | if tb_offset and len(elist) > tb_offset: |
|
627 | if tb_offset and len(elist) > tb_offset: | |
630 | elist = elist[tb_offset:] |
|
628 | elist = elist[tb_offset:] | |
631 |
|
629 | |||
632 | out_list.append('Traceback %s(most recent call last)%s:' % |
|
630 | out_list.append('Traceback %s(most recent call last)%s:' % | |
633 | (Colors.normalEm, Colors.Normal) + '\n') |
|
631 | (Colors.normalEm, Colors.Normal) + '\n') | |
634 | out_list.extend(self._format_list(elist)) |
|
632 | out_list.extend(self._format_list(elist)) | |
635 | # The exception info should be a single entry in the list. |
|
633 | # The exception info should be a single entry in the list. | |
636 | lines = ''.join(self._format_exception_only(etype, value)) |
|
634 | lines = ''.join(self._format_exception_only(etype, value)) | |
637 | out_list.append(lines) |
|
635 | out_list.append(lines) | |
638 |
|
636 | |||
639 | # Note: this code originally read: |
|
637 | # Note: this code originally read: | |
640 |
|
638 | |||
641 | ## for line in lines[:-1]: |
|
639 | ## for line in lines[:-1]: | |
642 | ## out_list.append(" "+line) |
|
640 | ## out_list.append(" "+line) | |
643 | ## out_list.append(lines[-1]) |
|
641 | ## out_list.append(lines[-1]) | |
644 |
|
642 | |||
645 | # This means it was indenting everything but the last line by a little |
|
643 | # This means it was indenting everything but the last line by a little | |
646 | # bit. I've disabled this for now, but if we see ugliness somewhere we |
|
644 | # bit. I've disabled this for now, but if we see ugliness somewhere we | |
647 | # can restore it. |
|
645 | # can restore it. | |
648 |
|
646 | |||
649 | return out_list |
|
647 | return out_list | |
650 |
|
648 | |||
651 | def _format_list(self, extracted_list): |
|
649 | def _format_list(self, extracted_list): | |
652 | """Format a list of traceback entry tuples for printing. |
|
650 | """Format a list of traceback entry tuples for printing. | |
653 |
|
651 | |||
654 | Given a list of tuples as returned by extract_tb() or |
|
652 | Given a list of tuples as returned by extract_tb() or | |
655 | extract_stack(), return a list of strings ready for printing. |
|
653 | extract_stack(), return a list of strings ready for printing. | |
656 | Each string in the resulting list corresponds to the item with the |
|
654 | Each string in the resulting list corresponds to the item with the | |
657 | same index in the argument list. Each string ends in a newline; |
|
655 | same index in the argument list. Each string ends in a newline; | |
658 | the strings may contain internal newlines as well, for those items |
|
656 | the strings may contain internal newlines as well, for those items | |
659 | whose source text line is not None. |
|
657 | whose source text line is not None. | |
660 |
|
658 | |||
661 | Lifted almost verbatim from traceback.py |
|
659 | Lifted almost verbatim from traceback.py | |
662 | """ |
|
660 | """ | |
663 |
|
661 | |||
664 | Colors = self.Colors |
|
662 | Colors = self.Colors | |
665 | list = [] |
|
663 | list = [] | |
666 | for filename, lineno, name, line in extracted_list[:-1]: |
|
664 | for filename, lineno, name, line in extracted_list[:-1]: | |
667 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ |
|
665 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ | |
668 | (Colors.filename, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.Normal, |
|
666 | (Colors.filename, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.Normal, | |
669 | Colors.lineno, lineno, Colors.Normal, |
|
667 | Colors.lineno, lineno, Colors.Normal, | |
670 | Colors.name, py3compat.cast_unicode_py2(name, "utf-8"), Colors.Normal) |
|
668 | Colors.name, py3compat.cast_unicode_py2(name, "utf-8"), Colors.Normal) | |
671 | if line: |
|
669 | if line: | |
672 | item += ' %s\n' % line.strip() |
|
670 | item += ' %s\n' % line.strip() | |
673 | list.append(item) |
|
671 | list.append(item) | |
674 | # Emphasize the last entry |
|
672 | # Emphasize the last entry | |
675 | filename, lineno, name, line = extracted_list[-1] |
|
673 | filename, lineno, name, line = extracted_list[-1] | |
676 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ |
|
674 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ | |
677 | (Colors.normalEm, |
|
675 | (Colors.normalEm, | |
678 | Colors.filenameEm, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.normalEm, |
|
676 | Colors.filenameEm, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.normalEm, | |
679 | Colors.linenoEm, lineno, Colors.normalEm, |
|
677 | Colors.linenoEm, lineno, Colors.normalEm, | |
680 | Colors.nameEm, py3compat.cast_unicode_py2(name, "utf-8"), Colors.normalEm, |
|
678 | Colors.nameEm, py3compat.cast_unicode_py2(name, "utf-8"), Colors.normalEm, | |
681 | Colors.Normal) |
|
679 | Colors.Normal) | |
682 | if line: |
|
680 | if line: | |
683 | item += '%s %s%s\n' % (Colors.line, line.strip(), |
|
681 | item += '%s %s%s\n' % (Colors.line, line.strip(), | |
684 | Colors.Normal) |
|
682 | Colors.Normal) | |
685 | list.append(item) |
|
683 | list.append(item) | |
686 | return list |
|
684 | return list | |
687 |
|
685 | |||
688 | def _format_exception_only(self, etype, value): |
|
686 | def _format_exception_only(self, etype, value): | |
689 | """Format the exception part of a traceback. |
|
687 | """Format the exception part of a traceback. | |
690 |
|
688 | |||
691 | The arguments are the exception type and value such as given by |
|
689 | The arguments are the exception type and value such as given by | |
692 | sys.exc_info()[:2]. The return value is a list of strings, each ending |
|
690 | sys.exc_info()[:2]. The return value is a list of strings, each ending | |
693 | in a newline. Normally, the list contains a single string; however, |
|
691 | in a newline. Normally, the list contains a single string; however, | |
694 | for SyntaxError exceptions, it contains several lines that (when |
|
692 | for SyntaxError exceptions, it contains several lines that (when | |
695 | printed) display detailed information about where the syntax error |
|
693 | printed) display detailed information about where the syntax error | |
696 | occurred. The message indicating which exception occurred is the |
|
694 | occurred. The message indicating which exception occurred is the | |
697 | always last string in the list. |
|
695 | always last string in the list. | |
698 |
|
696 | |||
699 | Also lifted nearly verbatim from traceback.py |
|
697 | Also lifted nearly verbatim from traceback.py | |
700 | """ |
|
698 | """ | |
701 | have_filedata = False |
|
699 | have_filedata = False | |
702 | Colors = self.Colors |
|
700 | Colors = self.Colors | |
703 | list = [] |
|
701 | list = [] | |
704 | stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal) |
|
702 | stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal) | |
705 | if value is None: |
|
703 | if value is None: | |
706 | # Not sure if this can still happen in Python 2.6 and above |
|
704 | # Not sure if this can still happen in Python 2.6 and above | |
707 | list.append(stype + '\n') |
|
705 | list.append(stype + '\n') | |
708 | else: |
|
706 | else: | |
709 | if issubclass(etype, SyntaxError): |
|
707 | if issubclass(etype, SyntaxError): | |
710 | have_filedata = True |
|
708 | have_filedata = True | |
711 | if not value.filename: value.filename = "<string>" |
|
709 | if not value.filename: value.filename = "<string>" | |
712 | if value.lineno: |
|
710 | if value.lineno: | |
713 | lineno = value.lineno |
|
711 | lineno = value.lineno | |
714 | textline = ulinecache.getline(value.filename, value.lineno) |
|
712 | textline = ulinecache.getline(value.filename, value.lineno) | |
715 | else: |
|
713 | else: | |
716 | lineno = 'unknown' |
|
714 | lineno = 'unknown' | |
717 | textline = '' |
|
715 | textline = '' | |
718 | list.append('%s File %s"%s"%s, line %s%s%s\n' % \ |
|
716 | list.append('%s File %s"%s"%s, line %s%s%s\n' % \ | |
719 | (Colors.normalEm, |
|
717 | (Colors.normalEm, | |
720 | Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm, |
|
718 | Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm, | |
721 | Colors.linenoEm, lineno, Colors.Normal )) |
|
719 | Colors.linenoEm, lineno, Colors.Normal )) | |
722 | if textline == '': |
|
720 | if textline == '': | |
723 | textline = py3compat.cast_unicode(value.text, "utf-8") |
|
721 | textline = py3compat.cast_unicode(value.text, "utf-8") | |
724 |
|
722 | |||
725 | if textline is not None: |
|
723 | if textline is not None: | |
726 | i = 0 |
|
724 | i = 0 | |
727 | while i < len(textline) and textline[i].isspace(): |
|
725 | while i < len(textline) and textline[i].isspace(): | |
728 | i += 1 |
|
726 | i += 1 | |
729 | list.append('%s %s%s\n' % (Colors.line, |
|
727 | list.append('%s %s%s\n' % (Colors.line, | |
730 | textline.strip(), |
|
728 | textline.strip(), | |
731 | Colors.Normal)) |
|
729 | Colors.Normal)) | |
732 | if value.offset is not None: |
|
730 | if value.offset is not None: | |
733 | s = ' ' |
|
731 | s = ' ' | |
734 | for c in textline[i:value.offset - 1]: |
|
732 | for c in textline[i:value.offset - 1]: | |
735 | if c.isspace(): |
|
733 | if c.isspace(): | |
736 | s += c |
|
734 | s += c | |
737 | else: |
|
735 | else: | |
738 | s += ' ' |
|
736 | s += ' ' | |
739 | list.append('%s%s^%s\n' % (Colors.caret, s, |
|
737 | list.append('%s%s^%s\n' % (Colors.caret, s, | |
740 | Colors.Normal)) |
|
738 | Colors.Normal)) | |
741 |
|
739 | |||
742 | try: |
|
740 | try: | |
743 | s = value.msg |
|
741 | s = value.msg | |
744 | except Exception: |
|
742 | except Exception: | |
745 | s = self._some_str(value) |
|
743 | s = self._some_str(value) | |
746 | if s: |
|
744 | if s: | |
747 | list.append('%s%s:%s %s\n' % (stype, Colors.excName, |
|
745 | list.append('%s%s:%s %s\n' % (stype, Colors.excName, | |
748 | Colors.Normal, s)) |
|
746 | Colors.Normal, s)) | |
749 | else: |
|
747 | else: | |
750 | list.append('%s\n' % stype) |
|
748 | list.append('%s\n' % stype) | |
751 |
|
749 | |||
752 | # sync with user hooks |
|
750 | # sync with user hooks | |
753 | if have_filedata: |
|
751 | if have_filedata: | |
754 | ipinst = get_ipython() |
|
752 | ipinst = get_ipython() | |
755 | if ipinst is not None: |
|
753 | if ipinst is not None: | |
756 | ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0) |
|
754 | ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0) | |
757 |
|
755 | |||
758 | return list |
|
756 | return list | |
759 |
|
757 | |||
760 | def get_exception_only(self, etype, value): |
|
758 | def get_exception_only(self, etype, value): | |
761 | """Only print the exception type and message, without a traceback. |
|
759 | """Only print the exception type and message, without a traceback. | |
762 |
|
760 | |||
763 | Parameters |
|
761 | Parameters | |
764 | ---------- |
|
762 | ---------- | |
765 | etype : exception type |
|
763 | etype : exception type | |
766 | value : exception value |
|
764 | value : exception value | |
767 | """ |
|
765 | """ | |
768 | return ListTB.structured_traceback(self, etype, value, []) |
|
766 | return ListTB.structured_traceback(self, etype, value, []) | |
769 |
|
767 | |||
770 | def show_exception_only(self, etype, evalue): |
|
768 | def show_exception_only(self, etype, evalue): | |
771 | """Only print the exception type and message, without a traceback. |
|
769 | """Only print the exception type and message, without a traceback. | |
772 |
|
770 | |||
773 | Parameters |
|
771 | Parameters | |
774 | ---------- |
|
772 | ---------- | |
775 | etype : exception type |
|
773 | etype : exception type | |
776 | value : exception value |
|
774 | value : exception value | |
777 | """ |
|
775 | """ | |
778 | # This method needs to use __call__ from *this* class, not the one from |
|
776 | # This method needs to use __call__ from *this* class, not the one from | |
779 | # a subclass whose signature or behavior may be different |
|
777 | # a subclass whose signature or behavior may be different | |
780 | ostream = self.ostream |
|
778 | ostream = self.ostream | |
781 | ostream.flush() |
|
779 | ostream.flush() | |
782 | ostream.write('\n'.join(self.get_exception_only(etype, evalue))) |
|
780 | ostream.write('\n'.join(self.get_exception_only(etype, evalue))) | |
783 | ostream.flush() |
|
781 | ostream.flush() | |
784 |
|
782 | |||
785 | def _some_str(self, value): |
|
783 | def _some_str(self, value): | |
786 | # Lifted from traceback.py |
|
784 | # Lifted from traceback.py | |
787 | try: |
|
785 | try: | |
788 | return py3compat.cast_unicode(str(value)) |
|
786 | return py3compat.cast_unicode(str(value)) | |
789 | except: |
|
787 | except: | |
790 | return u'<unprintable %s object>' % type(value).__name__ |
|
788 | return u'<unprintable %s object>' % type(value).__name__ | |
791 |
|
789 | |||
792 |
|
790 | |||
793 | #---------------------------------------------------------------------------- |
|
791 | #---------------------------------------------------------------------------- | |
794 | class VerboseTB(TBTools): |
|
792 | class VerboseTB(TBTools): | |
795 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead |
|
793 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead | |
796 | of HTML. Requires inspect and pydoc. Crazy, man. |
|
794 | of HTML. Requires inspect and pydoc. Crazy, man. | |
797 |
|
795 | |||
798 | Modified version which optionally strips the topmost entries from the |
|
796 | Modified version which optionally strips the topmost entries from the | |
799 | traceback, to be used with alternate interpreters (because their own code |
|
797 | traceback, to be used with alternate interpreters (because their own code | |
800 | would appear in the traceback).""" |
|
798 | would appear in the traceback).""" | |
801 |
|
799 | |||
802 | def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None, |
|
800 | def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None, | |
803 | tb_offset=0, long_header=False, include_vars=True, |
|
801 | tb_offset=0, long_header=False, include_vars=True, | |
804 |
check_cache=None, debugger_cls = None |
|
802 | check_cache=None, debugger_cls = None, | |
|
803 | parent=None, config=None): | |||
805 | """Specify traceback offset, headers and color scheme. |
|
804 | """Specify traceback offset, headers and color scheme. | |
806 |
|
805 | |||
807 | Define how many frames to drop from the tracebacks. Calling it with |
|
806 | Define how many frames to drop from the tracebacks. Calling it with | |
808 | tb_offset=1 allows use of this handler in interpreters which will have |
|
807 | tb_offset=1 allows use of this handler in interpreters which will have | |
809 | their own code at the top of the traceback (VerboseTB will first |
|
808 | their own code at the top of the traceback (VerboseTB will first | |
810 | remove that frame before printing the traceback info).""" |
|
809 | remove that frame before printing the traceback info).""" | |
811 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, |
|
810 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, | |
812 | ostream=ostream) |
|
811 | ostream=ostream, parent=parent, config=config) | |
813 | self.tb_offset = tb_offset |
|
812 | self.tb_offset = tb_offset | |
814 | self.long_header = long_header |
|
813 | self.long_header = long_header | |
815 | self.include_vars = include_vars |
|
814 | self.include_vars = include_vars | |
816 | # By default we use linecache.checkcache, but the user can provide a |
|
815 | # By default we use linecache.checkcache, but the user can provide a | |
817 | # different check_cache implementation. This is used by the IPython |
|
816 | # different check_cache implementation. This is used by the IPython | |
818 | # kernel to provide tracebacks for interactive code that is cached, |
|
817 | # kernel to provide tracebacks for interactive code that is cached, | |
819 | # by a compiler instance that flushes the linecache but preserves its |
|
818 | # by a compiler instance that flushes the linecache but preserves its | |
820 | # own code cache. |
|
819 | # own code cache. | |
821 | if check_cache is None: |
|
820 | if check_cache is None: | |
822 | check_cache = linecache.checkcache |
|
821 | check_cache = linecache.checkcache | |
823 | self.check_cache = check_cache |
|
822 | self.check_cache = check_cache | |
824 |
|
823 | |||
825 | self.debugger_cls = debugger_cls or debugger.Pdb |
|
824 | self.debugger_cls = debugger_cls or debugger.Pdb | |
826 |
|
825 | |||
827 | def format_records(self, records, last_unique, recursion_repeat): |
|
826 | def format_records(self, records, last_unique, recursion_repeat): | |
828 | """Format the stack frames of the traceback""" |
|
827 | """Format the stack frames of the traceback""" | |
829 | frames = [] |
|
828 | frames = [] | |
830 | for r in records[:last_unique+recursion_repeat+1]: |
|
829 | for r in records[:last_unique+recursion_repeat+1]: | |
831 | #print '*** record:',file,lnum,func,lines,index # dbg |
|
830 | #print '*** record:',file,lnum,func,lines,index # dbg | |
832 | frames.append(self.format_record(*r)) |
|
831 | frames.append(self.format_record(*r)) | |
833 |
|
832 | |||
834 | if recursion_repeat: |
|
833 | if recursion_repeat: | |
835 | frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat) |
|
834 | frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat) | |
836 | frames.append(self.format_record(*records[last_unique+recursion_repeat+1])) |
|
835 | frames.append(self.format_record(*records[last_unique+recursion_repeat+1])) | |
837 |
|
836 | |||
838 | return frames |
|
837 | return frames | |
839 |
|
838 | |||
840 | def format_record(self, frame, file, lnum, func, lines, index): |
|
839 | def format_record(self, frame, file, lnum, func, lines, index): | |
841 | """Format a single stack frame""" |
|
840 | """Format a single stack frame""" | |
842 | Colors = self.Colors # just a shorthand + quicker name lookup |
|
841 | Colors = self.Colors # just a shorthand + quicker name lookup | |
843 | ColorsNormal = Colors.Normal # used a lot |
|
842 | ColorsNormal = Colors.Normal # used a lot | |
844 | col_scheme = self.color_scheme_table.active_scheme_name |
|
843 | col_scheme = self.color_scheme_table.active_scheme_name | |
845 | indent = ' ' * INDENT_SIZE |
|
844 | indent = ' ' * INDENT_SIZE | |
846 | em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal) |
|
845 | em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal) | |
847 | undefined = '%sundefined%s' % (Colors.em, ColorsNormal) |
|
846 | undefined = '%sundefined%s' % (Colors.em, ColorsNormal) | |
848 | tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal) |
|
847 | tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal) | |
849 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, |
|
848 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, | |
850 | ColorsNormal) |
|
849 | ColorsNormal) | |
851 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ |
|
850 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ | |
852 | (Colors.vName, Colors.valEm, ColorsNormal) |
|
851 | (Colors.vName, Colors.valEm, ColorsNormal) | |
853 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) |
|
852 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) | |
854 | tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, |
|
853 | tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, | |
855 | Colors.vName, ColorsNormal) |
|
854 | Colors.vName, ColorsNormal) | |
856 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) |
|
855 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) | |
857 |
|
856 | |||
858 | tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal) |
|
857 | tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal) | |
859 | tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm, Colors.line, |
|
858 | tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm, Colors.line, | |
860 | ColorsNormal) |
|
859 | ColorsNormal) | |
861 |
|
860 | |||
862 | abspath = os.path.abspath |
|
861 | abspath = os.path.abspath | |
863 |
|
862 | |||
864 |
|
863 | |||
865 | if not file: |
|
864 | if not file: | |
866 | file = '?' |
|
865 | file = '?' | |
867 | elif file.startswith(str("<")) and file.endswith(str(">")): |
|
866 | elif file.startswith(str("<")) and file.endswith(str(">")): | |
868 | # Not a real filename, no problem... |
|
867 | # Not a real filename, no problem... | |
869 | pass |
|
868 | pass | |
870 | elif not os.path.isabs(file): |
|
869 | elif not os.path.isabs(file): | |
871 | # Try to make the filename absolute by trying all |
|
870 | # Try to make the filename absolute by trying all | |
872 | # sys.path entries (which is also what linecache does) |
|
871 | # sys.path entries (which is also what linecache does) | |
873 | for dirname in sys.path: |
|
872 | for dirname in sys.path: | |
874 | try: |
|
873 | try: | |
875 | fullname = os.path.join(dirname, file) |
|
874 | fullname = os.path.join(dirname, file) | |
876 | if os.path.isfile(fullname): |
|
875 | if os.path.isfile(fullname): | |
877 | file = os.path.abspath(fullname) |
|
876 | file = os.path.abspath(fullname) | |
878 | break |
|
877 | break | |
879 | except Exception: |
|
878 | except Exception: | |
880 | # Just in case that sys.path contains very |
|
879 | # Just in case that sys.path contains very | |
881 | # strange entries... |
|
880 | # strange entries... | |
882 | pass |
|
881 | pass | |
883 |
|
882 | |||
884 | file = py3compat.cast_unicode(file, util_path.fs_encoding) |
|
883 | file = py3compat.cast_unicode(file, util_path.fs_encoding) | |
885 | link = tpl_link % file |
|
884 | link = tpl_link % file | |
886 | args, varargs, varkw, locals = fixed_getargvalues(frame) |
|
885 | args, varargs, varkw, locals = fixed_getargvalues(frame) | |
887 |
|
886 | |||
888 | if func == '?': |
|
887 | if func == '?': | |
889 | call = '' |
|
888 | call = '' | |
890 | else: |
|
889 | else: | |
891 | # Decide whether to include variable details or not |
|
890 | # Decide whether to include variable details or not | |
892 | var_repr = self.include_vars and eqrepr or nullrepr |
|
891 | var_repr = self.include_vars and eqrepr or nullrepr | |
893 | try: |
|
892 | try: | |
894 | call = tpl_call % (func, inspect.formatargvalues(args, |
|
893 | call = tpl_call % (func, inspect.formatargvalues(args, | |
895 | varargs, varkw, |
|
894 | varargs, varkw, | |
896 | locals, formatvalue=var_repr)) |
|
895 | locals, formatvalue=var_repr)) | |
897 | except KeyError: |
|
896 | except KeyError: | |
898 | # This happens in situations like errors inside generator |
|
897 | # This happens in situations like errors inside generator | |
899 | # expressions, where local variables are listed in the |
|
898 | # expressions, where local variables are listed in the | |
900 | # line, but can't be extracted from the frame. I'm not |
|
899 | # line, but can't be extracted from the frame. I'm not | |
901 | # 100% sure this isn't actually a bug in inspect itself, |
|
900 | # 100% sure this isn't actually a bug in inspect itself, | |
902 | # but since there's no info for us to compute with, the |
|
901 | # but since there's no info for us to compute with, the | |
903 | # best we can do is report the failure and move on. Here |
|
902 | # best we can do is report the failure and move on. Here | |
904 | # we must *not* call any traceback construction again, |
|
903 | # we must *not* call any traceback construction again, | |
905 | # because that would mess up use of %debug later on. So we |
|
904 | # because that would mess up use of %debug later on. So we | |
906 | # simply report the failure and move on. The only |
|
905 | # simply report the failure and move on. The only | |
907 | # limitation will be that this frame won't have locals |
|
906 | # limitation will be that this frame won't have locals | |
908 | # listed in the call signature. Quite subtle problem... |
|
907 | # listed in the call signature. Quite subtle problem... | |
909 | # I can't think of a good way to validate this in a unit |
|
908 | # I can't think of a good way to validate this in a unit | |
910 | # test, but running a script consisting of: |
|
909 | # test, but running a script consisting of: | |
911 | # dict( (k,v.strip()) for (k,v) in range(10) ) |
|
910 | # dict( (k,v.strip()) for (k,v) in range(10) ) | |
912 | # will illustrate the error, if this exception catch is |
|
911 | # will illustrate the error, if this exception catch is | |
913 | # disabled. |
|
912 | # disabled. | |
914 | call = tpl_call_fail % func |
|
913 | call = tpl_call_fail % func | |
915 |
|
914 | |||
916 | # Don't attempt to tokenize binary files. |
|
915 | # Don't attempt to tokenize binary files. | |
917 | if file.endswith(('.so', '.pyd', '.dll')): |
|
916 | if file.endswith(('.so', '.pyd', '.dll')): | |
918 | return '%s %s\n' % (link, call) |
|
917 | return '%s %s\n' % (link, call) | |
919 |
|
918 | |||
920 | elif file.endswith(('.pyc', '.pyo')): |
|
919 | elif file.endswith(('.pyc', '.pyo')): | |
921 | # Look up the corresponding source file. |
|
920 | # Look up the corresponding source file. | |
922 | try: |
|
921 | try: | |
923 | file = openpy.source_from_cache(file) |
|
922 | file = openpy.source_from_cache(file) | |
924 | except ValueError: |
|
923 | except ValueError: | |
925 | # Failed to get the source file for some reason |
|
924 | # Failed to get the source file for some reason | |
926 | # E.g. https://github.com/ipython/ipython/issues/9486 |
|
925 | # E.g. https://github.com/ipython/ipython/issues/9486 | |
927 | return '%s %s\n' % (link, call) |
|
926 | return '%s %s\n' % (link, call) | |
928 |
|
927 | |||
929 | def linereader(file=file, lnum=[lnum], getline=ulinecache.getline): |
|
928 | def linereader(file=file, lnum=[lnum], getline=ulinecache.getline): | |
930 | line = getline(file, lnum[0]) |
|
929 | line = getline(file, lnum[0]) | |
931 | lnum[0] += 1 |
|
930 | lnum[0] += 1 | |
932 | return line |
|
931 | return line | |
933 |
|
932 | |||
934 | # Build the list of names on this line of code where the exception |
|
933 | # Build the list of names on this line of code where the exception | |
935 | # occurred. |
|
934 | # occurred. | |
936 | try: |
|
935 | try: | |
937 | names = [] |
|
936 | names = [] | |
938 | name_cont = False |
|
937 | name_cont = False | |
939 |
|
938 | |||
940 | for token_type, token, start, end, line in generate_tokens(linereader): |
|
939 | for token_type, token, start, end, line in generate_tokens(linereader): | |
941 | # build composite names |
|
940 | # build composite names | |
942 | if token_type == tokenize.NAME and token not in keyword.kwlist: |
|
941 | if token_type == tokenize.NAME and token not in keyword.kwlist: | |
943 | if name_cont: |
|
942 | if name_cont: | |
944 | # Continuation of a dotted name |
|
943 | # Continuation of a dotted name | |
945 | try: |
|
944 | try: | |
946 | names[-1].append(token) |
|
945 | names[-1].append(token) | |
947 | except IndexError: |
|
946 | except IndexError: | |
948 | names.append([token]) |
|
947 | names.append([token]) | |
949 | name_cont = False |
|
948 | name_cont = False | |
950 | else: |
|
949 | else: | |
951 | # Regular new names. We append everything, the caller |
|
950 | # Regular new names. We append everything, the caller | |
952 | # will be responsible for pruning the list later. It's |
|
951 | # will be responsible for pruning the list later. It's | |
953 | # very tricky to try to prune as we go, b/c composite |
|
952 | # very tricky to try to prune as we go, b/c composite | |
954 | # names can fool us. The pruning at the end is easy |
|
953 | # names can fool us. The pruning at the end is easy | |
955 | # to do (or the caller can print a list with repeated |
|
954 | # to do (or the caller can print a list with repeated | |
956 | # names if so desired. |
|
955 | # names if so desired. | |
957 | names.append([token]) |
|
956 | names.append([token]) | |
958 | elif token == '.': |
|
957 | elif token == '.': | |
959 | name_cont = True |
|
958 | name_cont = True | |
960 | elif token_type == tokenize.NEWLINE: |
|
959 | elif token_type == tokenize.NEWLINE: | |
961 | break |
|
960 | break | |
962 |
|
961 | |||
963 | except (IndexError, UnicodeDecodeError, SyntaxError): |
|
962 | except (IndexError, UnicodeDecodeError, SyntaxError): | |
964 | # signals exit of tokenizer |
|
963 | # signals exit of tokenizer | |
965 | # SyntaxError can occur if the file is not actually Python |
|
964 | # SyntaxError can occur if the file is not actually Python | |
966 | # - see gh-6300 |
|
965 | # - see gh-6300 | |
967 | pass |
|
966 | pass | |
968 | except tokenize.TokenError as msg: |
|
967 | except tokenize.TokenError as msg: | |
969 | _m = ("An unexpected error occurred while tokenizing input\n" |
|
968 | _m = ("An unexpected error occurred while tokenizing input\n" | |
970 | "The following traceback may be corrupted or invalid\n" |
|
969 | "The following traceback may be corrupted or invalid\n" | |
971 | "The error message is: %s\n" % msg) |
|
970 | "The error message is: %s\n" % msg) | |
972 | error(_m) |
|
971 | error(_m) | |
973 |
|
972 | |||
974 | # Join composite names (e.g. "dict.fromkeys") |
|
973 | # Join composite names (e.g. "dict.fromkeys") | |
975 | names = ['.'.join(n) for n in names] |
|
974 | names = ['.'.join(n) for n in names] | |
976 | # prune names list of duplicates, but keep the right order |
|
975 | # prune names list of duplicates, but keep the right order | |
977 | unique_names = uniq_stable(names) |
|
976 | unique_names = uniq_stable(names) | |
978 |
|
977 | |||
979 | # Start loop over vars |
|
978 | # Start loop over vars | |
980 | lvals = [] |
|
979 | lvals = [] | |
981 | if self.include_vars: |
|
980 | if self.include_vars: | |
982 | for name_full in unique_names: |
|
981 | for name_full in unique_names: | |
983 | name_base = name_full.split('.', 1)[0] |
|
982 | name_base = name_full.split('.', 1)[0] | |
984 | if name_base in frame.f_code.co_varnames: |
|
983 | if name_base in frame.f_code.co_varnames: | |
985 | if name_base in locals: |
|
984 | if name_base in locals: | |
986 | try: |
|
985 | try: | |
987 | value = repr(eval(name_full, locals)) |
|
986 | value = repr(eval(name_full, locals)) | |
988 | except: |
|
987 | except: | |
989 | value = undefined |
|
988 | value = undefined | |
990 | else: |
|
989 | else: | |
991 | value = undefined |
|
990 | value = undefined | |
992 | name = tpl_local_var % name_full |
|
991 | name = tpl_local_var % name_full | |
993 | else: |
|
992 | else: | |
994 | if name_base in frame.f_globals: |
|
993 | if name_base in frame.f_globals: | |
995 | try: |
|
994 | try: | |
996 | value = repr(eval(name_full, frame.f_globals)) |
|
995 | value = repr(eval(name_full, frame.f_globals)) | |
997 | except: |
|
996 | except: | |
998 | value = undefined |
|
997 | value = undefined | |
999 | else: |
|
998 | else: | |
1000 | value = undefined |
|
999 | value = undefined | |
1001 | name = tpl_global_var % name_full |
|
1000 | name = tpl_global_var % name_full | |
1002 | lvals.append(tpl_name_val % (name, value)) |
|
1001 | lvals.append(tpl_name_val % (name, value)) | |
1003 | if lvals: |
|
1002 | if lvals: | |
1004 | lvals = '%s%s' % (indent, em_normal.join(lvals)) |
|
1003 | lvals = '%s%s' % (indent, em_normal.join(lvals)) | |
1005 | else: |
|
1004 | else: | |
1006 | lvals = '' |
|
1005 | lvals = '' | |
1007 |
|
1006 | |||
1008 | level = '%s %s\n' % (link, call) |
|
1007 | level = '%s %s\n' % (link, call) | |
1009 |
|
1008 | |||
1010 | if index is None: |
|
1009 | if index is None: | |
1011 | return level |
|
1010 | return level | |
1012 | else: |
|
1011 | else: | |
|
1012 | _line_format = PyColorize.Parser(style=col_scheme, parent=self).format2 | |||
1013 | return '%s%s' % (level, ''.join( |
|
1013 | return '%s%s' % (level, ''.join( | |
1014 | _format_traceback_lines(lnum, index, lines, Colors, lvals, |
|
1014 | _format_traceback_lines(lnum, index, lines, Colors, lvals, | |
1015 |
|
|
1015 | _line_format))) | |
1016 |
|
1016 | |||
1017 | def prepare_chained_exception_message(self, cause): |
|
1017 | def prepare_chained_exception_message(self, cause): | |
1018 | direct_cause = "\nThe above exception was the direct cause of the following exception:\n" |
|
1018 | direct_cause = "\nThe above exception was the direct cause of the following exception:\n" | |
1019 | exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n" |
|
1019 | exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n" | |
1020 |
|
1020 | |||
1021 | if cause: |
|
1021 | if cause: | |
1022 | message = [[direct_cause]] |
|
1022 | message = [[direct_cause]] | |
1023 | else: |
|
1023 | else: | |
1024 | message = [[exception_during_handling]] |
|
1024 | message = [[exception_during_handling]] | |
1025 | return message |
|
1025 | return message | |
1026 |
|
1026 | |||
1027 | def prepare_header(self, etype, long_version=False): |
|
1027 | def prepare_header(self, etype, long_version=False): | |
1028 | colors = self.Colors # just a shorthand + quicker name lookup |
|
1028 | colors = self.Colors # just a shorthand + quicker name lookup | |
1029 | colorsnormal = colors.Normal # used a lot |
|
1029 | colorsnormal = colors.Normal # used a lot | |
1030 | exc = '%s%s%s' % (colors.excName, etype, colorsnormal) |
|
1030 | exc = '%s%s%s' % (colors.excName, etype, colorsnormal) | |
1031 | width = min(75, get_terminal_size()[0]) |
|
1031 | width = min(75, get_terminal_size()[0]) | |
1032 | if long_version: |
|
1032 | if long_version: | |
1033 | # Header with the exception type, python version, and date |
|
1033 | # Header with the exception type, python version, and date | |
1034 | pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable |
|
1034 | pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable | |
1035 | date = time.ctime(time.time()) |
|
1035 | date = time.ctime(time.time()) | |
1036 |
|
1036 | |||
1037 | head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal, |
|
1037 | head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal, | |
1038 | exc, ' ' * (width - len(str(etype)) - len(pyver)), |
|
1038 | exc, ' ' * (width - len(str(etype)) - len(pyver)), | |
1039 | pyver, date.rjust(width) ) |
|
1039 | pyver, date.rjust(width) ) | |
1040 | head += "\nA problem occurred executing Python code. Here is the sequence of function" \ |
|
1040 | head += "\nA problem occurred executing Python code. Here is the sequence of function" \ | |
1041 | "\ncalls leading up to the error, with the most recent (innermost) call last." |
|
1041 | "\ncalls leading up to the error, with the most recent (innermost) call last." | |
1042 | else: |
|
1042 | else: | |
1043 | # Simplified header |
|
1043 | # Simplified header | |
1044 | head = '%s%s' % (exc, 'Traceback (most recent call last)'. \ |
|
1044 | head = '%s%s' % (exc, 'Traceback (most recent call last)'. \ | |
1045 | rjust(width - len(str(etype))) ) |
|
1045 | rjust(width - len(str(etype))) ) | |
1046 |
|
1046 | |||
1047 | return head |
|
1047 | return head | |
1048 |
|
1048 | |||
1049 | def format_exception(self, etype, evalue): |
|
1049 | def format_exception(self, etype, evalue): | |
1050 | colors = self.Colors # just a shorthand + quicker name lookup |
|
1050 | colors = self.Colors # just a shorthand + quicker name lookup | |
1051 | colorsnormal = colors.Normal # used a lot |
|
1051 | colorsnormal = colors.Normal # used a lot | |
1052 | indent = ' ' * INDENT_SIZE |
|
1052 | indent = ' ' * INDENT_SIZE | |
1053 | # Get (safely) a string form of the exception info |
|
1053 | # Get (safely) a string form of the exception info | |
1054 | try: |
|
1054 | try: | |
1055 | etype_str, evalue_str = map(str, (etype, evalue)) |
|
1055 | etype_str, evalue_str = map(str, (etype, evalue)) | |
1056 | except: |
|
1056 | except: | |
1057 | # User exception is improperly defined. |
|
1057 | # User exception is improperly defined. | |
1058 | etype, evalue = str, sys.exc_info()[:2] |
|
1058 | etype, evalue = str, sys.exc_info()[:2] | |
1059 | etype_str, evalue_str = map(str, (etype, evalue)) |
|
1059 | etype_str, evalue_str = map(str, (etype, evalue)) | |
1060 | # ... and format it |
|
1060 | # ... and format it | |
1061 | exception = ['%s%s%s: %s' % (colors.excName, etype_str, |
|
1061 | exception = ['%s%s%s: %s' % (colors.excName, etype_str, | |
1062 | colorsnormal, py3compat.cast_unicode(evalue_str))] |
|
1062 | colorsnormal, py3compat.cast_unicode(evalue_str))] | |
1063 |
|
1063 | |||
1064 | if (not py3compat.PY3) and type(evalue) is types.InstanceType: |
|
1064 | if (not py3compat.PY3) and type(evalue) is types.InstanceType: | |
1065 | try: |
|
1065 | try: | |
1066 | names = [w for w in dir(evalue) if isinstance(w, py3compat.string_types)] |
|
1066 | names = [w for w in dir(evalue) if isinstance(w, py3compat.string_types)] | |
1067 | except: |
|
1067 | except: | |
1068 | # Every now and then, an object with funny internals blows up |
|
1068 | # Every now and then, an object with funny internals blows up | |
1069 | # when dir() is called on it. We do the best we can to report |
|
1069 | # when dir() is called on it. We do the best we can to report | |
1070 | # the problem and continue |
|
1070 | # the problem and continue | |
1071 | _m = '%sException reporting error (object with broken dir())%s:' |
|
1071 | _m = '%sException reporting error (object with broken dir())%s:' | |
1072 | exception.append(_m % (colors.excName, colorsnormal)) |
|
1072 | exception.append(_m % (colors.excName, colorsnormal)) | |
1073 | etype_str, evalue_str = map(str, sys.exc_info()[:2]) |
|
1073 | etype_str, evalue_str = map(str, sys.exc_info()[:2]) | |
1074 | exception.append('%s%s%s: %s' % (colors.excName, etype_str, |
|
1074 | exception.append('%s%s%s: %s' % (colors.excName, etype_str, | |
1075 | colorsnormal, py3compat.cast_unicode(evalue_str))) |
|
1075 | colorsnormal, py3compat.cast_unicode(evalue_str))) | |
1076 | names = [] |
|
1076 | names = [] | |
1077 | for name in names: |
|
1077 | for name in names: | |
1078 | value = text_repr(getattr(evalue, name)) |
|
1078 | value = text_repr(getattr(evalue, name)) | |
1079 | exception.append('\n%s%s = %s' % (indent, name, value)) |
|
1079 | exception.append('\n%s%s = %s' % (indent, name, value)) | |
1080 |
|
1080 | |||
1081 | return exception |
|
1081 | return exception | |
1082 |
|
1082 | |||
1083 | def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset): |
|
1083 | def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset): | |
1084 | """Formats the header, traceback and exception message for a single exception. |
|
1084 | """Formats the header, traceback and exception message for a single exception. | |
1085 |
|
1085 | |||
1086 | This may be called multiple times by Python 3 exception chaining |
|
1086 | This may be called multiple times by Python 3 exception chaining | |
1087 | (PEP 3134). |
|
1087 | (PEP 3134). | |
1088 | """ |
|
1088 | """ | |
1089 | # some locals |
|
1089 | # some locals | |
1090 | orig_etype = etype |
|
1090 | orig_etype = etype | |
1091 | try: |
|
1091 | try: | |
1092 | etype = etype.__name__ |
|
1092 | etype = etype.__name__ | |
1093 | except AttributeError: |
|
1093 | except AttributeError: | |
1094 | pass |
|
1094 | pass | |
1095 |
|
1095 | |||
1096 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
1096 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
1097 | head = self.prepare_header(etype, self.long_header) |
|
1097 | head = self.prepare_header(etype, self.long_header) | |
1098 | records = self.get_records(etb, number_of_lines_of_context, tb_offset) |
|
1098 | records = self.get_records(etb, number_of_lines_of_context, tb_offset) | |
1099 |
|
1099 | |||
1100 | if records is None: |
|
1100 | if records is None: | |
1101 | return "" |
|
1101 | return "" | |
1102 |
|
1102 | |||
1103 | last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records) |
|
1103 | last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records) | |
1104 |
|
1104 | |||
1105 | frames = self.format_records(records, last_unique, recursion_repeat) |
|
1105 | frames = self.format_records(records, last_unique, recursion_repeat) | |
1106 |
|
1106 | |||
1107 | formatted_exception = self.format_exception(etype, evalue) |
|
1107 | formatted_exception = self.format_exception(etype, evalue) | |
1108 | if records: |
|
1108 | if records: | |
1109 | filepath, lnum = records[-1][1:3] |
|
1109 | filepath, lnum = records[-1][1:3] | |
1110 | filepath = os.path.abspath(filepath) |
|
1110 | filepath = os.path.abspath(filepath) | |
1111 | ipinst = get_ipython() |
|
1111 | ipinst = get_ipython() | |
1112 | if ipinst is not None: |
|
1112 | if ipinst is not None: | |
1113 | ipinst.hooks.synchronize_with_editor(filepath, lnum, 0) |
|
1113 | ipinst.hooks.synchronize_with_editor(filepath, lnum, 0) | |
1114 |
|
1114 | |||
1115 | return [[head] + frames + [''.join(formatted_exception[0])]] |
|
1115 | return [[head] + frames + [''.join(formatted_exception[0])]] | |
1116 |
|
1116 | |||
1117 | def get_records(self, etb, number_of_lines_of_context, tb_offset): |
|
1117 | def get_records(self, etb, number_of_lines_of_context, tb_offset): | |
1118 | try: |
|
1118 | try: | |
1119 | # Try the default getinnerframes and Alex's: Alex's fixes some |
|
1119 | # Try the default getinnerframes and Alex's: Alex's fixes some | |
1120 | # problems, but it generates empty tracebacks for console errors |
|
1120 | # problems, but it generates empty tracebacks for console errors | |
1121 | # (5 blanks lines) where none should be returned. |
|
1121 | # (5 blanks lines) where none should be returned. | |
1122 | return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset) |
|
1122 | return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset) | |
1123 | except UnicodeDecodeError: |
|
1123 | except UnicodeDecodeError: | |
1124 | # This can occur if a file's encoding magic comment is wrong. |
|
1124 | # This can occur if a file's encoding magic comment is wrong. | |
1125 | # I can't see a way to recover without duplicating a bunch of code |
|
1125 | # I can't see a way to recover without duplicating a bunch of code | |
1126 | # from the stdlib traceback module. --TK |
|
1126 | # from the stdlib traceback module. --TK | |
1127 | error('\nUnicodeDecodeError while processing traceback.\n') |
|
1127 | error('\nUnicodeDecodeError while processing traceback.\n') | |
1128 | return None |
|
1128 | return None | |
1129 | except: |
|
1129 | except: | |
1130 | # FIXME: I've been getting many crash reports from python 2.3 |
|
1130 | # FIXME: I've been getting many crash reports from python 2.3 | |
1131 | # users, traceable to inspect.py. If I can find a small test-case |
|
1131 | # users, traceable to inspect.py. If I can find a small test-case | |
1132 | # to reproduce this, I should either write a better workaround or |
|
1132 | # to reproduce this, I should either write a better workaround or | |
1133 | # file a bug report against inspect (if that's the real problem). |
|
1133 | # file a bug report against inspect (if that's the real problem). | |
1134 | # So far, I haven't been able to find an isolated example to |
|
1134 | # So far, I haven't been able to find an isolated example to | |
1135 | # reproduce the problem. |
|
1135 | # reproduce the problem. | |
1136 | inspect_error() |
|
1136 | inspect_error() | |
1137 | traceback.print_exc(file=self.ostream) |
|
1137 | traceback.print_exc(file=self.ostream) | |
1138 | info('\nUnfortunately, your original traceback can not be constructed.\n') |
|
1138 | info('\nUnfortunately, your original traceback can not be constructed.\n') | |
1139 | return None |
|
1139 | return None | |
1140 |
|
1140 | |||
1141 | def get_parts_of_chained_exception(self, evalue): |
|
1141 | def get_parts_of_chained_exception(self, evalue): | |
1142 | def get_chained_exception(exception_value): |
|
1142 | def get_chained_exception(exception_value): | |
1143 | cause = getattr(exception_value, '__cause__', None) |
|
1143 | cause = getattr(exception_value, '__cause__', None) | |
1144 | if cause: |
|
1144 | if cause: | |
1145 | return cause |
|
1145 | return cause | |
1146 | if getattr(exception_value, '__suppress_context__', False): |
|
1146 | if getattr(exception_value, '__suppress_context__', False): | |
1147 | return None |
|
1147 | return None | |
1148 | return getattr(exception_value, '__context__', None) |
|
1148 | return getattr(exception_value, '__context__', None) | |
1149 |
|
1149 | |||
1150 | chained_evalue = get_chained_exception(evalue) |
|
1150 | chained_evalue = get_chained_exception(evalue) | |
1151 |
|
1151 | |||
1152 | if chained_evalue: |
|
1152 | if chained_evalue: | |
1153 | return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__ |
|
1153 | return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__ | |
1154 |
|
1154 | |||
1155 | def structured_traceback(self, etype, evalue, etb, tb_offset=None, |
|
1155 | def structured_traceback(self, etype, evalue, etb, tb_offset=None, | |
1156 | number_of_lines_of_context=5): |
|
1156 | number_of_lines_of_context=5): | |
1157 | """Return a nice text document describing the traceback.""" |
|
1157 | """Return a nice text document describing the traceback.""" | |
1158 |
|
1158 | |||
1159 | formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context, |
|
1159 | formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context, | |
1160 | tb_offset) |
|
1160 | tb_offset) | |
1161 |
|
1161 | |||
1162 | colors = self.Colors # just a shorthand + quicker name lookup |
|
1162 | colors = self.Colors # just a shorthand + quicker name lookup | |
1163 | colorsnormal = colors.Normal # used a lot |
|
1163 | colorsnormal = colors.Normal # used a lot | |
1164 | head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal) |
|
1164 | head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal) | |
1165 | structured_traceback_parts = [head] |
|
1165 | structured_traceback_parts = [head] | |
1166 | if py3compat.PY3: |
|
1166 | if py3compat.PY3: | |
1167 | chained_exceptions_tb_offset = 0 |
|
1167 | chained_exceptions_tb_offset = 0 | |
1168 | lines_of_context = 3 |
|
1168 | lines_of_context = 3 | |
1169 | formatted_exceptions = formatted_exception |
|
1169 | formatted_exceptions = formatted_exception | |
1170 | exception = self.get_parts_of_chained_exception(evalue) |
|
1170 | exception = self.get_parts_of_chained_exception(evalue) | |
1171 | if exception: |
|
1171 | if exception: | |
1172 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) |
|
1172 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) | |
1173 | etype, evalue, etb = exception |
|
1173 | etype, evalue, etb = exception | |
1174 | else: |
|
1174 | else: | |
1175 | evalue = None |
|
1175 | evalue = None | |
1176 | chained_exc_ids = set() |
|
1176 | chained_exc_ids = set() | |
1177 | while evalue: |
|
1177 | while evalue: | |
1178 | formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context, |
|
1178 | formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context, | |
1179 | chained_exceptions_tb_offset) |
|
1179 | chained_exceptions_tb_offset) | |
1180 | exception = self.get_parts_of_chained_exception(evalue) |
|
1180 | exception = self.get_parts_of_chained_exception(evalue) | |
1181 |
|
1181 | |||
1182 | if exception and not id(exception[1]) in chained_exc_ids: |
|
1182 | if exception and not id(exception[1]) in chained_exc_ids: | |
1183 | chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop |
|
1183 | chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop | |
1184 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) |
|
1184 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) | |
1185 | etype, evalue, etb = exception |
|
1185 | etype, evalue, etb = exception | |
1186 | else: |
|
1186 | else: | |
1187 | evalue = None |
|
1187 | evalue = None | |
1188 |
|
1188 | |||
1189 | # we want to see exceptions in a reversed order: |
|
1189 | # we want to see exceptions in a reversed order: | |
1190 | # the first exception should be on top |
|
1190 | # the first exception should be on top | |
1191 | for formatted_exception in reversed(formatted_exceptions): |
|
1191 | for formatted_exception in reversed(formatted_exceptions): | |
1192 | structured_traceback_parts += formatted_exception |
|
1192 | structured_traceback_parts += formatted_exception | |
1193 | else: |
|
1193 | else: | |
1194 | structured_traceback_parts += formatted_exception[0] |
|
1194 | structured_traceback_parts += formatted_exception[0] | |
1195 |
|
1195 | |||
1196 | return structured_traceback_parts |
|
1196 | return structured_traceback_parts | |
1197 |
|
1197 | |||
1198 | def debugger(self, force=False): |
|
1198 | def debugger(self, force=False): | |
1199 | """Call up the pdb debugger if desired, always clean up the tb |
|
1199 | """Call up the pdb debugger if desired, always clean up the tb | |
1200 | reference. |
|
1200 | reference. | |
1201 |
|
1201 | |||
1202 | Keywords: |
|
1202 | Keywords: | |
1203 |
|
1203 | |||
1204 | - force(False): by default, this routine checks the instance call_pdb |
|
1204 | - force(False): by default, this routine checks the instance call_pdb | |
1205 | flag and does not actually invoke the debugger if the flag is false. |
|
1205 | flag and does not actually invoke the debugger if the flag is false. | |
1206 | The 'force' option forces the debugger to activate even if the flag |
|
1206 | The 'force' option forces the debugger to activate even if the flag | |
1207 | is false. |
|
1207 | is false. | |
1208 |
|
1208 | |||
1209 | If the call_pdb flag is set, the pdb interactive debugger is |
|
1209 | If the call_pdb flag is set, the pdb interactive debugger is | |
1210 | invoked. In all cases, the self.tb reference to the current traceback |
|
1210 | invoked. In all cases, the self.tb reference to the current traceback | |
1211 | is deleted to prevent lingering references which hamper memory |
|
1211 | is deleted to prevent lingering references which hamper memory | |
1212 | management. |
|
1212 | management. | |
1213 |
|
1213 | |||
1214 | Note that each call to pdb() does an 'import readline', so if your app |
|
1214 | Note that each call to pdb() does an 'import readline', so if your app | |
1215 | requires a special setup for the readline completers, you'll have to |
|
1215 | requires a special setup for the readline completers, you'll have to | |
1216 | fix that by hand after invoking the exception handler.""" |
|
1216 | fix that by hand after invoking the exception handler.""" | |
1217 |
|
1217 | |||
1218 | if force or self.call_pdb: |
|
1218 | if force or self.call_pdb: | |
1219 | if self.pdb is None: |
|
1219 | if self.pdb is None: | |
1220 | self.pdb = self.debugger_cls() |
|
1220 | self.pdb = self.debugger_cls() | |
1221 | # the system displayhook may have changed, restore the original |
|
1221 | # the system displayhook may have changed, restore the original | |
1222 | # for pdb |
|
1222 | # for pdb | |
1223 | display_trap = DisplayTrap(hook=sys.__displayhook__) |
|
1223 | display_trap = DisplayTrap(hook=sys.__displayhook__) | |
1224 | with display_trap: |
|
1224 | with display_trap: | |
1225 | self.pdb.reset() |
|
1225 | self.pdb.reset() | |
1226 | # Find the right frame so we don't pop up inside ipython itself |
|
1226 | # Find the right frame so we don't pop up inside ipython itself | |
1227 | if hasattr(self, 'tb') and self.tb is not None: |
|
1227 | if hasattr(self, 'tb') and self.tb is not None: | |
1228 | etb = self.tb |
|
1228 | etb = self.tb | |
1229 | else: |
|
1229 | else: | |
1230 | etb = self.tb = sys.last_traceback |
|
1230 | etb = self.tb = sys.last_traceback | |
1231 | while self.tb is not None and self.tb.tb_next is not None: |
|
1231 | while self.tb is not None and self.tb.tb_next is not None: | |
1232 | self.tb = self.tb.tb_next |
|
1232 | self.tb = self.tb.tb_next | |
1233 | if etb and etb.tb_next: |
|
1233 | if etb and etb.tb_next: | |
1234 | etb = etb.tb_next |
|
1234 | etb = etb.tb_next | |
1235 | self.pdb.botframe = etb.tb_frame |
|
1235 | self.pdb.botframe = etb.tb_frame | |
1236 | self.pdb.interaction(self.tb.tb_frame, self.tb) |
|
1236 | self.pdb.interaction(self.tb.tb_frame, self.tb) | |
1237 |
|
1237 | |||
1238 | if hasattr(self, 'tb'): |
|
1238 | if hasattr(self, 'tb'): | |
1239 | del self.tb |
|
1239 | del self.tb | |
1240 |
|
1240 | |||
1241 | def handler(self, info=None): |
|
1241 | def handler(self, info=None): | |
1242 | (etype, evalue, etb) = info or sys.exc_info() |
|
1242 | (etype, evalue, etb) = info or sys.exc_info() | |
1243 | self.tb = etb |
|
1243 | self.tb = etb | |
1244 | ostream = self.ostream |
|
1244 | ostream = self.ostream | |
1245 | ostream.flush() |
|
1245 | ostream.flush() | |
1246 | ostream.write(self.text(etype, evalue, etb)) |
|
1246 | ostream.write(self.text(etype, evalue, etb)) | |
1247 | ostream.write('\n') |
|
1247 | ostream.write('\n') | |
1248 | ostream.flush() |
|
1248 | ostream.flush() | |
1249 |
|
1249 | |||
1250 | # Changed so an instance can just be called as VerboseTB_inst() and print |
|
1250 | # Changed so an instance can just be called as VerboseTB_inst() and print | |
1251 | # out the right info on its own. |
|
1251 | # out the right info on its own. | |
1252 | def __call__(self, etype=None, evalue=None, etb=None): |
|
1252 | def __call__(self, etype=None, evalue=None, etb=None): | |
1253 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" |
|
1253 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" | |
1254 | if etb is None: |
|
1254 | if etb is None: | |
1255 | self.handler() |
|
1255 | self.handler() | |
1256 | else: |
|
1256 | else: | |
1257 | self.handler((etype, evalue, etb)) |
|
1257 | self.handler((etype, evalue, etb)) | |
1258 | try: |
|
1258 | try: | |
1259 | self.debugger() |
|
1259 | self.debugger() | |
1260 | except KeyboardInterrupt: |
|
1260 | except KeyboardInterrupt: | |
1261 | print("\nKeyboardInterrupt") |
|
1261 | print("\nKeyboardInterrupt") | |
1262 |
|
1262 | |||
1263 |
|
1263 | |||
1264 | #---------------------------------------------------------------------------- |
|
1264 | #---------------------------------------------------------------------------- | |
1265 | class FormattedTB(VerboseTB, ListTB): |
|
1265 | class FormattedTB(VerboseTB, ListTB): | |
1266 | """Subclass ListTB but allow calling with a traceback. |
|
1266 | """Subclass ListTB but allow calling with a traceback. | |
1267 |
|
1267 | |||
1268 | It can thus be used as a sys.excepthook for Python > 2.1. |
|
1268 | It can thus be used as a sys.excepthook for Python > 2.1. | |
1269 |
|
1269 | |||
1270 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. |
|
1270 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. | |
1271 |
|
1271 | |||
1272 | Allows a tb_offset to be specified. This is useful for situations where |
|
1272 | Allows a tb_offset to be specified. This is useful for situations where | |
1273 | one needs to remove a number of topmost frames from the traceback (such as |
|
1273 | one needs to remove a number of topmost frames from the traceback (such as | |
1274 | occurs with python programs that themselves execute other python code, |
|
1274 | occurs with python programs that themselves execute other python code, | |
1275 | like Python shells). """ |
|
1275 | like Python shells). """ | |
1276 |
|
1276 | |||
1277 | def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False, |
|
1277 | def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False, | |
1278 | ostream=None, |
|
1278 | ostream=None, | |
1279 | tb_offset=0, long_header=False, include_vars=False, |
|
1279 | tb_offset=0, long_header=False, include_vars=False, | |
1280 |
check_cache=None, debugger_cls=None |
|
1280 | check_cache=None, debugger_cls=None, | |
|
1281 | parent=None, config=None): | |||
1281 |
|
1282 | |||
1282 | # NEVER change the order of this list. Put new modes at the end: |
|
1283 | # NEVER change the order of this list. Put new modes at the end: | |
1283 | self.valid_modes = ['Plain', 'Context', 'Verbose'] |
|
1284 | self.valid_modes = ['Plain', 'Context', 'Verbose'] | |
1284 | self.verbose_modes = self.valid_modes[1:3] |
|
1285 | self.verbose_modes = self.valid_modes[1:3] | |
1285 |
|
1286 | |||
1286 | VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, |
|
1287 | VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, | |
1287 | ostream=ostream, tb_offset=tb_offset, |
|
1288 | ostream=ostream, tb_offset=tb_offset, | |
1288 | long_header=long_header, include_vars=include_vars, |
|
1289 | long_header=long_header, include_vars=include_vars, | |
1289 |
check_cache=check_cache, debugger_cls=debugger_cls |
|
1290 | check_cache=check_cache, debugger_cls=debugger_cls, | |
|
1291 | parent=parent, config=config) | |||
1290 |
|
1292 | |||
1291 | # Different types of tracebacks are joined with different separators to |
|
1293 | # Different types of tracebacks are joined with different separators to | |
1292 | # form a single string. They are taken from this dict |
|
1294 | # form a single string. They are taken from this dict | |
1293 | self._join_chars = dict(Plain='', Context='\n', Verbose='\n') |
|
1295 | self._join_chars = dict(Plain='', Context='\n', Verbose='\n') | |
1294 | # set_mode also sets the tb_join_char attribute |
|
1296 | # set_mode also sets the tb_join_char attribute | |
1295 | self.set_mode(mode) |
|
1297 | self.set_mode(mode) | |
1296 |
|
1298 | |||
1297 | def _extract_tb(self, tb): |
|
1299 | def _extract_tb(self, tb): | |
1298 | if tb: |
|
1300 | if tb: | |
1299 | return traceback.extract_tb(tb) |
|
1301 | return traceback.extract_tb(tb) | |
1300 | else: |
|
1302 | else: | |
1301 | return None |
|
1303 | return None | |
1302 |
|
1304 | |||
1303 | def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5): |
|
1305 | def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5): | |
1304 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
1306 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
1305 | mode = self.mode |
|
1307 | mode = self.mode | |
1306 | if mode in self.verbose_modes: |
|
1308 | if mode in self.verbose_modes: | |
1307 | # Verbose modes need a full traceback |
|
1309 | # Verbose modes need a full traceback | |
1308 | return VerboseTB.structured_traceback( |
|
1310 | return VerboseTB.structured_traceback( | |
1309 | self, etype, value, tb, tb_offset, number_of_lines_of_context |
|
1311 | self, etype, value, tb, tb_offset, number_of_lines_of_context | |
1310 | ) |
|
1312 | ) | |
1311 | else: |
|
1313 | else: | |
1312 | # We must check the source cache because otherwise we can print |
|
1314 | # We must check the source cache because otherwise we can print | |
1313 | # out-of-date source code. |
|
1315 | # out-of-date source code. | |
1314 | self.check_cache() |
|
1316 | self.check_cache() | |
1315 | # Now we can extract and format the exception |
|
1317 | # Now we can extract and format the exception | |
1316 | elist = self._extract_tb(tb) |
|
1318 | elist = self._extract_tb(tb) | |
1317 | return ListTB.structured_traceback( |
|
1319 | return ListTB.structured_traceback( | |
1318 | self, etype, value, elist, tb_offset, number_of_lines_of_context |
|
1320 | self, etype, value, elist, tb_offset, number_of_lines_of_context | |
1319 | ) |
|
1321 | ) | |
1320 |
|
1322 | |||
1321 | def stb2text(self, stb): |
|
1323 | def stb2text(self, stb): | |
1322 | """Convert a structured traceback (a list) to a string.""" |
|
1324 | """Convert a structured traceback (a list) to a string.""" | |
1323 | return self.tb_join_char.join(stb) |
|
1325 | return self.tb_join_char.join(stb) | |
1324 |
|
1326 | |||
1325 |
|
1327 | |||
1326 | def set_mode(self, mode=None): |
|
1328 | def set_mode(self, mode=None): | |
1327 | """Switch to the desired mode. |
|
1329 | """Switch to the desired mode. | |
1328 |
|
1330 | |||
1329 | If mode is not specified, cycles through the available modes.""" |
|
1331 | If mode is not specified, cycles through the available modes.""" | |
1330 |
|
1332 | |||
1331 | if not mode: |
|
1333 | if not mode: | |
1332 | new_idx = (self.valid_modes.index(self.mode) + 1 ) % \ |
|
1334 | new_idx = (self.valid_modes.index(self.mode) + 1 ) % \ | |
1333 | len(self.valid_modes) |
|
1335 | len(self.valid_modes) | |
1334 | self.mode = self.valid_modes[new_idx] |
|
1336 | self.mode = self.valid_modes[new_idx] | |
1335 | elif mode not in self.valid_modes: |
|
1337 | elif mode not in self.valid_modes: | |
1336 | raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n' |
|
1338 | raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n' | |
1337 | 'Valid modes: ' + str(self.valid_modes)) |
|
1339 | 'Valid modes: ' + str(self.valid_modes)) | |
1338 | else: |
|
1340 | else: | |
1339 | self.mode = mode |
|
1341 | self.mode = mode | |
1340 | # include variable details only in 'Verbose' mode |
|
1342 | # include variable details only in 'Verbose' mode | |
1341 | self.include_vars = (self.mode == self.valid_modes[2]) |
|
1343 | self.include_vars = (self.mode == self.valid_modes[2]) | |
1342 | # Set the join character for generating text tracebacks |
|
1344 | # Set the join character for generating text tracebacks | |
1343 | self.tb_join_char = self._join_chars[self.mode] |
|
1345 | self.tb_join_char = self._join_chars[self.mode] | |
1344 |
|
1346 | |||
1345 | # some convenient shortcuts |
|
1347 | # some convenient shortcuts | |
1346 | def plain(self): |
|
1348 | def plain(self): | |
1347 | self.set_mode(self.valid_modes[0]) |
|
1349 | self.set_mode(self.valid_modes[0]) | |
1348 |
|
1350 | |||
1349 | def context(self): |
|
1351 | def context(self): | |
1350 | self.set_mode(self.valid_modes[1]) |
|
1352 | self.set_mode(self.valid_modes[1]) | |
1351 |
|
1353 | |||
1352 | def verbose(self): |
|
1354 | def verbose(self): | |
1353 | self.set_mode(self.valid_modes[2]) |
|
1355 | self.set_mode(self.valid_modes[2]) | |
1354 |
|
1356 | |||
1355 |
|
1357 | |||
1356 | #---------------------------------------------------------------------------- |
|
1358 | #---------------------------------------------------------------------------- | |
1357 | class AutoFormattedTB(FormattedTB): |
|
1359 | class AutoFormattedTB(FormattedTB): | |
1358 | """A traceback printer which can be called on the fly. |
|
1360 | """A traceback printer which can be called on the fly. | |
1359 |
|
1361 | |||
1360 | It will find out about exceptions by itself. |
|
1362 | It will find out about exceptions by itself. | |
1361 |
|
1363 | |||
1362 | A brief example:: |
|
1364 | A brief example:: | |
1363 |
|
1365 | |||
1364 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') |
|
1366 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') | |
1365 | try: |
|
1367 | try: | |
1366 | ... |
|
1368 | ... | |
1367 | except: |
|
1369 | except: | |
1368 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object |
|
1370 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object | |
1369 | """ |
|
1371 | """ | |
1370 |
|
1372 | |||
1371 | def __call__(self, etype=None, evalue=None, etb=None, |
|
1373 | def __call__(self, etype=None, evalue=None, etb=None, | |
1372 | out=None, tb_offset=None): |
|
1374 | out=None, tb_offset=None): | |
1373 | """Print out a formatted exception traceback. |
|
1375 | """Print out a formatted exception traceback. | |
1374 |
|
1376 | |||
1375 | Optional arguments: |
|
1377 | Optional arguments: | |
1376 | - out: an open file-like object to direct output to. |
|
1378 | - out: an open file-like object to direct output to. | |
1377 |
|
1379 | |||
1378 | - tb_offset: the number of frames to skip over in the stack, on a |
|
1380 | - tb_offset: the number of frames to skip over in the stack, on a | |
1379 | per-call basis (this overrides temporarily the instance's tb_offset |
|
1381 | per-call basis (this overrides temporarily the instance's tb_offset | |
1380 | given at initialization time. """ |
|
1382 | given at initialization time. """ | |
1381 |
|
1383 | |||
1382 | if out is None: |
|
1384 | if out is None: | |
1383 | out = self.ostream |
|
1385 | out = self.ostream | |
1384 | out.flush() |
|
1386 | out.flush() | |
1385 | out.write(self.text(etype, evalue, etb, tb_offset)) |
|
1387 | out.write(self.text(etype, evalue, etb, tb_offset)) | |
1386 | out.write('\n') |
|
1388 | out.write('\n') | |
1387 | out.flush() |
|
1389 | out.flush() | |
1388 | # FIXME: we should remove the auto pdb behavior from here and leave |
|
1390 | # FIXME: we should remove the auto pdb behavior from here and leave | |
1389 | # that to the clients. |
|
1391 | # that to the clients. | |
1390 | try: |
|
1392 | try: | |
1391 | self.debugger() |
|
1393 | self.debugger() | |
1392 | except KeyboardInterrupt: |
|
1394 | except KeyboardInterrupt: | |
1393 | print("\nKeyboardInterrupt") |
|
1395 | print("\nKeyboardInterrupt") | |
1394 |
|
1396 | |||
1395 | def structured_traceback(self, etype=None, value=None, tb=None, |
|
1397 | def structured_traceback(self, etype=None, value=None, tb=None, | |
1396 | tb_offset=None, number_of_lines_of_context=5): |
|
1398 | tb_offset=None, number_of_lines_of_context=5): | |
1397 | if etype is None: |
|
1399 | if etype is None: | |
1398 | etype, value, tb = sys.exc_info() |
|
1400 | etype, value, tb = sys.exc_info() | |
1399 | self.tb = tb |
|
1401 | self.tb = tb | |
1400 | return FormattedTB.structured_traceback( |
|
1402 | return FormattedTB.structured_traceback( | |
1401 | self, etype, value, tb, tb_offset, number_of_lines_of_context) |
|
1403 | self, etype, value, tb, tb_offset, number_of_lines_of_context) | |
1402 |
|
1404 | |||
1403 |
|
1405 | |||
1404 | #--------------------------------------------------------------------------- |
|
1406 | #--------------------------------------------------------------------------- | |
1405 |
|
1407 | |||
1406 | # A simple class to preserve Nathan's original functionality. |
|
1408 | # A simple class to preserve Nathan's original functionality. | |
1407 | class ColorTB(FormattedTB): |
|
1409 | class ColorTB(FormattedTB): | |
1408 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" |
|
1410 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" | |
1409 |
|
1411 | |||
1410 | def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs): |
|
1412 | def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs): | |
1411 | FormattedTB.__init__(self, color_scheme=color_scheme, |
|
1413 | FormattedTB.__init__(self, color_scheme=color_scheme, | |
1412 | call_pdb=call_pdb, **kwargs) |
|
1414 | call_pdb=call_pdb, **kwargs) | |
1413 |
|
1415 | |||
1414 |
|
1416 | |||
1415 | class SyntaxTB(ListTB): |
|
1417 | class SyntaxTB(ListTB): | |
1416 | """Extension which holds some state: the last exception value""" |
|
1418 | """Extension which holds some state: the last exception value""" | |
1417 |
|
1419 | |||
1418 | def __init__(self, color_scheme='NoColor'): |
|
1420 | def __init__(self, color_scheme='NoColor', parent=None, config=None): | |
1419 | ListTB.__init__(self, color_scheme) |
|
1421 | ListTB.__init__(self, color_scheme, parent=parent, config=config) | |
1420 | self.last_syntax_error = None |
|
1422 | self.last_syntax_error = None | |
1421 |
|
1423 | |||
1422 | def __call__(self, etype, value, elist): |
|
1424 | def __call__(self, etype, value, elist): | |
1423 | self.last_syntax_error = value |
|
1425 | self.last_syntax_error = value | |
1424 |
|
1426 | |||
1425 | ListTB.__call__(self, etype, value, elist) |
|
1427 | ListTB.__call__(self, etype, value, elist) | |
1426 |
|
1428 | |||
1427 | def structured_traceback(self, etype, value, elist, tb_offset=None, |
|
1429 | def structured_traceback(self, etype, value, elist, tb_offset=None, | |
1428 | context=5): |
|
1430 | context=5): | |
1429 | # If the source file has been edited, the line in the syntax error can |
|
1431 | # If the source file has been edited, the line in the syntax error can | |
1430 | # be wrong (retrieved from an outdated cache). This replaces it with |
|
1432 | # be wrong (retrieved from an outdated cache). This replaces it with | |
1431 | # the current value. |
|
1433 | # the current value. | |
1432 | if isinstance(value, SyntaxError) \ |
|
1434 | if isinstance(value, SyntaxError) \ | |
1433 | and isinstance(value.filename, py3compat.string_types) \ |
|
1435 | and isinstance(value.filename, py3compat.string_types) \ | |
1434 | and isinstance(value.lineno, int): |
|
1436 | and isinstance(value.lineno, int): | |
1435 | linecache.checkcache(value.filename) |
|
1437 | linecache.checkcache(value.filename) | |
1436 | newtext = ulinecache.getline(value.filename, value.lineno) |
|
1438 | newtext = ulinecache.getline(value.filename, value.lineno) | |
1437 | if newtext: |
|
1439 | if newtext: | |
1438 | value.text = newtext |
|
1440 | value.text = newtext | |
1439 | self.last_syntax_error = value |
|
1441 | self.last_syntax_error = value | |
1440 | return super(SyntaxTB, self).structured_traceback(etype, value, elist, |
|
1442 | return super(SyntaxTB, self).structured_traceback(etype, value, elist, | |
1441 | tb_offset=tb_offset, context=context) |
|
1443 | tb_offset=tb_offset, context=context) | |
1442 |
|
1444 | |||
1443 | def clear_err_state(self): |
|
1445 | def clear_err_state(self): | |
1444 | """Return the current error state and clear it""" |
|
1446 | """Return the current error state and clear it""" | |
1445 | e = self.last_syntax_error |
|
1447 | e = self.last_syntax_error | |
1446 | self.last_syntax_error = None |
|
1448 | self.last_syntax_error = None | |
1447 | return e |
|
1449 | return e | |
1448 |
|
1450 | |||
1449 | def stb2text(self, stb): |
|
1451 | def stb2text(self, stb): | |
1450 | """Convert a structured traceback (a list) to a string.""" |
|
1452 | """Convert a structured traceback (a list) to a string.""" | |
1451 | return ''.join(stb) |
|
1453 | return ''.join(stb) | |
1452 |
|
1454 | |||
1453 |
|
1455 | |||
1454 | # some internal-use functions |
|
1456 | # some internal-use functions | |
1455 | def text_repr(value): |
|
1457 | def text_repr(value): | |
1456 | """Hopefully pretty robust repr equivalent.""" |
|
1458 | """Hopefully pretty robust repr equivalent.""" | |
1457 | # this is pretty horrible but should always return *something* |
|
1459 | # this is pretty horrible but should always return *something* | |
1458 | try: |
|
1460 | try: | |
1459 | return pydoc.text.repr(value) |
|
1461 | return pydoc.text.repr(value) | |
1460 | except KeyboardInterrupt: |
|
1462 | except KeyboardInterrupt: | |
1461 | raise |
|
1463 | raise | |
1462 | except: |
|
1464 | except: | |
1463 | try: |
|
1465 | try: | |
1464 | return repr(value) |
|
1466 | return repr(value) | |
1465 | except KeyboardInterrupt: |
|
1467 | except KeyboardInterrupt: | |
1466 | raise |
|
1468 | raise | |
1467 | except: |
|
1469 | except: | |
1468 | try: |
|
1470 | try: | |
1469 | # all still in an except block so we catch |
|
1471 | # all still in an except block so we catch | |
1470 | # getattr raising |
|
1472 | # getattr raising | |
1471 | name = getattr(value, '__name__', None) |
|
1473 | name = getattr(value, '__name__', None) | |
1472 | if name: |
|
1474 | if name: | |
1473 | # ick, recursion |
|
1475 | # ick, recursion | |
1474 | return text_repr(name) |
|
1476 | return text_repr(name) | |
1475 | klass = getattr(value, '__class__', None) |
|
1477 | klass = getattr(value, '__class__', None) | |
1476 | if klass: |
|
1478 | if klass: | |
1477 | return '%s instance' % text_repr(klass) |
|
1479 | return '%s instance' % text_repr(klass) | |
1478 | except KeyboardInterrupt: |
|
1480 | except KeyboardInterrupt: | |
1479 | raise |
|
1481 | raise | |
1480 | except: |
|
1482 | except: | |
1481 | return 'UNRECOVERABLE REPR FAILURE' |
|
1483 | return 'UNRECOVERABLE REPR FAILURE' | |
1482 |
|
1484 | |||
1483 |
|
1485 | |||
1484 | def eqrepr(value, repr=text_repr): |
|
1486 | def eqrepr(value, repr=text_repr): | |
1485 | return '=%s' % repr(value) |
|
1487 | return '=%s' % repr(value) | |
1486 |
|
1488 | |||
1487 |
|
1489 | |||
1488 | def nullrepr(value, repr=text_repr): |
|
1490 | def nullrepr(value, repr=text_repr): | |
1489 | return '' |
|
1491 | return '' |
@@ -1,332 +1,327 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | Class and program to colorize python source code for ANSI terminals. |
|
3 | Class and program to colorize python source code for ANSI terminals. | |
4 |
|
4 | |||
5 | Based on an HTML code highlighter by Jurgen Hermann found at: |
|
5 | Based on an HTML code highlighter by Jurgen Hermann found at: | |
6 | http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52298 |
|
6 | http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52298 | |
7 |
|
7 | |||
8 | Modifications by Fernando Perez (fperez@colorado.edu). |
|
8 | Modifications by Fernando Perez (fperez@colorado.edu). | |
9 |
|
9 | |||
10 | Information on the original HTML highlighter follows: |
|
10 | Information on the original HTML highlighter follows: | |
11 |
|
11 | |||
12 | MoinMoin - Python Source Parser |
|
12 | MoinMoin - Python Source Parser | |
13 |
|
13 | |||
14 | Title: Colorize Python source using the built-in tokenizer |
|
14 | Title: Colorize Python source using the built-in tokenizer | |
15 |
|
15 | |||
16 | Submitter: Jurgen Hermann |
|
16 | Submitter: Jurgen Hermann | |
17 | Last Updated:2001/04/06 |
|
17 | Last Updated:2001/04/06 | |
18 |
|
18 | |||
19 | Version no:1.2 |
|
19 | Version no:1.2 | |
20 |
|
20 | |||
21 | Description: |
|
21 | Description: | |
22 |
|
22 | |||
23 | This code is part of MoinMoin (http://moin.sourceforge.net/) and converts |
|
23 | This code is part of MoinMoin (http://moin.sourceforge.net/) and converts | |
24 | Python source code to HTML markup, rendering comments, keywords, |
|
24 | Python source code to HTML markup, rendering comments, keywords, | |
25 | operators, numeric and string literals in different colors. |
|
25 | operators, numeric and string literals in different colors. | |
26 |
|
26 | |||
27 | It shows how to use the built-in keyword, token and tokenize modules to |
|
27 | It shows how to use the built-in keyword, token and tokenize modules to | |
28 | scan Python source code and re-emit it with no changes to its original |
|
28 | scan Python source code and re-emit it with no changes to its original | |
29 | formatting (which is the hard part). |
|
29 | formatting (which is the hard part). | |
30 | """ |
|
30 | """ | |
31 | from __future__ import print_function |
|
31 | from __future__ import print_function | |
32 | from __future__ import absolute_import |
|
32 | from __future__ import absolute_import | |
33 | from __future__ import unicode_literals |
|
33 | from __future__ import unicode_literals | |
34 |
|
34 | |||
35 | __all__ = ['ANSICodeColors','Parser'] |
|
35 | __all__ = ['ANSICodeColors','Parser'] | |
36 |
|
36 | |||
37 | _scheme_default = 'Linux' |
|
37 | _scheme_default = 'Linux' | |
38 |
|
38 | |||
39 |
|
39 | |||
40 | # Imports |
|
40 | # Imports | |
41 | import keyword |
|
41 | import keyword | |
42 | import os |
|
42 | import os | |
43 | import sys |
|
43 | import sys | |
44 | import token |
|
44 | import token | |
45 | import tokenize |
|
45 | import tokenize | |
46 |
|
46 | |||
47 | try: |
|
47 | generate_tokens = tokenize.generate_tokens | |
48 | generate_tokens = tokenize.generate_tokens |
|
|||
49 | except AttributeError: |
|
|||
50 | # Python 3. Note that we use the undocumented _tokenize because it expects |
|
|||
51 | # strings, not bytes. See also Python issue #9969. |
|
|||
52 | generate_tokens = tokenize._tokenize |
|
|||
53 |
|
48 | |||
54 | from IPython.utils.coloransi import TermColors, InputTermColors ,ColorScheme, ColorSchemeTable |
|
49 | from IPython.utils.coloransi import TermColors, InputTermColors ,ColorScheme, ColorSchemeTable | |
55 | from IPython.utils.py3compat import PY3 |
|
50 | from IPython.utils.py3compat import PY3 | |
56 |
|
51 | |||
57 | from .colorable import Colorable |
|
52 | from .colorable import Colorable | |
58 |
|
53 | |||
59 | if PY3: |
|
54 | if PY3: | |
60 | from io import StringIO |
|
55 | from io import StringIO | |
61 | else: |
|
56 | else: | |
62 | from StringIO import StringIO |
|
57 | from StringIO import StringIO | |
63 |
|
58 | |||
64 | ############################################################################# |
|
59 | ############################################################################# | |
65 | ### Python Source Parser (does Hilighting) |
|
60 | ### Python Source Parser (does Hilighting) | |
66 | ############################################################################# |
|
61 | ############################################################################# | |
67 |
|
62 | |||
68 | _KEYWORD = token.NT_OFFSET + 1 |
|
63 | _KEYWORD = token.NT_OFFSET + 1 | |
69 | _TEXT = token.NT_OFFSET + 2 |
|
64 | _TEXT = token.NT_OFFSET + 2 | |
70 |
|
65 | |||
71 | #**************************************************************************** |
|
66 | #**************************************************************************** | |
72 | # Builtin color schemes |
|
67 | # Builtin color schemes | |
73 |
|
68 | |||
74 | Colors = TermColors # just a shorthand |
|
69 | Colors = TermColors # just a shorthand | |
75 |
|
70 | |||
76 | # Build a few color schemes |
|
71 | # Build a few color schemes | |
77 | NoColor = ColorScheme( |
|
72 | NoColor = ColorScheme( | |
78 | 'NoColor',{ |
|
73 | 'NoColor',{ | |
79 | 'header' : Colors.NoColor, |
|
74 | 'header' : Colors.NoColor, | |
80 | token.NUMBER : Colors.NoColor, |
|
75 | token.NUMBER : Colors.NoColor, | |
81 | token.OP : Colors.NoColor, |
|
76 | token.OP : Colors.NoColor, | |
82 | token.STRING : Colors.NoColor, |
|
77 | token.STRING : Colors.NoColor, | |
83 | tokenize.COMMENT : Colors.NoColor, |
|
78 | tokenize.COMMENT : Colors.NoColor, | |
84 | token.NAME : Colors.NoColor, |
|
79 | token.NAME : Colors.NoColor, | |
85 | token.ERRORTOKEN : Colors.NoColor, |
|
80 | token.ERRORTOKEN : Colors.NoColor, | |
86 |
|
81 | |||
87 | _KEYWORD : Colors.NoColor, |
|
82 | _KEYWORD : Colors.NoColor, | |
88 | _TEXT : Colors.NoColor, |
|
83 | _TEXT : Colors.NoColor, | |
89 |
|
84 | |||
90 | 'in_prompt' : InputTermColors.NoColor, # Input prompt |
|
85 | 'in_prompt' : InputTermColors.NoColor, # Input prompt | |
91 | 'in_number' : InputTermColors.NoColor, # Input prompt number |
|
86 | 'in_number' : InputTermColors.NoColor, # Input prompt number | |
92 | 'in_prompt2' : InputTermColors.NoColor, # Continuation prompt |
|
87 | 'in_prompt2' : InputTermColors.NoColor, # Continuation prompt | |
93 | 'in_normal' : InputTermColors.NoColor, # color off (usu. Colors.Normal) |
|
88 | 'in_normal' : InputTermColors.NoColor, # color off (usu. Colors.Normal) | |
94 |
|
89 | |||
95 | 'out_prompt' : Colors.NoColor, # Output prompt |
|
90 | 'out_prompt' : Colors.NoColor, # Output prompt | |
96 | 'out_number' : Colors.NoColor, # Output prompt number |
|
91 | 'out_number' : Colors.NoColor, # Output prompt number | |
97 |
|
92 | |||
98 | 'normal' : Colors.NoColor # color off (usu. Colors.Normal) |
|
93 | 'normal' : Colors.NoColor # color off (usu. Colors.Normal) | |
99 | } ) |
|
94 | } ) | |
100 |
|
95 | |||
101 | LinuxColors = ColorScheme( |
|
96 | LinuxColors = ColorScheme( | |
102 | 'Linux',{ |
|
97 | 'Linux',{ | |
103 | 'header' : Colors.LightRed, |
|
98 | 'header' : Colors.LightRed, | |
104 | token.NUMBER : Colors.LightCyan, |
|
99 | token.NUMBER : Colors.LightCyan, | |
105 | token.OP : Colors.Yellow, |
|
100 | token.OP : Colors.Yellow, | |
106 | token.STRING : Colors.LightBlue, |
|
101 | token.STRING : Colors.LightBlue, | |
107 | tokenize.COMMENT : Colors.LightRed, |
|
102 | tokenize.COMMENT : Colors.LightRed, | |
108 | token.NAME : Colors.Normal, |
|
103 | token.NAME : Colors.Normal, | |
109 | token.ERRORTOKEN : Colors.Red, |
|
104 | token.ERRORTOKEN : Colors.Red, | |
110 |
|
105 | |||
111 | _KEYWORD : Colors.LightGreen, |
|
106 | _KEYWORD : Colors.LightGreen, | |
112 | _TEXT : Colors.Yellow, |
|
107 | _TEXT : Colors.Yellow, | |
113 |
|
108 | |||
114 | 'in_prompt' : InputTermColors.Green, |
|
109 | 'in_prompt' : InputTermColors.Green, | |
115 | 'in_number' : InputTermColors.LightGreen, |
|
110 | 'in_number' : InputTermColors.LightGreen, | |
116 | 'in_prompt2' : InputTermColors.Green, |
|
111 | 'in_prompt2' : InputTermColors.Green, | |
117 | 'in_normal' : InputTermColors.Normal, # color off (usu. Colors.Normal) |
|
112 | 'in_normal' : InputTermColors.Normal, # color off (usu. Colors.Normal) | |
118 |
|
113 | |||
119 | 'out_prompt' : Colors.Red, |
|
114 | 'out_prompt' : Colors.Red, | |
120 | 'out_number' : Colors.LightRed, |
|
115 | 'out_number' : Colors.LightRed, | |
121 |
|
116 | |||
122 | 'normal' : Colors.Normal # color off (usu. Colors.Normal) |
|
117 | 'normal' : Colors.Normal # color off (usu. Colors.Normal) | |
123 | } ) |
|
118 | } ) | |
124 |
|
119 | |||
125 | NeutralColors = ColorScheme( |
|
120 | NeutralColors = ColorScheme( | |
126 | 'Neutral',{ |
|
121 | 'Neutral',{ | |
127 | 'header' : Colors.Red, |
|
122 | 'header' : Colors.Red, | |
128 | token.NUMBER : Colors.Cyan, |
|
123 | token.NUMBER : Colors.Cyan, | |
129 | token.OP : Colors.Blue, |
|
124 | token.OP : Colors.Blue, | |
130 | token.STRING : Colors.Blue, |
|
125 | token.STRING : Colors.Blue, | |
131 | tokenize.COMMENT : Colors.Red, |
|
126 | tokenize.COMMENT : Colors.Red, | |
132 | token.NAME : Colors.Normal, |
|
127 | token.NAME : Colors.Normal, | |
133 | token.ERRORTOKEN : Colors.Red, |
|
128 | token.ERRORTOKEN : Colors.Red, | |
134 |
|
129 | |||
135 | _KEYWORD : Colors.Green, |
|
130 | _KEYWORD : Colors.Green, | |
136 | _TEXT : Colors.Blue, |
|
131 | _TEXT : Colors.Blue, | |
137 |
|
132 | |||
138 | 'in_prompt' : InputTermColors.Blue, |
|
133 | 'in_prompt' : InputTermColors.Blue, | |
139 | 'in_number' : InputTermColors.LightBlue, |
|
134 | 'in_number' : InputTermColors.LightBlue, | |
140 | 'in_prompt2' : InputTermColors.Blue, |
|
135 | 'in_prompt2' : InputTermColors.Blue, | |
141 | 'in_normal' : InputTermColors.Normal, # color off (usu. Colors.Normal) |
|
136 | 'in_normal' : InputTermColors.Normal, # color off (usu. Colors.Normal) | |
142 |
|
137 | |||
143 | 'out_prompt' : Colors.Red, |
|
138 | 'out_prompt' : Colors.Red, | |
144 | 'out_number' : Colors.LightRed, |
|
139 | 'out_number' : Colors.LightRed, | |
145 |
|
140 | |||
146 | 'normal' : Colors.Normal # color off (usu. Colors.Normal) |
|
141 | 'normal' : Colors.Normal # color off (usu. Colors.Normal) | |
147 | } ) |
|
142 | } ) | |
148 |
|
143 | |||
149 | # Hack: the 'neutral' colours are not very visible on a dark background on |
|
144 | # Hack: the 'neutral' colours are not very visible on a dark background on | |
150 | # Windows. Since Windows command prompts have a dark background by default, and |
|
145 | # Windows. Since Windows command prompts have a dark background by default, and | |
151 | # relatively few users are likely to alter that, we will use the 'Linux' colours, |
|
146 | # relatively few users are likely to alter that, we will use the 'Linux' colours, | |
152 | # designed for a dark background, as the default on Windows. Changing it here |
|
147 | # designed for a dark background, as the default on Windows. Changing it here | |
153 | # avoids affecting the prompt colours rendered by prompt_toolkit, where the |
|
148 | # avoids affecting the prompt colours rendered by prompt_toolkit, where the | |
154 | # neutral defaults do work OK. |
|
149 | # neutral defaults do work OK. | |
155 |
|
150 | |||
156 | if os.name == 'nt': |
|
151 | if os.name == 'nt': | |
157 | NeutralColors = LinuxColors.copy(name='Neutral') |
|
152 | NeutralColors = LinuxColors.copy(name='Neutral') | |
158 |
|
153 | |||
159 | LightBGColors = ColorScheme( |
|
154 | LightBGColors = ColorScheme( | |
160 | 'LightBG',{ |
|
155 | 'LightBG',{ | |
161 | 'header' : Colors.Red, |
|
156 | 'header' : Colors.Red, | |
162 | token.NUMBER : Colors.Cyan, |
|
157 | token.NUMBER : Colors.Cyan, | |
163 | token.OP : Colors.Blue, |
|
158 | token.OP : Colors.Blue, | |
164 | token.STRING : Colors.Blue, |
|
159 | token.STRING : Colors.Blue, | |
165 | tokenize.COMMENT : Colors.Red, |
|
160 | tokenize.COMMENT : Colors.Red, | |
166 | token.NAME : Colors.Normal, |
|
161 | token.NAME : Colors.Normal, | |
167 | token.ERRORTOKEN : Colors.Red, |
|
162 | token.ERRORTOKEN : Colors.Red, | |
168 |
|
163 | |||
169 |
|
164 | |||
170 | _KEYWORD : Colors.Green, |
|
165 | _KEYWORD : Colors.Green, | |
171 | _TEXT : Colors.Blue, |
|
166 | _TEXT : Colors.Blue, | |
172 |
|
167 | |||
173 | 'in_prompt' : InputTermColors.Blue, |
|
168 | 'in_prompt' : InputTermColors.Blue, | |
174 | 'in_number' : InputTermColors.LightBlue, |
|
169 | 'in_number' : InputTermColors.LightBlue, | |
175 | 'in_prompt2' : InputTermColors.Blue, |
|
170 | 'in_prompt2' : InputTermColors.Blue, | |
176 | 'in_normal' : InputTermColors.Normal, # color off (usu. Colors.Normal) |
|
171 | 'in_normal' : InputTermColors.Normal, # color off (usu. Colors.Normal) | |
177 |
|
172 | |||
178 | 'out_prompt' : Colors.Red, |
|
173 | 'out_prompt' : Colors.Red, | |
179 | 'out_number' : Colors.LightRed, |
|
174 | 'out_number' : Colors.LightRed, | |
180 |
|
175 | |||
181 | 'normal' : Colors.Normal # color off (usu. Colors.Normal) |
|
176 | 'normal' : Colors.Normal # color off (usu. Colors.Normal) | |
182 | } ) |
|
177 | } ) | |
183 |
|
178 | |||
184 | # Build table of color schemes (needed by the parser) |
|
179 | # Build table of color schemes (needed by the parser) | |
185 | ANSICodeColors = ColorSchemeTable([NoColor,LinuxColors,LightBGColors, NeutralColors], |
|
180 | ANSICodeColors = ColorSchemeTable([NoColor,LinuxColors,LightBGColors, NeutralColors], | |
186 | _scheme_default) |
|
181 | _scheme_default) | |
187 |
|
182 | |||
188 | Undefined = object() |
|
183 | Undefined = object() | |
189 |
|
184 | |||
190 | class Parser(Colorable): |
|
185 | class Parser(Colorable): | |
191 | """ Format colored Python source. |
|
186 | """ Format colored Python source. | |
192 | """ |
|
187 | """ | |
193 |
|
188 | |||
194 | def __init__(self, color_table=None, out = sys.stdout, parent=None, style=None): |
|
189 | def __init__(self, color_table=None, out = sys.stdout, parent=None, style=None): | |
195 | """ Create a parser with a specified color table and output channel. |
|
190 | """ Create a parser with a specified color table and output channel. | |
196 |
|
191 | |||
197 | Call format() to process code. |
|
192 | Call format() to process code. | |
198 | """ |
|
193 | """ | |
199 |
|
194 | |||
200 | super(Parser, self).__init__(parent=parent) |
|
195 | super(Parser, self).__init__(parent=parent) | |
201 |
|
196 | |||
202 | self.color_table = color_table and color_table or ANSICodeColors |
|
197 | self.color_table = color_table and color_table or ANSICodeColors | |
203 | self.out = out |
|
198 | self.out = out | |
204 | if not style: |
|
199 | if not style: | |
205 | self.style = self.default_style |
|
200 | self.style = self.default_style | |
206 | else: |
|
201 | else: | |
207 | self.style = style |
|
202 | self.style = style | |
208 |
|
203 | |||
209 |
|
204 | |||
210 | def format(self, raw, out=None, scheme=Undefined): |
|
205 | def format(self, raw, out=None, scheme=Undefined): | |
211 | import warnings |
|
206 | import warnings | |
212 | if scheme is not Undefined: |
|
207 | if scheme is not Undefined: | |
213 | warnings.warn('The `scheme` argument of IPython.utils.PyColorize:Parser.format is deprecated since IPython 6.0.' |
|
208 | warnings.warn('The `scheme` argument of IPython.utils.PyColorize:Parser.format is deprecated since IPython 6.0.' | |
214 | 'It will have no effect. Set the parser `style` directly.', |
|
209 | 'It will have no effect. Set the parser `style` directly.', | |
215 | stacklevel=2) |
|
210 | stacklevel=2) | |
216 | return self.format2(raw, out)[0] |
|
211 | return self.format2(raw, out)[0] | |
217 |
|
212 | |||
218 | def format2(self, raw, out = None): |
|
213 | def format2(self, raw, out = None): | |
219 | """ Parse and send the colored source. |
|
214 | """ Parse and send the colored source. | |
220 |
|
215 | |||
221 | If out and scheme are not specified, the defaults (given to |
|
216 | If out and scheme are not specified, the defaults (given to | |
222 | constructor) are used. |
|
217 | constructor) are used. | |
223 |
|
218 | |||
224 | out should be a file-type object. Optionally, out can be given as the |
|
219 | out should be a file-type object. Optionally, out can be given as the | |
225 | string 'str' and the parser will automatically return the output in a |
|
220 | string 'str' and the parser will automatically return the output in a | |
226 | string.""" |
|
221 | string.""" | |
227 |
|
222 | |||
228 | string_output = 0 |
|
223 | string_output = 0 | |
229 | if out == 'str' or self.out == 'str' or \ |
|
224 | if out == 'str' or self.out == 'str' or \ | |
230 | isinstance(self.out,StringIO): |
|
225 | isinstance(self.out,StringIO): | |
231 | # XXX - I don't really like this state handling logic, but at this |
|
226 | # XXX - I don't really like this state handling logic, but at this | |
232 | # point I don't want to make major changes, so adding the |
|
227 | # point I don't want to make major changes, so adding the | |
233 | # isinstance() check is the simplest I can do to ensure correct |
|
228 | # isinstance() check is the simplest I can do to ensure correct | |
234 | # behavior. |
|
229 | # behavior. | |
235 | out_old = self.out |
|
230 | out_old = self.out | |
236 | self.out = StringIO() |
|
231 | self.out = StringIO() | |
237 | string_output = 1 |
|
232 | string_output = 1 | |
238 | elif out is not None: |
|
233 | elif out is not None: | |
239 | self.out = out |
|
234 | self.out = out | |
240 |
|
235 | |||
241 | # Fast return of the unmodified input for NoColor scheme |
|
236 | # Fast return of the unmodified input for NoColor scheme | |
242 | if self.style == 'NoColor': |
|
237 | if self.style == 'NoColor': | |
243 | error = False |
|
238 | error = False | |
244 | self.out.write(raw) |
|
239 | self.out.write(raw) | |
245 | if string_output: |
|
240 | if string_output: | |
246 | return raw,error |
|
241 | return raw,error | |
247 | else: |
|
242 | else: | |
248 | return None,error |
|
243 | return None,error | |
249 |
|
244 | |||
250 | # local shorthands |
|
245 | # local shorthands | |
251 | colors = self.color_table[self.style].colors |
|
246 | colors = self.color_table[self.style].colors | |
252 | self.colors = colors # put in object so __call__ sees it |
|
247 | self.colors = colors # put in object so __call__ sees it | |
253 |
|
248 | |||
254 | # Remove trailing whitespace and normalize tabs |
|
249 | # Remove trailing whitespace and normalize tabs | |
255 | self.raw = raw.expandtabs().rstrip() |
|
250 | self.raw = raw.expandtabs().rstrip() | |
256 |
|
251 | |||
257 | # store line offsets in self.lines |
|
252 | # store line offsets in self.lines | |
258 | self.lines = [0, 0] |
|
253 | self.lines = [0, 0] | |
259 | pos = 0 |
|
254 | pos = 0 | |
260 | raw_find = self.raw.find |
|
255 | raw_find = self.raw.find | |
261 | lines_append = self.lines.append |
|
256 | lines_append = self.lines.append | |
262 | while 1: |
|
257 | while 1: | |
263 | pos = raw_find('\n', pos) + 1 |
|
258 | pos = raw_find('\n', pos) + 1 | |
264 | if not pos: break |
|
259 | if not pos: break | |
265 | lines_append(pos) |
|
260 | lines_append(pos) | |
266 | lines_append(len(self.raw)) |
|
261 | lines_append(len(self.raw)) | |
267 |
|
262 | |||
268 | # parse the source and write it |
|
263 | # parse the source and write it | |
269 | self.pos = 0 |
|
264 | self.pos = 0 | |
270 | text = StringIO(self.raw) |
|
265 | text = StringIO(self.raw) | |
271 |
|
266 | |||
272 | error = False |
|
267 | error = False | |
273 | try: |
|
268 | try: | |
274 | for atoken in generate_tokens(text.readline): |
|
269 | for atoken in generate_tokens(text.readline): | |
275 | self(*atoken) |
|
270 | self(*atoken) | |
276 | except tokenize.TokenError as ex: |
|
271 | except tokenize.TokenError as ex: | |
277 | msg = ex.args[0] |
|
272 | msg = ex.args[0] | |
278 | line = ex.args[1][0] |
|
273 | line = ex.args[1][0] | |
279 | self.out.write("%s\n\n*** ERROR: %s%s%s\n" % |
|
274 | self.out.write("%s\n\n*** ERROR: %s%s%s\n" % | |
280 | (colors[token.ERRORTOKEN], |
|
275 | (colors[token.ERRORTOKEN], | |
281 | msg, self.raw[self.lines[line]:], |
|
276 | msg, self.raw[self.lines[line]:], | |
282 | colors.normal) |
|
277 | colors.normal) | |
283 | ) |
|
278 | ) | |
284 | error = True |
|
279 | error = True | |
285 | self.out.write(colors.normal+'\n') |
|
280 | self.out.write(colors.normal+'\n') | |
286 | if string_output: |
|
281 | if string_output: | |
287 | output = self.out.getvalue() |
|
282 | output = self.out.getvalue() | |
288 | self.out = out_old |
|
283 | self.out = out_old | |
289 | return (output, error) |
|
284 | return (output, error) | |
290 | return (None, error) |
|
285 | return (None, error) | |
291 |
|
286 | |||
292 | def __call__(self, toktype, toktext, start_pos, end_pos, line): |
|
287 | def __call__(self, toktype, toktext, start_pos, end_pos, line): | |
293 | """ Token handler, with syntax highlighting.""" |
|
288 | """ Token handler, with syntax highlighting.""" | |
294 | (srow,scol) = start_pos |
|
289 | (srow,scol) = start_pos | |
295 | (erow,ecol) = end_pos |
|
290 | (erow,ecol) = end_pos | |
296 | colors = self.colors |
|
291 | colors = self.colors | |
297 | owrite = self.out.write |
|
292 | owrite = self.out.write | |
298 |
|
293 | |||
299 | # line separator, so this works across platforms |
|
294 | # line separator, so this works across platforms | |
300 | linesep = os.linesep |
|
295 | linesep = os.linesep | |
301 |
|
296 | |||
302 | # calculate new positions |
|
297 | # calculate new positions | |
303 | oldpos = self.pos |
|
298 | oldpos = self.pos | |
304 | newpos = self.lines[srow] + scol |
|
299 | newpos = self.lines[srow] + scol | |
305 | self.pos = newpos + len(toktext) |
|
300 | self.pos = newpos + len(toktext) | |
306 |
|
301 | |||
307 | # send the original whitespace, if needed |
|
302 | # send the original whitespace, if needed | |
308 | if newpos > oldpos: |
|
303 | if newpos > oldpos: | |
309 | owrite(self.raw[oldpos:newpos]) |
|
304 | owrite(self.raw[oldpos:newpos]) | |
310 |
|
305 | |||
311 | # skip indenting tokens |
|
306 | # skip indenting tokens | |
312 | if toktype in [token.INDENT, token.DEDENT]: |
|
307 | if toktype in [token.INDENT, token.DEDENT]: | |
313 | self.pos = newpos |
|
308 | self.pos = newpos | |
314 | return |
|
309 | return | |
315 |
|
310 | |||
316 | # map token type to a color group |
|
311 | # map token type to a color group | |
317 | if token.LPAR <= toktype <= token.OP: |
|
312 | if token.LPAR <= toktype <= token.OP: | |
318 | toktype = token.OP |
|
313 | toktype = token.OP | |
319 | elif toktype == token.NAME and keyword.iskeyword(toktext): |
|
314 | elif toktype == token.NAME and keyword.iskeyword(toktext): | |
320 | toktype = _KEYWORD |
|
315 | toktype = _KEYWORD | |
321 | color = colors.get(toktype, colors[_TEXT]) |
|
316 | color = colors.get(toktype, colors[_TEXT]) | |
322 |
|
317 | |||
323 | #print '<%s>' % toktext, # dbg |
|
318 | #print '<%s>' % toktext, # dbg | |
324 |
|
319 | |||
325 | # Triple quoted strings must be handled carefully so that backtracking |
|
320 | # Triple quoted strings must be handled carefully so that backtracking | |
326 | # in pagers works correctly. We need color terminators on _each_ line. |
|
321 | # in pagers works correctly. We need color terminators on _each_ line. | |
327 | if linesep in toktext: |
|
322 | if linesep in toktext: | |
328 | toktext = toktext.replace(linesep, '%s%s%s' % |
|
323 | toktext = toktext.replace(linesep, '%s%s%s' % | |
329 | (colors.normal,linesep,color)) |
|
324 | (colors.normal,linesep,color)) | |
330 |
|
325 | |||
331 | # send text |
|
326 | # send text | |
332 | owrite('%s%s%s' % (color,toktext,colors.normal)) |
|
327 | owrite('%s%s%s' % (color,toktext,colors.normal)) |
General Comments 0
You need to be logged in to leave comments.
Login now