Show More
@@ -1,254 +1,254 b'' | |||||
1 | """Tests for the key interactiveshell module, where the main ipython class is defined. |
|
1 | """Tests for the key interactiveshell module, where the main ipython class is defined. | |
2 | """ |
|
2 | """ | |
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Module imports |
|
4 | # Module imports | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 |
|
6 | |||
7 | # third party |
|
7 | # third party | |
8 | import nose.tools as nt |
|
8 | import nose.tools as nt | |
9 |
|
9 | |||
10 | # our own packages |
|
10 | # our own packages | |
11 | from IPython.testing.globalipapp import get_ipython |
|
11 | from IPython.testing.globalipapp import get_ipython | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Globals |
|
14 | # Globals | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | # Get the public instance of IPython |
|
17 | # Get the public instance of IPython | |
18 | ip = get_ipython() |
|
18 | ip = get_ipython() | |
19 |
|
19 | |||
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | # Test functions |
|
21 | # Test functions | |
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 |
|
23 | |||
24 | def test_reset(): |
|
24 | def test_reset(): | |
25 | """reset must clear most namespaces.""" |
|
25 | """reset must clear most namespaces.""" | |
26 |
|
26 | |||
27 | # Check that reset runs without error |
|
27 | # Check that reset runs without error | |
28 | ip.reset() |
|
28 | ip.reset() | |
29 |
|
29 | |||
30 | # Once we've reset it (to clear of any junk that might have been there from |
|
30 | # Once we've reset it (to clear of any junk that might have been there from | |
31 | # other tests, we can count how many variables are in the user's namespace |
|
31 | # other tests, we can count how many variables are in the user's namespace | |
32 | nvars_user_ns = len(ip.user_ns) |
|
32 | nvars_user_ns = len(ip.user_ns) | |
33 | nvars_hidden = len(ip.user_ns_hidden) |
|
33 | nvars_hidden = len(ip.user_ns_hidden) | |
34 |
|
34 | |||
35 | # Now add a few variables to user_ns, and check that reset clears them |
|
35 | # Now add a few variables to user_ns, and check that reset clears them | |
36 | ip.user_ns['x'] = 1 |
|
36 | ip.user_ns['x'] = 1 | |
37 | ip.user_ns['y'] = 1 |
|
37 | ip.user_ns['y'] = 1 | |
38 | ip.reset() |
|
38 | ip.reset() | |
39 |
|
39 | |||
40 | # Finally, check that all namespaces have only as many variables as we |
|
40 | # Finally, check that all namespaces have only as many variables as we | |
41 | # expect to find in them: |
|
41 | # expect to find in them: | |
42 | nt.assert_equal(len(ip.user_ns), nvars_user_ns) |
|
42 | nt.assert_equal(len(ip.user_ns), nvars_user_ns) | |
43 | nt.assert_equal(len(ip.user_ns_hidden), nvars_hidden) |
|
43 | nt.assert_equal(len(ip.user_ns_hidden), nvars_hidden) | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | # Tests for reporting of exceptions in various modes, handling of SystemExit, |
|
46 | # Tests for reporting of exceptions in various modes, handling of SystemExit, | |
47 | # and %tb functionality. This is really a mix of testing ultraTB and interactiveshell. |
|
47 | # and %tb functionality. This is really a mix of testing ultraTB and interactiveshell. | |
48 |
|
48 | |||
49 | def doctest_tb_plain(): |
|
49 | def doctest_tb_plain(): | |
50 | """ |
|
50 | """ | |
51 | In [18]: xmode plain |
|
51 | In [18]: xmode plain | |
52 | Exception reporting mode: Plain |
|
52 | Exception reporting mode: Plain | |
53 |
|
53 | |||
54 | In [19]: run simpleerr.py |
|
54 | In [19]: run simpleerr.py | |
55 | Traceback (most recent call last): |
|
55 | Traceback (most recent call last): | |
56 | ...line 32, in <module> |
|
56 | ...line 32, in <module> | |
57 | bar(mode) |
|
57 | bar(mode) | |
58 | ...line 16, in bar |
|
58 | ...line 16, in bar | |
59 | div0() |
|
59 | div0() | |
60 | ...line 8, in div0 |
|
60 | ...line 8, in div0 | |
61 | x/y |
|
61 | x/y | |
62 | ZeroDivisionError: ... |
|
62 | ZeroDivisionError: ... | |
63 | """ |
|
63 | """ | |
64 |
|
64 | |||
65 |
|
65 | |||
66 | def doctest_tb_context(): |
|
66 | def doctest_tb_context(): | |
67 | """ |
|
67 | """ | |
68 | In [3]: xmode context |
|
68 | In [3]: xmode context | |
69 | Exception reporting mode: Context |
|
69 | Exception reporting mode: Context | |
70 |
|
70 | |||
71 | In [4]: run simpleerr.py |
|
71 | In [4]: run simpleerr.py | |
72 | --------------------------------------------------------------------------- |
|
72 | --------------------------------------------------------------------------- | |
73 | ZeroDivisionError Traceback (most recent call last) |
|
73 | ZeroDivisionError Traceback (most recent call last) | |
74 | <BLANKLINE> |
|
74 | <BLANKLINE> | |
75 |
... in <module> |
|
75 | ... in <module> | |
76 | 30 mode = 'div' |
|
76 | 30 mode = 'div' | |
77 | 31 |
|
77 | 31 | |
78 | ---> 32 bar(mode) |
|
78 | ---> 32 bar(mode) | |
79 | <BLANKLINE> |
|
79 | <BLANKLINE> | |
80 | ... in bar(mode) |
|
80 | ... in bar(mode) | |
81 | 14 "bar" |
|
81 | 14 "bar" | |
82 | 15 if mode=='div': |
|
82 | 15 if mode=='div': | |
83 | ---> 16 div0() |
|
83 | ---> 16 div0() | |
84 | 17 elif mode=='exit': |
|
84 | 17 elif mode=='exit': | |
85 | 18 try: |
|
85 | 18 try: | |
86 | <BLANKLINE> |
|
86 | <BLANKLINE> | |
87 | ... in div0() |
|
87 | ... in div0() | |
88 | 6 x = 1 |
|
88 | 6 x = 1 | |
89 | 7 y = 0 |
|
89 | 7 y = 0 | |
90 | ----> 8 x/y |
|
90 | ----> 8 x/y | |
91 | 9 |
|
91 | 9 | |
92 | 10 def sysexit(stat, mode): |
|
92 | 10 def sysexit(stat, mode): | |
93 | <BLANKLINE> |
|
93 | <BLANKLINE> | |
94 | ZeroDivisionError: ... |
|
94 | ZeroDivisionError: ... | |
95 | """ |
|
95 | """ | |
96 |
|
96 | |||
97 |
|
97 | |||
98 | def doctest_tb_verbose(): |
|
98 | def doctest_tb_verbose(): | |
99 | """ |
|
99 | """ | |
100 | In [5]: xmode verbose |
|
100 | In [5]: xmode verbose | |
101 | Exception reporting mode: Verbose |
|
101 | Exception reporting mode: Verbose | |
102 |
|
102 | |||
103 | In [6]: run simpleerr.py |
|
103 | In [6]: run simpleerr.py | |
104 | --------------------------------------------------------------------------- |
|
104 | --------------------------------------------------------------------------- | |
105 | ZeroDivisionError Traceback (most recent call last) |
|
105 | ZeroDivisionError Traceback (most recent call last) | |
106 | <BLANKLINE> |
|
106 | <BLANKLINE> | |
107 |
... in <module> |
|
107 | ... in <module> | |
108 | 30 mode = 'div' |
|
108 | 30 mode = 'div' | |
109 | 31 |
|
109 | 31 | |
110 | ---> 32 bar(mode) |
|
110 | ---> 32 bar(mode) | |
111 | global bar = <function bar at ...> |
|
111 | global bar = <function bar at ...> | |
112 | global mode = 'div' |
|
112 | global mode = 'div' | |
113 | <BLANKLINE> |
|
113 | <BLANKLINE> | |
114 | ... in bar(mode='div') |
|
114 | ... in bar(mode='div') | |
115 | 14 "bar" |
|
115 | 14 "bar" | |
116 | 15 if mode=='div': |
|
116 | 15 if mode=='div': | |
117 | ---> 16 div0() |
|
117 | ---> 16 div0() | |
118 | global div0 = <function div0 at ...> |
|
118 | global div0 = <function div0 at ...> | |
119 | 17 elif mode=='exit': |
|
119 | 17 elif mode=='exit': | |
120 | 18 try: |
|
120 | 18 try: | |
121 | <BLANKLINE> |
|
121 | <BLANKLINE> | |
122 | ... in div0() |
|
122 | ... in div0() | |
123 | 6 x = 1 |
|
123 | 6 x = 1 | |
124 | 7 y = 0 |
|
124 | 7 y = 0 | |
125 | ----> 8 x/y |
|
125 | ----> 8 x/y | |
126 | x = 1 |
|
126 | x = 1 | |
127 | y = 0 |
|
127 | y = 0 | |
128 | 9 |
|
128 | 9 | |
129 | 10 def sysexit(stat, mode): |
|
129 | 10 def sysexit(stat, mode): | |
130 | <BLANKLINE> |
|
130 | <BLANKLINE> | |
131 | ZeroDivisionError: ... |
|
131 | ZeroDivisionError: ... | |
132 | """ |
|
132 | """ | |
133 |
|
133 | |||
134 | def doctest_tb_sysexit(): |
|
134 | def doctest_tb_sysexit(): | |
135 | """ |
|
135 | """ | |
136 | In [17]: %xmode plain |
|
136 | In [17]: %xmode plain | |
137 | Exception reporting mode: Plain |
|
137 | Exception reporting mode: Plain | |
138 |
|
138 | |||
139 | In [18]: %run simpleerr.py exit |
|
139 | In [18]: %run simpleerr.py exit | |
140 | An exception has occurred, use %tb to see the full traceback. |
|
140 | An exception has occurred, use %tb to see the full traceback. | |
141 | SystemExit: (1, 'Mode = exit') |
|
141 | SystemExit: (1, 'Mode = exit') | |
142 |
|
142 | |||
143 | In [19]: %run simpleerr.py exit 2 |
|
143 | In [19]: %run simpleerr.py exit 2 | |
144 | An exception has occurred, use %tb to see the full traceback. |
|
144 | An exception has occurred, use %tb to see the full traceback. | |
145 | SystemExit: (2, 'Mode = exit') |
|
145 | SystemExit: (2, 'Mode = exit') | |
146 |
|
146 | |||
147 | In [20]: %tb |
|
147 | In [20]: %tb | |
148 | Traceback (most recent call last): |
|
148 | Traceback (most recent call last): | |
149 | File ... in <module> |
|
149 | File ... in <module> | |
150 | bar(mode) |
|
150 | bar(mode) | |
151 | File ... line 22, in bar |
|
151 | File ... line 22, in bar | |
152 | sysexit(stat, mode) |
|
152 | sysexit(stat, mode) | |
153 | File ... line 11, in sysexit |
|
153 | File ... line 11, in sysexit | |
154 | raise SystemExit(stat, 'Mode = %s' % mode) |
|
154 | raise SystemExit(stat, 'Mode = %s' % mode) | |
155 | SystemExit: (2, 'Mode = exit') |
|
155 | SystemExit: (2, 'Mode = exit') | |
156 |
|
156 | |||
157 | In [21]: %xmode context |
|
157 | In [21]: %xmode context | |
158 | Exception reporting mode: Context |
|
158 | Exception reporting mode: Context | |
159 |
|
159 | |||
160 | In [22]: %tb |
|
160 | In [22]: %tb | |
161 | --------------------------------------------------------------------------- |
|
161 | --------------------------------------------------------------------------- | |
162 | SystemExit Traceback (most recent call last) |
|
162 | SystemExit Traceback (most recent call last) | |
163 | <BLANKLINE> |
|
163 | <BLANKLINE> | |
164 |
...<module> |
|
164 | ...<module> | |
165 | 30 mode = 'div' |
|
165 | 30 mode = 'div' | |
166 | 31 |
|
166 | 31 | |
167 | ---> 32 bar(mode) |
|
167 | ---> 32 bar(mode) | |
168 | <BLANKLINE> |
|
168 | <BLANKLINE> | |
169 | ...bar(mode) |
|
169 | ...bar(mode) | |
170 | 20 except: |
|
170 | 20 except: | |
171 | 21 stat = 1 |
|
171 | 21 stat = 1 | |
172 | ---> 22 sysexit(stat, mode) |
|
172 | ---> 22 sysexit(stat, mode) | |
173 | 23 else: |
|
173 | 23 else: | |
174 | 24 raise ValueError('Unknown mode') |
|
174 | 24 raise ValueError('Unknown mode') | |
175 | <BLANKLINE> |
|
175 | <BLANKLINE> | |
176 | ...sysexit(stat, mode) |
|
176 | ...sysexit(stat, mode) | |
177 | 9 |
|
177 | 9 | |
178 | 10 def sysexit(stat, mode): |
|
178 | 10 def sysexit(stat, mode): | |
179 | ---> 11 raise SystemExit(stat, 'Mode = %s' % mode) |
|
179 | ---> 11 raise SystemExit(stat, 'Mode = %s' % mode) | |
180 | 12 |
|
180 | 12 | |
181 | 13 def bar(mode): |
|
181 | 13 def bar(mode): | |
182 | <BLANKLINE> |
|
182 | <BLANKLINE> | |
183 | SystemExit: (2, 'Mode = exit') |
|
183 | SystemExit: (2, 'Mode = exit') | |
184 |
|
184 | |||
185 | In [23]: %xmode verbose |
|
185 | In [23]: %xmode verbose | |
186 | Exception reporting mode: Verbose |
|
186 | Exception reporting mode: Verbose | |
187 |
|
187 | |||
188 | In [24]: %tb |
|
188 | In [24]: %tb | |
189 | --------------------------------------------------------------------------- |
|
189 | --------------------------------------------------------------------------- | |
190 | SystemExit Traceback (most recent call last) |
|
190 | SystemExit Traceback (most recent call last) | |
191 | <BLANKLINE> |
|
191 | <BLANKLINE> | |
192 |
... in <module> |
|
192 | ... in <module> | |
193 | 30 mode = 'div' |
|
193 | 30 mode = 'div' | |
194 | 31 |
|
194 | 31 | |
195 | ---> 32 bar(mode) |
|
195 | ---> 32 bar(mode) | |
196 | global bar = <function bar at ...> |
|
196 | global bar = <function bar at ...> | |
197 | global mode = 'exit' |
|
197 | global mode = 'exit' | |
198 | <BLANKLINE> |
|
198 | <BLANKLINE> | |
199 | ... in bar(mode='exit') |
|
199 | ... in bar(mode='exit') | |
200 | 20 except: |
|
200 | 20 except: | |
201 | 21 stat = 1 |
|
201 | 21 stat = 1 | |
202 | ---> 22 sysexit(stat, mode) |
|
202 | ---> 22 sysexit(stat, mode) | |
203 | global sysexit = <function sysexit at ...> |
|
203 | global sysexit = <function sysexit at ...> | |
204 | stat = 2 |
|
204 | stat = 2 | |
205 | mode = 'exit' |
|
205 | mode = 'exit' | |
206 | 23 else: |
|
206 | 23 else: | |
207 | 24 raise ValueError('Unknown mode') |
|
207 | 24 raise ValueError('Unknown mode') | |
208 | <BLANKLINE> |
|
208 | <BLANKLINE> | |
209 | ... in sysexit(stat=2, mode='exit') |
|
209 | ... in sysexit(stat=2, mode='exit') | |
210 | 9 |
|
210 | 9 | |
211 | 10 def sysexit(stat, mode): |
|
211 | 10 def sysexit(stat, mode): | |
212 | ---> 11 raise SystemExit(stat, 'Mode = %s' % mode) |
|
212 | ---> 11 raise SystemExit(stat, 'Mode = %s' % mode) | |
213 | global SystemExit = undefined |
|
213 | global SystemExit = undefined | |
214 | stat = 2 |
|
214 | stat = 2 | |
215 | mode = 'exit' |
|
215 | mode = 'exit' | |
216 | 12 |
|
216 | 12 | |
217 | 13 def bar(mode): |
|
217 | 13 def bar(mode): | |
218 | <BLANKLINE> |
|
218 | <BLANKLINE> | |
219 | SystemExit: (2, 'Mode = exit') |
|
219 | SystemExit: (2, 'Mode = exit') | |
220 | """ |
|
220 | """ | |
221 |
|
221 | |||
222 |
|
222 | |||
223 | def test_run_cell(): |
|
223 | def test_run_cell(): | |
224 | import textwrap |
|
224 | import textwrap | |
225 | ip.run_cell('a = 10\na+=1') |
|
225 | ip.run_cell('a = 10\na+=1') | |
226 | ip.run_cell('assert a == 11\nassert 1') |
|
226 | ip.run_cell('assert a == 11\nassert 1') | |
227 |
|
227 | |||
228 | nt.assert_equal(ip.user_ns['a'], 11) |
|
228 | nt.assert_equal(ip.user_ns['a'], 11) | |
229 | complex = textwrap.dedent(""" |
|
229 | complex = textwrap.dedent(""" | |
230 | if 1: |
|
230 | if 1: | |
231 | print "hello" |
|
231 | print "hello" | |
232 | if 1: |
|
232 | if 1: | |
233 | print "world" |
|
233 | print "world" | |
234 |
|
234 | |||
235 | if 2: |
|
235 | if 2: | |
236 | print "foo" |
|
236 | print "foo" | |
237 |
|
237 | |||
238 | if 3: |
|
238 | if 3: | |
239 | print "bar" |
|
239 | print "bar" | |
240 |
|
240 | |||
241 | if 4: |
|
241 | if 4: | |
242 | print "bar" |
|
242 | print "bar" | |
243 |
|
243 | |||
244 | """) |
|
244 | """) | |
245 | # Simply verifies that this kind of input is run |
|
245 | # Simply verifies that this kind of input is run | |
246 | ip.run_cell(complex) |
|
246 | ip.run_cell(complex) | |
247 |
|
247 | |||
248 |
|
248 | |||
249 | def test_db(): |
|
249 | def test_db(): | |
250 | """Test the internal database used for variable persistence.""" |
|
250 | """Test the internal database used for variable persistence.""" | |
251 | ip.db['__unittest_'] = 12 |
|
251 | ip.db['__unittest_'] = 12 | |
252 | nt.assert_equal(ip.db['__unittest_'], 12) |
|
252 | nt.assert_equal(ip.db['__unittest_'], 12) | |
253 | del ip.db['__unittest_'] |
|
253 | del ip.db['__unittest_'] | |
254 | assert '__unittest_' not in ip.db |
|
254 | assert '__unittest_' not in ip.db |
@@ -1,1458 +1,1460 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 |
|
91 | |||
92 | import dis |
|
92 | import dis | |
93 | import inspect |
|
93 | import inspect | |
94 | import keyword |
|
94 | import keyword | |
95 | import linecache |
|
95 | import linecache | |
96 | import os |
|
96 | import os | |
97 | import pydoc |
|
97 | import pydoc | |
98 | import re |
|
98 | import re | |
99 | import sys |
|
99 | import sys | |
100 | import time |
|
100 | import time | |
101 | import tokenize |
|
101 | import tokenize | |
102 | import traceback |
|
102 | import traceback | |
103 |
|
103 | |||
104 | try: # Python 2 |
|
104 | try: # Python 2 | |
105 | generate_tokens = tokenize.generate_tokens |
|
105 | generate_tokens = tokenize.generate_tokens | |
106 | except AttributeError: # Python 3 |
|
106 | except AttributeError: # Python 3 | |
107 | generate_tokens = tokenize.tokenize |
|
107 | generate_tokens = tokenize.tokenize | |
108 |
|
108 | |||
109 | # For purposes of monkeypatching inspect to fix a bug in it. |
|
109 | # For purposes of monkeypatching inspect to fix a bug in it. | |
110 | from inspect import getsourcefile, getfile, getmodule, \ |
|
110 | from inspect import getsourcefile, getfile, getmodule, \ | |
111 | ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode |
|
111 | ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode | |
112 |
|
112 | |||
113 | # IPython's own modules |
|
113 | # IPython's own modules | |
114 | from IPython import get_ipython |
|
114 | from IPython import get_ipython | |
115 | from IPython.core import debugger |
|
115 | from IPython.core import debugger | |
116 | from IPython.core.display_trap import DisplayTrap |
|
116 | from IPython.core.display_trap import DisplayTrap | |
117 | from IPython.core.excolors import exception_colors |
|
117 | from IPython.core.excolors import exception_colors | |
118 | from IPython.utils import PyColorize |
|
118 | from IPython.utils import PyColorize | |
119 | from IPython.utils import openpy |
|
119 | from IPython.utils import openpy | |
120 | from IPython.utils import path as util_path |
|
120 | from IPython.utils import path as util_path | |
121 | from IPython.utils import py3compat |
|
121 | from IPython.utils import py3compat | |
122 | from IPython.utils.data import uniq_stable |
|
122 | from IPython.utils.data import uniq_stable | |
123 | from IPython.utils.terminal import get_terminal_size |
|
123 | from IPython.utils.terminal import get_terminal_size | |
124 |
|
124 | |||
125 | from logging import info, error, debug |
|
125 | from logging import info, error, debug | |
126 |
|
126 | |||
127 | import IPython.utils.colorable as colorable |
|
127 | import IPython.utils.colorable as colorable | |
128 |
|
128 | |||
129 | # Globals |
|
129 | # Globals | |
130 | # amount of space to put line numbers before verbose tracebacks |
|
130 | # amount of space to put line numbers before verbose tracebacks | |
131 | INDENT_SIZE = 8 |
|
131 | INDENT_SIZE = 8 | |
132 |
|
132 | |||
133 | # Default color scheme. This is used, for example, by the traceback |
|
133 | # Default color scheme. This is used, for example, by the traceback | |
134 | # formatter. When running in an actual IPython instance, the user's rc.colors |
|
134 | # formatter. When running in an actual IPython instance, the user's rc.colors | |
135 | # value is used, but having a module global makes this functionality available |
|
135 | # value is used, but having a module global makes this functionality available | |
136 | # to users of ultratb who are NOT running inside ipython. |
|
136 | # to users of ultratb who are NOT running inside ipython. | |
137 | DEFAULT_SCHEME = 'NoColor' |
|
137 | DEFAULT_SCHEME = 'NoColor' | |
138 |
|
138 | |||
139 | # --------------------------------------------------------------------------- |
|
139 | # --------------------------------------------------------------------------- | |
140 | # Code begins |
|
140 | # Code begins | |
141 |
|
141 | |||
142 | # Utility functions |
|
142 | # Utility functions | |
143 | def inspect_error(): |
|
143 | def inspect_error(): | |
144 | """Print a message about internal inspect errors. |
|
144 | """Print a message about internal inspect errors. | |
145 |
|
145 | |||
146 | These are unfortunately quite common.""" |
|
146 | These are unfortunately quite common.""" | |
147 |
|
147 | |||
148 | error('Internal Python error in the inspect module.\n' |
|
148 | error('Internal Python error in the inspect module.\n' | |
149 | 'Below is the traceback from this internal error.\n') |
|
149 | 'Below is the traceback from this internal error.\n') | |
150 |
|
150 | |||
151 |
|
151 | |||
152 | # This function is a monkeypatch we apply to the Python inspect module. We have |
|
152 | # This function is a monkeypatch we apply to the Python inspect module. We have | |
153 | # now found when it's needed (see discussion on issue gh-1456), and we have a |
|
153 | # now found when it's needed (see discussion on issue gh-1456), and we have a | |
154 | # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if |
|
154 | # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if | |
155 | # the monkeypatch is not applied. TK, Aug 2012. |
|
155 | # the monkeypatch is not applied. TK, Aug 2012. | |
156 | def findsource(object): |
|
156 | def findsource(object): | |
157 | """Return the entire source file and starting line number for an object. |
|
157 | """Return the entire source file and starting line number for an object. | |
158 |
|
158 | |||
159 | The argument may be a module, class, method, function, traceback, frame, |
|
159 | The argument may be a module, class, method, function, traceback, frame, | |
160 | or code object. The source code is returned as a list of all the lines |
|
160 | or code object. The source code is returned as a list of all the lines | |
161 | in the file and the line number indexes a line in that list. An IOError |
|
161 | in the file and the line number indexes a line in that list. An IOError | |
162 | is raised if the source code cannot be retrieved. |
|
162 | is raised if the source code cannot be retrieved. | |
163 |
|
163 | |||
164 | FIXED version with which we monkeypatch the stdlib to work around a bug.""" |
|
164 | FIXED version with which we monkeypatch the stdlib to work around a bug.""" | |
165 |
|
165 | |||
166 | file = getsourcefile(object) or getfile(object) |
|
166 | file = getsourcefile(object) or getfile(object) | |
167 | # If the object is a frame, then trying to get the globals dict from its |
|
167 | # If the object is a frame, then trying to get the globals dict from its | |
168 | # module won't work. Instead, the frame object itself has the globals |
|
168 | # module won't work. Instead, the frame object itself has the globals | |
169 | # dictionary. |
|
169 | # dictionary. | |
170 | globals_dict = None |
|
170 | globals_dict = None | |
171 | if inspect.isframe(object): |
|
171 | if inspect.isframe(object): | |
172 | # XXX: can this ever be false? |
|
172 | # XXX: can this ever be false? | |
173 | globals_dict = object.f_globals |
|
173 | globals_dict = object.f_globals | |
174 | else: |
|
174 | else: | |
175 | module = getmodule(object, file) |
|
175 | module = getmodule(object, file) | |
176 | if module: |
|
176 | if module: | |
177 | globals_dict = module.__dict__ |
|
177 | globals_dict = module.__dict__ | |
178 | lines = linecache.getlines(file, globals_dict) |
|
178 | lines = linecache.getlines(file, globals_dict) | |
179 | if not lines: |
|
179 | if not lines: | |
180 | raise IOError('could not get source code') |
|
180 | raise IOError('could not get source code') | |
181 |
|
181 | |||
182 | if ismodule(object): |
|
182 | if ismodule(object): | |
183 | return lines, 0 |
|
183 | return lines, 0 | |
184 |
|
184 | |||
185 | if isclass(object): |
|
185 | if isclass(object): | |
186 | name = object.__name__ |
|
186 | name = object.__name__ | |
187 | pat = re.compile(r'^(\s*)class\s*' + name + r'\b') |
|
187 | pat = re.compile(r'^(\s*)class\s*' + name + r'\b') | |
188 | # make some effort to find the best matching class definition: |
|
188 | # make some effort to find the best matching class definition: | |
189 | # use the one with the least indentation, which is the one |
|
189 | # use the one with the least indentation, which is the one | |
190 | # that's most probably not inside a function definition. |
|
190 | # that's most probably not inside a function definition. | |
191 | candidates = [] |
|
191 | candidates = [] | |
192 | for i, line in enumerate(lines): |
|
192 | for i, line in enumerate(lines): | |
193 | match = pat.match(line) |
|
193 | match = pat.match(line) | |
194 | if match: |
|
194 | if match: | |
195 | # if it's at toplevel, it's already the best one |
|
195 | # if it's at toplevel, it's already the best one | |
196 | if line[0] == 'c': |
|
196 | if line[0] == 'c': | |
197 | return lines, i |
|
197 | return lines, i | |
198 | # else add whitespace to candidate list |
|
198 | # else add whitespace to candidate list | |
199 | candidates.append((match.group(1), i)) |
|
199 | candidates.append((match.group(1), i)) | |
200 | if candidates: |
|
200 | if candidates: | |
201 | # this will sort by whitespace, and by line number, |
|
201 | # this will sort by whitespace, and by line number, | |
202 | # less whitespace first |
|
202 | # less whitespace first | |
203 | candidates.sort() |
|
203 | candidates.sort() | |
204 | return lines, candidates[0][1] |
|
204 | return lines, candidates[0][1] | |
205 | else: |
|
205 | else: | |
206 | raise IOError('could not find class definition') |
|
206 | raise IOError('could not find class definition') | |
207 |
|
207 | |||
208 | if ismethod(object): |
|
208 | if ismethod(object): | |
209 | object = object.__func__ |
|
209 | object = object.__func__ | |
210 | if isfunction(object): |
|
210 | if isfunction(object): | |
211 | object = object.__code__ |
|
211 | object = object.__code__ | |
212 | if istraceback(object): |
|
212 | if istraceback(object): | |
213 | object = object.tb_frame |
|
213 | object = object.tb_frame | |
214 | if isframe(object): |
|
214 | if isframe(object): | |
215 | object = object.f_code |
|
215 | object = object.f_code | |
216 | if iscode(object): |
|
216 | if iscode(object): | |
217 | if not hasattr(object, 'co_firstlineno'): |
|
217 | if not hasattr(object, 'co_firstlineno'): | |
218 | raise IOError('could not find function definition') |
|
218 | raise IOError('could not find function definition') | |
219 | pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') |
|
219 | pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') | |
220 | pmatch = pat.match |
|
220 | pmatch = pat.match | |
221 | # fperez - fix: sometimes, co_firstlineno can give a number larger than |
|
221 | # fperez - fix: sometimes, co_firstlineno can give a number larger than | |
222 | # the length of lines, which causes an error. Safeguard against that. |
|
222 | # the length of lines, which causes an error. Safeguard against that. | |
223 | lnum = min(object.co_firstlineno, len(lines)) - 1 |
|
223 | lnum = min(object.co_firstlineno, len(lines)) - 1 | |
224 | while lnum > 0: |
|
224 | while lnum > 0: | |
225 | if pmatch(lines[lnum]): |
|
225 | if pmatch(lines[lnum]): | |
226 | break |
|
226 | break | |
227 | lnum -= 1 |
|
227 | lnum -= 1 | |
228 |
|
228 | |||
229 | return lines, lnum |
|
229 | return lines, lnum | |
230 | raise IOError('could not find code object') |
|
230 | raise IOError('could not find code object') | |
231 |
|
231 | |||
232 |
|
232 | |||
233 | # This is a patched version of inspect.getargs that applies the (unmerged) |
|
233 | # This is a patched version of inspect.getargs that applies the (unmerged) | |
234 | # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes |
|
234 | # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes | |
235 | # https://github.com/ipython/ipython/issues/8205 and |
|
235 | # https://github.com/ipython/ipython/issues/8205 and | |
236 | # https://github.com/ipython/ipython/issues/8293 |
|
236 | # https://github.com/ipython/ipython/issues/8293 | |
237 | def getargs(co): |
|
237 | def getargs(co): | |
238 | """Get information about the arguments accepted by a code object. |
|
238 | """Get information about the arguments accepted by a code object. | |
239 |
|
239 | |||
240 | Three things are returned: (args, varargs, varkw), where 'args' is |
|
240 | Three things are returned: (args, varargs, varkw), where 'args' is | |
241 | a list of argument names (possibly containing nested lists), and |
|
241 | a list of argument names (possibly containing nested lists), and | |
242 | 'varargs' and 'varkw' are the names of the * and ** arguments or None.""" |
|
242 | 'varargs' and 'varkw' are the names of the * and ** arguments or None.""" | |
243 | if not iscode(co): |
|
243 | if not iscode(co): | |
244 | raise TypeError('{!r} is not a code object'.format(co)) |
|
244 | raise TypeError('{!r} is not a code object'.format(co)) | |
245 |
|
245 | |||
246 | nargs = co.co_argcount |
|
246 | nargs = co.co_argcount | |
247 | names = co.co_varnames |
|
247 | names = co.co_varnames | |
248 | args = list(names[:nargs]) |
|
248 | args = list(names[:nargs]) | |
249 | step = 0 |
|
249 | step = 0 | |
250 |
|
250 | |||
251 | # The following acrobatics are for anonymous (tuple) arguments. |
|
251 | # The following acrobatics are for anonymous (tuple) arguments. | |
252 | for i in range(nargs): |
|
252 | for i in range(nargs): | |
253 | if args[i][:1] in ('', '.'): |
|
253 | if args[i][:1] in ('', '.'): | |
254 | stack, remain, count = [], [], [] |
|
254 | stack, remain, count = [], [], [] | |
255 | while step < len(co.co_code): |
|
255 | while step < len(co.co_code): | |
256 | op = ord(co.co_code[step]) |
|
256 | op = ord(co.co_code[step]) | |
257 | step = step + 1 |
|
257 | step = step + 1 | |
258 | if op >= dis.HAVE_ARGUMENT: |
|
258 | if op >= dis.HAVE_ARGUMENT: | |
259 | opname = dis.opname[op] |
|
259 | opname = dis.opname[op] | |
260 | value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256 |
|
260 | value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256 | |
261 | step = step + 2 |
|
261 | step = step + 2 | |
262 | if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'): |
|
262 | if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'): | |
263 | remain.append(value) |
|
263 | remain.append(value) | |
264 | count.append(value) |
|
264 | count.append(value) | |
265 | elif opname in ('STORE_FAST', 'STORE_DEREF'): |
|
265 | elif opname in ('STORE_FAST', 'STORE_DEREF'): | |
266 | if op in dis.haslocal: |
|
266 | if op in dis.haslocal: | |
267 | stack.append(co.co_varnames[value]) |
|
267 | stack.append(co.co_varnames[value]) | |
268 | elif op in dis.hasfree: |
|
268 | elif op in dis.hasfree: | |
269 | stack.append((co.co_cellvars + co.co_freevars)[value]) |
|
269 | stack.append((co.co_cellvars + co.co_freevars)[value]) | |
270 | # Special case for sublists of length 1: def foo((bar)) |
|
270 | # Special case for sublists of length 1: def foo((bar)) | |
271 | # doesn't generate the UNPACK_TUPLE bytecode, so if |
|
271 | # doesn't generate the UNPACK_TUPLE bytecode, so if | |
272 | # `remain` is empty here, we have such a sublist. |
|
272 | # `remain` is empty here, we have such a sublist. | |
273 | if not remain: |
|
273 | if not remain: | |
274 | stack[0] = [stack[0]] |
|
274 | stack[0] = [stack[0]] | |
275 | break |
|
275 | break | |
276 | else: |
|
276 | else: | |
277 | remain[-1] = remain[-1] - 1 |
|
277 | remain[-1] = remain[-1] - 1 | |
278 | while remain[-1] == 0: |
|
278 | while remain[-1] == 0: | |
279 | remain.pop() |
|
279 | remain.pop() | |
280 | size = count.pop() |
|
280 | size = count.pop() | |
281 | stack[-size:] = [stack[-size:]] |
|
281 | stack[-size:] = [stack[-size:]] | |
282 | if not remain: |
|
282 | if not remain: | |
283 | break |
|
283 | break | |
284 | remain[-1] = remain[-1] - 1 |
|
284 | remain[-1] = remain[-1] - 1 | |
285 | if not remain: |
|
285 | if not remain: | |
286 | break |
|
286 | break | |
287 | args[i] = stack[0] |
|
287 | args[i] = stack[0] | |
288 |
|
288 | |||
289 | varargs = None |
|
289 | varargs = None | |
290 | if co.co_flags & inspect.CO_VARARGS: |
|
290 | if co.co_flags & inspect.CO_VARARGS: | |
291 | varargs = co.co_varnames[nargs] |
|
291 | varargs = co.co_varnames[nargs] | |
292 | nargs = nargs + 1 |
|
292 | nargs = nargs + 1 | |
293 | varkw = None |
|
293 | varkw = None | |
294 | if co.co_flags & inspect.CO_VARKEYWORDS: |
|
294 | if co.co_flags & inspect.CO_VARKEYWORDS: | |
295 | varkw = co.co_varnames[nargs] |
|
295 | varkw = co.co_varnames[nargs] | |
296 | return inspect.Arguments(args, varargs, varkw) |
|
296 | return inspect.Arguments(args, varargs, varkw) | |
297 |
|
297 | |||
298 |
|
298 | |||
299 | # Monkeypatch inspect to apply our bugfix. |
|
299 | # Monkeypatch inspect to apply our bugfix. | |
300 | def with_patch_inspect(f): |
|
300 | def with_patch_inspect(f): | |
301 | """ |
|
301 | """ | |
302 | Deprecated since IPython 6.0 |
|
302 | Deprecated since IPython 6.0 | |
303 | decorator for monkeypatching inspect.findsource |
|
303 | decorator for monkeypatching inspect.findsource | |
304 | """ |
|
304 | """ | |
305 |
|
305 | |||
306 | def wrapped(*args, **kwargs): |
|
306 | def wrapped(*args, **kwargs): | |
307 | save_findsource = inspect.findsource |
|
307 | save_findsource = inspect.findsource | |
308 | save_getargs = inspect.getargs |
|
308 | save_getargs = inspect.getargs | |
309 | inspect.findsource = findsource |
|
309 | inspect.findsource = findsource | |
310 | inspect.getargs = getargs |
|
310 | inspect.getargs = getargs | |
311 | try: |
|
311 | try: | |
312 | return f(*args, **kwargs) |
|
312 | return f(*args, **kwargs) | |
313 | finally: |
|
313 | finally: | |
314 | inspect.findsource = save_findsource |
|
314 | inspect.findsource = save_findsource | |
315 | inspect.getargs = save_getargs |
|
315 | inspect.getargs = save_getargs | |
316 |
|
316 | |||
317 | return wrapped |
|
317 | return wrapped | |
318 |
|
318 | |||
319 |
|
319 | |||
320 | def fix_frame_records_filenames(records): |
|
320 | def fix_frame_records_filenames(records): | |
321 | """Try to fix the filenames in each record from inspect.getinnerframes(). |
|
321 | """Try to fix the filenames in each record from inspect.getinnerframes(). | |
322 |
|
322 | |||
323 | Particularly, modules loaded from within zip files have useless filenames |
|
323 | Particularly, modules loaded from within zip files have useless filenames | |
324 | attached to their code object, and inspect.getinnerframes() just uses it. |
|
324 | attached to their code object, and inspect.getinnerframes() just uses it. | |
325 | """ |
|
325 | """ | |
326 | fixed_records = [] |
|
326 | fixed_records = [] | |
327 | for frame, filename, line_no, func_name, lines, index in records: |
|
327 | for frame, filename, line_no, func_name, lines, index in records: | |
328 | # Look inside the frame's globals dictionary for __file__, |
|
328 | # Look inside the frame's globals dictionary for __file__, | |
329 | # which should be better. However, keep Cython filenames since |
|
329 | # which should be better. However, keep Cython filenames since | |
330 | # we prefer the source filenames over the compiled .so file. |
|
330 | # we prefer the source filenames over the compiled .so file. | |
331 | if not filename.endswith(('.pyx', '.pxd', '.pxi')): |
|
331 | if not filename.endswith(('.pyx', '.pxd', '.pxi')): | |
332 | better_fn = frame.f_globals.get('__file__', None) |
|
332 | better_fn = frame.f_globals.get('__file__', None) | |
333 | if isinstance(better_fn, str): |
|
333 | if isinstance(better_fn, str): | |
334 | # Check the type just in case someone did something weird with |
|
334 | # Check the type just in case someone did something weird with | |
335 | # __file__. It might also be None if the error occurred during |
|
335 | # __file__. It might also be None if the error occurred during | |
336 | # import. |
|
336 | # import. | |
337 | filename = better_fn |
|
337 | filename = better_fn | |
338 | fixed_records.append((frame, filename, line_no, func_name, lines, index)) |
|
338 | fixed_records.append((frame, filename, line_no, func_name, lines, index)) | |
339 | return fixed_records |
|
339 | return fixed_records | |
340 |
|
340 | |||
341 |
|
341 | |||
342 | @with_patch_inspect |
|
342 | @with_patch_inspect | |
343 | def _fixed_getinnerframes(etb, context=1, tb_offset=0): |
|
343 | def _fixed_getinnerframes(etb, context=1, tb_offset=0): | |
344 | LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 |
|
344 | LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 | |
345 |
|
345 | |||
346 | records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) |
|
346 | records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) | |
347 | # If the error is at the console, don't build any context, since it would |
|
347 | # If the error is at the console, don't build any context, since it would | |
348 | # otherwise produce 5 blank lines printed out (there is no file at the |
|
348 | # otherwise produce 5 blank lines printed out (there is no file at the | |
349 | # console) |
|
349 | # console) | |
350 | rec_check = records[tb_offset:] |
|
350 | rec_check = records[tb_offset:] | |
351 | try: |
|
351 | try: | |
352 | rname = rec_check[0][1] |
|
352 | rname = rec_check[0][1] | |
353 | if rname == '<ipython console>' or rname.endswith('<string>'): |
|
353 | if rname == '<ipython console>' or rname.endswith('<string>'): | |
354 | return rec_check |
|
354 | return rec_check | |
355 | except IndexError: |
|
355 | except IndexError: | |
356 | pass |
|
356 | pass | |
357 |
|
357 | |||
358 | aux = traceback.extract_tb(etb) |
|
358 | aux = traceback.extract_tb(etb) | |
359 | assert len(records) == len(aux) |
|
359 | assert len(records) == len(aux) | |
360 | for i, (file, lnum, _, _) in enumerate(aux): |
|
360 | for i, (file, lnum, _, _) in enumerate(aux): | |
361 | maybeStart = lnum - 1 - context // 2 |
|
361 | maybeStart = lnum - 1 - context // 2 | |
362 | start = max(maybeStart, 0) |
|
362 | start = max(maybeStart, 0) | |
363 | end = start + context |
|
363 | end = start + context | |
364 | lines = linecache.getlines(file)[start:end] |
|
364 | lines = linecache.getlines(file)[start:end] | |
365 | buf = list(records[i]) |
|
365 | buf = list(records[i]) | |
366 | buf[LNUM_POS] = lnum |
|
366 | buf[LNUM_POS] = lnum | |
367 | buf[INDEX_POS] = lnum - 1 - start |
|
367 | buf[INDEX_POS] = lnum - 1 - start | |
368 | buf[LINES_POS] = lines |
|
368 | buf[LINES_POS] = lines | |
369 | records[i] = tuple(buf) |
|
369 | records[i] = tuple(buf) | |
370 | return records[tb_offset:] |
|
370 | return records[tb_offset:] | |
371 |
|
371 | |||
372 | # Helper function -- largely belongs to VerboseTB, but we need the same |
|
372 | # Helper function -- largely belongs to VerboseTB, but we need the same | |
373 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they |
|
373 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they | |
374 | # can be recognized properly by ipython.el's py-traceback-line-re |
|
374 | # can be recognized properly by ipython.el's py-traceback-line-re | |
375 | # (SyntaxErrors have to be treated specially because they have no traceback) |
|
375 | # (SyntaxErrors have to be treated specially because they have no traceback) | |
376 |
|
376 | |||
377 |
|
377 | |||
378 | def _format_traceback_lines(lnum, index, lines, Colors, lvals, _line_format): |
|
378 | def _format_traceback_lines(lnum, index, lines, Colors, lvals, _line_format): | |
379 | """ |
|
379 | """ | |
380 | Format tracebacks lines with pointing arrow, leading numbers... |
|
380 | Format tracebacks lines with pointing arrow, leading numbers... | |
381 |
|
381 | |||
382 | Parameters |
|
382 | Parameters | |
383 | ========== |
|
383 | ========== | |
384 |
|
384 | |||
385 | lnum: int |
|
385 | lnum: int | |
386 | index: int |
|
386 | index: int | |
387 | lines: list[string] |
|
387 | lines: list[string] | |
388 | Colors: |
|
388 | Colors: | |
389 | ColorScheme used. |
|
389 | ColorScheme used. | |
390 | lvals: bytes |
|
390 | lvals: bytes | |
391 | Values of local variables, already colored, to inject just after the error line. |
|
391 | Values of local variables, already colored, to inject just after the error line. | |
392 | _line_format: f (str) -> (str, bool) |
|
392 | _line_format: f (str) -> (str, bool) | |
393 | return (colorized version of str, failure to do so) |
|
393 | return (colorized version of str, failure to do so) | |
394 | """ |
|
394 | """ | |
395 | numbers_width = INDENT_SIZE - 1 |
|
395 | numbers_width = INDENT_SIZE - 1 | |
396 | res = [] |
|
396 | res = [] | |
397 |
|
397 | |||
398 | for i,line in enumerate(lines, lnum-index): |
|
398 | for i,line in enumerate(lines, lnum-index): | |
399 | line = py3compat.cast_unicode(line) |
|
399 | line = py3compat.cast_unicode(line) | |
400 |
|
400 | |||
401 | new_line, err = _line_format(line, 'str') |
|
401 | new_line, err = _line_format(line, 'str') | |
402 | if not err: |
|
402 | if not err: | |
403 | line = new_line |
|
403 | line = new_line | |
404 |
|
404 | |||
405 | if i == lnum: |
|
405 | if i == lnum: | |
406 | # This is the line with the error |
|
406 | # This is the line with the error | |
407 | pad = numbers_width - len(str(i)) |
|
407 | pad = numbers_width - len(str(i)) | |
408 | num = '%s%s' % (debugger.make_arrow(pad), str(lnum)) |
|
408 | num = '%s%s' % (debugger.make_arrow(pad), str(lnum)) | |
409 | line = '%s%s%s %s%s' % (Colors.linenoEm, num, |
|
409 | line = '%s%s%s %s%s' % (Colors.linenoEm, num, | |
410 | Colors.line, line, Colors.Normal) |
|
410 | Colors.line, line, Colors.Normal) | |
411 | else: |
|
411 | else: | |
412 | num = '%*s' % (numbers_width, i) |
|
412 | num = '%*s' % (numbers_width, i) | |
413 | line = '%s%s%s %s' % (Colors.lineno, num, |
|
413 | line = '%s%s%s %s' % (Colors.lineno, num, | |
414 | Colors.Normal, line) |
|
414 | Colors.Normal, line) | |
415 |
|
415 | |||
416 | res.append(line) |
|
416 | res.append(line) | |
417 | if lvals and i == lnum: |
|
417 | if lvals and i == lnum: | |
418 | res.append(lvals + '\n') |
|
418 | res.append(lvals + '\n') | |
419 | return res |
|
419 | return res | |
420 |
|
420 | |||
421 | def is_recursion_error(etype, value, records): |
|
421 | def is_recursion_error(etype, value, records): | |
422 | try: |
|
422 | try: | |
423 | # RecursionError is new in Python 3.5 |
|
423 | # RecursionError is new in Python 3.5 | |
424 | recursion_error_type = RecursionError |
|
424 | recursion_error_type = RecursionError | |
425 | except NameError: |
|
425 | except NameError: | |
426 | recursion_error_type = RuntimeError |
|
426 | recursion_error_type = RuntimeError | |
427 |
|
427 | |||
428 | # The default recursion limit is 1000, but some of that will be taken up |
|
428 | # The default recursion limit is 1000, but some of that will be taken up | |
429 | # by stack frames in IPython itself. >500 frames probably indicates |
|
429 | # by stack frames in IPython itself. >500 frames probably indicates | |
430 | # a recursion error. |
|
430 | # a recursion error. | |
431 | return (etype is recursion_error_type) \ |
|
431 | return (etype is recursion_error_type) \ | |
432 | and "recursion" in str(value).lower() \ |
|
432 | and "recursion" in str(value).lower() \ | |
433 | and len(records) > 500 |
|
433 | and len(records) > 500 | |
434 |
|
434 | |||
435 | def find_recursion(etype, value, records): |
|
435 | def find_recursion(etype, value, records): | |
436 | """Identify the repeating stack frames from a RecursionError traceback |
|
436 | """Identify the repeating stack frames from a RecursionError traceback | |
437 |
|
437 | |||
438 | 'records' is a list as returned by VerboseTB.get_records() |
|
438 | 'records' is a list as returned by VerboseTB.get_records() | |
439 |
|
439 | |||
440 | Returns (last_unique, repeat_length) |
|
440 | Returns (last_unique, repeat_length) | |
441 | """ |
|
441 | """ | |
442 | # This involves a bit of guesswork - we want to show enough of the traceback |
|
442 | # This involves a bit of guesswork - we want to show enough of the traceback | |
443 | # to indicate where the recursion is occurring. We guess that the innermost |
|
443 | # to indicate where the recursion is occurring. We guess that the innermost | |
444 | # quarter of the traceback (250 frames by default) is repeats, and find the |
|
444 | # quarter of the traceback (250 frames by default) is repeats, and find the | |
445 | # first frame (from in to out) that looks different. |
|
445 | # first frame (from in to out) that looks different. | |
446 | if not is_recursion_error(etype, value, records): |
|
446 | if not is_recursion_error(etype, value, records): | |
447 | return len(records), 0 |
|
447 | return len(records), 0 | |
448 |
|
448 | |||
449 | # Select filename, lineno, func_name to track frames with |
|
449 | # Select filename, lineno, func_name to track frames with | |
450 | records = [r[1:4] for r in records] |
|
450 | records = [r[1:4] for r in records] | |
451 | inner_frames = records[-(len(records)//4):] |
|
451 | inner_frames = records[-(len(records)//4):] | |
452 | frames_repeated = set(inner_frames) |
|
452 | frames_repeated = set(inner_frames) | |
453 |
|
453 | |||
454 | last_seen_at = {} |
|
454 | last_seen_at = {} | |
455 | longest_repeat = 0 |
|
455 | longest_repeat = 0 | |
456 | i = len(records) |
|
456 | i = len(records) | |
457 | for frame in reversed(records): |
|
457 | for frame in reversed(records): | |
458 | i -= 1 |
|
458 | i -= 1 | |
459 | if frame not in frames_repeated: |
|
459 | if frame not in frames_repeated: | |
460 | last_unique = i |
|
460 | last_unique = i | |
461 | break |
|
461 | break | |
462 |
|
462 | |||
463 | if frame in last_seen_at: |
|
463 | if frame in last_seen_at: | |
464 | distance = last_seen_at[frame] - i |
|
464 | distance = last_seen_at[frame] - i | |
465 | longest_repeat = max(longest_repeat, distance) |
|
465 | longest_repeat = max(longest_repeat, distance) | |
466 |
|
466 | |||
467 | last_seen_at[frame] = i |
|
467 | last_seen_at[frame] = i | |
468 | else: |
|
468 | else: | |
469 | last_unique = 0 # The whole traceback was recursion |
|
469 | last_unique = 0 # The whole traceback was recursion | |
470 |
|
470 | |||
471 | return last_unique, longest_repeat |
|
471 | return last_unique, longest_repeat | |
472 |
|
472 | |||
473 | #--------------------------------------------------------------------------- |
|
473 | #--------------------------------------------------------------------------- | |
474 | # Module classes |
|
474 | # Module classes | |
475 | class TBTools(colorable.Colorable): |
|
475 | class TBTools(colorable.Colorable): | |
476 | """Basic tools used by all traceback printer classes.""" |
|
476 | """Basic tools used by all traceback printer classes.""" | |
477 |
|
477 | |||
478 | # Number of frames to skip when reporting tracebacks |
|
478 | # Number of frames to skip when reporting tracebacks | |
479 | tb_offset = 0 |
|
479 | tb_offset = 0 | |
480 |
|
480 | |||
481 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None): |
|
481 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None): | |
482 | # Whether to call the interactive pdb debugger after printing |
|
482 | # Whether to call the interactive pdb debugger after printing | |
483 | # tracebacks or not |
|
483 | # tracebacks or not | |
484 | super(TBTools, self).__init__(parent=parent, config=config) |
|
484 | super(TBTools, self).__init__(parent=parent, config=config) | |
485 | self.call_pdb = call_pdb |
|
485 | self.call_pdb = call_pdb | |
486 |
|
486 | |||
487 | # Output stream to write to. Note that we store the original value in |
|
487 | # Output stream to write to. Note that we store the original value in | |
488 | # a private attribute and then make the public ostream a property, so |
|
488 | # a private attribute and then make the public ostream a property, so | |
489 | # that we can delay accessing sys.stdout until runtime. The way |
|
489 | # that we can delay accessing sys.stdout until runtime. The way | |
490 | # things are written now, the sys.stdout object is dynamically managed |
|
490 | # things are written now, the sys.stdout object is dynamically managed | |
491 | # so a reference to it should NEVER be stored statically. This |
|
491 | # so a reference to it should NEVER be stored statically. This | |
492 | # property approach confines this detail to a single location, and all |
|
492 | # property approach confines this detail to a single location, and all | |
493 | # subclasses can simply access self.ostream for writing. |
|
493 | # subclasses can simply access self.ostream for writing. | |
494 | self._ostream = ostream |
|
494 | self._ostream = ostream | |
495 |
|
495 | |||
496 | # Create color table |
|
496 | # Create color table | |
497 | self.color_scheme_table = exception_colors() |
|
497 | self.color_scheme_table = exception_colors() | |
498 |
|
498 | |||
499 | self.set_colors(color_scheme) |
|
499 | self.set_colors(color_scheme) | |
500 | self.old_scheme = color_scheme # save initial value for toggles |
|
500 | self.old_scheme = color_scheme # save initial value for toggles | |
501 |
|
501 | |||
502 | if call_pdb: |
|
502 | if call_pdb: | |
503 | self.pdb = debugger.Pdb() |
|
503 | self.pdb = debugger.Pdb() | |
504 | else: |
|
504 | else: | |
505 | self.pdb = None |
|
505 | self.pdb = None | |
506 |
|
506 | |||
507 | def _get_ostream(self): |
|
507 | def _get_ostream(self): | |
508 | """Output stream that exceptions are written to. |
|
508 | """Output stream that exceptions are written to. | |
509 |
|
509 | |||
510 | Valid values are: |
|
510 | Valid values are: | |
511 |
|
511 | |||
512 | - None: the default, which means that IPython will dynamically resolve |
|
512 | - None: the default, which means that IPython will dynamically resolve | |
513 | to sys.stdout. This ensures compatibility with most tools, including |
|
513 | to sys.stdout. This ensures compatibility with most tools, including | |
514 | Windows (where plain stdout doesn't recognize ANSI escapes). |
|
514 | Windows (where plain stdout doesn't recognize ANSI escapes). | |
515 |
|
515 | |||
516 | - Any object with 'write' and 'flush' attributes. |
|
516 | - Any object with 'write' and 'flush' attributes. | |
517 | """ |
|
517 | """ | |
518 | return sys.stdout if self._ostream is None else self._ostream |
|
518 | return sys.stdout if self._ostream is None else self._ostream | |
519 |
|
519 | |||
520 | def _set_ostream(self, val): |
|
520 | def _set_ostream(self, val): | |
521 | assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush')) |
|
521 | assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush')) | |
522 | self._ostream = val |
|
522 | self._ostream = val | |
523 |
|
523 | |||
524 | ostream = property(_get_ostream, _set_ostream) |
|
524 | ostream = property(_get_ostream, _set_ostream) | |
525 |
|
525 | |||
526 | def set_colors(self, *args, **kw): |
|
526 | def set_colors(self, *args, **kw): | |
527 | """Shorthand access to the color table scheme selector method.""" |
|
527 | """Shorthand access to the color table scheme selector method.""" | |
528 |
|
528 | |||
529 | # Set own color table |
|
529 | # Set own color table | |
530 | self.color_scheme_table.set_active_scheme(*args, **kw) |
|
530 | self.color_scheme_table.set_active_scheme(*args, **kw) | |
531 | # for convenience, set Colors to the active scheme |
|
531 | # for convenience, set Colors to the active scheme | |
532 | self.Colors = self.color_scheme_table.active_colors |
|
532 | self.Colors = self.color_scheme_table.active_colors | |
533 | # Also set colors of debugger |
|
533 | # Also set colors of debugger | |
534 | if hasattr(self, 'pdb') and self.pdb is not None: |
|
534 | if hasattr(self, 'pdb') and self.pdb is not None: | |
535 | self.pdb.set_colors(*args, **kw) |
|
535 | self.pdb.set_colors(*args, **kw) | |
536 |
|
536 | |||
537 | def color_toggle(self): |
|
537 | def color_toggle(self): | |
538 | """Toggle between the currently active color scheme and NoColor.""" |
|
538 | """Toggle between the currently active color scheme and NoColor.""" | |
539 |
|
539 | |||
540 | if self.color_scheme_table.active_scheme_name == 'NoColor': |
|
540 | if self.color_scheme_table.active_scheme_name == 'NoColor': | |
541 | self.color_scheme_table.set_active_scheme(self.old_scheme) |
|
541 | self.color_scheme_table.set_active_scheme(self.old_scheme) | |
542 | self.Colors = self.color_scheme_table.active_colors |
|
542 | self.Colors = self.color_scheme_table.active_colors | |
543 | else: |
|
543 | else: | |
544 | self.old_scheme = self.color_scheme_table.active_scheme_name |
|
544 | self.old_scheme = self.color_scheme_table.active_scheme_name | |
545 | self.color_scheme_table.set_active_scheme('NoColor') |
|
545 | self.color_scheme_table.set_active_scheme('NoColor') | |
546 | self.Colors = self.color_scheme_table.active_colors |
|
546 | self.Colors = self.color_scheme_table.active_colors | |
547 |
|
547 | |||
548 | def stb2text(self, stb): |
|
548 | def stb2text(self, stb): | |
549 | """Convert a structured traceback (a list) to a string.""" |
|
549 | """Convert a structured traceback (a list) to a string.""" | |
550 | return '\n'.join(stb) |
|
550 | return '\n'.join(stb) | |
551 |
|
551 | |||
552 | def text(self, etype, value, tb, tb_offset=None, context=5): |
|
552 | def text(self, etype, value, tb, tb_offset=None, context=5): | |
553 | """Return formatted traceback. |
|
553 | """Return formatted traceback. | |
554 |
|
554 | |||
555 | Subclasses may override this if they add extra arguments. |
|
555 | Subclasses may override this if they add extra arguments. | |
556 | """ |
|
556 | """ | |
557 | tb_list = self.structured_traceback(etype, value, tb, |
|
557 | tb_list = self.structured_traceback(etype, value, tb, | |
558 | tb_offset, context) |
|
558 | tb_offset, context) | |
559 | return self.stb2text(tb_list) |
|
559 | return self.stb2text(tb_list) | |
560 |
|
560 | |||
561 | def structured_traceback(self, etype, evalue, tb, tb_offset=None, |
|
561 | def structured_traceback(self, etype, evalue, tb, tb_offset=None, | |
562 | context=5, mode=None): |
|
562 | context=5, mode=None): | |
563 | """Return a list of traceback frames. |
|
563 | """Return a list of traceback frames. | |
564 |
|
564 | |||
565 | Must be implemented by each class. |
|
565 | Must be implemented by each class. | |
566 | """ |
|
566 | """ | |
567 | raise NotImplementedError() |
|
567 | raise NotImplementedError() | |
568 |
|
568 | |||
569 |
|
569 | |||
570 | #--------------------------------------------------------------------------- |
|
570 | #--------------------------------------------------------------------------- | |
571 | class ListTB(TBTools): |
|
571 | class ListTB(TBTools): | |
572 | """Print traceback information from a traceback list, with optional color. |
|
572 | """Print traceback information from a traceback list, with optional color. | |
573 |
|
573 | |||
574 | Calling requires 3 arguments: (etype, evalue, elist) |
|
574 | Calling requires 3 arguments: (etype, evalue, elist) | |
575 | as would be obtained by:: |
|
575 | as would be obtained by:: | |
576 |
|
576 | |||
577 | etype, evalue, tb = sys.exc_info() |
|
577 | etype, evalue, tb = sys.exc_info() | |
578 | if tb: |
|
578 | if tb: | |
579 | elist = traceback.extract_tb(tb) |
|
579 | elist = traceback.extract_tb(tb) | |
580 | else: |
|
580 | else: | |
581 | elist = None |
|
581 | elist = None | |
582 |
|
582 | |||
583 | It can thus be used by programs which need to process the traceback before |
|
583 | It can thus be used by programs which need to process the traceback before | |
584 | printing (such as console replacements based on the code module from the |
|
584 | printing (such as console replacements based on the code module from the | |
585 | standard library). |
|
585 | standard library). | |
586 |
|
586 | |||
587 | Because they are meant to be called without a full traceback (only a |
|
587 | Because they are meant to be called without a full traceback (only a | |
588 | list), instances of this class can't call the interactive pdb debugger.""" |
|
588 | list), instances of this class can't call the interactive pdb debugger.""" | |
589 |
|
589 | |||
590 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None): |
|
590 | def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None): | |
591 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, |
|
591 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, | |
592 | ostream=ostream, parent=parent,config=config) |
|
592 | ostream=ostream, parent=parent,config=config) | |
593 |
|
593 | |||
594 | def __call__(self, etype, value, elist): |
|
594 | def __call__(self, etype, value, elist): | |
595 | self.ostream.flush() |
|
595 | self.ostream.flush() | |
596 | self.ostream.write(self.text(etype, value, elist)) |
|
596 | self.ostream.write(self.text(etype, value, elist)) | |
597 | self.ostream.write('\n') |
|
597 | self.ostream.write('\n') | |
598 |
|
598 | |||
599 | def structured_traceback(self, etype, value, elist, tb_offset=None, |
|
599 | def structured_traceback(self, etype, value, elist, tb_offset=None, | |
600 | context=5): |
|
600 | context=5): | |
601 | """Return a color formatted string with the traceback info. |
|
601 | """Return a color formatted string with the traceback info. | |
602 |
|
602 | |||
603 | Parameters |
|
603 | Parameters | |
604 | ---------- |
|
604 | ---------- | |
605 | etype : exception type |
|
605 | etype : exception type | |
606 | Type of the exception raised. |
|
606 | Type of the exception raised. | |
607 |
|
607 | |||
608 | value : object |
|
608 | value : object | |
609 | Data stored in the exception |
|
609 | Data stored in the exception | |
610 |
|
610 | |||
611 | elist : list |
|
611 | elist : list | |
612 | List of frames, see class docstring for details. |
|
612 | List of frames, see class docstring for details. | |
613 |
|
613 | |||
614 | tb_offset : int, optional |
|
614 | tb_offset : int, optional | |
615 | Number of frames in the traceback to skip. If not given, the |
|
615 | Number of frames in the traceback to skip. If not given, the | |
616 | instance value is used (set in constructor). |
|
616 | instance value is used (set in constructor). | |
617 |
|
617 | |||
618 | context : int, optional |
|
618 | context : int, optional | |
619 | Number of lines of context information to print. |
|
619 | Number of lines of context information to print. | |
620 |
|
620 | |||
621 | Returns |
|
621 | Returns | |
622 | ------- |
|
622 | ------- | |
623 | String with formatted exception. |
|
623 | String with formatted exception. | |
624 | """ |
|
624 | """ | |
625 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
625 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
626 | Colors = self.Colors |
|
626 | Colors = self.Colors | |
627 | out_list = [] |
|
627 | out_list = [] | |
628 | if elist: |
|
628 | if elist: | |
629 |
|
629 | |||
630 | if tb_offset and len(elist) > tb_offset: |
|
630 | if tb_offset and len(elist) > tb_offset: | |
631 | elist = elist[tb_offset:] |
|
631 | elist = elist[tb_offset:] | |
632 |
|
632 | |||
633 | out_list.append('Traceback %s(most recent call last)%s:' % |
|
633 | out_list.append('Traceback %s(most recent call last)%s:' % | |
634 | (Colors.normalEm, Colors.Normal) + '\n') |
|
634 | (Colors.normalEm, Colors.Normal) + '\n') | |
635 | out_list.extend(self._format_list(elist)) |
|
635 | out_list.extend(self._format_list(elist)) | |
636 | # The exception info should be a single entry in the list. |
|
636 | # The exception info should be a single entry in the list. | |
637 | lines = ''.join(self._format_exception_only(etype, value)) |
|
637 | lines = ''.join(self._format_exception_only(etype, value)) | |
638 | out_list.append(lines) |
|
638 | out_list.append(lines) | |
639 |
|
639 | |||
640 | return out_list |
|
640 | return out_list | |
641 |
|
641 | |||
642 | def _format_list(self, extracted_list): |
|
642 | def _format_list(self, extracted_list): | |
643 | """Format a list of traceback entry tuples for printing. |
|
643 | """Format a list of traceback entry tuples for printing. | |
644 |
|
644 | |||
645 | Given a list of tuples as returned by extract_tb() or |
|
645 | Given a list of tuples as returned by extract_tb() or | |
646 | extract_stack(), return a list of strings ready for printing. |
|
646 | extract_stack(), return a list of strings ready for printing. | |
647 | Each string in the resulting list corresponds to the item with the |
|
647 | Each string in the resulting list corresponds to the item with the | |
648 | same index in the argument list. Each string ends in a newline; |
|
648 | same index in the argument list. Each string ends in a newline; | |
649 | the strings may contain internal newlines as well, for those items |
|
649 | the strings may contain internal newlines as well, for those items | |
650 | whose source text line is not None. |
|
650 | whose source text line is not None. | |
651 |
|
651 | |||
652 | Lifted almost verbatim from traceback.py |
|
652 | Lifted almost verbatim from traceback.py | |
653 | """ |
|
653 | """ | |
654 |
|
654 | |||
655 | Colors = self.Colors |
|
655 | Colors = self.Colors | |
656 | list = [] |
|
656 | list = [] | |
657 | for filename, lineno, name, line in extracted_list[:-1]: |
|
657 | for filename, lineno, name, line in extracted_list[:-1]: | |
658 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ |
|
658 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ | |
659 | (Colors.filename, filename, Colors.Normal, |
|
659 | (Colors.filename, filename, Colors.Normal, | |
660 | Colors.lineno, lineno, Colors.Normal, |
|
660 | Colors.lineno, lineno, Colors.Normal, | |
661 | Colors.name, name, Colors.Normal) |
|
661 | Colors.name, name, Colors.Normal) | |
662 | if line: |
|
662 | if line: | |
663 | item += ' %s\n' % line.strip() |
|
663 | item += ' %s\n' % line.strip() | |
664 | list.append(item) |
|
664 | list.append(item) | |
665 | # Emphasize the last entry |
|
665 | # Emphasize the last entry | |
666 | filename, lineno, name, line = extracted_list[-1] |
|
666 | filename, lineno, name, line = extracted_list[-1] | |
667 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ |
|
667 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ | |
668 | (Colors.normalEm, |
|
668 | (Colors.normalEm, | |
669 | Colors.filenameEm, filename, Colors.normalEm, |
|
669 | Colors.filenameEm, filename, Colors.normalEm, | |
670 | Colors.linenoEm, lineno, Colors.normalEm, |
|
670 | Colors.linenoEm, lineno, Colors.normalEm, | |
671 | Colors.nameEm, name, Colors.normalEm, |
|
671 | Colors.nameEm, name, Colors.normalEm, | |
672 | Colors.Normal) |
|
672 | Colors.Normal) | |
673 | if line: |
|
673 | if line: | |
674 | item += '%s %s%s\n' % (Colors.line, line.strip(), |
|
674 | item += '%s %s%s\n' % (Colors.line, line.strip(), | |
675 | Colors.Normal) |
|
675 | Colors.Normal) | |
676 | list.append(item) |
|
676 | list.append(item) | |
677 | return list |
|
677 | return list | |
678 |
|
678 | |||
679 | def _format_exception_only(self, etype, value): |
|
679 | def _format_exception_only(self, etype, value): | |
680 | """Format the exception part of a traceback. |
|
680 | """Format the exception part of a traceback. | |
681 |
|
681 | |||
682 | The arguments are the exception type and value such as given by |
|
682 | The arguments are the exception type and value such as given by | |
683 | sys.exc_info()[:2]. The return value is a list of strings, each ending |
|
683 | sys.exc_info()[:2]. The return value is a list of strings, each ending | |
684 | in a newline. Normally, the list contains a single string; however, |
|
684 | in a newline. Normally, the list contains a single string; however, | |
685 | for SyntaxError exceptions, it contains several lines that (when |
|
685 | for SyntaxError exceptions, it contains several lines that (when | |
686 | printed) display detailed information about where the syntax error |
|
686 | printed) display detailed information about where the syntax error | |
687 | occurred. The message indicating which exception occurred is the |
|
687 | occurred. The message indicating which exception occurred is the | |
688 | always last string in the list. |
|
688 | always last string in the list. | |
689 |
|
689 | |||
690 | Also lifted nearly verbatim from traceback.py |
|
690 | Also lifted nearly verbatim from traceback.py | |
691 | """ |
|
691 | """ | |
692 | have_filedata = False |
|
692 | have_filedata = False | |
693 | Colors = self.Colors |
|
693 | Colors = self.Colors | |
694 | list = [] |
|
694 | list = [] | |
695 | stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal) |
|
695 | stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal) | |
696 | if value is None: |
|
696 | if value is None: | |
697 | # Not sure if this can still happen in Python 2.6 and above |
|
697 | # Not sure if this can still happen in Python 2.6 and above | |
698 | list.append(stype + '\n') |
|
698 | list.append(stype + '\n') | |
699 | else: |
|
699 | else: | |
700 | if issubclass(etype, SyntaxError): |
|
700 | if issubclass(etype, SyntaxError): | |
701 | have_filedata = True |
|
701 | have_filedata = True | |
702 | if not value.filename: value.filename = "<string>" |
|
702 | if not value.filename: value.filename = "<string>" | |
703 | if value.lineno: |
|
703 | if value.lineno: | |
704 | lineno = value.lineno |
|
704 | lineno = value.lineno | |
705 | textline = linecache.getline(value.filename, value.lineno) |
|
705 | textline = linecache.getline(value.filename, value.lineno) | |
706 | else: |
|
706 | else: | |
707 | lineno = 'unknown' |
|
707 | lineno = 'unknown' | |
708 | textline = '' |
|
708 | textline = '' | |
709 | list.append('%s File %s"%s"%s, line %s%s%s\n' % \ |
|
709 | list.append('%s File %s"%s"%s, line %s%s%s\n' % \ | |
710 | (Colors.normalEm, |
|
710 | (Colors.normalEm, | |
711 | Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm, |
|
711 | Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm, | |
712 | Colors.linenoEm, lineno, Colors.Normal )) |
|
712 | Colors.linenoEm, lineno, Colors.Normal )) | |
713 | if textline == '': |
|
713 | if textline == '': | |
714 | textline = py3compat.cast_unicode(value.text, "utf-8") |
|
714 | textline = py3compat.cast_unicode(value.text, "utf-8") | |
715 |
|
715 | |||
716 | if textline is not None: |
|
716 | if textline is not None: | |
717 | i = 0 |
|
717 | i = 0 | |
718 | while i < len(textline) and textline[i].isspace(): |
|
718 | while i < len(textline) and textline[i].isspace(): | |
719 | i += 1 |
|
719 | i += 1 | |
720 | list.append('%s %s%s\n' % (Colors.line, |
|
720 | list.append('%s %s%s\n' % (Colors.line, | |
721 | textline.strip(), |
|
721 | textline.strip(), | |
722 | Colors.Normal)) |
|
722 | Colors.Normal)) | |
723 | if value.offset is not None: |
|
723 | if value.offset is not None: | |
724 | s = ' ' |
|
724 | s = ' ' | |
725 | for c in textline[i:value.offset - 1]: |
|
725 | for c in textline[i:value.offset - 1]: | |
726 | if c.isspace(): |
|
726 | if c.isspace(): | |
727 | s += c |
|
727 | s += c | |
728 | else: |
|
728 | else: | |
729 | s += ' ' |
|
729 | s += ' ' | |
730 | list.append('%s%s^%s\n' % (Colors.caret, s, |
|
730 | list.append('%s%s^%s\n' % (Colors.caret, s, | |
731 | Colors.Normal)) |
|
731 | Colors.Normal)) | |
732 |
|
732 | |||
733 | try: |
|
733 | try: | |
734 | s = value.msg |
|
734 | s = value.msg | |
735 | except Exception: |
|
735 | except Exception: | |
736 | s = self._some_str(value) |
|
736 | s = self._some_str(value) | |
737 | if s: |
|
737 | if s: | |
738 | list.append('%s%s:%s %s\n' % (stype, Colors.excName, |
|
738 | list.append('%s%s:%s %s\n' % (stype, Colors.excName, | |
739 | Colors.Normal, s)) |
|
739 | Colors.Normal, s)) | |
740 | else: |
|
740 | else: | |
741 | list.append('%s\n' % stype) |
|
741 | list.append('%s\n' % stype) | |
742 |
|
742 | |||
743 | # sync with user hooks |
|
743 | # sync with user hooks | |
744 | if have_filedata: |
|
744 | if have_filedata: | |
745 | ipinst = get_ipython() |
|
745 | ipinst = get_ipython() | |
746 | if ipinst is not None: |
|
746 | if ipinst is not None: | |
747 | ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0) |
|
747 | ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0) | |
748 |
|
748 | |||
749 | return list |
|
749 | return list | |
750 |
|
750 | |||
751 | def get_exception_only(self, etype, value): |
|
751 | def get_exception_only(self, etype, value): | |
752 | """Only print the exception type and message, without a traceback. |
|
752 | """Only print the exception type and message, without a traceback. | |
753 |
|
753 | |||
754 | Parameters |
|
754 | Parameters | |
755 | ---------- |
|
755 | ---------- | |
756 | etype : exception type |
|
756 | etype : exception type | |
757 | value : exception value |
|
757 | value : exception value | |
758 | """ |
|
758 | """ | |
759 | return ListTB.structured_traceback(self, etype, value, []) |
|
759 | return ListTB.structured_traceback(self, etype, value, []) | |
760 |
|
760 | |||
761 | def show_exception_only(self, etype, evalue): |
|
761 | def show_exception_only(self, etype, evalue): | |
762 | """Only print the exception type and message, without a traceback. |
|
762 | """Only print the exception type and message, without a traceback. | |
763 |
|
763 | |||
764 | Parameters |
|
764 | Parameters | |
765 | ---------- |
|
765 | ---------- | |
766 | etype : exception type |
|
766 | etype : exception type | |
767 | value : exception value |
|
767 | value : exception value | |
768 | """ |
|
768 | """ | |
769 | # This method needs to use __call__ from *this* class, not the one from |
|
769 | # This method needs to use __call__ from *this* class, not the one from | |
770 | # a subclass whose signature or behavior may be different |
|
770 | # a subclass whose signature or behavior may be different | |
771 | ostream = self.ostream |
|
771 | ostream = self.ostream | |
772 | ostream.flush() |
|
772 | ostream.flush() | |
773 | ostream.write('\n'.join(self.get_exception_only(etype, evalue))) |
|
773 | ostream.write('\n'.join(self.get_exception_only(etype, evalue))) | |
774 | ostream.flush() |
|
774 | ostream.flush() | |
775 |
|
775 | |||
776 | def _some_str(self, value): |
|
776 | def _some_str(self, value): | |
777 | # Lifted from traceback.py |
|
777 | # Lifted from traceback.py | |
778 | try: |
|
778 | try: | |
779 | return py3compat.cast_unicode(str(value)) |
|
779 | return py3compat.cast_unicode(str(value)) | |
780 | except: |
|
780 | except: | |
781 | return u'<unprintable %s object>' % type(value).__name__ |
|
781 | return u'<unprintable %s object>' % type(value).__name__ | |
782 |
|
782 | |||
783 |
|
783 | |||
784 | #---------------------------------------------------------------------------- |
|
784 | #---------------------------------------------------------------------------- | |
785 | class VerboseTB(TBTools): |
|
785 | class VerboseTB(TBTools): | |
786 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead |
|
786 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead | |
787 | of HTML. Requires inspect and pydoc. Crazy, man. |
|
787 | of HTML. Requires inspect and pydoc. Crazy, man. | |
788 |
|
788 | |||
789 | Modified version which optionally strips the topmost entries from the |
|
789 | Modified version which optionally strips the topmost entries from the | |
790 | traceback, to be used with alternate interpreters (because their own code |
|
790 | traceback, to be used with alternate interpreters (because their own code | |
791 | would appear in the traceback).""" |
|
791 | would appear in the traceback).""" | |
792 |
|
792 | |||
793 | def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None, |
|
793 | def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None, | |
794 | tb_offset=0, long_header=False, include_vars=True, |
|
794 | tb_offset=0, long_header=False, include_vars=True, | |
795 | check_cache=None, debugger_cls = None, |
|
795 | check_cache=None, debugger_cls = None, | |
796 | parent=None, config=None): |
|
796 | parent=None, config=None): | |
797 | """Specify traceback offset, headers and color scheme. |
|
797 | """Specify traceback offset, headers and color scheme. | |
798 |
|
798 | |||
799 | Define how many frames to drop from the tracebacks. Calling it with |
|
799 | Define how many frames to drop from the tracebacks. Calling it with | |
800 | tb_offset=1 allows use of this handler in interpreters which will have |
|
800 | tb_offset=1 allows use of this handler in interpreters which will have | |
801 | their own code at the top of the traceback (VerboseTB will first |
|
801 | their own code at the top of the traceback (VerboseTB will first | |
802 | remove that frame before printing the traceback info).""" |
|
802 | remove that frame before printing the traceback info).""" | |
803 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, |
|
803 | TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, | |
804 | ostream=ostream, parent=parent, config=config) |
|
804 | ostream=ostream, parent=parent, config=config) | |
805 | self.tb_offset = tb_offset |
|
805 | self.tb_offset = tb_offset | |
806 | self.long_header = long_header |
|
806 | self.long_header = long_header | |
807 | self.include_vars = include_vars |
|
807 | self.include_vars = include_vars | |
808 | # By default we use linecache.checkcache, but the user can provide a |
|
808 | # By default we use linecache.checkcache, but the user can provide a | |
809 | # different check_cache implementation. This is used by the IPython |
|
809 | # different check_cache implementation. This is used by the IPython | |
810 | # kernel to provide tracebacks for interactive code that is cached, |
|
810 | # kernel to provide tracebacks for interactive code that is cached, | |
811 | # by a compiler instance that flushes the linecache but preserves its |
|
811 | # by a compiler instance that flushes the linecache but preserves its | |
812 | # own code cache. |
|
812 | # own code cache. | |
813 | if check_cache is None: |
|
813 | if check_cache is None: | |
814 | check_cache = linecache.checkcache |
|
814 | check_cache = linecache.checkcache | |
815 | self.check_cache = check_cache |
|
815 | self.check_cache = check_cache | |
816 |
|
816 | |||
817 | self.debugger_cls = debugger_cls or debugger.Pdb |
|
817 | self.debugger_cls = debugger_cls or debugger.Pdb | |
818 |
|
818 | |||
819 | def format_records(self, records, last_unique, recursion_repeat): |
|
819 | def format_records(self, records, last_unique, recursion_repeat): | |
820 | """Format the stack frames of the traceback""" |
|
820 | """Format the stack frames of the traceback""" | |
821 | frames = [] |
|
821 | frames = [] | |
822 | for r in records[:last_unique+recursion_repeat+1]: |
|
822 | for r in records[:last_unique+recursion_repeat+1]: | |
823 | #print '*** record:',file,lnum,func,lines,index # dbg |
|
823 | #print '*** record:',file,lnum,func,lines,index # dbg | |
824 | frames.append(self.format_record(*r)) |
|
824 | frames.append(self.format_record(*r)) | |
825 |
|
825 | |||
826 | if recursion_repeat: |
|
826 | if recursion_repeat: | |
827 | frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat) |
|
827 | frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat) | |
828 | frames.append(self.format_record(*records[last_unique+recursion_repeat+1])) |
|
828 | frames.append(self.format_record(*records[last_unique+recursion_repeat+1])) | |
829 |
|
829 | |||
830 | return frames |
|
830 | return frames | |
831 |
|
831 | |||
832 | def format_record(self, frame, file, lnum, func, lines, index): |
|
832 | def format_record(self, frame, file, lnum, func, lines, index): | |
833 | """Format a single stack frame""" |
|
833 | """Format a single stack frame""" | |
834 | Colors = self.Colors # just a shorthand + quicker name lookup |
|
834 | Colors = self.Colors # just a shorthand + quicker name lookup | |
835 | ColorsNormal = Colors.Normal # used a lot |
|
835 | ColorsNormal = Colors.Normal # used a lot | |
836 | col_scheme = self.color_scheme_table.active_scheme_name |
|
836 | col_scheme = self.color_scheme_table.active_scheme_name | |
837 | indent = ' ' * INDENT_SIZE |
|
837 | indent = ' ' * INDENT_SIZE | |
838 | em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal) |
|
838 | em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal) | |
839 | undefined = '%sundefined%s' % (Colors.em, ColorsNormal) |
|
839 | undefined = '%sundefined%s' % (Colors.em, ColorsNormal) | |
840 | tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal) |
|
840 | tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal) | |
841 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, |
|
841 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, | |
842 | ColorsNormal) |
|
842 | ColorsNormal) | |
843 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ |
|
843 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ | |
844 | (Colors.vName, Colors.valEm, ColorsNormal) |
|
844 | (Colors.vName, Colors.valEm, ColorsNormal) | |
845 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) |
|
845 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) | |
846 | tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, |
|
846 | tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, | |
847 | Colors.vName, ColorsNormal) |
|
847 | Colors.vName, ColorsNormal) | |
848 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) |
|
848 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) | |
849 |
|
849 | |||
850 | if not file: |
|
850 | if not file: | |
851 | file = '?' |
|
851 | file = '?' | |
852 | elif file.startswith(str("<")) and file.endswith(str(">")): |
|
852 | elif file.startswith(str("<")) and file.endswith(str(">")): | |
853 | # Not a real filename, no problem... |
|
853 | # Not a real filename, no problem... | |
854 | pass |
|
854 | pass | |
855 | elif not os.path.isabs(file): |
|
855 | elif not os.path.isabs(file): | |
856 | # Try to make the filename absolute by trying all |
|
856 | # Try to make the filename absolute by trying all | |
857 | # sys.path entries (which is also what linecache does) |
|
857 | # sys.path entries (which is also what linecache does) | |
858 | for dirname in sys.path: |
|
858 | for dirname in sys.path: | |
859 | try: |
|
859 | try: | |
860 | fullname = os.path.join(dirname, file) |
|
860 | fullname = os.path.join(dirname, file) | |
861 | if os.path.isfile(fullname): |
|
861 | if os.path.isfile(fullname): | |
862 | file = os.path.abspath(fullname) |
|
862 | file = os.path.abspath(fullname) | |
863 | break |
|
863 | break | |
864 | except Exception: |
|
864 | except Exception: | |
865 | # Just in case that sys.path contains very |
|
865 | # Just in case that sys.path contains very | |
866 | # strange entries... |
|
866 | # strange entries... | |
867 | pass |
|
867 | pass | |
868 |
|
868 | |||
869 | file = py3compat.cast_unicode(file, util_path.fs_encoding) |
|
869 | file = py3compat.cast_unicode(file, util_path.fs_encoding) | |
870 | link = tpl_link % util_path.compress_user(file) |
|
870 | link = tpl_link % util_path.compress_user(file) | |
871 | args, varargs, varkw, locals_ = inspect.getargvalues(frame) |
|
871 | args, varargs, varkw, locals_ = inspect.getargvalues(frame) | |
872 |
|
872 | |||
873 | if func == '?': |
|
873 | if func == '?': | |
874 | call = '' |
|
874 | call = '' | |
|
875 | elif func == '<module>': | |||
|
876 | call = tpl_call % (func, '') | |||
875 | else: |
|
877 | else: | |
876 | # Decide whether to include variable details or not |
|
878 | # Decide whether to include variable details or not | |
877 | var_repr = eqrepr if self.include_vars else nullrepr |
|
879 | var_repr = eqrepr if self.include_vars else nullrepr | |
878 | try: |
|
880 | try: | |
879 | call = tpl_call % (func, inspect.formatargvalues(args, |
|
881 | call = tpl_call % (func, inspect.formatargvalues(args, | |
880 | varargs, varkw, |
|
882 | varargs, varkw, | |
881 | locals_, formatvalue=var_repr)) |
|
883 | locals_, formatvalue=var_repr)) | |
882 | except KeyError: |
|
884 | except KeyError: | |
883 | # This happens in situations like errors inside generator |
|
885 | # This happens in situations like errors inside generator | |
884 | # expressions, where local variables are listed in the |
|
886 | # expressions, where local variables are listed in the | |
885 | # line, but can't be extracted from the frame. I'm not |
|
887 | # line, but can't be extracted from the frame. I'm not | |
886 | # 100% sure this isn't actually a bug in inspect itself, |
|
888 | # 100% sure this isn't actually a bug in inspect itself, | |
887 | # but since there's no info for us to compute with, the |
|
889 | # but since there's no info for us to compute with, the | |
888 | # best we can do is report the failure and move on. Here |
|
890 | # best we can do is report the failure and move on. Here | |
889 | # we must *not* call any traceback construction again, |
|
891 | # we must *not* call any traceback construction again, | |
890 | # because that would mess up use of %debug later on. So we |
|
892 | # because that would mess up use of %debug later on. So we | |
891 | # simply report the failure and move on. The only |
|
893 | # simply report the failure and move on. The only | |
892 | # limitation will be that this frame won't have locals |
|
894 | # limitation will be that this frame won't have locals | |
893 | # listed in the call signature. Quite subtle problem... |
|
895 | # listed in the call signature. Quite subtle problem... | |
894 | # I can't think of a good way to validate this in a unit |
|
896 | # I can't think of a good way to validate this in a unit | |
895 | # test, but running a script consisting of: |
|
897 | # test, but running a script consisting of: | |
896 | # dict( (k,v.strip()) for (k,v) in range(10) ) |
|
898 | # dict( (k,v.strip()) for (k,v) in range(10) ) | |
897 | # will illustrate the error, if this exception catch is |
|
899 | # will illustrate the error, if this exception catch is | |
898 | # disabled. |
|
900 | # disabled. | |
899 | call = tpl_call_fail % func |
|
901 | call = tpl_call_fail % func | |
900 |
|
902 | |||
901 | # Don't attempt to tokenize binary files. |
|
903 | # Don't attempt to tokenize binary files. | |
902 | if file.endswith(('.so', '.pyd', '.dll')): |
|
904 | if file.endswith(('.so', '.pyd', '.dll')): | |
903 | return '%s %s\n' % (link, call) |
|
905 | return '%s %s\n' % (link, call) | |
904 |
|
906 | |||
905 | elif file.endswith(('.pyc', '.pyo')): |
|
907 | elif file.endswith(('.pyc', '.pyo')): | |
906 | # Look up the corresponding source file. |
|
908 | # Look up the corresponding source file. | |
907 | try: |
|
909 | try: | |
908 | file = openpy.source_from_cache(file) |
|
910 | file = openpy.source_from_cache(file) | |
909 | except ValueError: |
|
911 | except ValueError: | |
910 | # Failed to get the source file for some reason |
|
912 | # Failed to get the source file for some reason | |
911 | # E.g. https://github.com/ipython/ipython/issues/9486 |
|
913 | # E.g. https://github.com/ipython/ipython/issues/9486 | |
912 | return '%s %s\n' % (link, call) |
|
914 | return '%s %s\n' % (link, call) | |
913 |
|
915 | |||
914 | def linereader(file=file, lnum=[lnum], getline=linecache.getline): |
|
916 | def linereader(file=file, lnum=[lnum], getline=linecache.getline): | |
915 | line = getline(file, lnum[0]) |
|
917 | line = getline(file, lnum[0]) | |
916 | lnum[0] += 1 |
|
918 | lnum[0] += 1 | |
917 | return line |
|
919 | return line | |
918 |
|
920 | |||
919 | # Build the list of names on this line of code where the exception |
|
921 | # Build the list of names on this line of code where the exception | |
920 | # occurred. |
|
922 | # occurred. | |
921 | try: |
|
923 | try: | |
922 | names = [] |
|
924 | names = [] | |
923 | name_cont = False |
|
925 | name_cont = False | |
924 |
|
926 | |||
925 | for token_type, token, start, end, line in generate_tokens(linereader): |
|
927 | for token_type, token, start, end, line in generate_tokens(linereader): | |
926 | # build composite names |
|
928 | # build composite names | |
927 | if token_type == tokenize.NAME and token not in keyword.kwlist: |
|
929 | if token_type == tokenize.NAME and token not in keyword.kwlist: | |
928 | if name_cont: |
|
930 | if name_cont: | |
929 | # Continuation of a dotted name |
|
931 | # Continuation of a dotted name | |
930 | try: |
|
932 | try: | |
931 | names[-1].append(token) |
|
933 | names[-1].append(token) | |
932 | except IndexError: |
|
934 | except IndexError: | |
933 | names.append([token]) |
|
935 | names.append([token]) | |
934 | name_cont = False |
|
936 | name_cont = False | |
935 | else: |
|
937 | else: | |
936 | # Regular new names. We append everything, the caller |
|
938 | # Regular new names. We append everything, the caller | |
937 | # will be responsible for pruning the list later. It's |
|
939 | # will be responsible for pruning the list later. It's | |
938 | # very tricky to try to prune as we go, b/c composite |
|
940 | # very tricky to try to prune as we go, b/c composite | |
939 | # names can fool us. The pruning at the end is easy |
|
941 | # names can fool us. The pruning at the end is easy | |
940 | # to do (or the caller can print a list with repeated |
|
942 | # to do (or the caller can print a list with repeated | |
941 | # names if so desired. |
|
943 | # names if so desired. | |
942 | names.append([token]) |
|
944 | names.append([token]) | |
943 | elif token == '.': |
|
945 | elif token == '.': | |
944 | name_cont = True |
|
946 | name_cont = True | |
945 | elif token_type == tokenize.NEWLINE: |
|
947 | elif token_type == tokenize.NEWLINE: | |
946 | break |
|
948 | break | |
947 |
|
949 | |||
948 | except (IndexError, UnicodeDecodeError, SyntaxError): |
|
950 | except (IndexError, UnicodeDecodeError, SyntaxError): | |
949 | # signals exit of tokenizer |
|
951 | # signals exit of tokenizer | |
950 | # SyntaxError can occur if the file is not actually Python |
|
952 | # SyntaxError can occur if the file is not actually Python | |
951 | # - see gh-6300 |
|
953 | # - see gh-6300 | |
952 | pass |
|
954 | pass | |
953 | except tokenize.TokenError as msg: |
|
955 | except tokenize.TokenError as msg: | |
954 | # Tokenizing may fail for various reasons, many of which are |
|
956 | # Tokenizing may fail for various reasons, many of which are | |
955 | # harmless. (A good example is when the line in question is the |
|
957 | # harmless. (A good example is when the line in question is the | |
956 | # close of a triple-quoted string, cf gh-6864). We don't want to |
|
958 | # close of a triple-quoted string, cf gh-6864). We don't want to | |
957 | # show this to users, but want make it available for debugging |
|
959 | # show this to users, but want make it available for debugging | |
958 | # purposes. |
|
960 | # purposes. | |
959 | _m = ("An unexpected error occurred while tokenizing input\n" |
|
961 | _m = ("An unexpected error occurred while tokenizing input\n" | |
960 | "The following traceback may be corrupted or invalid\n" |
|
962 | "The following traceback may be corrupted or invalid\n" | |
961 | "The error message is: %s\n" % msg) |
|
963 | "The error message is: %s\n" % msg) | |
962 | debug(_m) |
|
964 | debug(_m) | |
963 |
|
965 | |||
964 | # Join composite names (e.g. "dict.fromkeys") |
|
966 | # Join composite names (e.g. "dict.fromkeys") | |
965 | names = ['.'.join(n) for n in names] |
|
967 | names = ['.'.join(n) for n in names] | |
966 | # prune names list of duplicates, but keep the right order |
|
968 | # prune names list of duplicates, but keep the right order | |
967 | unique_names = uniq_stable(names) |
|
969 | unique_names = uniq_stable(names) | |
968 |
|
970 | |||
969 | # Start loop over vars |
|
971 | # Start loop over vars | |
970 | lvals = '' |
|
972 | lvals = '' | |
971 | lvals_list = [] |
|
973 | lvals_list = [] | |
972 | if self.include_vars: |
|
974 | if self.include_vars: | |
973 | for name_full in unique_names: |
|
975 | for name_full in unique_names: | |
974 | name_base = name_full.split('.', 1)[0] |
|
976 | name_base = name_full.split('.', 1)[0] | |
975 | if name_base in frame.f_code.co_varnames: |
|
977 | if name_base in frame.f_code.co_varnames: | |
976 | if name_base in locals_: |
|
978 | if name_base in locals_: | |
977 | try: |
|
979 | try: | |
978 | value = repr(eval(name_full, locals_)) |
|
980 | value = repr(eval(name_full, locals_)) | |
979 | except: |
|
981 | except: | |
980 | value = undefined |
|
982 | value = undefined | |
981 | else: |
|
983 | else: | |
982 | value = undefined |
|
984 | value = undefined | |
983 | name = tpl_local_var % name_full |
|
985 | name = tpl_local_var % name_full | |
984 | else: |
|
986 | else: | |
985 | if name_base in frame.f_globals: |
|
987 | if name_base in frame.f_globals: | |
986 | try: |
|
988 | try: | |
987 | value = repr(eval(name_full, frame.f_globals)) |
|
989 | value = repr(eval(name_full, frame.f_globals)) | |
988 | except: |
|
990 | except: | |
989 | value = undefined |
|
991 | value = undefined | |
990 | else: |
|
992 | else: | |
991 | value = undefined |
|
993 | value = undefined | |
992 | name = tpl_global_var % name_full |
|
994 | name = tpl_global_var % name_full | |
993 | lvals_list.append(tpl_name_val % (name, value)) |
|
995 | lvals_list.append(tpl_name_val % (name, value)) | |
994 | if lvals_list: |
|
996 | if lvals_list: | |
995 | lvals = '%s%s' % (indent, em_normal.join(lvals_list)) |
|
997 | lvals = '%s%s' % (indent, em_normal.join(lvals_list)) | |
996 |
|
998 | |||
997 | level = '%s %s\n' % (link, call) |
|
999 | level = '%s %s\n' % (link, call) | |
998 |
|
1000 | |||
999 | if index is None: |
|
1001 | if index is None: | |
1000 | return level |
|
1002 | return level | |
1001 | else: |
|
1003 | else: | |
1002 | _line_format = PyColorize.Parser(style=col_scheme, parent=self).format2 |
|
1004 | _line_format = PyColorize.Parser(style=col_scheme, parent=self).format2 | |
1003 | return '%s%s' % (level, ''.join( |
|
1005 | return '%s%s' % (level, ''.join( | |
1004 | _format_traceback_lines(lnum, index, lines, Colors, lvals, |
|
1006 | _format_traceback_lines(lnum, index, lines, Colors, lvals, | |
1005 | _line_format))) |
|
1007 | _line_format))) | |
1006 |
|
1008 | |||
1007 | def prepare_chained_exception_message(self, cause): |
|
1009 | def prepare_chained_exception_message(self, cause): | |
1008 | direct_cause = "\nThe above exception was the direct cause of the following exception:\n" |
|
1010 | direct_cause = "\nThe above exception was the direct cause of the following exception:\n" | |
1009 | exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n" |
|
1011 | exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n" | |
1010 |
|
1012 | |||
1011 | if cause: |
|
1013 | if cause: | |
1012 | message = [[direct_cause]] |
|
1014 | message = [[direct_cause]] | |
1013 | else: |
|
1015 | else: | |
1014 | message = [[exception_during_handling]] |
|
1016 | message = [[exception_during_handling]] | |
1015 | return message |
|
1017 | return message | |
1016 |
|
1018 | |||
1017 | def prepare_header(self, etype, long_version=False): |
|
1019 | def prepare_header(self, etype, long_version=False): | |
1018 | colors = self.Colors # just a shorthand + quicker name lookup |
|
1020 | colors = self.Colors # just a shorthand + quicker name lookup | |
1019 | colorsnormal = colors.Normal # used a lot |
|
1021 | colorsnormal = colors.Normal # used a lot | |
1020 | exc = '%s%s%s' % (colors.excName, etype, colorsnormal) |
|
1022 | exc = '%s%s%s' % (colors.excName, etype, colorsnormal) | |
1021 | width = min(75, get_terminal_size()[0]) |
|
1023 | width = min(75, get_terminal_size()[0]) | |
1022 | if long_version: |
|
1024 | if long_version: | |
1023 | # Header with the exception type, python version, and date |
|
1025 | # Header with the exception type, python version, and date | |
1024 | pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable |
|
1026 | pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable | |
1025 | date = time.ctime(time.time()) |
|
1027 | date = time.ctime(time.time()) | |
1026 |
|
1028 | |||
1027 | head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal, |
|
1029 | head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal, | |
1028 | exc, ' ' * (width - len(str(etype)) - len(pyver)), |
|
1030 | exc, ' ' * (width - len(str(etype)) - len(pyver)), | |
1029 | pyver, date.rjust(width) ) |
|
1031 | pyver, date.rjust(width) ) | |
1030 | head += "\nA problem occurred executing Python code. Here is the sequence of function" \ |
|
1032 | head += "\nA problem occurred executing Python code. Here is the sequence of function" \ | |
1031 | "\ncalls leading up to the error, with the most recent (innermost) call last." |
|
1033 | "\ncalls leading up to the error, with the most recent (innermost) call last." | |
1032 | else: |
|
1034 | else: | |
1033 | # Simplified header |
|
1035 | # Simplified header | |
1034 | head = '%s%s' % (exc, 'Traceback (most recent call last)'. \ |
|
1036 | head = '%s%s' % (exc, 'Traceback (most recent call last)'. \ | |
1035 | rjust(width - len(str(etype))) ) |
|
1037 | rjust(width - len(str(etype))) ) | |
1036 |
|
1038 | |||
1037 | return head |
|
1039 | return head | |
1038 |
|
1040 | |||
1039 | def format_exception(self, etype, evalue): |
|
1041 | def format_exception(self, etype, evalue): | |
1040 | colors = self.Colors # just a shorthand + quicker name lookup |
|
1042 | colors = self.Colors # just a shorthand + quicker name lookup | |
1041 | colorsnormal = colors.Normal # used a lot |
|
1043 | colorsnormal = colors.Normal # used a lot | |
1042 | # Get (safely) a string form of the exception info |
|
1044 | # Get (safely) a string form of the exception info | |
1043 | try: |
|
1045 | try: | |
1044 | etype_str, evalue_str = map(str, (etype, evalue)) |
|
1046 | etype_str, evalue_str = map(str, (etype, evalue)) | |
1045 | except: |
|
1047 | except: | |
1046 | # User exception is improperly defined. |
|
1048 | # User exception is improperly defined. | |
1047 | etype, evalue = str, sys.exc_info()[:2] |
|
1049 | etype, evalue = str, sys.exc_info()[:2] | |
1048 | etype_str, evalue_str = map(str, (etype, evalue)) |
|
1050 | etype_str, evalue_str = map(str, (etype, evalue)) | |
1049 | # ... and format it |
|
1051 | # ... and format it | |
1050 | return ['%s%s%s: %s' % (colors.excName, etype_str, |
|
1052 | return ['%s%s%s: %s' % (colors.excName, etype_str, | |
1051 | colorsnormal, py3compat.cast_unicode(evalue_str))] |
|
1053 | colorsnormal, py3compat.cast_unicode(evalue_str))] | |
1052 |
|
1054 | |||
1053 | def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset): |
|
1055 | def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset): | |
1054 | """Formats the header, traceback and exception message for a single exception. |
|
1056 | """Formats the header, traceback and exception message for a single exception. | |
1055 |
|
1057 | |||
1056 | This may be called multiple times by Python 3 exception chaining |
|
1058 | This may be called multiple times by Python 3 exception chaining | |
1057 | (PEP 3134). |
|
1059 | (PEP 3134). | |
1058 | """ |
|
1060 | """ | |
1059 | # some locals |
|
1061 | # some locals | |
1060 | orig_etype = etype |
|
1062 | orig_etype = etype | |
1061 | try: |
|
1063 | try: | |
1062 | etype = etype.__name__ |
|
1064 | etype = etype.__name__ | |
1063 | except AttributeError: |
|
1065 | except AttributeError: | |
1064 | pass |
|
1066 | pass | |
1065 |
|
1067 | |||
1066 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
1068 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
1067 | head = self.prepare_header(etype, self.long_header) |
|
1069 | head = self.prepare_header(etype, self.long_header) | |
1068 | records = self.get_records(etb, number_of_lines_of_context, tb_offset) |
|
1070 | records = self.get_records(etb, number_of_lines_of_context, tb_offset) | |
1069 |
|
1071 | |||
1070 | if records is None: |
|
1072 | if records is None: | |
1071 | return "" |
|
1073 | return "" | |
1072 |
|
1074 | |||
1073 | last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records) |
|
1075 | last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records) | |
1074 |
|
1076 | |||
1075 | frames = self.format_records(records, last_unique, recursion_repeat) |
|
1077 | frames = self.format_records(records, last_unique, recursion_repeat) | |
1076 |
|
1078 | |||
1077 | formatted_exception = self.format_exception(etype, evalue) |
|
1079 | formatted_exception = self.format_exception(etype, evalue) | |
1078 | if records: |
|
1080 | if records: | |
1079 | filepath, lnum = records[-1][1:3] |
|
1081 | filepath, lnum = records[-1][1:3] | |
1080 | filepath = os.path.abspath(filepath) |
|
1082 | filepath = os.path.abspath(filepath) | |
1081 | ipinst = get_ipython() |
|
1083 | ipinst = get_ipython() | |
1082 | if ipinst is not None: |
|
1084 | if ipinst is not None: | |
1083 | ipinst.hooks.synchronize_with_editor(filepath, lnum, 0) |
|
1085 | ipinst.hooks.synchronize_with_editor(filepath, lnum, 0) | |
1084 |
|
1086 | |||
1085 | return [[head] + frames + [''.join(formatted_exception[0])]] |
|
1087 | return [[head] + frames + [''.join(formatted_exception[0])]] | |
1086 |
|
1088 | |||
1087 | def get_records(self, etb, number_of_lines_of_context, tb_offset): |
|
1089 | def get_records(self, etb, number_of_lines_of_context, tb_offset): | |
1088 | try: |
|
1090 | try: | |
1089 | # Try the default getinnerframes and Alex's: Alex's fixes some |
|
1091 | # Try the default getinnerframes and Alex's: Alex's fixes some | |
1090 | # problems, but it generates empty tracebacks for console errors |
|
1092 | # problems, but it generates empty tracebacks for console errors | |
1091 | # (5 blanks lines) where none should be returned. |
|
1093 | # (5 blanks lines) where none should be returned. | |
1092 | return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset) |
|
1094 | return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset) | |
1093 | except UnicodeDecodeError: |
|
1095 | except UnicodeDecodeError: | |
1094 | # This can occur if a file's encoding magic comment is wrong. |
|
1096 | # This can occur if a file's encoding magic comment is wrong. | |
1095 | # I can't see a way to recover without duplicating a bunch of code |
|
1097 | # I can't see a way to recover without duplicating a bunch of code | |
1096 | # from the stdlib traceback module. --TK |
|
1098 | # from the stdlib traceback module. --TK | |
1097 | error('\nUnicodeDecodeError while processing traceback.\n') |
|
1099 | error('\nUnicodeDecodeError while processing traceback.\n') | |
1098 | return None |
|
1100 | return None | |
1099 | except: |
|
1101 | except: | |
1100 | # FIXME: I've been getting many crash reports from python 2.3 |
|
1102 | # FIXME: I've been getting many crash reports from python 2.3 | |
1101 | # users, traceable to inspect.py. If I can find a small test-case |
|
1103 | # users, traceable to inspect.py. If I can find a small test-case | |
1102 | # to reproduce this, I should either write a better workaround or |
|
1104 | # to reproduce this, I should either write a better workaround or | |
1103 | # file a bug report against inspect (if that's the real problem). |
|
1105 | # file a bug report against inspect (if that's the real problem). | |
1104 | # So far, I haven't been able to find an isolated example to |
|
1106 | # So far, I haven't been able to find an isolated example to | |
1105 | # reproduce the problem. |
|
1107 | # reproduce the problem. | |
1106 | inspect_error() |
|
1108 | inspect_error() | |
1107 | traceback.print_exc(file=self.ostream) |
|
1109 | traceback.print_exc(file=self.ostream) | |
1108 | info('\nUnfortunately, your original traceback can not be constructed.\n') |
|
1110 | info('\nUnfortunately, your original traceback can not be constructed.\n') | |
1109 | return None |
|
1111 | return None | |
1110 |
|
1112 | |||
1111 | def get_parts_of_chained_exception(self, evalue): |
|
1113 | def get_parts_of_chained_exception(self, evalue): | |
1112 | def get_chained_exception(exception_value): |
|
1114 | def get_chained_exception(exception_value): | |
1113 | cause = getattr(exception_value, '__cause__', None) |
|
1115 | cause = getattr(exception_value, '__cause__', None) | |
1114 | if cause: |
|
1116 | if cause: | |
1115 | return cause |
|
1117 | return cause | |
1116 | if getattr(exception_value, '__suppress_context__', False): |
|
1118 | if getattr(exception_value, '__suppress_context__', False): | |
1117 | return None |
|
1119 | return None | |
1118 | return getattr(exception_value, '__context__', None) |
|
1120 | return getattr(exception_value, '__context__', None) | |
1119 |
|
1121 | |||
1120 | chained_evalue = get_chained_exception(evalue) |
|
1122 | chained_evalue = get_chained_exception(evalue) | |
1121 |
|
1123 | |||
1122 | if chained_evalue: |
|
1124 | if chained_evalue: | |
1123 | return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__ |
|
1125 | return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__ | |
1124 |
|
1126 | |||
1125 | def structured_traceback(self, etype, evalue, etb, tb_offset=None, |
|
1127 | def structured_traceback(self, etype, evalue, etb, tb_offset=None, | |
1126 | number_of_lines_of_context=5): |
|
1128 | number_of_lines_of_context=5): | |
1127 | """Return a nice text document describing the traceback.""" |
|
1129 | """Return a nice text document describing the traceback.""" | |
1128 |
|
1130 | |||
1129 | formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context, |
|
1131 | formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context, | |
1130 | tb_offset) |
|
1132 | tb_offset) | |
1131 |
|
1133 | |||
1132 | colors = self.Colors # just a shorthand + quicker name lookup |
|
1134 | colors = self.Colors # just a shorthand + quicker name lookup | |
1133 | colorsnormal = colors.Normal # used a lot |
|
1135 | colorsnormal = colors.Normal # used a lot | |
1134 | head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal) |
|
1136 | head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal) | |
1135 | structured_traceback_parts = [head] |
|
1137 | structured_traceback_parts = [head] | |
1136 | chained_exceptions_tb_offset = 0 |
|
1138 | chained_exceptions_tb_offset = 0 | |
1137 | lines_of_context = 3 |
|
1139 | lines_of_context = 3 | |
1138 | formatted_exceptions = formatted_exception |
|
1140 | formatted_exceptions = formatted_exception | |
1139 | exception = self.get_parts_of_chained_exception(evalue) |
|
1141 | exception = self.get_parts_of_chained_exception(evalue) | |
1140 | if exception: |
|
1142 | if exception: | |
1141 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) |
|
1143 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) | |
1142 | etype, evalue, etb = exception |
|
1144 | etype, evalue, etb = exception | |
1143 | else: |
|
1145 | else: | |
1144 | evalue = None |
|
1146 | evalue = None | |
1145 | chained_exc_ids = set() |
|
1147 | chained_exc_ids = set() | |
1146 | while evalue: |
|
1148 | while evalue: | |
1147 | formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context, |
|
1149 | formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context, | |
1148 | chained_exceptions_tb_offset) |
|
1150 | chained_exceptions_tb_offset) | |
1149 | exception = self.get_parts_of_chained_exception(evalue) |
|
1151 | exception = self.get_parts_of_chained_exception(evalue) | |
1150 |
|
1152 | |||
1151 | if exception and not id(exception[1]) in chained_exc_ids: |
|
1153 | if exception and not id(exception[1]) in chained_exc_ids: | |
1152 | chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop |
|
1154 | chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop | |
1153 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) |
|
1155 | formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__) | |
1154 | etype, evalue, etb = exception |
|
1156 | etype, evalue, etb = exception | |
1155 | else: |
|
1157 | else: | |
1156 | evalue = None |
|
1158 | evalue = None | |
1157 |
|
1159 | |||
1158 | # we want to see exceptions in a reversed order: |
|
1160 | # we want to see exceptions in a reversed order: | |
1159 | # the first exception should be on top |
|
1161 | # the first exception should be on top | |
1160 | for formatted_exception in reversed(formatted_exceptions): |
|
1162 | for formatted_exception in reversed(formatted_exceptions): | |
1161 | structured_traceback_parts += formatted_exception |
|
1163 | structured_traceback_parts += formatted_exception | |
1162 |
|
1164 | |||
1163 | return structured_traceback_parts |
|
1165 | return structured_traceback_parts | |
1164 |
|
1166 | |||
1165 | def debugger(self, force=False): |
|
1167 | def debugger(self, force=False): | |
1166 | """Call up the pdb debugger if desired, always clean up the tb |
|
1168 | """Call up the pdb debugger if desired, always clean up the tb | |
1167 | reference. |
|
1169 | reference. | |
1168 |
|
1170 | |||
1169 | Keywords: |
|
1171 | Keywords: | |
1170 |
|
1172 | |||
1171 | - force(False): by default, this routine checks the instance call_pdb |
|
1173 | - force(False): by default, this routine checks the instance call_pdb | |
1172 | flag and does not actually invoke the debugger if the flag is false. |
|
1174 | flag and does not actually invoke the debugger if the flag is false. | |
1173 | The 'force' option forces the debugger to activate even if the flag |
|
1175 | The 'force' option forces the debugger to activate even if the flag | |
1174 | is false. |
|
1176 | is false. | |
1175 |
|
1177 | |||
1176 | If the call_pdb flag is set, the pdb interactive debugger is |
|
1178 | If the call_pdb flag is set, the pdb interactive debugger is | |
1177 | invoked. In all cases, the self.tb reference to the current traceback |
|
1179 | invoked. In all cases, the self.tb reference to the current traceback | |
1178 | is deleted to prevent lingering references which hamper memory |
|
1180 | is deleted to prevent lingering references which hamper memory | |
1179 | management. |
|
1181 | management. | |
1180 |
|
1182 | |||
1181 | Note that each call to pdb() does an 'import readline', so if your app |
|
1183 | Note that each call to pdb() does an 'import readline', so if your app | |
1182 | requires a special setup for the readline completers, you'll have to |
|
1184 | requires a special setup for the readline completers, you'll have to | |
1183 | fix that by hand after invoking the exception handler.""" |
|
1185 | fix that by hand after invoking the exception handler.""" | |
1184 |
|
1186 | |||
1185 | if force or self.call_pdb: |
|
1187 | if force or self.call_pdb: | |
1186 | if self.pdb is None: |
|
1188 | if self.pdb is None: | |
1187 | self.pdb = self.debugger_cls() |
|
1189 | self.pdb = self.debugger_cls() | |
1188 | # the system displayhook may have changed, restore the original |
|
1190 | # the system displayhook may have changed, restore the original | |
1189 | # for pdb |
|
1191 | # for pdb | |
1190 | display_trap = DisplayTrap(hook=sys.__displayhook__) |
|
1192 | display_trap = DisplayTrap(hook=sys.__displayhook__) | |
1191 | with display_trap: |
|
1193 | with display_trap: | |
1192 | self.pdb.reset() |
|
1194 | self.pdb.reset() | |
1193 | # Find the right frame so we don't pop up inside ipython itself |
|
1195 | # Find the right frame so we don't pop up inside ipython itself | |
1194 | if hasattr(self, 'tb') and self.tb is not None: |
|
1196 | if hasattr(self, 'tb') and self.tb is not None: | |
1195 | etb = self.tb |
|
1197 | etb = self.tb | |
1196 | else: |
|
1198 | else: | |
1197 | etb = self.tb = sys.last_traceback |
|
1199 | etb = self.tb = sys.last_traceback | |
1198 | while self.tb is not None and self.tb.tb_next is not None: |
|
1200 | while self.tb is not None and self.tb.tb_next is not None: | |
1199 | self.tb = self.tb.tb_next |
|
1201 | self.tb = self.tb.tb_next | |
1200 | if etb and etb.tb_next: |
|
1202 | if etb and etb.tb_next: | |
1201 | etb = etb.tb_next |
|
1203 | etb = etb.tb_next | |
1202 | self.pdb.botframe = etb.tb_frame |
|
1204 | self.pdb.botframe = etb.tb_frame | |
1203 | self.pdb.interaction(self.tb.tb_frame, self.tb) |
|
1205 | self.pdb.interaction(self.tb.tb_frame, self.tb) | |
1204 |
|
1206 | |||
1205 | if hasattr(self, 'tb'): |
|
1207 | if hasattr(self, 'tb'): | |
1206 | del self.tb |
|
1208 | del self.tb | |
1207 |
|
1209 | |||
1208 | def handler(self, info=None): |
|
1210 | def handler(self, info=None): | |
1209 | (etype, evalue, etb) = info or sys.exc_info() |
|
1211 | (etype, evalue, etb) = info or sys.exc_info() | |
1210 | self.tb = etb |
|
1212 | self.tb = etb | |
1211 | ostream = self.ostream |
|
1213 | ostream = self.ostream | |
1212 | ostream.flush() |
|
1214 | ostream.flush() | |
1213 | ostream.write(self.text(etype, evalue, etb)) |
|
1215 | ostream.write(self.text(etype, evalue, etb)) | |
1214 | ostream.write('\n') |
|
1216 | ostream.write('\n') | |
1215 | ostream.flush() |
|
1217 | ostream.flush() | |
1216 |
|
1218 | |||
1217 | # Changed so an instance can just be called as VerboseTB_inst() and print |
|
1219 | # Changed so an instance can just be called as VerboseTB_inst() and print | |
1218 | # out the right info on its own. |
|
1220 | # out the right info on its own. | |
1219 | def __call__(self, etype=None, evalue=None, etb=None): |
|
1221 | def __call__(self, etype=None, evalue=None, etb=None): | |
1220 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" |
|
1222 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" | |
1221 | if etb is None: |
|
1223 | if etb is None: | |
1222 | self.handler() |
|
1224 | self.handler() | |
1223 | else: |
|
1225 | else: | |
1224 | self.handler((etype, evalue, etb)) |
|
1226 | self.handler((etype, evalue, etb)) | |
1225 | try: |
|
1227 | try: | |
1226 | self.debugger() |
|
1228 | self.debugger() | |
1227 | except KeyboardInterrupt: |
|
1229 | except KeyboardInterrupt: | |
1228 | print("\nKeyboardInterrupt") |
|
1230 | print("\nKeyboardInterrupt") | |
1229 |
|
1231 | |||
1230 |
|
1232 | |||
1231 | #---------------------------------------------------------------------------- |
|
1233 | #---------------------------------------------------------------------------- | |
1232 | class FormattedTB(VerboseTB, ListTB): |
|
1234 | class FormattedTB(VerboseTB, ListTB): | |
1233 | """Subclass ListTB but allow calling with a traceback. |
|
1235 | """Subclass ListTB but allow calling with a traceback. | |
1234 |
|
1236 | |||
1235 | It can thus be used as a sys.excepthook for Python > 2.1. |
|
1237 | It can thus be used as a sys.excepthook for Python > 2.1. | |
1236 |
|
1238 | |||
1237 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. |
|
1239 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. | |
1238 |
|
1240 | |||
1239 | Allows a tb_offset to be specified. This is useful for situations where |
|
1241 | Allows a tb_offset to be specified. This is useful for situations where | |
1240 | one needs to remove a number of topmost frames from the traceback (such as |
|
1242 | one needs to remove a number of topmost frames from the traceback (such as | |
1241 | occurs with python programs that themselves execute other python code, |
|
1243 | occurs with python programs that themselves execute other python code, | |
1242 | like Python shells). """ |
|
1244 | like Python shells). """ | |
1243 |
|
1245 | |||
1244 | def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False, |
|
1246 | def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False, | |
1245 | ostream=None, |
|
1247 | ostream=None, | |
1246 | tb_offset=0, long_header=False, include_vars=False, |
|
1248 | tb_offset=0, long_header=False, include_vars=False, | |
1247 | check_cache=None, debugger_cls=None, |
|
1249 | check_cache=None, debugger_cls=None, | |
1248 | parent=None, config=None): |
|
1250 | parent=None, config=None): | |
1249 |
|
1251 | |||
1250 | # NEVER change the order of this list. Put new modes at the end: |
|
1252 | # NEVER change the order of this list. Put new modes at the end: | |
1251 | self.valid_modes = ['Plain', 'Context', 'Verbose'] |
|
1253 | self.valid_modes = ['Plain', 'Context', 'Verbose'] | |
1252 | self.verbose_modes = self.valid_modes[1:3] |
|
1254 | self.verbose_modes = self.valid_modes[1:3] | |
1253 |
|
1255 | |||
1254 | VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, |
|
1256 | VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb, | |
1255 | ostream=ostream, tb_offset=tb_offset, |
|
1257 | ostream=ostream, tb_offset=tb_offset, | |
1256 | long_header=long_header, include_vars=include_vars, |
|
1258 | long_header=long_header, include_vars=include_vars, | |
1257 | check_cache=check_cache, debugger_cls=debugger_cls, |
|
1259 | check_cache=check_cache, debugger_cls=debugger_cls, | |
1258 | parent=parent, config=config) |
|
1260 | parent=parent, config=config) | |
1259 |
|
1261 | |||
1260 | # Different types of tracebacks are joined with different separators to |
|
1262 | # Different types of tracebacks are joined with different separators to | |
1261 | # form a single string. They are taken from this dict |
|
1263 | # form a single string. They are taken from this dict | |
1262 | self._join_chars = dict(Plain='', Context='\n', Verbose='\n') |
|
1264 | self._join_chars = dict(Plain='', Context='\n', Verbose='\n') | |
1263 | # set_mode also sets the tb_join_char attribute |
|
1265 | # set_mode also sets the tb_join_char attribute | |
1264 | self.set_mode(mode) |
|
1266 | self.set_mode(mode) | |
1265 |
|
1267 | |||
1266 | def _extract_tb(self, tb): |
|
1268 | def _extract_tb(self, tb): | |
1267 | if tb: |
|
1269 | if tb: | |
1268 | return traceback.extract_tb(tb) |
|
1270 | return traceback.extract_tb(tb) | |
1269 | else: |
|
1271 | else: | |
1270 | return None |
|
1272 | return None | |
1271 |
|
1273 | |||
1272 | def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5): |
|
1274 | def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5): | |
1273 | tb_offset = self.tb_offset if tb_offset is None else tb_offset |
|
1275 | tb_offset = self.tb_offset if tb_offset is None else tb_offset | |
1274 | mode = self.mode |
|
1276 | mode = self.mode | |
1275 | if mode in self.verbose_modes: |
|
1277 | if mode in self.verbose_modes: | |
1276 | # Verbose modes need a full traceback |
|
1278 | # Verbose modes need a full traceback | |
1277 | return VerboseTB.structured_traceback( |
|
1279 | return VerboseTB.structured_traceback( | |
1278 | self, etype, value, tb, tb_offset, number_of_lines_of_context |
|
1280 | self, etype, value, tb, tb_offset, number_of_lines_of_context | |
1279 | ) |
|
1281 | ) | |
1280 | else: |
|
1282 | else: | |
1281 | # We must check the source cache because otherwise we can print |
|
1283 | # We must check the source cache because otherwise we can print | |
1282 | # out-of-date source code. |
|
1284 | # out-of-date source code. | |
1283 | self.check_cache() |
|
1285 | self.check_cache() | |
1284 | # Now we can extract and format the exception |
|
1286 | # Now we can extract and format the exception | |
1285 | elist = self._extract_tb(tb) |
|
1287 | elist = self._extract_tb(tb) | |
1286 | return ListTB.structured_traceback( |
|
1288 | return ListTB.structured_traceback( | |
1287 | self, etype, value, elist, tb_offset, number_of_lines_of_context |
|
1289 | self, etype, value, elist, tb_offset, number_of_lines_of_context | |
1288 | ) |
|
1290 | ) | |
1289 |
|
1291 | |||
1290 | def stb2text(self, stb): |
|
1292 | def stb2text(self, stb): | |
1291 | """Convert a structured traceback (a list) to a string.""" |
|
1293 | """Convert a structured traceback (a list) to a string.""" | |
1292 | return self.tb_join_char.join(stb) |
|
1294 | return self.tb_join_char.join(stb) | |
1293 |
|
1295 | |||
1294 |
|
1296 | |||
1295 | def set_mode(self, mode=None): |
|
1297 | def set_mode(self, mode=None): | |
1296 | """Switch to the desired mode. |
|
1298 | """Switch to the desired mode. | |
1297 |
|
1299 | |||
1298 | If mode is not specified, cycles through the available modes.""" |
|
1300 | If mode is not specified, cycles through the available modes.""" | |
1299 |
|
1301 | |||
1300 | if not mode: |
|
1302 | if not mode: | |
1301 | new_idx = (self.valid_modes.index(self.mode) + 1 ) % \ |
|
1303 | new_idx = (self.valid_modes.index(self.mode) + 1 ) % \ | |
1302 | len(self.valid_modes) |
|
1304 | len(self.valid_modes) | |
1303 | self.mode = self.valid_modes[new_idx] |
|
1305 | self.mode = self.valid_modes[new_idx] | |
1304 | elif mode not in self.valid_modes: |
|
1306 | elif mode not in self.valid_modes: | |
1305 | raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n' |
|
1307 | raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n' | |
1306 | 'Valid modes: ' + str(self.valid_modes)) |
|
1308 | 'Valid modes: ' + str(self.valid_modes)) | |
1307 | else: |
|
1309 | else: | |
1308 | self.mode = mode |
|
1310 | self.mode = mode | |
1309 | # include variable details only in 'Verbose' mode |
|
1311 | # include variable details only in 'Verbose' mode | |
1310 | self.include_vars = (self.mode == self.valid_modes[2]) |
|
1312 | self.include_vars = (self.mode == self.valid_modes[2]) | |
1311 | # Set the join character for generating text tracebacks |
|
1313 | # Set the join character for generating text tracebacks | |
1312 | self.tb_join_char = self._join_chars[self.mode] |
|
1314 | self.tb_join_char = self._join_chars[self.mode] | |
1313 |
|
1315 | |||
1314 | # some convenient shortcuts |
|
1316 | # some convenient shortcuts | |
1315 | def plain(self): |
|
1317 | def plain(self): | |
1316 | self.set_mode(self.valid_modes[0]) |
|
1318 | self.set_mode(self.valid_modes[0]) | |
1317 |
|
1319 | |||
1318 | def context(self): |
|
1320 | def context(self): | |
1319 | self.set_mode(self.valid_modes[1]) |
|
1321 | self.set_mode(self.valid_modes[1]) | |
1320 |
|
1322 | |||
1321 | def verbose(self): |
|
1323 | def verbose(self): | |
1322 | self.set_mode(self.valid_modes[2]) |
|
1324 | self.set_mode(self.valid_modes[2]) | |
1323 |
|
1325 | |||
1324 |
|
1326 | |||
1325 | #---------------------------------------------------------------------------- |
|
1327 | #---------------------------------------------------------------------------- | |
1326 | class AutoFormattedTB(FormattedTB): |
|
1328 | class AutoFormattedTB(FormattedTB): | |
1327 | """A traceback printer which can be called on the fly. |
|
1329 | """A traceback printer which can be called on the fly. | |
1328 |
|
1330 | |||
1329 | It will find out about exceptions by itself. |
|
1331 | It will find out about exceptions by itself. | |
1330 |
|
1332 | |||
1331 | A brief example:: |
|
1333 | A brief example:: | |
1332 |
|
1334 | |||
1333 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') |
|
1335 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') | |
1334 | try: |
|
1336 | try: | |
1335 | ... |
|
1337 | ... | |
1336 | except: |
|
1338 | except: | |
1337 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object |
|
1339 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object | |
1338 | """ |
|
1340 | """ | |
1339 |
|
1341 | |||
1340 | def __call__(self, etype=None, evalue=None, etb=None, |
|
1342 | def __call__(self, etype=None, evalue=None, etb=None, | |
1341 | out=None, tb_offset=None): |
|
1343 | out=None, tb_offset=None): | |
1342 | """Print out a formatted exception traceback. |
|
1344 | """Print out a formatted exception traceback. | |
1343 |
|
1345 | |||
1344 | Optional arguments: |
|
1346 | Optional arguments: | |
1345 | - out: an open file-like object to direct output to. |
|
1347 | - out: an open file-like object to direct output to. | |
1346 |
|
1348 | |||
1347 | - tb_offset: the number of frames to skip over in the stack, on a |
|
1349 | - tb_offset: the number of frames to skip over in the stack, on a | |
1348 | per-call basis (this overrides temporarily the instance's tb_offset |
|
1350 | per-call basis (this overrides temporarily the instance's tb_offset | |
1349 | given at initialization time. """ |
|
1351 | given at initialization time. """ | |
1350 |
|
1352 | |||
1351 | if out is None: |
|
1353 | if out is None: | |
1352 | out = self.ostream |
|
1354 | out = self.ostream | |
1353 | out.flush() |
|
1355 | out.flush() | |
1354 | out.write(self.text(etype, evalue, etb, tb_offset)) |
|
1356 | out.write(self.text(etype, evalue, etb, tb_offset)) | |
1355 | out.write('\n') |
|
1357 | out.write('\n') | |
1356 | out.flush() |
|
1358 | out.flush() | |
1357 | # FIXME: we should remove the auto pdb behavior from here and leave |
|
1359 | # FIXME: we should remove the auto pdb behavior from here and leave | |
1358 | # that to the clients. |
|
1360 | # that to the clients. | |
1359 | try: |
|
1361 | try: | |
1360 | self.debugger() |
|
1362 | self.debugger() | |
1361 | except KeyboardInterrupt: |
|
1363 | except KeyboardInterrupt: | |
1362 | print("\nKeyboardInterrupt") |
|
1364 | print("\nKeyboardInterrupt") | |
1363 |
|
1365 | |||
1364 | def structured_traceback(self, etype=None, value=None, tb=None, |
|
1366 | def structured_traceback(self, etype=None, value=None, tb=None, | |
1365 | tb_offset=None, number_of_lines_of_context=5): |
|
1367 | tb_offset=None, number_of_lines_of_context=5): | |
1366 | if etype is None: |
|
1368 | if etype is None: | |
1367 | etype, value, tb = sys.exc_info() |
|
1369 | etype, value, tb = sys.exc_info() | |
1368 | self.tb = tb |
|
1370 | self.tb = tb | |
1369 | return FormattedTB.structured_traceback( |
|
1371 | return FormattedTB.structured_traceback( | |
1370 | self, etype, value, tb, tb_offset, number_of_lines_of_context) |
|
1372 | self, etype, value, tb, tb_offset, number_of_lines_of_context) | |
1371 |
|
1373 | |||
1372 |
|
1374 | |||
1373 | #--------------------------------------------------------------------------- |
|
1375 | #--------------------------------------------------------------------------- | |
1374 |
|
1376 | |||
1375 | # A simple class to preserve Nathan's original functionality. |
|
1377 | # A simple class to preserve Nathan's original functionality. | |
1376 | class ColorTB(FormattedTB): |
|
1378 | class ColorTB(FormattedTB): | |
1377 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" |
|
1379 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" | |
1378 |
|
1380 | |||
1379 | def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs): |
|
1381 | def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs): | |
1380 | FormattedTB.__init__(self, color_scheme=color_scheme, |
|
1382 | FormattedTB.__init__(self, color_scheme=color_scheme, | |
1381 | call_pdb=call_pdb, **kwargs) |
|
1383 | call_pdb=call_pdb, **kwargs) | |
1382 |
|
1384 | |||
1383 |
|
1385 | |||
1384 | class SyntaxTB(ListTB): |
|
1386 | class SyntaxTB(ListTB): | |
1385 | """Extension which holds some state: the last exception value""" |
|
1387 | """Extension which holds some state: the last exception value""" | |
1386 |
|
1388 | |||
1387 | def __init__(self, color_scheme='NoColor', parent=None, config=None): |
|
1389 | def __init__(self, color_scheme='NoColor', parent=None, config=None): | |
1388 | ListTB.__init__(self, color_scheme, parent=parent, config=config) |
|
1390 | ListTB.__init__(self, color_scheme, parent=parent, config=config) | |
1389 | self.last_syntax_error = None |
|
1391 | self.last_syntax_error = None | |
1390 |
|
1392 | |||
1391 | def __call__(self, etype, value, elist): |
|
1393 | def __call__(self, etype, value, elist): | |
1392 | self.last_syntax_error = value |
|
1394 | self.last_syntax_error = value | |
1393 |
|
1395 | |||
1394 | ListTB.__call__(self, etype, value, elist) |
|
1396 | ListTB.__call__(self, etype, value, elist) | |
1395 |
|
1397 | |||
1396 | def structured_traceback(self, etype, value, elist, tb_offset=None, |
|
1398 | def structured_traceback(self, etype, value, elist, tb_offset=None, | |
1397 | context=5): |
|
1399 | context=5): | |
1398 | # If the source file has been edited, the line in the syntax error can |
|
1400 | # If the source file has been edited, the line in the syntax error can | |
1399 | # be wrong (retrieved from an outdated cache). This replaces it with |
|
1401 | # be wrong (retrieved from an outdated cache). This replaces it with | |
1400 | # the current value. |
|
1402 | # the current value. | |
1401 | if isinstance(value, SyntaxError) \ |
|
1403 | if isinstance(value, SyntaxError) \ | |
1402 | and isinstance(value.filename, str) \ |
|
1404 | and isinstance(value.filename, str) \ | |
1403 | and isinstance(value.lineno, int): |
|
1405 | and isinstance(value.lineno, int): | |
1404 | linecache.checkcache(value.filename) |
|
1406 | linecache.checkcache(value.filename) | |
1405 | newtext = linecache.getline(value.filename, value.lineno) |
|
1407 | newtext = linecache.getline(value.filename, value.lineno) | |
1406 | if newtext: |
|
1408 | if newtext: | |
1407 | value.text = newtext |
|
1409 | value.text = newtext | |
1408 | self.last_syntax_error = value |
|
1410 | self.last_syntax_error = value | |
1409 | return super(SyntaxTB, self).structured_traceback(etype, value, elist, |
|
1411 | return super(SyntaxTB, self).structured_traceback(etype, value, elist, | |
1410 | tb_offset=tb_offset, context=context) |
|
1412 | tb_offset=tb_offset, context=context) | |
1411 |
|
1413 | |||
1412 | def clear_err_state(self): |
|
1414 | def clear_err_state(self): | |
1413 | """Return the current error state and clear it""" |
|
1415 | """Return the current error state and clear it""" | |
1414 | e = self.last_syntax_error |
|
1416 | e = self.last_syntax_error | |
1415 | self.last_syntax_error = None |
|
1417 | self.last_syntax_error = None | |
1416 | return e |
|
1418 | return e | |
1417 |
|
1419 | |||
1418 | def stb2text(self, stb): |
|
1420 | def stb2text(self, stb): | |
1419 | """Convert a structured traceback (a list) to a string.""" |
|
1421 | """Convert a structured traceback (a list) to a string.""" | |
1420 | return ''.join(stb) |
|
1422 | return ''.join(stb) | |
1421 |
|
1423 | |||
1422 |
|
1424 | |||
1423 | # some internal-use functions |
|
1425 | # some internal-use functions | |
1424 | def text_repr(value): |
|
1426 | def text_repr(value): | |
1425 | """Hopefully pretty robust repr equivalent.""" |
|
1427 | """Hopefully pretty robust repr equivalent.""" | |
1426 | # this is pretty horrible but should always return *something* |
|
1428 | # this is pretty horrible but should always return *something* | |
1427 | try: |
|
1429 | try: | |
1428 | return pydoc.text.repr(value) |
|
1430 | return pydoc.text.repr(value) | |
1429 | except KeyboardInterrupt: |
|
1431 | except KeyboardInterrupt: | |
1430 | raise |
|
1432 | raise | |
1431 | except: |
|
1433 | except: | |
1432 | try: |
|
1434 | try: | |
1433 | return repr(value) |
|
1435 | return repr(value) | |
1434 | except KeyboardInterrupt: |
|
1436 | except KeyboardInterrupt: | |
1435 | raise |
|
1437 | raise | |
1436 | except: |
|
1438 | except: | |
1437 | try: |
|
1439 | try: | |
1438 | # all still in an except block so we catch |
|
1440 | # all still in an except block so we catch | |
1439 | # getattr raising |
|
1441 | # getattr raising | |
1440 | name = getattr(value, '__name__', None) |
|
1442 | name = getattr(value, '__name__', None) | |
1441 | if name: |
|
1443 | if name: | |
1442 | # ick, recursion |
|
1444 | # ick, recursion | |
1443 | return text_repr(name) |
|
1445 | return text_repr(name) | |
1444 | klass = getattr(value, '__class__', None) |
|
1446 | klass = getattr(value, '__class__', None) | |
1445 | if klass: |
|
1447 | if klass: | |
1446 | return '%s instance' % text_repr(klass) |
|
1448 | return '%s instance' % text_repr(klass) | |
1447 | except KeyboardInterrupt: |
|
1449 | except KeyboardInterrupt: | |
1448 | raise |
|
1450 | raise | |
1449 | except: |
|
1451 | except: | |
1450 | return 'UNRECOVERABLE REPR FAILURE' |
|
1452 | return 'UNRECOVERABLE REPR FAILURE' | |
1451 |
|
1453 | |||
1452 |
|
1454 | |||
1453 | def eqrepr(value, repr=text_repr): |
|
1455 | def eqrepr(value, repr=text_repr): | |
1454 | return '=%s' % repr(value) |
|
1456 | return '=%s' % repr(value) | |
1455 |
|
1457 | |||
1456 |
|
1458 | |||
1457 | def nullrepr(value, repr=text_repr): |
|
1459 | def nullrepr(value, repr=text_repr): | |
1458 | return '' |
|
1460 | return '' |
@@ -1,531 +1,531 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | Defines a variety of Pygments lexers for highlighting IPython code. |
|
3 | Defines a variety of Pygments lexers for highlighting IPython code. | |
4 |
|
4 | |||
5 | This includes: |
|
5 | This includes: | |
6 |
|
6 | |||
7 | IPythonLexer, IPython3Lexer |
|
7 | IPythonLexer, IPython3Lexer | |
8 | Lexers for pure IPython (python + magic/shell commands) |
|
8 | Lexers for pure IPython (python + magic/shell commands) | |
9 |
|
9 | |||
10 | IPythonPartialTracebackLexer, IPythonTracebackLexer |
|
10 | IPythonPartialTracebackLexer, IPythonTracebackLexer | |
11 | Supports 2.x and 3.x via keyword `python3`. The partial traceback |
|
11 | Supports 2.x and 3.x via keyword `python3`. The partial traceback | |
12 | lexer reads everything but the Python code appearing in a traceback. |
|
12 | lexer reads everything but the Python code appearing in a traceback. | |
13 | The full lexer combines the partial lexer with an IPython lexer. |
|
13 | The full lexer combines the partial lexer with an IPython lexer. | |
14 |
|
14 | |||
15 | IPythonConsoleLexer |
|
15 | IPythonConsoleLexer | |
16 | A lexer for IPython console sessions, with support for tracebacks. |
|
16 | A lexer for IPython console sessions, with support for tracebacks. | |
17 |
|
17 | |||
18 | IPyLexer |
|
18 | IPyLexer | |
19 | A friendly lexer which examines the first line of text and from it, |
|
19 | A friendly lexer which examines the first line of text and from it, | |
20 | decides whether to use an IPython lexer or an IPython console lexer. |
|
20 | decides whether to use an IPython lexer or an IPython console lexer. | |
21 | This is probably the only lexer that needs to be explicitly added |
|
21 | This is probably the only lexer that needs to be explicitly added | |
22 | to Pygments. |
|
22 | to Pygments. | |
23 |
|
23 | |||
24 | """ |
|
24 | """ | |
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 | # Copyright (c) 2013, the IPython Development Team. |
|
26 | # Copyright (c) 2013, the IPython Development Team. | |
27 | # |
|
27 | # | |
28 | # Distributed under the terms of the Modified BSD License. |
|
28 | # Distributed under the terms of the Modified BSD License. | |
29 | # |
|
29 | # | |
30 | # The full license is in the file COPYING.txt, distributed with this software. |
|
30 | # The full license is in the file COPYING.txt, distributed with this software. | |
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 |
|
32 | |||
33 | # Standard library |
|
33 | # Standard library | |
34 | import re |
|
34 | import re | |
35 |
|
35 | |||
36 | # Third party |
|
36 | # Third party | |
37 | from pygments.lexers import ( |
|
37 | from pygments.lexers import ( | |
38 | BashLexer, HtmlLexer, JavascriptLexer, RubyLexer, PerlLexer, PythonLexer, |
|
38 | BashLexer, HtmlLexer, JavascriptLexer, RubyLexer, PerlLexer, PythonLexer, | |
39 | Python3Lexer, TexLexer) |
|
39 | Python3Lexer, TexLexer) | |
40 | from pygments.lexer import ( |
|
40 | from pygments.lexer import ( | |
41 | Lexer, DelegatingLexer, RegexLexer, do_insertions, bygroups, using, |
|
41 | Lexer, DelegatingLexer, RegexLexer, do_insertions, bygroups, using, | |
42 | ) |
|
42 | ) | |
43 | from pygments.token import ( |
|
43 | from pygments.token import ( | |
44 | Generic, Keyword, Literal, Name, Operator, Other, Text, Error, |
|
44 | Generic, Keyword, Literal, Name, Operator, Other, Text, Error, | |
45 | ) |
|
45 | ) | |
46 | from pygments.util import get_bool_opt |
|
46 | from pygments.util import get_bool_opt | |
47 |
|
47 | |||
48 | # Local |
|
48 | # Local | |
49 |
|
49 | |||
50 | line_re = re.compile('.*?\n') |
|
50 | line_re = re.compile('.*?\n') | |
51 |
|
51 | |||
52 | __all__ = ['build_ipy_lexer', 'IPython3Lexer', 'IPythonLexer', |
|
52 | __all__ = ['build_ipy_lexer', 'IPython3Lexer', 'IPythonLexer', | |
53 | 'IPythonPartialTracebackLexer', 'IPythonTracebackLexer', |
|
53 | 'IPythonPartialTracebackLexer', 'IPythonTracebackLexer', | |
54 | 'IPythonConsoleLexer', 'IPyLexer'] |
|
54 | 'IPythonConsoleLexer', 'IPyLexer'] | |
55 |
|
55 | |||
56 |
|
56 | |||
57 | def build_ipy_lexer(python3): |
|
57 | def build_ipy_lexer(python3): | |
58 | """Builds IPython lexers depending on the value of `python3`. |
|
58 | """Builds IPython lexers depending on the value of `python3`. | |
59 |
|
59 | |||
60 | The lexer inherits from an appropriate Python lexer and then adds |
|
60 | The lexer inherits from an appropriate Python lexer and then adds | |
61 | information about IPython specific keywords (i.e. magic commands, |
|
61 | information about IPython specific keywords (i.e. magic commands, | |
62 | shell commands, etc.) |
|
62 | shell commands, etc.) | |
63 |
|
63 | |||
64 | Parameters |
|
64 | Parameters | |
65 | ---------- |
|
65 | ---------- | |
66 | python3 : bool |
|
66 | python3 : bool | |
67 | If `True`, then build an IPython lexer from a Python 3 lexer. |
|
67 | If `True`, then build an IPython lexer from a Python 3 lexer. | |
68 |
|
68 | |||
69 | """ |
|
69 | """ | |
70 | # It would be nice to have a single IPython lexer class which takes |
|
70 | # It would be nice to have a single IPython lexer class which takes | |
71 | # a boolean `python3`. But since there are two Python lexer classes, |
|
71 | # a boolean `python3`. But since there are two Python lexer classes, | |
72 | # we will also have two IPython lexer classes. |
|
72 | # we will also have two IPython lexer classes. | |
73 | if python3: |
|
73 | if python3: | |
74 | PyLexer = Python3Lexer |
|
74 | PyLexer = Python3Lexer | |
75 | name = 'IPython3' |
|
75 | name = 'IPython3' | |
76 | aliases = ['ipython3'] |
|
76 | aliases = ['ipython3'] | |
77 | doc = """IPython3 Lexer""" |
|
77 | doc = """IPython3 Lexer""" | |
78 | else: |
|
78 | else: | |
79 | PyLexer = PythonLexer |
|
79 | PyLexer = PythonLexer | |
80 | name = 'IPython' |
|
80 | name = 'IPython' | |
81 | aliases = ['ipython2', 'ipython'] |
|
81 | aliases = ['ipython2', 'ipython'] | |
82 | doc = """IPython Lexer""" |
|
82 | doc = """IPython Lexer""" | |
83 |
|
83 | |||
84 | ipython_tokens = [ |
|
84 | ipython_tokens = [ | |
85 | (r'(?s)(\s*)(%%capture)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
85 | (r'(?s)(\s*)(%%capture)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
86 | (r'(?s)(\s*)(%%debug)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
86 | (r'(?s)(\s*)(%%debug)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
87 | (r'(?is)(\s*)(%%html)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(HtmlLexer))), |
|
87 | (r'(?is)(\s*)(%%html)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(HtmlLexer))), | |
88 | (r'(?s)(\s*)(%%javascript)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(JavascriptLexer))), |
|
88 | (r'(?s)(\s*)(%%javascript)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(JavascriptLexer))), | |
89 | (r'(?s)(\s*)(%%js)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(JavascriptLexer))), |
|
89 | (r'(?s)(\s*)(%%js)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(JavascriptLexer))), | |
90 | (r'(?s)(\s*)(%%latex)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(TexLexer))), |
|
90 | (r'(?s)(\s*)(%%latex)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(TexLexer))), | |
91 | (r'(?s)(\s*)(%%pypy)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PerlLexer))), |
|
91 | (r'(?s)(\s*)(%%pypy)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PerlLexer))), | |
92 | (r'(?s)(\s*)(%%prun)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
92 | (r'(?s)(\s*)(%%prun)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
93 | (r'(?s)(\s*)(%%pypy)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
93 | (r'(?s)(\s*)(%%pypy)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
94 | (r'(?s)(\s*)(%%python)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
94 | (r'(?s)(\s*)(%%python)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
95 | (r'(?s)(\s*)(%%python2)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PythonLexer))), |
|
95 | (r'(?s)(\s*)(%%python2)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PythonLexer))), | |
96 | (r'(?s)(\s*)(%%python3)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(Python3Lexer))), |
|
96 | (r'(?s)(\s*)(%%python3)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(Python3Lexer))), | |
97 | (r'(?s)(\s*)(%%ruby)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(RubyLexer))), |
|
97 | (r'(?s)(\s*)(%%ruby)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(RubyLexer))), | |
98 | (r'(?s)(\s*)(%%time)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
98 | (r'(?s)(\s*)(%%time)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
99 | (r'(?s)(\s*)(%%timeit)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
99 | (r'(?s)(\s*)(%%timeit)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
100 | (r'(?s)(\s*)(%%writefile)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), |
|
100 | (r'(?s)(\s*)(%%writefile)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(PyLexer))), | |
101 | (r"(?s)(\s*)(%%)(\w+)(.*)", bygroups(Text, Operator, Keyword, Text)), |
|
101 | (r"(?s)(\s*)(%%)(\w+)(.*)", bygroups(Text, Operator, Keyword, Text)), | |
102 | (r'(?s)(^\s*)(%%!)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(BashLexer))), |
|
102 | (r'(?s)(^\s*)(%%!)([^\n]*\n)(.*)', bygroups(Text, Operator, Text, using(BashLexer))), | |
103 | (r"(%%?)(\w+)(\?\??)$", bygroups(Operator, Keyword, Operator)), |
|
103 | (r"(%%?)(\w+)(\?\??)$", bygroups(Operator, Keyword, Operator)), | |
104 | (r"\b(\?\??)(\s*)$", bygroups(Operator, Text)), |
|
104 | (r"\b(\?\??)(\s*)$", bygroups(Operator, Text)), | |
105 | (r'(%)(sx|sc|system)(.*)(\n)', bygroups(Operator, Keyword, |
|
105 | (r'(%)(sx|sc|system)(.*)(\n)', bygroups(Operator, Keyword, | |
106 | using(BashLexer), Text)), |
|
106 | using(BashLexer), Text)), | |
107 | (r'(%)(\w+)(.*\n)', bygroups(Operator, Keyword, Text)), |
|
107 | (r'(%)(\w+)(.*\n)', bygroups(Operator, Keyword, Text)), | |
108 | (r'^(!!)(.+)(\n)', bygroups(Operator, using(BashLexer), Text)), |
|
108 | (r'^(!!)(.+)(\n)', bygroups(Operator, using(BashLexer), Text)), | |
109 | (r'(!)(?!=)(.+)(\n)', bygroups(Operator, using(BashLexer), Text)), |
|
109 | (r'(!)(?!=)(.+)(\n)', bygroups(Operator, using(BashLexer), Text)), | |
110 | (r'^(\s*)(\?\??)(\s*%{0,2}[\w\.\*]*)', bygroups(Text, Operator, Text)), |
|
110 | (r'^(\s*)(\?\??)(\s*%{0,2}[\w\.\*]*)', bygroups(Text, Operator, Text)), | |
111 | (r'(\s*%{0,2}[\w\.\*]*)(\?\??)(\s*)$', bygroups(Text, Operator, Text)), |
|
111 | (r'(\s*%{0,2}[\w\.\*]*)(\?\??)(\s*)$', bygroups(Text, Operator, Text)), | |
112 | ] |
|
112 | ] | |
113 |
|
113 | |||
114 | tokens = PyLexer.tokens.copy() |
|
114 | tokens = PyLexer.tokens.copy() | |
115 | tokens['root'] = ipython_tokens + tokens['root'] |
|
115 | tokens['root'] = ipython_tokens + tokens['root'] | |
116 |
|
116 | |||
117 | attrs = {'name': name, 'aliases': aliases, 'filenames': [], |
|
117 | attrs = {'name': name, 'aliases': aliases, 'filenames': [], | |
118 | '__doc__': doc, 'tokens': tokens} |
|
118 | '__doc__': doc, 'tokens': tokens} | |
119 |
|
119 | |||
120 | return type(name, (PyLexer,), attrs) |
|
120 | return type(name, (PyLexer,), attrs) | |
121 |
|
121 | |||
122 |
|
122 | |||
123 | IPython3Lexer = build_ipy_lexer(python3=True) |
|
123 | IPython3Lexer = build_ipy_lexer(python3=True) | |
124 | IPythonLexer = build_ipy_lexer(python3=False) |
|
124 | IPythonLexer = build_ipy_lexer(python3=False) | |
125 |
|
125 | |||
126 |
|
126 | |||
127 | class IPythonPartialTracebackLexer(RegexLexer): |
|
127 | class IPythonPartialTracebackLexer(RegexLexer): | |
128 | """ |
|
128 | """ | |
129 | Partial lexer for IPython tracebacks. |
|
129 | Partial lexer for IPython tracebacks. | |
130 |
|
130 | |||
131 | Handles all the non-python output. This works for both Python 2.x and 3.x. |
|
131 | Handles all the non-python output. This works for both Python 2.x and 3.x. | |
132 |
|
132 | |||
133 | """ |
|
133 | """ | |
134 | name = 'IPython Partial Traceback' |
|
134 | name = 'IPython Partial Traceback' | |
135 |
|
135 | |||
136 | tokens = { |
|
136 | tokens = { | |
137 | 'root': [ |
|
137 | 'root': [ | |
138 | # Tracebacks for syntax errors have a different style. |
|
138 | # Tracebacks for syntax errors have a different style. | |
139 | # For both types of tracebacks, we mark the first line with |
|
139 | # For both types of tracebacks, we mark the first line with | |
140 | # Generic.Traceback. For syntax errors, we mark the filename |
|
140 | # Generic.Traceback. For syntax errors, we mark the filename | |
141 | # as we mark the filenames for non-syntax tracebacks. |
|
141 | # as we mark the filenames for non-syntax tracebacks. | |
142 | # |
|
142 | # | |
143 | # These two regexps define how IPythonConsoleLexer finds a |
|
143 | # These two regexps define how IPythonConsoleLexer finds a | |
144 | # traceback. |
|
144 | # traceback. | |
145 | # |
|
145 | # | |
146 | ## Non-syntax traceback |
|
146 | ## Non-syntax traceback | |
147 | (r'^(\^C)?(-+\n)', bygroups(Error, Generic.Traceback)), |
|
147 | (r'^(\^C)?(-+\n)', bygroups(Error, Generic.Traceback)), | |
148 | ## Syntax traceback |
|
148 | ## Syntax traceback | |
149 | (r'^( File)(.*)(, line )(\d+\n)', |
|
149 | (r'^( File)(.*)(, line )(\d+\n)', | |
150 | bygroups(Generic.Traceback, Name.Namespace, |
|
150 | bygroups(Generic.Traceback, Name.Namespace, | |
151 | Generic.Traceback, Literal.Number.Integer)), |
|
151 | Generic.Traceback, Literal.Number.Integer)), | |
152 |
|
152 | |||
153 | # (Exception Identifier)(Whitespace)(Traceback Message) |
|
153 | # (Exception Identifier)(Whitespace)(Traceback Message) | |
154 | (r'(?u)(^[^\d\W]\w*)(\s*)(Traceback.*?\n)', |
|
154 | (r'(?u)(^[^\d\W]\w*)(\s*)(Traceback.*?\n)', | |
155 | bygroups(Name.Exception, Generic.Whitespace, Text)), |
|
155 | bygroups(Name.Exception, Generic.Whitespace, Text)), | |
156 | # (Module/Filename)(Text)(Callee)(Function Signature) |
|
156 | # (Module/Filename)(Text)(Callee)(Function Signature) | |
157 | # Better options for callee and function signature? |
|
157 | # Better options for callee and function signature? | |
158 | (r'(.*)( in )(.*)(\(.*\)\n)', |
|
158 | (r'(.*)( in )(.*)(\(.*\)\n)', | |
159 | bygroups(Name.Namespace, Text, Name.Entity, Name.Tag)), |
|
159 | bygroups(Name.Namespace, Text, Name.Entity, Name.Tag)), | |
160 | # Regular line: (Whitespace)(Line Number)(Python Code) |
|
160 | # Regular line: (Whitespace)(Line Number)(Python Code) | |
161 | (r'(\s*?)(\d+)(.*?\n)', |
|
161 | (r'(\s*?)(\d+)(.*?\n)', | |
162 | bygroups(Generic.Whitespace, Literal.Number.Integer, Other)), |
|
162 | bygroups(Generic.Whitespace, Literal.Number.Integer, Other)), | |
163 | # Emphasized line: (Arrow)(Line Number)(Python Code) |
|
163 | # Emphasized line: (Arrow)(Line Number)(Python Code) | |
164 | # Using Exception token so arrow color matches the Exception. |
|
164 | # Using Exception token so arrow color matches the Exception. | |
165 | (r'(-*>?\s?)(\d+)(.*?\n)', |
|
165 | (r'(-*>?\s?)(\d+)(.*?\n)', | |
166 | bygroups(Name.Exception, Literal.Number.Integer, Other)), |
|
166 | bygroups(Name.Exception, Literal.Number.Integer, Other)), | |
167 | # (Exception Identifier)(Message) |
|
167 | # (Exception Identifier)(Message) | |
168 | (r'(?u)(^[^\d\W]\w*)(:.*?\n)', |
|
168 | (r'(?u)(^[^\d\W]\w*)(:.*?\n)', | |
169 | bygroups(Name.Exception, Text)), |
|
169 | bygroups(Name.Exception, Text)), | |
170 | # Tag everything else as Other, will be handled later. |
|
170 | # Tag everything else as Other, will be handled later. | |
171 | (r'.*\n', Other), |
|
171 | (r'.*\n', Other), | |
172 | ], |
|
172 | ], | |
173 | } |
|
173 | } | |
174 |
|
174 | |||
175 |
|
175 | |||
176 | class IPythonTracebackLexer(DelegatingLexer): |
|
176 | class IPythonTracebackLexer(DelegatingLexer): | |
177 | """ |
|
177 | """ | |
178 | IPython traceback lexer. |
|
178 | IPython traceback lexer. | |
179 |
|
179 | |||
180 | For doctests, the tracebacks can be snipped as much as desired with the |
|
180 | For doctests, the tracebacks can be snipped as much as desired with the | |
181 | exception to the lines that designate a traceback. For non-syntax error |
|
181 | exception to the lines that designate a traceback. For non-syntax error | |
182 | tracebacks, this is the line of hyphens. For syntax error tracebacks, |
|
182 | tracebacks, this is the line of hyphens. For syntax error tracebacks, | |
183 | this is the line which lists the File and line number. |
|
183 | this is the line which lists the File and line number. | |
184 |
|
184 | |||
185 | """ |
|
185 | """ | |
186 | # The lexer inherits from DelegatingLexer. The "root" lexer is an |
|
186 | # The lexer inherits from DelegatingLexer. The "root" lexer is an | |
187 | # appropriate IPython lexer, which depends on the value of the boolean |
|
187 | # appropriate IPython lexer, which depends on the value of the boolean | |
188 | # `python3`. First, we parse with the partial IPython traceback lexer. |
|
188 | # `python3`. First, we parse with the partial IPython traceback lexer. | |
189 | # Then, any code marked with the "Other" token is delegated to the root |
|
189 | # Then, any code marked with the "Other" token is delegated to the root | |
190 | # lexer. |
|
190 | # lexer. | |
191 | # |
|
191 | # | |
192 | name = 'IPython Traceback' |
|
192 | name = 'IPython Traceback' | |
193 | aliases = ['ipythontb'] |
|
193 | aliases = ['ipythontb'] | |
194 |
|
194 | |||
195 | def __init__(self, **options): |
|
195 | def __init__(self, **options): | |
196 | self.python3 = get_bool_opt(options, 'python3', False) |
|
196 | self.python3 = get_bool_opt(options, 'python3', False) | |
197 | if self.python3: |
|
197 | if self.python3: | |
198 | self.aliases = ['ipython3tb'] |
|
198 | self.aliases = ['ipython3tb'] | |
199 | else: |
|
199 | else: | |
200 | self.aliases = ['ipython2tb', 'ipythontb'] |
|
200 | self.aliases = ['ipython2tb', 'ipythontb'] | |
201 |
|
201 | |||
202 | if self.python3: |
|
202 | if self.python3: | |
203 | IPyLexer = IPython3Lexer |
|
203 | IPyLexer = IPython3Lexer | |
204 | else: |
|
204 | else: | |
205 | IPyLexer = IPythonLexer |
|
205 | IPyLexer = IPythonLexer | |
206 |
|
206 | |||
207 | DelegatingLexer.__init__(self, IPyLexer, |
|
207 | DelegatingLexer.__init__(self, IPyLexer, | |
208 | IPythonPartialTracebackLexer, **options) |
|
208 | IPythonPartialTracebackLexer, **options) | |
209 |
|
209 | |||
210 | class IPythonConsoleLexer(Lexer): |
|
210 | class IPythonConsoleLexer(Lexer): | |
211 | """ |
|
211 | """ | |
212 | An IPython console lexer for IPython code-blocks and doctests, such as: |
|
212 | An IPython console lexer for IPython code-blocks and doctests, such as: | |
213 |
|
213 | |||
214 | .. code-block:: rst |
|
214 | .. code-block:: rst | |
215 |
|
215 | |||
216 | .. code-block:: ipythonconsole |
|
216 | .. code-block:: ipythonconsole | |
217 |
|
217 | |||
218 | In [1]: a = 'foo' |
|
218 | In [1]: a = 'foo' | |
219 |
|
219 | |||
220 | In [2]: a |
|
220 | In [2]: a | |
221 | Out[2]: 'foo' |
|
221 | Out[2]: 'foo' | |
222 |
|
222 | |||
223 | In [3]: print a |
|
223 | In [3]: print a | |
224 | foo |
|
224 | foo | |
225 |
|
225 | |||
226 | In [4]: 1 / 0 |
|
226 | In [4]: 1 / 0 | |
227 |
|
227 | |||
228 |
|
228 | |||
229 | Support is also provided for IPython exceptions: |
|
229 | Support is also provided for IPython exceptions: | |
230 |
|
230 | |||
231 | .. code-block:: rst |
|
231 | .. code-block:: rst | |
232 |
|
232 | |||
233 | .. code-block:: ipythonconsole |
|
233 | .. code-block:: ipythonconsole | |
234 |
|
234 | |||
235 | In [1]: raise Exception |
|
235 | In [1]: raise Exception | |
236 |
|
236 | |||
237 | --------------------------------------------------------------------------- |
|
237 | --------------------------------------------------------------------------- | |
238 | Exception Traceback (most recent call last) |
|
238 | Exception Traceback (most recent call last) | |
239 |
<ipython-input-1-fca2ab0ca76b> in <module> |
|
239 | <ipython-input-1-fca2ab0ca76b> in <module> | |
240 | ----> 1 raise Exception |
|
240 | ----> 1 raise Exception | |
241 |
|
241 | |||
242 | Exception: |
|
242 | Exception: | |
243 |
|
243 | |||
244 | """ |
|
244 | """ | |
245 | name = 'IPython console session' |
|
245 | name = 'IPython console session' | |
246 | aliases = ['ipythonconsole'] |
|
246 | aliases = ['ipythonconsole'] | |
247 | mimetypes = ['text/x-ipython-console'] |
|
247 | mimetypes = ['text/x-ipython-console'] | |
248 |
|
248 | |||
249 | # The regexps used to determine what is input and what is output. |
|
249 | # The regexps used to determine what is input and what is output. | |
250 | # The default prompts for IPython are: |
|
250 | # The default prompts for IPython are: | |
251 | # |
|
251 | # | |
252 | # in = 'In [#]: ' |
|
252 | # in = 'In [#]: ' | |
253 | # continuation = ' .D.: ' |
|
253 | # continuation = ' .D.: ' | |
254 | # template = 'Out[#]: ' |
|
254 | # template = 'Out[#]: ' | |
255 | # |
|
255 | # | |
256 | # Where '#' is the 'prompt number' or 'execution count' and 'D' |
|
256 | # Where '#' is the 'prompt number' or 'execution count' and 'D' | |
257 | # D is a number of dots matching the width of the execution count |
|
257 | # D is a number of dots matching the width of the execution count | |
258 | # |
|
258 | # | |
259 | in1_regex = r'In \[[0-9]+\]: ' |
|
259 | in1_regex = r'In \[[0-9]+\]: ' | |
260 | in2_regex = r' \.\.+\.: ' |
|
260 | in2_regex = r' \.\.+\.: ' | |
261 | out_regex = r'Out\[[0-9]+\]: ' |
|
261 | out_regex = r'Out\[[0-9]+\]: ' | |
262 |
|
262 | |||
263 | #: The regex to determine when a traceback starts. |
|
263 | #: The regex to determine when a traceback starts. | |
264 | ipytb_start = re.compile(r'^(\^C)?(-+\n)|^( File)(.*)(, line )(\d+\n)') |
|
264 | ipytb_start = re.compile(r'^(\^C)?(-+\n)|^( File)(.*)(, line )(\d+\n)') | |
265 |
|
265 | |||
266 | def __init__(self, **options): |
|
266 | def __init__(self, **options): | |
267 | """Initialize the IPython console lexer. |
|
267 | """Initialize the IPython console lexer. | |
268 |
|
268 | |||
269 | Parameters |
|
269 | Parameters | |
270 | ---------- |
|
270 | ---------- | |
271 | python3 : bool |
|
271 | python3 : bool | |
272 | If `True`, then the console inputs are parsed using a Python 3 |
|
272 | If `True`, then the console inputs are parsed using a Python 3 | |
273 | lexer. Otherwise, they are parsed using a Python 2 lexer. |
|
273 | lexer. Otherwise, they are parsed using a Python 2 lexer. | |
274 | in1_regex : RegexObject |
|
274 | in1_regex : RegexObject | |
275 | The compiled regular expression used to detect the start |
|
275 | The compiled regular expression used to detect the start | |
276 | of inputs. Although the IPython configuration setting may have a |
|
276 | of inputs. Although the IPython configuration setting may have a | |
277 | trailing whitespace, do not include it in the regex. If `None`, |
|
277 | trailing whitespace, do not include it in the regex. If `None`, | |
278 | then the default input prompt is assumed. |
|
278 | then the default input prompt is assumed. | |
279 | in2_regex : RegexObject |
|
279 | in2_regex : RegexObject | |
280 | The compiled regular expression used to detect the continuation |
|
280 | The compiled regular expression used to detect the continuation | |
281 | of inputs. Although the IPython configuration setting may have a |
|
281 | of inputs. Although the IPython configuration setting may have a | |
282 | trailing whitespace, do not include it in the regex. If `None`, |
|
282 | trailing whitespace, do not include it in the regex. If `None`, | |
283 | then the default input prompt is assumed. |
|
283 | then the default input prompt is assumed. | |
284 | out_regex : RegexObject |
|
284 | out_regex : RegexObject | |
285 | The compiled regular expression used to detect outputs. If `None`, |
|
285 | The compiled regular expression used to detect outputs. If `None`, | |
286 | then the default output prompt is assumed. |
|
286 | then the default output prompt is assumed. | |
287 |
|
287 | |||
288 | """ |
|
288 | """ | |
289 | self.python3 = get_bool_opt(options, 'python3', False) |
|
289 | self.python3 = get_bool_opt(options, 'python3', False) | |
290 | if self.python3: |
|
290 | if self.python3: | |
291 | self.aliases = ['ipython3console'] |
|
291 | self.aliases = ['ipython3console'] | |
292 | else: |
|
292 | else: | |
293 | self.aliases = ['ipython2console', 'ipythonconsole'] |
|
293 | self.aliases = ['ipython2console', 'ipythonconsole'] | |
294 |
|
294 | |||
295 | in1_regex = options.get('in1_regex', self.in1_regex) |
|
295 | in1_regex = options.get('in1_regex', self.in1_regex) | |
296 | in2_regex = options.get('in2_regex', self.in2_regex) |
|
296 | in2_regex = options.get('in2_regex', self.in2_regex) | |
297 | out_regex = options.get('out_regex', self.out_regex) |
|
297 | out_regex = options.get('out_regex', self.out_regex) | |
298 |
|
298 | |||
299 | # So that we can work with input and output prompts which have been |
|
299 | # So that we can work with input and output prompts which have been | |
300 | # rstrip'd (possibly by editors) we also need rstrip'd variants. If |
|
300 | # rstrip'd (possibly by editors) we also need rstrip'd variants. If | |
301 | # we do not do this, then such prompts will be tagged as 'output'. |
|
301 | # we do not do this, then such prompts will be tagged as 'output'. | |
302 | # The reason can't just use the rstrip'd variants instead is because |
|
302 | # The reason can't just use the rstrip'd variants instead is because | |
303 | # we want any whitespace associated with the prompt to be inserted |
|
303 | # we want any whitespace associated with the prompt to be inserted | |
304 | # with the token. This allows formatted code to be modified so as hide |
|
304 | # with the token. This allows formatted code to be modified so as hide | |
305 | # the appearance of prompts, with the whitespace included. One example |
|
305 | # the appearance of prompts, with the whitespace included. One example | |
306 | # use of this is in copybutton.js from the standard lib Python docs. |
|
306 | # use of this is in copybutton.js from the standard lib Python docs. | |
307 | in1_regex_rstrip = in1_regex.rstrip() + '\n' |
|
307 | in1_regex_rstrip = in1_regex.rstrip() + '\n' | |
308 | in2_regex_rstrip = in2_regex.rstrip() + '\n' |
|
308 | in2_regex_rstrip = in2_regex.rstrip() + '\n' | |
309 | out_regex_rstrip = out_regex.rstrip() + '\n' |
|
309 | out_regex_rstrip = out_regex.rstrip() + '\n' | |
310 |
|
310 | |||
311 | # Compile and save them all. |
|
311 | # Compile and save them all. | |
312 | attrs = ['in1_regex', 'in2_regex', 'out_regex', |
|
312 | attrs = ['in1_regex', 'in2_regex', 'out_regex', | |
313 | 'in1_regex_rstrip', 'in2_regex_rstrip', 'out_regex_rstrip'] |
|
313 | 'in1_regex_rstrip', 'in2_regex_rstrip', 'out_regex_rstrip'] | |
314 | for attr in attrs: |
|
314 | for attr in attrs: | |
315 | self.__setattr__(attr, re.compile(locals()[attr])) |
|
315 | self.__setattr__(attr, re.compile(locals()[attr])) | |
316 |
|
316 | |||
317 | Lexer.__init__(self, **options) |
|
317 | Lexer.__init__(self, **options) | |
318 |
|
318 | |||
319 | if self.python3: |
|
319 | if self.python3: | |
320 | pylexer = IPython3Lexer |
|
320 | pylexer = IPython3Lexer | |
321 | tblexer = IPythonTracebackLexer |
|
321 | tblexer = IPythonTracebackLexer | |
322 | else: |
|
322 | else: | |
323 | pylexer = IPythonLexer |
|
323 | pylexer = IPythonLexer | |
324 | tblexer = IPythonTracebackLexer |
|
324 | tblexer = IPythonTracebackLexer | |
325 |
|
325 | |||
326 | self.pylexer = pylexer(**options) |
|
326 | self.pylexer = pylexer(**options) | |
327 | self.tblexer = tblexer(**options) |
|
327 | self.tblexer = tblexer(**options) | |
328 |
|
328 | |||
329 | self.reset() |
|
329 | self.reset() | |
330 |
|
330 | |||
331 | def reset(self): |
|
331 | def reset(self): | |
332 | self.mode = 'output' |
|
332 | self.mode = 'output' | |
333 | self.index = 0 |
|
333 | self.index = 0 | |
334 | self.buffer = u'' |
|
334 | self.buffer = u'' | |
335 | self.insertions = [] |
|
335 | self.insertions = [] | |
336 |
|
336 | |||
337 | def buffered_tokens(self): |
|
337 | def buffered_tokens(self): | |
338 | """ |
|
338 | """ | |
339 | Generator of unprocessed tokens after doing insertions and before |
|
339 | Generator of unprocessed tokens after doing insertions and before | |
340 | changing to a new state. |
|
340 | changing to a new state. | |
341 |
|
341 | |||
342 | """ |
|
342 | """ | |
343 | if self.mode == 'output': |
|
343 | if self.mode == 'output': | |
344 | tokens = [(0, Generic.Output, self.buffer)] |
|
344 | tokens = [(0, Generic.Output, self.buffer)] | |
345 | elif self.mode == 'input': |
|
345 | elif self.mode == 'input': | |
346 | tokens = self.pylexer.get_tokens_unprocessed(self.buffer) |
|
346 | tokens = self.pylexer.get_tokens_unprocessed(self.buffer) | |
347 | else: # traceback |
|
347 | else: # traceback | |
348 | tokens = self.tblexer.get_tokens_unprocessed(self.buffer) |
|
348 | tokens = self.tblexer.get_tokens_unprocessed(self.buffer) | |
349 |
|
349 | |||
350 | for i, t, v in do_insertions(self.insertions, tokens): |
|
350 | for i, t, v in do_insertions(self.insertions, tokens): | |
351 | # All token indexes are relative to the buffer. |
|
351 | # All token indexes are relative to the buffer. | |
352 | yield self.index + i, t, v |
|
352 | yield self.index + i, t, v | |
353 |
|
353 | |||
354 | # Clear it all |
|
354 | # Clear it all | |
355 | self.index += len(self.buffer) |
|
355 | self.index += len(self.buffer) | |
356 | self.buffer = u'' |
|
356 | self.buffer = u'' | |
357 | self.insertions = [] |
|
357 | self.insertions = [] | |
358 |
|
358 | |||
359 | def get_mci(self, line): |
|
359 | def get_mci(self, line): | |
360 | """ |
|
360 | """ | |
361 | Parses the line and returns a 3-tuple: (mode, code, insertion). |
|
361 | Parses the line and returns a 3-tuple: (mode, code, insertion). | |
362 |
|
362 | |||
363 | `mode` is the next mode (or state) of the lexer, and is always equal |
|
363 | `mode` is the next mode (or state) of the lexer, and is always equal | |
364 | to 'input', 'output', or 'tb'. |
|
364 | to 'input', 'output', or 'tb'. | |
365 |
|
365 | |||
366 | `code` is a portion of the line that should be added to the buffer |
|
366 | `code` is a portion of the line that should be added to the buffer | |
367 | corresponding to the next mode and eventually lexed by another lexer. |
|
367 | corresponding to the next mode and eventually lexed by another lexer. | |
368 | For example, `code` could be Python code if `mode` were 'input'. |
|
368 | For example, `code` could be Python code if `mode` were 'input'. | |
369 |
|
369 | |||
370 | `insertion` is a 3-tuple (index, token, text) representing an |
|
370 | `insertion` is a 3-tuple (index, token, text) representing an | |
371 | unprocessed "token" that will be inserted into the stream of tokens |
|
371 | unprocessed "token" that will be inserted into the stream of tokens | |
372 | that are created from the buffer once we change modes. This is usually |
|
372 | that are created from the buffer once we change modes. This is usually | |
373 | the input or output prompt. |
|
373 | the input or output prompt. | |
374 |
|
374 | |||
375 | In general, the next mode depends on current mode and on the contents |
|
375 | In general, the next mode depends on current mode and on the contents | |
376 | of `line`. |
|
376 | of `line`. | |
377 |
|
377 | |||
378 | """ |
|
378 | """ | |
379 | # To reduce the number of regex match checks, we have multiple |
|
379 | # To reduce the number of regex match checks, we have multiple | |
380 | # 'if' blocks instead of 'if-elif' blocks. |
|
380 | # 'if' blocks instead of 'if-elif' blocks. | |
381 |
|
381 | |||
382 | # Check for possible end of input |
|
382 | # Check for possible end of input | |
383 | in2_match = self.in2_regex.match(line) |
|
383 | in2_match = self.in2_regex.match(line) | |
384 | in2_match_rstrip = self.in2_regex_rstrip.match(line) |
|
384 | in2_match_rstrip = self.in2_regex_rstrip.match(line) | |
385 | if (in2_match and in2_match.group().rstrip() == line.rstrip()) or \ |
|
385 | if (in2_match and in2_match.group().rstrip() == line.rstrip()) or \ | |
386 | in2_match_rstrip: |
|
386 | in2_match_rstrip: | |
387 | end_input = True |
|
387 | end_input = True | |
388 | else: |
|
388 | else: | |
389 | end_input = False |
|
389 | end_input = False | |
390 | if end_input and self.mode != 'tb': |
|
390 | if end_input and self.mode != 'tb': | |
391 | # Only look for an end of input when not in tb mode. |
|
391 | # Only look for an end of input when not in tb mode. | |
392 | # An ellipsis could appear within the traceback. |
|
392 | # An ellipsis could appear within the traceback. | |
393 | mode = 'output' |
|
393 | mode = 'output' | |
394 | code = u'' |
|
394 | code = u'' | |
395 | insertion = (0, Generic.Prompt, line) |
|
395 | insertion = (0, Generic.Prompt, line) | |
396 | return mode, code, insertion |
|
396 | return mode, code, insertion | |
397 |
|
397 | |||
398 | # Check for output prompt |
|
398 | # Check for output prompt | |
399 | out_match = self.out_regex.match(line) |
|
399 | out_match = self.out_regex.match(line) | |
400 | out_match_rstrip = self.out_regex_rstrip.match(line) |
|
400 | out_match_rstrip = self.out_regex_rstrip.match(line) | |
401 | if out_match or out_match_rstrip: |
|
401 | if out_match or out_match_rstrip: | |
402 | mode = 'output' |
|
402 | mode = 'output' | |
403 | if out_match: |
|
403 | if out_match: | |
404 | idx = out_match.end() |
|
404 | idx = out_match.end() | |
405 | else: |
|
405 | else: | |
406 | idx = out_match_rstrip.end() |
|
406 | idx = out_match_rstrip.end() | |
407 | code = line[idx:] |
|
407 | code = line[idx:] | |
408 | # Use the 'heading' token for output. We cannot use Generic.Error |
|
408 | # Use the 'heading' token for output. We cannot use Generic.Error | |
409 | # since it would conflict with exceptions. |
|
409 | # since it would conflict with exceptions. | |
410 | insertion = (0, Generic.Heading, line[:idx]) |
|
410 | insertion = (0, Generic.Heading, line[:idx]) | |
411 | return mode, code, insertion |
|
411 | return mode, code, insertion | |
412 |
|
412 | |||
413 |
|
413 | |||
414 | # Check for input or continuation prompt (non stripped version) |
|
414 | # Check for input or continuation prompt (non stripped version) | |
415 | in1_match = self.in1_regex.match(line) |
|
415 | in1_match = self.in1_regex.match(line) | |
416 | if in1_match or (in2_match and self.mode != 'tb'): |
|
416 | if in1_match or (in2_match and self.mode != 'tb'): | |
417 | # New input or when not in tb, continued input. |
|
417 | # New input or when not in tb, continued input. | |
418 | # We do not check for continued input when in tb since it is |
|
418 | # We do not check for continued input when in tb since it is | |
419 | # allowable to replace a long stack with an ellipsis. |
|
419 | # allowable to replace a long stack with an ellipsis. | |
420 | mode = 'input' |
|
420 | mode = 'input' | |
421 | if in1_match: |
|
421 | if in1_match: | |
422 | idx = in1_match.end() |
|
422 | idx = in1_match.end() | |
423 | else: # in2_match |
|
423 | else: # in2_match | |
424 | idx = in2_match.end() |
|
424 | idx = in2_match.end() | |
425 | code = line[idx:] |
|
425 | code = line[idx:] | |
426 | insertion = (0, Generic.Prompt, line[:idx]) |
|
426 | insertion = (0, Generic.Prompt, line[:idx]) | |
427 | return mode, code, insertion |
|
427 | return mode, code, insertion | |
428 |
|
428 | |||
429 | # Check for input or continuation prompt (stripped version) |
|
429 | # Check for input or continuation prompt (stripped version) | |
430 | in1_match_rstrip = self.in1_regex_rstrip.match(line) |
|
430 | in1_match_rstrip = self.in1_regex_rstrip.match(line) | |
431 | if in1_match_rstrip or (in2_match_rstrip and self.mode != 'tb'): |
|
431 | if in1_match_rstrip or (in2_match_rstrip and self.mode != 'tb'): | |
432 | # New input or when not in tb, continued input. |
|
432 | # New input or when not in tb, continued input. | |
433 | # We do not check for continued input when in tb since it is |
|
433 | # We do not check for continued input when in tb since it is | |
434 | # allowable to replace a long stack with an ellipsis. |
|
434 | # allowable to replace a long stack with an ellipsis. | |
435 | mode = 'input' |
|
435 | mode = 'input' | |
436 | if in1_match_rstrip: |
|
436 | if in1_match_rstrip: | |
437 | idx = in1_match_rstrip.end() |
|
437 | idx = in1_match_rstrip.end() | |
438 | else: # in2_match |
|
438 | else: # in2_match | |
439 | idx = in2_match_rstrip.end() |
|
439 | idx = in2_match_rstrip.end() | |
440 | code = line[idx:] |
|
440 | code = line[idx:] | |
441 | insertion = (0, Generic.Prompt, line[:idx]) |
|
441 | insertion = (0, Generic.Prompt, line[:idx]) | |
442 | return mode, code, insertion |
|
442 | return mode, code, insertion | |
443 |
|
443 | |||
444 | # Check for traceback |
|
444 | # Check for traceback | |
445 | if self.ipytb_start.match(line): |
|
445 | if self.ipytb_start.match(line): | |
446 | mode = 'tb' |
|
446 | mode = 'tb' | |
447 | code = line |
|
447 | code = line | |
448 | insertion = None |
|
448 | insertion = None | |
449 | return mode, code, insertion |
|
449 | return mode, code, insertion | |
450 |
|
450 | |||
451 | # All other stuff... |
|
451 | # All other stuff... | |
452 | if self.mode in ('input', 'output'): |
|
452 | if self.mode in ('input', 'output'): | |
453 | # We assume all other text is output. Multiline input that |
|
453 | # We assume all other text is output. Multiline input that | |
454 | # does not use the continuation marker cannot be detected. |
|
454 | # does not use the continuation marker cannot be detected. | |
455 | # For example, the 3 in the following is clearly output: |
|
455 | # For example, the 3 in the following is clearly output: | |
456 | # |
|
456 | # | |
457 | # In [1]: print 3 |
|
457 | # In [1]: print 3 | |
458 | # 3 |
|
458 | # 3 | |
459 | # |
|
459 | # | |
460 | # But the following second line is part of the input: |
|
460 | # But the following second line is part of the input: | |
461 | # |
|
461 | # | |
462 | # In [2]: while True: |
|
462 | # In [2]: while True: | |
463 | # print True |
|
463 | # print True | |
464 | # |
|
464 | # | |
465 | # In both cases, the 2nd line will be 'output'. |
|
465 | # In both cases, the 2nd line will be 'output'. | |
466 | # |
|
466 | # | |
467 | mode = 'output' |
|
467 | mode = 'output' | |
468 | else: |
|
468 | else: | |
469 | mode = 'tb' |
|
469 | mode = 'tb' | |
470 |
|
470 | |||
471 | code = line |
|
471 | code = line | |
472 | insertion = None |
|
472 | insertion = None | |
473 |
|
473 | |||
474 | return mode, code, insertion |
|
474 | return mode, code, insertion | |
475 |
|
475 | |||
476 | def get_tokens_unprocessed(self, text): |
|
476 | def get_tokens_unprocessed(self, text): | |
477 | self.reset() |
|
477 | self.reset() | |
478 | for match in line_re.finditer(text): |
|
478 | for match in line_re.finditer(text): | |
479 | line = match.group() |
|
479 | line = match.group() | |
480 | mode, code, insertion = self.get_mci(line) |
|
480 | mode, code, insertion = self.get_mci(line) | |
481 |
|
481 | |||
482 | if mode != self.mode: |
|
482 | if mode != self.mode: | |
483 | # Yield buffered tokens before transitioning to new mode. |
|
483 | # Yield buffered tokens before transitioning to new mode. | |
484 | for token in self.buffered_tokens(): |
|
484 | for token in self.buffered_tokens(): | |
485 | yield token |
|
485 | yield token | |
486 | self.mode = mode |
|
486 | self.mode = mode | |
487 |
|
487 | |||
488 | if insertion: |
|
488 | if insertion: | |
489 | self.insertions.append((len(self.buffer), [insertion])) |
|
489 | self.insertions.append((len(self.buffer), [insertion])) | |
490 | self.buffer += code |
|
490 | self.buffer += code | |
491 |
|
491 | |||
492 | for token in self.buffered_tokens(): |
|
492 | for token in self.buffered_tokens(): | |
493 | yield token |
|
493 | yield token | |
494 |
|
494 | |||
495 | class IPyLexer(Lexer): |
|
495 | class IPyLexer(Lexer): | |
496 | """ |
|
496 | """ | |
497 | Primary lexer for all IPython-like code. |
|
497 | Primary lexer for all IPython-like code. | |
498 |
|
498 | |||
499 | This is a simple helper lexer. If the first line of the text begins with |
|
499 | This is a simple helper lexer. If the first line of the text begins with | |
500 | "In \[[0-9]+\]:", then the entire text is parsed with an IPython console |
|
500 | "In \[[0-9]+\]:", then the entire text is parsed with an IPython console | |
501 | lexer. If not, then the entire text is parsed with an IPython lexer. |
|
501 | lexer. If not, then the entire text is parsed with an IPython lexer. | |
502 |
|
502 | |||
503 | The goal is to reduce the number of lexers that are registered |
|
503 | The goal is to reduce the number of lexers that are registered | |
504 | with Pygments. |
|
504 | with Pygments. | |
505 |
|
505 | |||
506 | """ |
|
506 | """ | |
507 | name = 'IPy session' |
|
507 | name = 'IPy session' | |
508 | aliases = ['ipy'] |
|
508 | aliases = ['ipy'] | |
509 |
|
509 | |||
510 | def __init__(self, **options): |
|
510 | def __init__(self, **options): | |
511 | self.python3 = get_bool_opt(options, 'python3', False) |
|
511 | self.python3 = get_bool_opt(options, 'python3', False) | |
512 | if self.python3: |
|
512 | if self.python3: | |
513 | self.aliases = ['ipy3'] |
|
513 | self.aliases = ['ipy3'] | |
514 | else: |
|
514 | else: | |
515 | self.aliases = ['ipy2', 'ipy'] |
|
515 | self.aliases = ['ipy2', 'ipy'] | |
516 |
|
516 | |||
517 | Lexer.__init__(self, **options) |
|
517 | Lexer.__init__(self, **options) | |
518 |
|
518 | |||
519 | self.IPythonLexer = IPythonLexer(**options) |
|
519 | self.IPythonLexer = IPythonLexer(**options) | |
520 | self.IPythonConsoleLexer = IPythonConsoleLexer(**options) |
|
520 | self.IPythonConsoleLexer = IPythonConsoleLexer(**options) | |
521 |
|
521 | |||
522 | def get_tokens_unprocessed(self, text): |
|
522 | def get_tokens_unprocessed(self, text): | |
523 | # Search for the input prompt anywhere...this allows code blocks to |
|
523 | # Search for the input prompt anywhere...this allows code blocks to | |
524 | # begin with comments as well. |
|
524 | # begin with comments as well. | |
525 | if re.match(r'.*(In \[[0-9]+\]:)', text.strip(), re.DOTALL): |
|
525 | if re.match(r'.*(In \[[0-9]+\]:)', text.strip(), re.DOTALL): | |
526 | lex = self.IPythonConsoleLexer |
|
526 | lex = self.IPythonConsoleLexer | |
527 | else: |
|
527 | else: | |
528 | lex = self.IPythonLexer |
|
528 | lex = self.IPythonLexer | |
529 | for token in lex.get_tokens_unprocessed(text): |
|
529 | for token in lex.get_tokens_unprocessed(text): | |
530 | yield token |
|
530 | yield token | |
531 |
|
531 |
@@ -1,1037 +1,1037 b'' | |||||
1 | ================= |
|
1 | ================= | |
2 | IPython reference |
|
2 | IPython reference | |
3 | ================= |
|
3 | ================= | |
4 |
|
4 | |||
5 | .. _command_line_options: |
|
5 | .. _command_line_options: | |
6 |
|
6 | |||
7 | Command-line usage |
|
7 | Command-line usage | |
8 | ================== |
|
8 | ================== | |
9 |
|
9 | |||
10 | You start IPython with the command:: |
|
10 | You start IPython with the command:: | |
11 |
|
11 | |||
12 | $ ipython [options] files |
|
12 | $ ipython [options] files | |
13 |
|
13 | |||
14 | If invoked with no options, it executes all the files listed in sequence and |
|
14 | If invoked with no options, it executes all the files listed in sequence and | |
15 | exits. If you add the ``-i`` flag, it drops you into the interpreter while still |
|
15 | exits. If you add the ``-i`` flag, it drops you into the interpreter while still | |
16 | acknowledging any options you may have set in your ``ipython_config.py``. This |
|
16 | acknowledging any options you may have set in your ``ipython_config.py``. This | |
17 | behavior is different from standard Python, which when called as python ``-i`` |
|
17 | behavior is different from standard Python, which when called as python ``-i`` | |
18 | will only execute one file and ignore your configuration setup. |
|
18 | will only execute one file and ignore your configuration setup. | |
19 |
|
19 | |||
20 | Please note that some of the configuration options are not available at the |
|
20 | Please note that some of the configuration options are not available at the | |
21 | command line, simply because they are not practical here. Look into your |
|
21 | command line, simply because they are not practical here. Look into your | |
22 | configuration files for details on those. There are separate configuration files |
|
22 | configuration files for details on those. There are separate configuration files | |
23 | for each profile, and the files look like :file:`ipython_config.py` or |
|
23 | for each profile, and the files look like :file:`ipython_config.py` or | |
24 | :file:`ipython_config_{frontendname}.py`. Profile directories look like |
|
24 | :file:`ipython_config_{frontendname}.py`. Profile directories look like | |
25 | :file:`profile_{profilename}` and are typically installed in the |
|
25 | :file:`profile_{profilename}` and are typically installed in the | |
26 | :envvar:`IPYTHONDIR` directory, which defaults to :file:`$HOME/.ipython`. For |
|
26 | :envvar:`IPYTHONDIR` directory, which defaults to :file:`$HOME/.ipython`. For | |
27 | Windows users, :envvar:`HOME` resolves to :file:`C:\\Users\\{YourUserName}` in |
|
27 | Windows users, :envvar:`HOME` resolves to :file:`C:\\Users\\{YourUserName}` in | |
28 | most instances. |
|
28 | most instances. | |
29 |
|
29 | |||
30 | Command-line Options |
|
30 | Command-line Options | |
31 | -------------------- |
|
31 | -------------------- | |
32 |
|
32 | |||
33 | To see the options IPython accepts, use ``ipython --help`` (and you probably |
|
33 | To see the options IPython accepts, use ``ipython --help`` (and you probably | |
34 | should run the output through a pager such as ``ipython --help | less`` for |
|
34 | should run the output through a pager such as ``ipython --help | less`` for | |
35 | more convenient reading). This shows all the options that have a single-word |
|
35 | more convenient reading). This shows all the options that have a single-word | |
36 | alias to control them, but IPython lets you configure all of its objects from |
|
36 | alias to control them, but IPython lets you configure all of its objects from | |
37 | the command-line by passing the full class name and a corresponding value; type |
|
37 | the command-line by passing the full class name and a corresponding value; type | |
38 | ``ipython --help-all`` to see this full list. For example:: |
|
38 | ``ipython --help-all`` to see this full list. For example:: | |
39 |
|
39 | |||
40 | $ ipython --help-all |
|
40 | $ ipython --help-all | |
41 | <...snip...> |
|
41 | <...snip...> | |
42 | --matplotlib=<CaselessStrEnum> (InteractiveShellApp.matplotlib) |
|
42 | --matplotlib=<CaselessStrEnum> (InteractiveShellApp.matplotlib) | |
43 | Default: None |
|
43 | Default: None | |
44 | Choices: ['auto', 'gtk', 'gtk3', 'inline', 'nbagg', 'notebook', 'osx', 'qt', 'qt4', 'qt5', 'tk', 'wx'] |
|
44 | Choices: ['auto', 'gtk', 'gtk3', 'inline', 'nbagg', 'notebook', 'osx', 'qt', 'qt4', 'qt5', 'tk', 'wx'] | |
45 | Configure matplotlib for interactive use with the default matplotlib |
|
45 | Configure matplotlib for interactive use with the default matplotlib | |
46 | backend. |
|
46 | backend. | |
47 | <...snip...> |
|
47 | <...snip...> | |
48 |
|
48 | |||
49 |
|
49 | |||
50 | Indicate that the following:: |
|
50 | Indicate that the following:: | |
51 |
|
51 | |||
52 | $ ipython --matplotlib qt |
|
52 | $ ipython --matplotlib qt | |
53 |
|
53 | |||
54 |
|
54 | |||
55 | is equivalent to:: |
|
55 | is equivalent to:: | |
56 |
|
56 | |||
57 | $ ipython --TerminalIPythonApp.matplotlib='qt' |
|
57 | $ ipython --TerminalIPythonApp.matplotlib='qt' | |
58 |
|
58 | |||
59 | Note that in the second form, you *must* use the equal sign, as the expression |
|
59 | Note that in the second form, you *must* use the equal sign, as the expression | |
60 | is evaluated as an actual Python assignment. While in the above example the |
|
60 | is evaluated as an actual Python assignment. While in the above example the | |
61 | short form is more convenient, only the most common options have a short form, |
|
61 | short form is more convenient, only the most common options have a short form, | |
62 | while any configurable variable in IPython can be set at the command-line by |
|
62 | while any configurable variable in IPython can be set at the command-line by | |
63 | using the long form. This long form is the same syntax used in the |
|
63 | using the long form. This long form is the same syntax used in the | |
64 | configuration files, if you want to set these options permanently. |
|
64 | configuration files, if you want to set these options permanently. | |
65 |
|
65 | |||
66 |
|
66 | |||
67 | Interactive use |
|
67 | Interactive use | |
68 | =============== |
|
68 | =============== | |
69 |
|
69 | |||
70 | IPython is meant to work as a drop-in replacement for the standard interactive |
|
70 | IPython is meant to work as a drop-in replacement for the standard interactive | |
71 | interpreter. As such, any code which is valid python should execute normally |
|
71 | interpreter. As such, any code which is valid python should execute normally | |
72 | under IPython (cases where this is not true should be reported as bugs). It |
|
72 | under IPython (cases where this is not true should be reported as bugs). It | |
73 | does, however, offer many features which are not available at a standard python |
|
73 | does, however, offer many features which are not available at a standard python | |
74 | prompt. What follows is a list of these. |
|
74 | prompt. What follows is a list of these. | |
75 |
|
75 | |||
76 |
|
76 | |||
77 | Caution for Windows users |
|
77 | Caution for Windows users | |
78 | ------------------------- |
|
78 | ------------------------- | |
79 |
|
79 | |||
80 | Windows, unfortunately, uses the '\\' character as a path separator. This is a |
|
80 | Windows, unfortunately, uses the '\\' character as a path separator. This is a | |
81 | terrible choice, because '\\' also represents the escape character in most |
|
81 | terrible choice, because '\\' also represents the escape character in most | |
82 | modern programming languages, including Python. For this reason, using '/' |
|
82 | modern programming languages, including Python. For this reason, using '/' | |
83 | character is recommended if you have problems with ``\``. However, in Windows |
|
83 | character is recommended if you have problems with ``\``. However, in Windows | |
84 | commands '/' flags options, so you can not use it for the root directory. This |
|
84 | commands '/' flags options, so you can not use it for the root directory. This | |
85 | means that paths beginning at the root must be typed in a contrived manner |
|
85 | means that paths beginning at the root must be typed in a contrived manner | |
86 | like: ``%copy \opt/foo/bar.txt \tmp`` |
|
86 | like: ``%copy \opt/foo/bar.txt \tmp`` | |
87 |
|
87 | |||
88 | .. _magic: |
|
88 | .. _magic: | |
89 |
|
89 | |||
90 | Magic command system |
|
90 | Magic command system | |
91 | -------------------- |
|
91 | -------------------- | |
92 |
|
92 | |||
93 | IPython will treat any line whose first character is a % as a special |
|
93 | IPython will treat any line whose first character is a % as a special | |
94 | call to a 'magic' function. These allow you to control the behavior of |
|
94 | call to a 'magic' function. These allow you to control the behavior of | |
95 | IPython itself, plus a lot of system-type features. They are all |
|
95 | IPython itself, plus a lot of system-type features. They are all | |
96 | prefixed with a % character, but parameters are given without |
|
96 | prefixed with a % character, but parameters are given without | |
97 | parentheses or quotes. |
|
97 | parentheses or quotes. | |
98 |
|
98 | |||
99 | Lines that begin with ``%%`` signal a *cell magic*: they take as arguments not |
|
99 | Lines that begin with ``%%`` signal a *cell magic*: they take as arguments not | |
100 | only the rest of the current line, but all lines below them as well, in the |
|
100 | only the rest of the current line, but all lines below them as well, in the | |
101 | current execution block. Cell magics can in fact make arbitrary modifications |
|
101 | current execution block. Cell magics can in fact make arbitrary modifications | |
102 | to the input they receive, which need not even be valid Python code at all. |
|
102 | to the input they receive, which need not even be valid Python code at all. | |
103 | They receive the whole block as a single string. |
|
103 | They receive the whole block as a single string. | |
104 |
|
104 | |||
105 | As a line magic example, the :magic:`cd` magic works just like the OS command of |
|
105 | As a line magic example, the :magic:`cd` magic works just like the OS command of | |
106 | the same name:: |
|
106 | the same name:: | |
107 |
|
107 | |||
108 | In [8]: %cd |
|
108 | In [8]: %cd | |
109 | /home/fperez |
|
109 | /home/fperez | |
110 |
|
110 | |||
111 | The following uses the builtin :magic:`timeit` in cell mode:: |
|
111 | The following uses the builtin :magic:`timeit` in cell mode:: | |
112 |
|
112 | |||
113 | In [10]: %%timeit x = range(10000) |
|
113 | In [10]: %%timeit x = range(10000) | |
114 | ...: min(x) |
|
114 | ...: min(x) | |
115 | ...: max(x) |
|
115 | ...: max(x) | |
116 | ...: |
|
116 | ...: | |
117 | 1000 loops, best of 3: 438 us per loop |
|
117 | 1000 loops, best of 3: 438 us per loop | |
118 |
|
118 | |||
119 | In this case, ``x = range(10000)`` is called as the line argument, and the |
|
119 | In this case, ``x = range(10000)`` is called as the line argument, and the | |
120 | block with ``min(x)`` and ``max(x)`` is called as the cell body. The |
|
120 | block with ``min(x)`` and ``max(x)`` is called as the cell body. The | |
121 | :magic:`timeit` magic receives both. |
|
121 | :magic:`timeit` magic receives both. | |
122 |
|
122 | |||
123 | If you have 'automagic' enabled (as it is by default), you don't need to type in |
|
123 | If you have 'automagic' enabled (as it is by default), you don't need to type in | |
124 | the single ``%`` explicitly for line magics; IPython will scan its internal |
|
124 | the single ``%`` explicitly for line magics; IPython will scan its internal | |
125 | list of magic functions and call one if it exists. With automagic on you can |
|
125 | list of magic functions and call one if it exists. With automagic on you can | |
126 | then just type ``cd mydir`` to go to directory 'mydir':: |
|
126 | then just type ``cd mydir`` to go to directory 'mydir':: | |
127 |
|
127 | |||
128 | In [9]: cd mydir |
|
128 | In [9]: cd mydir | |
129 | /home/fperez/mydir |
|
129 | /home/fperez/mydir | |
130 |
|
130 | |||
131 | Cell magics *always* require an explicit ``%%`` prefix, automagic |
|
131 | Cell magics *always* require an explicit ``%%`` prefix, automagic | |
132 | calling only works for line magics. |
|
132 | calling only works for line magics. | |
133 |
|
133 | |||
134 | The automagic system has the lowest possible precedence in name searches, so |
|
134 | The automagic system has the lowest possible precedence in name searches, so | |
135 | you can freely use variables with the same names as magic commands. If a magic |
|
135 | you can freely use variables with the same names as magic commands. If a magic | |
136 | command is 'shadowed' by a variable, you will need the explicit ``%`` prefix to |
|
136 | command is 'shadowed' by a variable, you will need the explicit ``%`` prefix to | |
137 | use it: |
|
137 | use it: | |
138 |
|
138 | |||
139 | .. sourcecode:: ipython |
|
139 | .. sourcecode:: ipython | |
140 |
|
140 | |||
141 | In [1]: cd ipython # %cd is called by automagic |
|
141 | In [1]: cd ipython # %cd is called by automagic | |
142 | /home/fperez/ipython |
|
142 | /home/fperez/ipython | |
143 |
|
143 | |||
144 | In [2]: cd=1 # now cd is just a variable |
|
144 | In [2]: cd=1 # now cd is just a variable | |
145 |
|
145 | |||
146 | In [3]: cd .. # and doesn't work as a function anymore |
|
146 | In [3]: cd .. # and doesn't work as a function anymore | |
147 | File "<ipython-input-3-9fedb3aff56c>", line 1 |
|
147 | File "<ipython-input-3-9fedb3aff56c>", line 1 | |
148 | cd .. |
|
148 | cd .. | |
149 | ^ |
|
149 | ^ | |
150 | SyntaxError: invalid syntax |
|
150 | SyntaxError: invalid syntax | |
151 |
|
151 | |||
152 |
|
152 | |||
153 | In [4]: %cd .. # but %cd always works |
|
153 | In [4]: %cd .. # but %cd always works | |
154 | /home/fperez |
|
154 | /home/fperez | |
155 |
|
155 | |||
156 | In [5]: del cd # if you remove the cd variable, automagic works again |
|
156 | In [5]: del cd # if you remove the cd variable, automagic works again | |
157 |
|
157 | |||
158 | In [6]: cd ipython |
|
158 | In [6]: cd ipython | |
159 |
|
159 | |||
160 | /home/fperez/ipython |
|
160 | /home/fperez/ipython | |
161 |
|
161 | |||
162 | Line magics, if they return a value, can be assigned to a variable using the |
|
162 | Line magics, if they return a value, can be assigned to a variable using the | |
163 | syntax ``l = %sx ls`` (which in this particular case returns the result of `ls` |
|
163 | syntax ``l = %sx ls`` (which in this particular case returns the result of `ls` | |
164 | as a python list). See :ref:`below <manual_capture>` for more information. |
|
164 | as a python list). See :ref:`below <manual_capture>` for more information. | |
165 |
|
165 | |||
166 | Type ``%magic`` for more information, including a list of all available magic |
|
166 | Type ``%magic`` for more information, including a list of all available magic | |
167 | functions at any time and their docstrings. You can also type |
|
167 | functions at any time and their docstrings. You can also type | |
168 | ``%magic_function_name?`` (see :ref:`below <dynamic_object_info>` for |
|
168 | ``%magic_function_name?`` (see :ref:`below <dynamic_object_info>` for | |
169 | information on the '?' system) to get information about any particular magic |
|
169 | information on the '?' system) to get information about any particular magic | |
170 | function you are interested in. |
|
170 | function you are interested in. | |
171 |
|
171 | |||
172 | The API documentation for the :mod:`IPython.core.magic` module contains the full |
|
172 | The API documentation for the :mod:`IPython.core.magic` module contains the full | |
173 | docstrings of all currently available magic commands. |
|
173 | docstrings of all currently available magic commands. | |
174 |
|
174 | |||
175 | .. seealso:: |
|
175 | .. seealso:: | |
176 |
|
176 | |||
177 | :doc:`magics` |
|
177 | :doc:`magics` | |
178 | A list of the line and cell magics available in IPython by default |
|
178 | A list of the line and cell magics available in IPython by default | |
179 |
|
179 | |||
180 | :ref:`defining_magics` |
|
180 | :ref:`defining_magics` | |
181 | How to define and register additional magic functions |
|
181 | How to define and register additional magic functions | |
182 |
|
182 | |||
183 |
|
183 | |||
184 | Access to the standard Python help |
|
184 | Access to the standard Python help | |
185 | ---------------------------------- |
|
185 | ---------------------------------- | |
186 |
|
186 | |||
187 | Simply type ``help()`` to access Python's standard help system. You can |
|
187 | Simply type ``help()`` to access Python's standard help system. You can | |
188 | also type ``help(object)`` for information about a given object, or |
|
188 | also type ``help(object)`` for information about a given object, or | |
189 | ``help('keyword')`` for information on a keyword. You may need to configure your |
|
189 | ``help('keyword')`` for information on a keyword. You may need to configure your | |
190 | PYTHONDOCS environment variable for this feature to work correctly. |
|
190 | PYTHONDOCS environment variable for this feature to work correctly. | |
191 |
|
191 | |||
192 | .. _dynamic_object_info: |
|
192 | .. _dynamic_object_info: | |
193 |
|
193 | |||
194 | Dynamic object information |
|
194 | Dynamic object information | |
195 | -------------------------- |
|
195 | -------------------------- | |
196 |
|
196 | |||
197 | Typing ``?word`` or ``word?`` prints detailed information about an object. If |
|
197 | Typing ``?word`` or ``word?`` prints detailed information about an object. If | |
198 | certain strings in the object are too long (e.g. function signatures) they get |
|
198 | certain strings in the object are too long (e.g. function signatures) they get | |
199 | snipped in the center for brevity. This system gives access variable types and |
|
199 | snipped in the center for brevity. This system gives access variable types and | |
200 | values, docstrings, function prototypes and other useful information. |
|
200 | values, docstrings, function prototypes and other useful information. | |
201 |
|
201 | |||
202 | If the information will not fit in the terminal, it is displayed in a pager |
|
202 | If the information will not fit in the terminal, it is displayed in a pager | |
203 | (``less`` if available, otherwise a basic internal pager). |
|
203 | (``less`` if available, otherwise a basic internal pager). | |
204 |
|
204 | |||
205 | Typing ``??word`` or ``word??`` gives access to the full information, including |
|
205 | Typing ``??word`` or ``word??`` gives access to the full information, including | |
206 | the source code where possible. Long strings are not snipped. |
|
206 | the source code where possible. Long strings are not snipped. | |
207 |
|
207 | |||
208 | The following magic functions are particularly useful for gathering |
|
208 | The following magic functions are particularly useful for gathering | |
209 | information about your working environment: |
|
209 | information about your working environment: | |
210 |
|
210 | |||
211 | * :magic:`pdoc` **<object>**: Print (or run through a pager if too long) the |
|
211 | * :magic:`pdoc` **<object>**: Print (or run through a pager if too long) the | |
212 | docstring for an object. If the given object is a class, it will |
|
212 | docstring for an object. If the given object is a class, it will | |
213 | print both the class and the constructor docstrings. |
|
213 | print both the class and the constructor docstrings. | |
214 | * :magic:`pdef` **<object>**: Print the call signature for any callable |
|
214 | * :magic:`pdef` **<object>**: Print the call signature for any callable | |
215 | object. If the object is a class, print the constructor information. |
|
215 | object. If the object is a class, print the constructor information. | |
216 | * :magic:`psource` **<object>**: Print (or run through a pager if too long) |
|
216 | * :magic:`psource` **<object>**: Print (or run through a pager if too long) | |
217 | the source code for an object. |
|
217 | the source code for an object. | |
218 | * :magic:`pfile` **<object>**: Show the entire source file where an object was |
|
218 | * :magic:`pfile` **<object>**: Show the entire source file where an object was | |
219 | defined via a pager, opening it at the line where the object |
|
219 | defined via a pager, opening it at the line where the object | |
220 | definition begins. |
|
220 | definition begins. | |
221 | * :magic:`who`/:magic:`whos`: These functions give information about identifiers |
|
221 | * :magic:`who`/:magic:`whos`: These functions give information about identifiers | |
222 | you have defined interactively (not things you loaded or defined |
|
222 | you have defined interactively (not things you loaded or defined | |
223 | in your configuration files). %who just prints a list of |
|
223 | in your configuration files). %who just prints a list of | |
224 | identifiers and %whos prints a table with some basic details about |
|
224 | identifiers and %whos prints a table with some basic details about | |
225 | each identifier. |
|
225 | each identifier. | |
226 |
|
226 | |||
227 | The dynamic object information functions (?/??, ``%pdoc``, |
|
227 | The dynamic object information functions (?/??, ``%pdoc``, | |
228 | ``%pfile``, ``%pdef``, ``%psource``) work on object attributes, as well as |
|
228 | ``%pfile``, ``%pdef``, ``%psource``) work on object attributes, as well as | |
229 | directly on variables. For example, after doing ``import os``, you can use |
|
229 | directly on variables. For example, after doing ``import os``, you can use | |
230 | ``os.path.abspath??``. |
|
230 | ``os.path.abspath??``. | |
231 |
|
231 | |||
232 |
|
232 | |||
233 | Command line completion |
|
233 | Command line completion | |
234 | +++++++++++++++++++++++ |
|
234 | +++++++++++++++++++++++ | |
235 |
|
235 | |||
236 | At any time, hitting TAB will complete any available python commands or |
|
236 | At any time, hitting TAB will complete any available python commands or | |
237 | variable names, and show you a list of the possible completions if |
|
237 | variable names, and show you a list of the possible completions if | |
238 | there's no unambiguous one. It will also complete filenames in the |
|
238 | there's no unambiguous one. It will also complete filenames in the | |
239 | current directory if no python names match what you've typed so far. |
|
239 | current directory if no python names match what you've typed so far. | |
240 |
|
240 | |||
241 |
|
241 | |||
242 | Search command history |
|
242 | Search command history | |
243 | ++++++++++++++++++++++ |
|
243 | ++++++++++++++++++++++ | |
244 |
|
244 | |||
245 | IPython provides two ways for searching through previous input and thus |
|
245 | IPython provides two ways for searching through previous input and thus | |
246 | reduce the need for repetitive typing: |
|
246 | reduce the need for repetitive typing: | |
247 |
|
247 | |||
248 | 1. Start typing, and then use the up and down arrow keys (or :kbd:`Ctrl-p` |
|
248 | 1. Start typing, and then use the up and down arrow keys (or :kbd:`Ctrl-p` | |
249 | and :kbd:`Ctrl-n`) to search through only the history items that match |
|
249 | and :kbd:`Ctrl-n`) to search through only the history items that match | |
250 | what you've typed so far. |
|
250 | what you've typed so far. | |
251 | 2. Hit :kbd:`Ctrl-r`: to open a search prompt. Begin typing and the system |
|
251 | 2. Hit :kbd:`Ctrl-r`: to open a search prompt. Begin typing and the system | |
252 | searches your history for lines that contain what you've typed so |
|
252 | searches your history for lines that contain what you've typed so | |
253 | far, completing as much as it can. |
|
253 | far, completing as much as it can. | |
254 |
|
254 | |||
255 | IPython will save your input history when it leaves and reload it next |
|
255 | IPython will save your input history when it leaves and reload it next | |
256 | time you restart it. By default, the history file is named |
|
256 | time you restart it. By default, the history file is named | |
257 | :file:`.ipython/profile_{name}/history.sqlite`. |
|
257 | :file:`.ipython/profile_{name}/history.sqlite`. | |
258 |
|
258 | |||
259 | Autoindent |
|
259 | Autoindent | |
260 | ++++++++++ |
|
260 | ++++++++++ | |
261 |
|
261 | |||
262 | Starting with 5.0, IPython uses `prompt_toolkit` in place of ``readline``, |
|
262 | Starting with 5.0, IPython uses `prompt_toolkit` in place of ``readline``, | |
263 | it thus can recognize lines ending in ':' and indent the next line, |
|
263 | it thus can recognize lines ending in ':' and indent the next line, | |
264 | while also un-indenting automatically after 'raise' or 'return', |
|
264 | while also un-indenting automatically after 'raise' or 'return', | |
265 | and support real multi-line editing as well as syntactic coloration |
|
265 | and support real multi-line editing as well as syntactic coloration | |
266 | during edition. |
|
266 | during edition. | |
267 |
|
267 | |||
268 | This feature does not use the ``readline`` library anymore, so it will |
|
268 | This feature does not use the ``readline`` library anymore, so it will | |
269 | not honor your :file:`~/.inputrc` configuration (or whatever |
|
269 | not honor your :file:`~/.inputrc` configuration (or whatever | |
270 | file your :envvar:`INPUTRC` environment variable points to). |
|
270 | file your :envvar:`INPUTRC` environment variable points to). | |
271 |
|
271 | |||
272 | In particular if you want to change the input mode to ``vi``, you will need to |
|
272 | In particular if you want to change the input mode to ``vi``, you will need to | |
273 | set the ``TerminalInteractiveShell.editing_mode`` configuration option of IPython. |
|
273 | set the ``TerminalInteractiveShell.editing_mode`` configuration option of IPython. | |
274 |
|
274 | |||
275 | Session logging and restoring |
|
275 | Session logging and restoring | |
276 | ----------------------------- |
|
276 | ----------------------------- | |
277 |
|
277 | |||
278 | You can log all input from a session either by starting IPython with the |
|
278 | You can log all input from a session either by starting IPython with the | |
279 | command line switch ``--logfile=foo.py`` (see :ref:`here <command_line_options>`) |
|
279 | command line switch ``--logfile=foo.py`` (see :ref:`here <command_line_options>`) | |
280 | or by activating the logging at any moment with the magic function :magic:`logstart`. |
|
280 | or by activating the logging at any moment with the magic function :magic:`logstart`. | |
281 |
|
281 | |||
282 | Log files can later be reloaded by running them as scripts and IPython |
|
282 | Log files can later be reloaded by running them as scripts and IPython | |
283 | will attempt to 'replay' the log by executing all the lines in it, thus |
|
283 | will attempt to 'replay' the log by executing all the lines in it, thus | |
284 | restoring the state of a previous session. This feature is not quite |
|
284 | restoring the state of a previous session. This feature is not quite | |
285 | perfect, but can still be useful in many cases. |
|
285 | perfect, but can still be useful in many cases. | |
286 |
|
286 | |||
287 | The log files can also be used as a way to have a permanent record of |
|
287 | The log files can also be used as a way to have a permanent record of | |
288 | any code you wrote while experimenting. Log files are regular text files |
|
288 | any code you wrote while experimenting. Log files are regular text files | |
289 | which you can later open in your favorite text editor to extract code or |
|
289 | which you can later open in your favorite text editor to extract code or | |
290 | to 'clean them up' before using them to replay a session. |
|
290 | to 'clean them up' before using them to replay a session. | |
291 |
|
291 | |||
292 | The :magic:`logstart` function for activating logging in mid-session is used as |
|
292 | The :magic:`logstart` function for activating logging in mid-session is used as | |
293 | follows:: |
|
293 | follows:: | |
294 |
|
294 | |||
295 | %logstart [log_name [log_mode]] |
|
295 | %logstart [log_name [log_mode]] | |
296 |
|
296 | |||
297 | If no name is given, it defaults to a file named 'ipython_log.py' in your |
|
297 | If no name is given, it defaults to a file named 'ipython_log.py' in your | |
298 | current working directory, in 'rotate' mode (see below). |
|
298 | current working directory, in 'rotate' mode (see below). | |
299 |
|
299 | |||
300 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your |
|
300 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your | |
301 | history up to that point and then continues logging. |
|
301 | history up to that point and then continues logging. | |
302 |
|
302 | |||
303 | %logstart takes a second optional parameter: logging mode. This can be |
|
303 | %logstart takes a second optional parameter: logging mode. This can be | |
304 | one of (note that the modes are given unquoted): |
|
304 | one of (note that the modes are given unquoted): | |
305 |
|
305 | |||
306 | * [over:] overwrite existing log_name. |
|
306 | * [over:] overwrite existing log_name. | |
307 | * [backup:] rename (if exists) to log_name~ and start log_name. |
|
307 | * [backup:] rename (if exists) to log_name~ and start log_name. | |
308 | * [append:] well, that says it. |
|
308 | * [append:] well, that says it. | |
309 | * [rotate:] create rotating logs log_name.1~, log_name.2~, etc. |
|
309 | * [rotate:] create rotating logs log_name.1~, log_name.2~, etc. | |
310 |
|
310 | |||
311 | The :magic:`logoff` and :magic:`logon` functions allow you to temporarily stop and |
|
311 | The :magic:`logoff` and :magic:`logon` functions allow you to temporarily stop and | |
312 | resume logging to a file which had previously been started with |
|
312 | resume logging to a file which had previously been started with | |
313 | %logstart. They will fail (with an explanation) if you try to use them |
|
313 | %logstart. They will fail (with an explanation) if you try to use them | |
314 | before logging has been started. |
|
314 | before logging has been started. | |
315 |
|
315 | |||
316 | .. _system_shell_access: |
|
316 | .. _system_shell_access: | |
317 |
|
317 | |||
318 | System shell access |
|
318 | System shell access | |
319 | ------------------- |
|
319 | ------------------- | |
320 |
|
320 | |||
321 | Any input line beginning with a ``!`` character is passed verbatim (minus |
|
321 | Any input line beginning with a ``!`` character is passed verbatim (minus | |
322 | the ``!``, of course) to the underlying operating system. For example, |
|
322 | the ``!``, of course) to the underlying operating system. For example, | |
323 | typing ``!ls`` will run 'ls' in the current directory. |
|
323 | typing ``!ls`` will run 'ls' in the current directory. | |
324 |
|
324 | |||
325 | .. _manual_capture: |
|
325 | .. _manual_capture: | |
326 |
|
326 | |||
327 | Manual capture of command output and magic output |
|
327 | Manual capture of command output and magic output | |
328 | ------------------------------------------------- |
|
328 | ------------------------------------------------- | |
329 |
|
329 | |||
330 | You can assign the result of a system command to a Python variable with the |
|
330 | You can assign the result of a system command to a Python variable with the | |
331 | syntax ``myfiles = !ls``. Similarly, the result of a magic (as long as it returns |
|
331 | syntax ``myfiles = !ls``. Similarly, the result of a magic (as long as it returns | |
332 | a value) can be assigned to a variable. For example, the syntax ``myfiles = %sx ls`` |
|
332 | a value) can be assigned to a variable. For example, the syntax ``myfiles = %sx ls`` | |
333 | is equivalent to the above system command example (the :magic:`sx` magic runs a shell command |
|
333 | is equivalent to the above system command example (the :magic:`sx` magic runs a shell command | |
334 | and captures the output). Each of these gets machine |
|
334 | and captures the output). Each of these gets machine | |
335 | readable output from stdout (e.g. without colours), and splits on newlines. To |
|
335 | readable output from stdout (e.g. without colours), and splits on newlines. To | |
336 | explicitly get this sort of output without assigning to a variable, use two |
|
336 | explicitly get this sort of output without assigning to a variable, use two | |
337 | exclamation marks (``!!ls``) or the :magic:`sx` magic command without an assignment. |
|
337 | exclamation marks (``!!ls``) or the :magic:`sx` magic command without an assignment. | |
338 | (However, ``!!`` commands cannot be assigned to a variable.) |
|
338 | (However, ``!!`` commands cannot be assigned to a variable.) | |
339 |
|
339 | |||
340 | The captured list in this example has some convenience features. ``myfiles.n`` or ``myfiles.s`` |
|
340 | The captured list in this example has some convenience features. ``myfiles.n`` or ``myfiles.s`` | |
341 | returns a string delimited by newlines or spaces, respectively. ``myfiles.p`` |
|
341 | returns a string delimited by newlines or spaces, respectively. ``myfiles.p`` | |
342 | produces `path objects <http://pypi.python.org/pypi/path.py>`_ from the list items. |
|
342 | produces `path objects <http://pypi.python.org/pypi/path.py>`_ from the list items. | |
343 | See :ref:`string_lists` for details. |
|
343 | See :ref:`string_lists` for details. | |
344 |
|
344 | |||
345 | IPython also allows you to expand the value of python variables when |
|
345 | IPython also allows you to expand the value of python variables when | |
346 | making system calls. Wrap variables or expressions in {braces}:: |
|
346 | making system calls. Wrap variables or expressions in {braces}:: | |
347 |
|
347 | |||
348 | In [1]: pyvar = 'Hello world' |
|
348 | In [1]: pyvar = 'Hello world' | |
349 | In [2]: !echo "A python variable: {pyvar}" |
|
349 | In [2]: !echo "A python variable: {pyvar}" | |
350 | A python variable: Hello world |
|
350 | A python variable: Hello world | |
351 | In [3]: import math |
|
351 | In [3]: import math | |
352 | In [4]: x = 8 |
|
352 | In [4]: x = 8 | |
353 | In [5]: !echo {math.factorial(x)} |
|
353 | In [5]: !echo {math.factorial(x)} | |
354 | 40320 |
|
354 | 40320 | |
355 |
|
355 | |||
356 | For simple cases, you can alternatively prepend $ to a variable name:: |
|
356 | For simple cases, you can alternatively prepend $ to a variable name:: | |
357 |
|
357 | |||
358 | In [6]: !echo $sys.argv |
|
358 | In [6]: !echo $sys.argv | |
359 | [/home/fperez/usr/bin/ipython] |
|
359 | [/home/fperez/usr/bin/ipython] | |
360 | In [7]: !echo "A system variable: $$HOME" # Use $$ for literal $ |
|
360 | In [7]: !echo "A system variable: $$HOME" # Use $$ for literal $ | |
361 | A system variable: /home/fperez |
|
361 | A system variable: /home/fperez | |
362 |
|
362 | |||
363 | Note that `$$` is used to represent a literal `$`. |
|
363 | Note that `$$` is used to represent a literal `$`. | |
364 |
|
364 | |||
365 | System command aliases |
|
365 | System command aliases | |
366 | ---------------------- |
|
366 | ---------------------- | |
367 |
|
367 | |||
368 | The :magic:`alias` magic function allows you to define magic functions which are in fact |
|
368 | The :magic:`alias` magic function allows you to define magic functions which are in fact | |
369 | system shell commands. These aliases can have parameters. |
|
369 | system shell commands. These aliases can have parameters. | |
370 |
|
370 | |||
371 | ``%alias alias_name cmd`` defines 'alias_name' as an alias for 'cmd' |
|
371 | ``%alias alias_name cmd`` defines 'alias_name' as an alias for 'cmd' | |
372 |
|
372 | |||
373 | Then, typing ``alias_name params`` will execute the system command 'cmd |
|
373 | Then, typing ``alias_name params`` will execute the system command 'cmd | |
374 | params' (from your underlying operating system). |
|
374 | params' (from your underlying operating system). | |
375 |
|
375 | |||
376 | You can also define aliases with parameters using ``%s`` specifiers (one per |
|
376 | You can also define aliases with parameters using ``%s`` specifiers (one per | |
377 | parameter). The following example defines the parts function as an |
|
377 | parameter). The following example defines the parts function as an | |
378 | alias to the command ``echo first %s second %s`` where each ``%s`` will be |
|
378 | alias to the command ``echo first %s second %s`` where each ``%s`` will be | |
379 | replaced by a positional parameter to the call to %parts:: |
|
379 | replaced by a positional parameter to the call to %parts:: | |
380 |
|
380 | |||
381 | In [1]: %alias parts echo first %s second %s |
|
381 | In [1]: %alias parts echo first %s second %s | |
382 | In [2]: parts A B |
|
382 | In [2]: parts A B | |
383 | first A second B |
|
383 | first A second B | |
384 | In [3]: parts A |
|
384 | In [3]: parts A | |
385 | ERROR: Alias <parts> requires 2 arguments, 1 given. |
|
385 | ERROR: Alias <parts> requires 2 arguments, 1 given. | |
386 |
|
386 | |||
387 | If called with no parameters, :magic:`alias` prints the table of currently |
|
387 | If called with no parameters, :magic:`alias` prints the table of currently | |
388 | defined aliases. |
|
388 | defined aliases. | |
389 |
|
389 | |||
390 | The :magic:`rehashx` magic allows you to load your entire $PATH as |
|
390 | The :magic:`rehashx` magic allows you to load your entire $PATH as | |
391 | ipython aliases. See its docstring for further details. |
|
391 | ipython aliases. See its docstring for further details. | |
392 |
|
392 | |||
393 |
|
393 | |||
394 | .. _dreload: |
|
394 | .. _dreload: | |
395 |
|
395 | |||
396 | Recursive reload |
|
396 | Recursive reload | |
397 | ---------------- |
|
397 | ---------------- | |
398 |
|
398 | |||
399 | The :mod:`IPython.lib.deepreload` module allows you to recursively reload a |
|
399 | The :mod:`IPython.lib.deepreload` module allows you to recursively reload a | |
400 | module: changes made to any of its dependencies will be reloaded without |
|
400 | module: changes made to any of its dependencies will be reloaded without | |
401 | having to exit. To start using it, do:: |
|
401 | having to exit. To start using it, do:: | |
402 |
|
402 | |||
403 | from IPython.lib.deepreload import reload as dreload |
|
403 | from IPython.lib.deepreload import reload as dreload | |
404 |
|
404 | |||
405 |
|
405 | |||
406 | Verbose and colored exception traceback printouts |
|
406 | Verbose and colored exception traceback printouts | |
407 | ------------------------------------------------- |
|
407 | ------------------------------------------------- | |
408 |
|
408 | |||
409 | IPython provides the option to see very detailed exception tracebacks, |
|
409 | IPython provides the option to see very detailed exception tracebacks, | |
410 | which can be especially useful when debugging large programs. You can |
|
410 | which can be especially useful when debugging large programs. You can | |
411 | run any Python file with the %run function to benefit from these |
|
411 | run any Python file with the %run function to benefit from these | |
412 | detailed tracebacks. Furthermore, both normal and verbose tracebacks can |
|
412 | detailed tracebacks. Furthermore, both normal and verbose tracebacks can | |
413 | be colored (if your terminal supports it) which makes them much easier |
|
413 | be colored (if your terminal supports it) which makes them much easier | |
414 | to parse visually. |
|
414 | to parse visually. | |
415 |
|
415 | |||
416 | See the magic :magic:`xmode` and :magic:`colors` functions for details. |
|
416 | See the magic :magic:`xmode` and :magic:`colors` functions for details. | |
417 |
|
417 | |||
418 | These features are basically a terminal version of Ka-Ping Yee's cgitb |
|
418 | These features are basically a terminal version of Ka-Ping Yee's cgitb | |
419 | module, now part of the standard Python library. |
|
419 | module, now part of the standard Python library. | |
420 |
|
420 | |||
421 |
|
421 | |||
422 | .. _input_caching: |
|
422 | .. _input_caching: | |
423 |
|
423 | |||
424 | Input caching system |
|
424 | Input caching system | |
425 | -------------------- |
|
425 | -------------------- | |
426 |
|
426 | |||
427 | IPython offers numbered prompts (In/Out) with input and output caching |
|
427 | IPython offers numbered prompts (In/Out) with input and output caching | |
428 | (also referred to as 'input history'). All input is saved and can be |
|
428 | (also referred to as 'input history'). All input is saved and can be | |
429 | retrieved as variables (besides the usual arrow key recall), in |
|
429 | retrieved as variables (besides the usual arrow key recall), in | |
430 | addition to the :magic:`rep` magic command that brings a history entry |
|
430 | addition to the :magic:`rep` magic command that brings a history entry | |
431 | up for editing on the next command line. |
|
431 | up for editing on the next command line. | |
432 |
|
432 | |||
433 | The following variables always exist: |
|
433 | The following variables always exist: | |
434 |
|
434 | |||
435 | * ``_i``, ``_ii``, ``_iii``: store previous, next previous and next-next |
|
435 | * ``_i``, ``_ii``, ``_iii``: store previous, next previous and next-next | |
436 | previous inputs. |
|
436 | previous inputs. | |
437 |
|
437 | |||
438 | * ``In``, ``_ih`` : a list of all inputs; ``_ih[n]`` is the input from line |
|
438 | * ``In``, ``_ih`` : a list of all inputs; ``_ih[n]`` is the input from line | |
439 | ``n``. If you overwrite In with a variable of your own, you can remake the |
|
439 | ``n``. If you overwrite In with a variable of your own, you can remake the | |
440 | assignment to the internal list with a simple ``In=_ih``. |
|
440 | assignment to the internal list with a simple ``In=_ih``. | |
441 |
|
441 | |||
442 | Additionally, global variables named ``_i<n>`` are dynamically created (``<n>`` |
|
442 | Additionally, global variables named ``_i<n>`` are dynamically created (``<n>`` | |
443 | being the prompt counter), so ``_i<n> == _ih[<n>] == In[<n>]``. |
|
443 | being the prompt counter), so ``_i<n> == _ih[<n>] == In[<n>]``. | |
444 |
|
444 | |||
445 | For example, what you typed at prompt 14 is available as ``_i14``, ``_ih[14]`` |
|
445 | For example, what you typed at prompt 14 is available as ``_i14``, ``_ih[14]`` | |
446 | and ``In[14]``. |
|
446 | and ``In[14]``. | |
447 |
|
447 | |||
448 | This allows you to easily cut and paste multi line interactive prompts |
|
448 | This allows you to easily cut and paste multi line interactive prompts | |
449 | by printing them out: they print like a clean string, without prompt |
|
449 | by printing them out: they print like a clean string, without prompt | |
450 | characters. You can also manipulate them like regular variables (they |
|
450 | characters. You can also manipulate them like regular variables (they | |
451 | are strings), modify or exec them. |
|
451 | are strings), modify or exec them. | |
452 |
|
452 | |||
453 | You can also re-execute multiple lines of input easily by using the magic |
|
453 | You can also re-execute multiple lines of input easily by using the magic | |
454 | :magic:`rerun` or :magic:`macro` functions. The macro system also allows you to |
|
454 | :magic:`rerun` or :magic:`macro` functions. The macro system also allows you to | |
455 | re-execute previous lines which include magic function calls (which require |
|
455 | re-execute previous lines which include magic function calls (which require | |
456 | special processing). Type %macro? for more details on the macro system. |
|
456 | special processing). Type %macro? for more details on the macro system. | |
457 |
|
457 | |||
458 | A history function :magic:`history` allows you to see any part of your input |
|
458 | A history function :magic:`history` allows you to see any part of your input | |
459 | history by printing a range of the _i variables. |
|
459 | history by printing a range of the _i variables. | |
460 |
|
460 | |||
461 | You can also search ('grep') through your history by typing |
|
461 | You can also search ('grep') through your history by typing | |
462 | ``%hist -g somestring``. This is handy for searching for URLs, IP addresses, |
|
462 | ``%hist -g somestring``. This is handy for searching for URLs, IP addresses, | |
463 | etc. You can bring history entries listed by '%hist -g' up for editing |
|
463 | etc. You can bring history entries listed by '%hist -g' up for editing | |
464 | with the %recall command, or run them immediately with :magic:`rerun`. |
|
464 | with the %recall command, or run them immediately with :magic:`rerun`. | |
465 |
|
465 | |||
466 | .. _output_caching: |
|
466 | .. _output_caching: | |
467 |
|
467 | |||
468 | Output caching system |
|
468 | Output caching system | |
469 | --------------------- |
|
469 | --------------------- | |
470 |
|
470 | |||
471 | For output that is returned from actions, a system similar to the input |
|
471 | For output that is returned from actions, a system similar to the input | |
472 | cache exists but using _ instead of _i. Only actions that produce a |
|
472 | cache exists but using _ instead of _i. Only actions that produce a | |
473 | result (NOT assignments, for example) are cached. If you are familiar |
|
473 | result (NOT assignments, for example) are cached. If you are familiar | |
474 | with Mathematica, IPython's _ variables behave exactly like |
|
474 | with Mathematica, IPython's _ variables behave exactly like | |
475 | Mathematica's % variables. |
|
475 | Mathematica's % variables. | |
476 |
|
476 | |||
477 | The following variables always exist: |
|
477 | The following variables always exist: | |
478 |
|
478 | |||
479 | * [_] (a single underscore): stores previous output, like Python's |
|
479 | * [_] (a single underscore): stores previous output, like Python's | |
480 | default interpreter. |
|
480 | default interpreter. | |
481 | * [__] (two underscores): next previous. |
|
481 | * [__] (two underscores): next previous. | |
482 | * [___] (three underscores): next-next previous. |
|
482 | * [___] (three underscores): next-next previous. | |
483 |
|
483 | |||
484 | Additionally, global variables named _<n> are dynamically created (<n> |
|
484 | Additionally, global variables named _<n> are dynamically created (<n> | |
485 | being the prompt counter), such that the result of output <n> is always |
|
485 | being the prompt counter), such that the result of output <n> is always | |
486 | available as _<n> (don't use the angle brackets, just the number, e.g. |
|
486 | available as _<n> (don't use the angle brackets, just the number, e.g. | |
487 | ``_21``). |
|
487 | ``_21``). | |
488 |
|
488 | |||
489 | These variables are also stored in a global dictionary (not a |
|
489 | These variables are also stored in a global dictionary (not a | |
490 | list, since it only has entries for lines which returned a result) |
|
490 | list, since it only has entries for lines which returned a result) | |
491 | available under the names _oh and Out (similar to _ih and In). So the |
|
491 | available under the names _oh and Out (similar to _ih and In). So the | |
492 | output from line 12 can be obtained as ``_12``, ``Out[12]`` or ``_oh[12]``. If you |
|
492 | output from line 12 can be obtained as ``_12``, ``Out[12]`` or ``_oh[12]``. If you | |
493 | accidentally overwrite the Out variable you can recover it by typing |
|
493 | accidentally overwrite the Out variable you can recover it by typing | |
494 | ``Out=_oh`` at the prompt. |
|
494 | ``Out=_oh`` at the prompt. | |
495 |
|
495 | |||
496 | This system obviously can potentially put heavy memory demands on your |
|
496 | This system obviously can potentially put heavy memory demands on your | |
497 | system, since it prevents Python's garbage collector from removing any |
|
497 | system, since it prevents Python's garbage collector from removing any | |
498 | previously computed results. You can control how many results are kept |
|
498 | previously computed results. You can control how many results are kept | |
499 | in memory with the configuration option ``InteractiveShell.cache_size``. |
|
499 | in memory with the configuration option ``InteractiveShell.cache_size``. | |
500 | If you set it to 0, output caching is disabled. You can also use the :magic:`reset` |
|
500 | If you set it to 0, output caching is disabled. You can also use the :magic:`reset` | |
501 | and :magic:`xdel` magics to clear large items from memory. |
|
501 | and :magic:`xdel` magics to clear large items from memory. | |
502 |
|
502 | |||
503 | Directory history |
|
503 | Directory history | |
504 | ----------------- |
|
504 | ----------------- | |
505 |
|
505 | |||
506 | Your history of visited directories is kept in the global list _dh, and |
|
506 | Your history of visited directories is kept in the global list _dh, and | |
507 | the magic :magic:`cd` command can be used to go to any entry in that list. The |
|
507 | the magic :magic:`cd` command can be used to go to any entry in that list. The | |
508 | :magic:`dhist` command allows you to view this history. Do ``cd -<TAB>`` to |
|
508 | :magic:`dhist` command allows you to view this history. Do ``cd -<TAB>`` to | |
509 | conveniently view the directory history. |
|
509 | conveniently view the directory history. | |
510 |
|
510 | |||
511 |
|
511 | |||
512 | Automatic parentheses and quotes |
|
512 | Automatic parentheses and quotes | |
513 | -------------------------------- |
|
513 | -------------------------------- | |
514 |
|
514 | |||
515 | These features were adapted from Nathan Gray's LazyPython. They are |
|
515 | These features were adapted from Nathan Gray's LazyPython. They are | |
516 | meant to allow less typing for common situations. |
|
516 | meant to allow less typing for common situations. | |
517 |
|
517 | |||
518 | Callable objects (i.e. functions, methods, etc) can be invoked like this |
|
518 | Callable objects (i.e. functions, methods, etc) can be invoked like this | |
519 | (notice the commas between the arguments):: |
|
519 | (notice the commas between the arguments):: | |
520 |
|
520 | |||
521 | In [1]: callable_ob arg1, arg2, arg3 |
|
521 | In [1]: callable_ob arg1, arg2, arg3 | |
522 | ------> callable_ob(arg1, arg2, arg3) |
|
522 | ------> callable_ob(arg1, arg2, arg3) | |
523 |
|
523 | |||
524 | .. note:: |
|
524 | .. note:: | |
525 | This feature is disabled by default. To enable it, use the ``%autocall`` |
|
525 | This feature is disabled by default. To enable it, use the ``%autocall`` | |
526 | magic command. The commands below with special prefixes will always work, |
|
526 | magic command. The commands below with special prefixes will always work, | |
527 | however. |
|
527 | however. | |
528 |
|
528 | |||
529 | You can force automatic parentheses by using '/' as the first character |
|
529 | You can force automatic parentheses by using '/' as the first character | |
530 | of a line. For example:: |
|
530 | of a line. For example:: | |
531 |
|
531 | |||
532 | In [2]: /globals # becomes 'globals()' |
|
532 | In [2]: /globals # becomes 'globals()' | |
533 |
|
533 | |||
534 | Note that the '/' MUST be the first character on the line! This won't work:: |
|
534 | Note that the '/' MUST be the first character on the line! This won't work:: | |
535 |
|
535 | |||
536 | In [3]: print /globals # syntax error |
|
536 | In [3]: print /globals # syntax error | |
537 |
|
537 | |||
538 | In most cases the automatic algorithm should work, so you should rarely |
|
538 | In most cases the automatic algorithm should work, so you should rarely | |
539 | need to explicitly invoke /. One notable exception is if you are trying |
|
539 | need to explicitly invoke /. One notable exception is if you are trying | |
540 | to call a function with a list of tuples as arguments (the parenthesis |
|
540 | to call a function with a list of tuples as arguments (the parenthesis | |
541 | will confuse IPython):: |
|
541 | will confuse IPython):: | |
542 |
|
542 | |||
543 | In [4]: zip (1,2,3),(4,5,6) # won't work |
|
543 | In [4]: zip (1,2,3),(4,5,6) # won't work | |
544 |
|
544 | |||
545 | but this will work:: |
|
545 | but this will work:: | |
546 |
|
546 | |||
547 | In [5]: /zip (1,2,3),(4,5,6) |
|
547 | In [5]: /zip (1,2,3),(4,5,6) | |
548 | ------> zip ((1,2,3),(4,5,6)) |
|
548 | ------> zip ((1,2,3),(4,5,6)) | |
549 | Out[5]: [(1, 4), (2, 5), (3, 6)] |
|
549 | Out[5]: [(1, 4), (2, 5), (3, 6)] | |
550 |
|
550 | |||
551 | IPython tells you that it has altered your command line by displaying |
|
551 | IPython tells you that it has altered your command line by displaying | |
552 | the new command line preceded by ``--->``. |
|
552 | the new command line preceded by ``--->``. | |
553 |
|
553 | |||
554 | You can force automatic quoting of a function's arguments by using ``,`` |
|
554 | You can force automatic quoting of a function's arguments by using ``,`` | |
555 | or ``;`` as the first character of a line. For example:: |
|
555 | or ``;`` as the first character of a line. For example:: | |
556 |
|
556 | |||
557 | In [1]: ,my_function /home/me # becomes my_function("/home/me") |
|
557 | In [1]: ,my_function /home/me # becomes my_function("/home/me") | |
558 |
|
558 | |||
559 | If you use ';' the whole argument is quoted as a single string, while ',' splits |
|
559 | If you use ';' the whole argument is quoted as a single string, while ',' splits | |
560 | on whitespace:: |
|
560 | on whitespace:: | |
561 |
|
561 | |||
562 | In [2]: ,my_function a b c # becomes my_function("a","b","c") |
|
562 | In [2]: ,my_function a b c # becomes my_function("a","b","c") | |
563 |
|
563 | |||
564 | In [3]: ;my_function a b c # becomes my_function("a b c") |
|
564 | In [3]: ;my_function a b c # becomes my_function("a b c") | |
565 |
|
565 | |||
566 | Note that the ',' or ';' MUST be the first character on the line! This |
|
566 | Note that the ',' or ';' MUST be the first character on the line! This | |
567 | won't work:: |
|
567 | won't work:: | |
568 |
|
568 | |||
569 | In [4]: x = ,my_function /home/me # syntax error |
|
569 | In [4]: x = ,my_function /home/me # syntax error | |
570 |
|
570 | |||
571 | IPython as your default Python environment |
|
571 | IPython as your default Python environment | |
572 | ========================================== |
|
572 | ========================================== | |
573 |
|
573 | |||
574 | Python honors the environment variable :envvar:`PYTHONSTARTUP` and will |
|
574 | Python honors the environment variable :envvar:`PYTHONSTARTUP` and will | |
575 | execute at startup the file referenced by this variable. If you put the |
|
575 | execute at startup the file referenced by this variable. If you put the | |
576 | following code at the end of that file, then IPython will be your working |
|
576 | following code at the end of that file, then IPython will be your working | |
577 | environment anytime you start Python:: |
|
577 | environment anytime you start Python:: | |
578 |
|
578 | |||
579 | import os, IPython |
|
579 | import os, IPython | |
580 | os.environ['PYTHONSTARTUP'] = '' # Prevent running this again |
|
580 | os.environ['PYTHONSTARTUP'] = '' # Prevent running this again | |
581 | IPython.start_ipython() |
|
581 | IPython.start_ipython() | |
582 | raise SystemExit |
|
582 | raise SystemExit | |
583 |
|
583 | |||
584 | The ``raise SystemExit`` is needed to exit Python when |
|
584 | The ``raise SystemExit`` is needed to exit Python when | |
585 | it finishes, otherwise you'll be back at the normal Python ``>>>`` |
|
585 | it finishes, otherwise you'll be back at the normal Python ``>>>`` | |
586 | prompt. |
|
586 | prompt. | |
587 |
|
587 | |||
588 | This is probably useful to developers who manage multiple Python |
|
588 | This is probably useful to developers who manage multiple Python | |
589 | versions and don't want to have correspondingly multiple IPython |
|
589 | versions and don't want to have correspondingly multiple IPython | |
590 | versions. Note that in this mode, there is no way to pass IPython any |
|
590 | versions. Note that in this mode, there is no way to pass IPython any | |
591 | command-line options, as those are trapped first by Python itself. |
|
591 | command-line options, as those are trapped first by Python itself. | |
592 |
|
592 | |||
593 | .. _Embedding: |
|
593 | .. _Embedding: | |
594 |
|
594 | |||
595 | Embedding IPython |
|
595 | Embedding IPython | |
596 | ================= |
|
596 | ================= | |
597 |
|
597 | |||
598 | You can start a regular IPython session with |
|
598 | You can start a regular IPython session with | |
599 |
|
599 | |||
600 | .. sourcecode:: python |
|
600 | .. sourcecode:: python | |
601 |
|
601 | |||
602 | import IPython |
|
602 | import IPython | |
603 | IPython.start_ipython(argv=[]) |
|
603 | IPython.start_ipython(argv=[]) | |
604 |
|
604 | |||
605 | at any point in your program. This will load IPython configuration, |
|
605 | at any point in your program. This will load IPython configuration, | |
606 | startup files, and everything, just as if it were a normal IPython session. |
|
606 | startup files, and everything, just as if it were a normal IPython session. | |
607 | For information on setting configuration options when running IPython from |
|
607 | For information on setting configuration options when running IPython from | |
608 | python, see :ref:`configure_start_ipython`. |
|
608 | python, see :ref:`configure_start_ipython`. | |
609 |
|
609 | |||
610 | It is also possible to embed an IPython shell in a namespace in your Python |
|
610 | It is also possible to embed an IPython shell in a namespace in your Python | |
611 | code. This allows you to evaluate dynamically the state of your code, operate |
|
611 | code. This allows you to evaluate dynamically the state of your code, operate | |
612 | with your variables, analyze them, etc. For example, if you run the following |
|
612 | with your variables, analyze them, etc. For example, if you run the following | |
613 | code snippet:: |
|
613 | code snippet:: | |
614 |
|
614 | |||
615 | import IPython |
|
615 | import IPython | |
616 |
|
616 | |||
617 | a = 42 |
|
617 | a = 42 | |
618 | IPython.embed() |
|
618 | IPython.embed() | |
619 |
|
619 | |||
620 | and within the IPython shell, you reassign `a` to `23` to do further testing of |
|
620 | and within the IPython shell, you reassign `a` to `23` to do further testing of | |
621 | some sort, you can then exit:: |
|
621 | some sort, you can then exit:: | |
622 |
|
622 | |||
623 | >>> IPython.embed() |
|
623 | >>> IPython.embed() | |
624 | Python 3.6.2 (default, Jul 17 2017, 16:44:45) |
|
624 | Python 3.6.2 (default, Jul 17 2017, 16:44:45) | |
625 | Type 'copyright', 'credits' or 'license' for more information |
|
625 | Type 'copyright', 'credits' or 'license' for more information | |
626 | IPython 6.2.0.dev -- An enhanced Interactive Python. Type '?' for help. |
|
626 | IPython 6.2.0.dev -- An enhanced Interactive Python. Type '?' for help. | |
627 |
|
627 | |||
628 | In [1]: a = 23 |
|
628 | In [1]: a = 23 | |
629 |
|
629 | |||
630 | In [2]: exit() |
|
630 | In [2]: exit() | |
631 |
|
631 | |||
632 | Once you exit and print `a`, the value 23 will be shown:: |
|
632 | Once you exit and print `a`, the value 23 will be shown:: | |
633 |
|
633 | |||
634 |
|
634 | |||
635 | In: print(a) |
|
635 | In: print(a) | |
636 | 23 |
|
636 | 23 | |
637 |
|
637 | |||
638 | It's important to note that the code run in the embedded IPython shell will |
|
638 | It's important to note that the code run in the embedded IPython shell will | |
639 | *not* change the state of your code and variables, **unless** the shell is |
|
639 | *not* change the state of your code and variables, **unless** the shell is | |
640 | contained within the global namespace. In the above example, `a` is changed |
|
640 | contained within the global namespace. In the above example, `a` is changed | |
641 | because this is true. |
|
641 | because this is true. | |
642 |
|
642 | |||
643 | To further exemplify this, consider the following example:: |
|
643 | To further exemplify this, consider the following example:: | |
644 |
|
644 | |||
645 | import IPython |
|
645 | import IPython | |
646 | def do(): |
|
646 | def do(): | |
647 | a = 42 |
|
647 | a = 42 | |
648 | print(a) |
|
648 | print(a) | |
649 | IPython.embed() |
|
649 | IPython.embed() | |
650 | print(a) |
|
650 | print(a) | |
651 |
|
651 | |||
652 | Now if call the function and complete the state changes as we did above, the |
|
652 | Now if call the function and complete the state changes as we did above, the | |
653 | value `42` will be printed. Again, this is because it's not in the global |
|
653 | value `42` will be printed. Again, this is because it's not in the global | |
654 | namespace:: |
|
654 | namespace:: | |
655 |
|
655 | |||
656 | do() |
|
656 | do() | |
657 |
|
657 | |||
658 | Running a file with the above code can lead to the following session:: |
|
658 | Running a file with the above code can lead to the following session:: | |
659 |
|
659 | |||
660 | >>> do() |
|
660 | >>> do() | |
661 | 42 |
|
661 | 42 | |
662 | Python 3.6.2 (default, Jul 17 2017, 16:44:45) |
|
662 | Python 3.6.2 (default, Jul 17 2017, 16:44:45) | |
663 | Type 'copyright', 'credits' or 'license' for more information |
|
663 | Type 'copyright', 'credits' or 'license' for more information | |
664 | IPython 6.2.0.dev -- An enhanced Interactive Python. Type '?' for help. |
|
664 | IPython 6.2.0.dev -- An enhanced Interactive Python. Type '?' for help. | |
665 |
|
665 | |||
666 | In [1]: a = 23 |
|
666 | In [1]: a = 23 | |
667 |
|
667 | |||
668 | In [2]: exit() |
|
668 | In [2]: exit() | |
669 | 42 |
|
669 | 42 | |
670 |
|
670 | |||
671 | .. note:: |
|
671 | .. note:: | |
672 |
|
672 | |||
673 | At present, embedding IPython cannot be done from inside IPython. |
|
673 | At present, embedding IPython cannot be done from inside IPython. | |
674 | Run the code samples below outside IPython. |
|
674 | Run the code samples below outside IPython. | |
675 |
|
675 | |||
676 | This feature allows you to easily have a fully functional python |
|
676 | This feature allows you to easily have a fully functional python | |
677 | environment for doing object introspection anywhere in your code with a |
|
677 | environment for doing object introspection anywhere in your code with a | |
678 | simple function call. In some cases a simple print statement is enough, |
|
678 | simple function call. In some cases a simple print statement is enough, | |
679 | but if you need to do more detailed analysis of a code fragment this |
|
679 | but if you need to do more detailed analysis of a code fragment this | |
680 | feature can be very valuable. |
|
680 | feature can be very valuable. | |
681 |
|
681 | |||
682 | It can also be useful in scientific computing situations where it is |
|
682 | It can also be useful in scientific computing situations where it is | |
683 | common to need to do some automatic, computationally intensive part and |
|
683 | common to need to do some automatic, computationally intensive part and | |
684 | then stop to look at data, plots, etc. |
|
684 | then stop to look at data, plots, etc. | |
685 | Opening an IPython instance will give you full access to your data and |
|
685 | Opening an IPython instance will give you full access to your data and | |
686 | functions, and you can resume program execution once you are done with |
|
686 | functions, and you can resume program execution once you are done with | |
687 | the interactive part (perhaps to stop again later, as many times as |
|
687 | the interactive part (perhaps to stop again later, as many times as | |
688 | needed). |
|
688 | needed). | |
689 |
|
689 | |||
690 | The following code snippet is the bare minimum you need to include in |
|
690 | The following code snippet is the bare minimum you need to include in | |
691 | your Python programs for this to work (detailed examples follow later):: |
|
691 | your Python programs for this to work (detailed examples follow later):: | |
692 |
|
692 | |||
693 | from IPython import embed |
|
693 | from IPython import embed | |
694 |
|
694 | |||
695 | embed() # this call anywhere in your program will start IPython |
|
695 | embed() # this call anywhere in your program will start IPython | |
696 |
|
696 | |||
697 | You can also embed an IPython *kernel*, for use with qtconsole, etc. via |
|
697 | You can also embed an IPython *kernel*, for use with qtconsole, etc. via | |
698 | ``IPython.embed_kernel()``. This should function work the same way, but you can |
|
698 | ``IPython.embed_kernel()``. This should function work the same way, but you can | |
699 | connect an external frontend (``ipython qtconsole`` or ``ipython console``), |
|
699 | connect an external frontend (``ipython qtconsole`` or ``ipython console``), | |
700 | rather than interacting with it in the terminal. |
|
700 | rather than interacting with it in the terminal. | |
701 |
|
701 | |||
702 | You can run embedded instances even in code which is itself being run at |
|
702 | You can run embedded instances even in code which is itself being run at | |
703 | the IPython interactive prompt with '%run <filename>'. Since it's easy |
|
703 | the IPython interactive prompt with '%run <filename>'. Since it's easy | |
704 | to get lost as to where you are (in your top-level IPython or in your |
|
704 | to get lost as to where you are (in your top-level IPython or in your | |
705 | embedded one), it's a good idea in such cases to set the in/out prompts |
|
705 | embedded one), it's a good idea in such cases to set the in/out prompts | |
706 | to something different for the embedded instances. The code examples |
|
706 | to something different for the embedded instances. The code examples | |
707 | below illustrate this. |
|
707 | below illustrate this. | |
708 |
|
708 | |||
709 | You can also have multiple IPython instances in your program and open |
|
709 | You can also have multiple IPython instances in your program and open | |
710 | them separately, for example with different options for data |
|
710 | them separately, for example with different options for data | |
711 | presentation. If you close and open the same instance multiple times, |
|
711 | presentation. If you close and open the same instance multiple times, | |
712 | its prompt counters simply continue from each execution to the next. |
|
712 | its prompt counters simply continue from each execution to the next. | |
713 |
|
713 | |||
714 | Please look at the docstrings in the :mod:`~IPython.frontend.terminal.embed` |
|
714 | Please look at the docstrings in the :mod:`~IPython.frontend.terminal.embed` | |
715 | module for more details on the use of this system. |
|
715 | module for more details on the use of this system. | |
716 |
|
716 | |||
717 | The following sample file illustrating how to use the embedding |
|
717 | The following sample file illustrating how to use the embedding | |
718 | functionality is provided in the examples directory as embed_class_long.py. |
|
718 | functionality is provided in the examples directory as embed_class_long.py. | |
719 | It should be fairly self-explanatory: |
|
719 | It should be fairly self-explanatory: | |
720 |
|
720 | |||
721 | .. literalinclude:: ../../../examples/Embedding/embed_class_long.py |
|
721 | .. literalinclude:: ../../../examples/Embedding/embed_class_long.py | |
722 | :language: python |
|
722 | :language: python | |
723 |
|
723 | |||
724 | Once you understand how the system functions, you can use the following |
|
724 | Once you understand how the system functions, you can use the following | |
725 | code fragments in your programs which are ready for cut and paste: |
|
725 | code fragments in your programs which are ready for cut and paste: | |
726 |
|
726 | |||
727 | .. literalinclude:: ../../../examples/Embedding/embed_class_short.py |
|
727 | .. literalinclude:: ../../../examples/Embedding/embed_class_short.py | |
728 | :language: python |
|
728 | :language: python | |
729 |
|
729 | |||
730 | Using the Python debugger (pdb) |
|
730 | Using the Python debugger (pdb) | |
731 | =============================== |
|
731 | =============================== | |
732 |
|
732 | |||
733 | Running entire programs via pdb |
|
733 | Running entire programs via pdb | |
734 | ------------------------------- |
|
734 | ------------------------------- | |
735 |
|
735 | |||
736 | pdb, the Python debugger, is a powerful interactive debugger which |
|
736 | pdb, the Python debugger, is a powerful interactive debugger which | |
737 | allows you to step through code, set breakpoints, watch variables, |
|
737 | allows you to step through code, set breakpoints, watch variables, | |
738 | etc. IPython makes it very easy to start any script under the control |
|
738 | etc. IPython makes it very easy to start any script under the control | |
739 | of pdb, regardless of whether you have wrapped it into a 'main()' |
|
739 | of pdb, regardless of whether you have wrapped it into a 'main()' | |
740 | function or not. For this, simply type ``%run -d myscript`` at an |
|
740 | function or not. For this, simply type ``%run -d myscript`` at an | |
741 | IPython prompt. See the :magic:`run` command's documentation for more details, including |
|
741 | IPython prompt. See the :magic:`run` command's documentation for more details, including | |
742 | how to control where pdb will stop execution first. |
|
742 | how to control where pdb will stop execution first. | |
743 |
|
743 | |||
744 | For more information on the use of the pdb debugger, see :ref:`debugger-commands` |
|
744 | For more information on the use of the pdb debugger, see :ref:`debugger-commands` | |
745 | in the Python documentation. |
|
745 | in the Python documentation. | |
746 |
|
746 | |||
747 | IPython extends the debugger with a few useful additions, like coloring of |
|
747 | IPython extends the debugger with a few useful additions, like coloring of | |
748 | tracebacks. The debugger will adopt the color scheme selected for IPython. |
|
748 | tracebacks. The debugger will adopt the color scheme selected for IPython. | |
749 |
|
749 | |||
750 | The ``where`` command has also been extended to take as argument the number of |
|
750 | The ``where`` command has also been extended to take as argument the number of | |
751 | context line to show. This allows to a many line of context on shallow stack trace: |
|
751 | context line to show. This allows to a many line of context on shallow stack trace: | |
752 |
|
752 | |||
753 | .. code:: |
|
753 | .. code:: | |
754 |
|
754 | |||
755 | In [5]: def foo(x): |
|
755 | In [5]: def foo(x): | |
756 | ...: 1 |
|
756 | ...: 1 | |
757 | ...: 2 |
|
757 | ...: 2 | |
758 | ...: 3 |
|
758 | ...: 3 | |
759 | ...: return 1/x+foo(x-1) |
|
759 | ...: return 1/x+foo(x-1) | |
760 | ...: 5 |
|
760 | ...: 5 | |
761 | ...: 6 |
|
761 | ...: 6 | |
762 | ...: 7 |
|
762 | ...: 7 | |
763 | ...: |
|
763 | ...: | |
764 |
|
764 | |||
765 | In[6]: foo(1) |
|
765 | In[6]: foo(1) | |
766 | # ... |
|
766 | # ... | |
767 | ipdb> where 8 |
|
767 | ipdb> where 8 | |
768 |
<ipython-input-6-9e45007b2b59>(1)<module> |
|
768 | <ipython-input-6-9e45007b2b59>(1)<module> | |
769 | ----> 1 foo(1) |
|
769 | ----> 1 foo(1) | |
770 |
|
770 | |||
771 | <ipython-input-5-7baadc3d1465>(5)foo() |
|
771 | <ipython-input-5-7baadc3d1465>(5)foo() | |
772 | 1 def foo(x): |
|
772 | 1 def foo(x): | |
773 | 2 1 |
|
773 | 2 1 | |
774 | 3 2 |
|
774 | 3 2 | |
775 | 4 3 |
|
775 | 4 3 | |
776 | ----> 5 return 1/x+foo(x-1) |
|
776 | ----> 5 return 1/x+foo(x-1) | |
777 | 6 5 |
|
777 | 6 5 | |
778 | 7 6 |
|
778 | 7 6 | |
779 | 8 7 |
|
779 | 8 7 | |
780 |
|
780 | |||
781 | > <ipython-input-5-7baadc3d1465>(5)foo() |
|
781 | > <ipython-input-5-7baadc3d1465>(5)foo() | |
782 | 1 def foo(x): |
|
782 | 1 def foo(x): | |
783 | 2 1 |
|
783 | 2 1 | |
784 | 3 2 |
|
784 | 3 2 | |
785 | 4 3 |
|
785 | 4 3 | |
786 | ----> 5 return 1/x+foo(x-1) |
|
786 | ----> 5 return 1/x+foo(x-1) | |
787 | 6 5 |
|
787 | 6 5 | |
788 | 7 6 |
|
788 | 7 6 | |
789 | 8 7 |
|
789 | 8 7 | |
790 |
|
790 | |||
791 |
|
791 | |||
792 | And less context on shallower Stack Trace: |
|
792 | And less context on shallower Stack Trace: | |
793 |
|
793 | |||
794 | .. code:: |
|
794 | .. code:: | |
795 |
|
795 | |||
796 | ipdb> where 1 |
|
796 | ipdb> where 1 | |
797 |
<ipython-input-13-afa180a57233>(1)<module> |
|
797 | <ipython-input-13-afa180a57233>(1)<module> | |
798 | ----> 1 foo(7) |
|
798 | ----> 1 foo(7) | |
799 |
|
799 | |||
800 | <ipython-input-5-7baadc3d1465>(5)foo() |
|
800 | <ipython-input-5-7baadc3d1465>(5)foo() | |
801 | ----> 5 return 1/x+foo(x-1) |
|
801 | ----> 5 return 1/x+foo(x-1) | |
802 |
|
802 | |||
803 | <ipython-input-5-7baadc3d1465>(5)foo() |
|
803 | <ipython-input-5-7baadc3d1465>(5)foo() | |
804 | ----> 5 return 1/x+foo(x-1) |
|
804 | ----> 5 return 1/x+foo(x-1) | |
805 |
|
805 | |||
806 | <ipython-input-5-7baadc3d1465>(5)foo() |
|
806 | <ipython-input-5-7baadc3d1465>(5)foo() | |
807 | ----> 5 return 1/x+foo(x-1) |
|
807 | ----> 5 return 1/x+foo(x-1) | |
808 |
|
808 | |||
809 | <ipython-input-5-7baadc3d1465>(5)foo() |
|
809 | <ipython-input-5-7baadc3d1465>(5)foo() | |
810 | ----> 5 return 1/x+foo(x-1) |
|
810 | ----> 5 return 1/x+foo(x-1) | |
811 |
|
811 | |||
812 |
|
812 | |||
813 | Post-mortem debugging |
|
813 | Post-mortem debugging | |
814 | --------------------- |
|
814 | --------------------- | |
815 |
|
815 | |||
816 | Going into a debugger when an exception occurs can be |
|
816 | Going into a debugger when an exception occurs can be | |
817 | extremely useful in order to find the origin of subtle bugs, because pdb |
|
817 | extremely useful in order to find the origin of subtle bugs, because pdb | |
818 | opens up at the point in your code which triggered the exception, and |
|
818 | opens up at the point in your code which triggered the exception, and | |
819 | while your program is at this point 'dead', all the data is still |
|
819 | while your program is at this point 'dead', all the data is still | |
820 | available and you can walk up and down the stack frame and understand |
|
820 | available and you can walk up and down the stack frame and understand | |
821 | the origin of the problem. |
|
821 | the origin of the problem. | |
822 |
|
822 | |||
823 | You can use the :magic:`debug` magic after an exception has occurred to start |
|
823 | You can use the :magic:`debug` magic after an exception has occurred to start | |
824 | post-mortem debugging. IPython can also call debugger every time your code |
|
824 | post-mortem debugging. IPython can also call debugger every time your code | |
825 | triggers an uncaught exception. This feature can be toggled with the :magic:`pdb` magic |
|
825 | triggers an uncaught exception. This feature can be toggled with the :magic:`pdb` magic | |
826 | command, or you can start IPython with the ``--pdb`` option. |
|
826 | command, or you can start IPython with the ``--pdb`` option. | |
827 |
|
827 | |||
828 | For a post-mortem debugger in your programs outside IPython, |
|
828 | For a post-mortem debugger in your programs outside IPython, | |
829 | put the following lines toward the top of your 'main' routine:: |
|
829 | put the following lines toward the top of your 'main' routine:: | |
830 |
|
830 | |||
831 | import sys |
|
831 | import sys | |
832 | from IPython.core import ultratb |
|
832 | from IPython.core import ultratb | |
833 | sys.excepthook = ultratb.FormattedTB(mode='Verbose', |
|
833 | sys.excepthook = ultratb.FormattedTB(mode='Verbose', | |
834 | color_scheme='Linux', call_pdb=1) |
|
834 | color_scheme='Linux', call_pdb=1) | |
835 |
|
835 | |||
836 | The mode keyword can be either 'Verbose' or 'Plain', giving either very |
|
836 | The mode keyword can be either 'Verbose' or 'Plain', giving either very | |
837 | detailed or normal tracebacks respectively. The color_scheme keyword can |
|
837 | detailed or normal tracebacks respectively. The color_scheme keyword can | |
838 | be one of 'NoColor', 'Linux' (default) or 'LightBG'. These are the same |
|
838 | be one of 'NoColor', 'Linux' (default) or 'LightBG'. These are the same | |
839 | options which can be set in IPython with ``--colors`` and ``--xmode``. |
|
839 | options which can be set in IPython with ``--colors`` and ``--xmode``. | |
840 |
|
840 | |||
841 | This will give any of your programs detailed, colored tracebacks with |
|
841 | This will give any of your programs detailed, colored tracebacks with | |
842 | automatic invocation of pdb. |
|
842 | automatic invocation of pdb. | |
843 |
|
843 | |||
844 | .. _pasting_with_prompts: |
|
844 | .. _pasting_with_prompts: | |
845 |
|
845 | |||
846 | Pasting of code starting with Python or IPython prompts |
|
846 | Pasting of code starting with Python or IPython prompts | |
847 | ======================================================= |
|
847 | ======================================================= | |
848 |
|
848 | |||
849 | IPython is smart enough to filter out input prompts, be they plain Python ones |
|
849 | IPython is smart enough to filter out input prompts, be they plain Python ones | |
850 | (``>>>`` and ``...``) or IPython ones (``In [N]:`` and ``...:``). You can |
|
850 | (``>>>`` and ``...``) or IPython ones (``In [N]:`` and ``...:``). You can | |
851 | therefore copy and paste from existing interactive sessions without worry. |
|
851 | therefore copy and paste from existing interactive sessions without worry. | |
852 |
|
852 | |||
853 | The following is a 'screenshot' of how things work, copying an example from the |
|
853 | The following is a 'screenshot' of how things work, copying an example from the | |
854 | standard Python tutorial:: |
|
854 | standard Python tutorial:: | |
855 |
|
855 | |||
856 | In [1]: >>> # Fibonacci series: |
|
856 | In [1]: >>> # Fibonacci series: | |
857 |
|
857 | |||
858 | In [2]: ... # the sum of two elements defines the next |
|
858 | In [2]: ... # the sum of two elements defines the next | |
859 |
|
859 | |||
860 | In [3]: ... a, b = 0, 1 |
|
860 | In [3]: ... a, b = 0, 1 | |
861 |
|
861 | |||
862 | In [4]: >>> while b < 10: |
|
862 | In [4]: >>> while b < 10: | |
863 | ...: ... print(b) |
|
863 | ...: ... print(b) | |
864 | ...: ... a, b = b, a+b |
|
864 | ...: ... a, b = b, a+b | |
865 | ...: |
|
865 | ...: | |
866 | 1 |
|
866 | 1 | |
867 | 1 |
|
867 | 1 | |
868 | 2 |
|
868 | 2 | |
869 | 3 |
|
869 | 3 | |
870 | 5 |
|
870 | 5 | |
871 | 8 |
|
871 | 8 | |
872 |
|
872 | |||
873 | And pasting from IPython sessions works equally well:: |
|
873 | And pasting from IPython sessions works equally well:: | |
874 |
|
874 | |||
875 | In [1]: In [5]: def f(x): |
|
875 | In [1]: In [5]: def f(x): | |
876 | ...: ...: "A simple function" |
|
876 | ...: ...: "A simple function" | |
877 | ...: ...: return x**2 |
|
877 | ...: ...: return x**2 | |
878 | ...: ...: |
|
878 | ...: ...: | |
879 |
|
879 | |||
880 | In [2]: f(3) |
|
880 | In [2]: f(3) | |
881 | Out[2]: 9 |
|
881 | Out[2]: 9 | |
882 |
|
882 | |||
883 | .. _gui_support: |
|
883 | .. _gui_support: | |
884 |
|
884 | |||
885 | GUI event loop support |
|
885 | GUI event loop support | |
886 | ====================== |
|
886 | ====================== | |
887 |
|
887 | |||
888 | IPython has excellent support for working interactively with Graphical User |
|
888 | IPython has excellent support for working interactively with Graphical User | |
889 | Interface (GUI) toolkits, such as wxPython, PyQt4/PySide, PyGTK and Tk. This is |
|
889 | Interface (GUI) toolkits, such as wxPython, PyQt4/PySide, PyGTK and Tk. This is | |
890 | implemented by running the toolkit's event loop while IPython is waiting for |
|
890 | implemented by running the toolkit's event loop while IPython is waiting for | |
891 | input. |
|
891 | input. | |
892 |
|
892 | |||
893 | For users, enabling GUI event loop integration is simple. You simple use the |
|
893 | For users, enabling GUI event loop integration is simple. You simple use the | |
894 | :magic:`gui` magic as follows:: |
|
894 | :magic:`gui` magic as follows:: | |
895 |
|
895 | |||
896 | %gui [GUINAME] |
|
896 | %gui [GUINAME] | |
897 |
|
897 | |||
898 | With no arguments, ``%gui`` removes all GUI support. Valid ``GUINAME`` |
|
898 | With no arguments, ``%gui`` removes all GUI support. Valid ``GUINAME`` | |
899 | arguments include ``wx``, ``qt``, ``qt5``, ``gtk``, ``gtk3`` and ``tk``. |
|
899 | arguments include ``wx``, ``qt``, ``qt5``, ``gtk``, ``gtk3`` and ``tk``. | |
900 |
|
900 | |||
901 | Thus, to use wxPython interactively and create a running :class:`wx.App` |
|
901 | Thus, to use wxPython interactively and create a running :class:`wx.App` | |
902 | object, do:: |
|
902 | object, do:: | |
903 |
|
903 | |||
904 | %gui wx |
|
904 | %gui wx | |
905 |
|
905 | |||
906 | You can also start IPython with an event loop set up using the `--gui` |
|
906 | You can also start IPython with an event loop set up using the `--gui` | |
907 | flag:: |
|
907 | flag:: | |
908 |
|
908 | |||
909 | $ ipython --gui=qt |
|
909 | $ ipython --gui=qt | |
910 |
|
910 | |||
911 | For information on IPython's matplotlib_ integration (and the ``matplotlib`` |
|
911 | For information on IPython's matplotlib_ integration (and the ``matplotlib`` | |
912 | mode) see :ref:`this section <matplotlib_support>`. |
|
912 | mode) see :ref:`this section <matplotlib_support>`. | |
913 |
|
913 | |||
914 | For developers that want to integrate additional event loops with IPython, see |
|
914 | For developers that want to integrate additional event loops with IPython, see | |
915 | :doc:`/config/eventloops`. |
|
915 | :doc:`/config/eventloops`. | |
916 |
|
916 | |||
917 | When running inside IPython with an integrated event loop, a GUI application |
|
917 | When running inside IPython with an integrated event loop, a GUI application | |
918 | should *not* start its own event loop. This means that applications that are |
|
918 | should *not* start its own event loop. This means that applications that are | |
919 | meant to be used both |
|
919 | meant to be used both | |
920 | in IPython and as standalone apps need to have special code to detects how the |
|
920 | in IPython and as standalone apps need to have special code to detects how the | |
921 | application is being run. We highly recommend using IPython's support for this. |
|
921 | application is being run. We highly recommend using IPython's support for this. | |
922 | Since the details vary slightly between toolkits, we point you to the various |
|
922 | Since the details vary slightly between toolkits, we point you to the various | |
923 | examples in our source directory :file:`examples/IPython Kernel/gui/` that |
|
923 | examples in our source directory :file:`examples/IPython Kernel/gui/` that | |
924 | demonstrate these capabilities. |
|
924 | demonstrate these capabilities. | |
925 |
|
925 | |||
926 | PyQt and PySide |
|
926 | PyQt and PySide | |
927 | --------------- |
|
927 | --------------- | |
928 |
|
928 | |||
929 | .. attempt at explanation of the complete mess that is Qt support |
|
929 | .. attempt at explanation of the complete mess that is Qt support | |
930 |
|
930 | |||
931 | When you use ``--gui=qt`` or ``--matplotlib=qt``, IPython can work with either |
|
931 | When you use ``--gui=qt`` or ``--matplotlib=qt``, IPython can work with either | |
932 | PyQt4 or PySide. There are three options for configuration here, because |
|
932 | PyQt4 or PySide. There are three options for configuration here, because | |
933 | PyQt4 has two APIs for QString and QVariant: v1, which is the default on |
|
933 | PyQt4 has two APIs for QString and QVariant: v1, which is the default on | |
934 | Python 2, and the more natural v2, which is the only API supported by PySide. |
|
934 | Python 2, and the more natural v2, which is the only API supported by PySide. | |
935 | v2 is also the default for PyQt4 on Python 3. IPython's code for the QtConsole |
|
935 | v2 is also the default for PyQt4 on Python 3. IPython's code for the QtConsole | |
936 | uses v2, but you can still use any interface in your code, since the |
|
936 | uses v2, but you can still use any interface in your code, since the | |
937 | Qt frontend is in a different process. |
|
937 | Qt frontend is in a different process. | |
938 |
|
938 | |||
939 | The default will be to import PyQt4 without configuration of the APIs, thus |
|
939 | The default will be to import PyQt4 without configuration of the APIs, thus | |
940 | matching what most applications would expect. It will fall back to PySide if |
|
940 | matching what most applications would expect. It will fall back to PySide if | |
941 | PyQt4 is unavailable. |
|
941 | PyQt4 is unavailable. | |
942 |
|
942 | |||
943 | If specified, IPython will respect the environment variable ``QT_API`` used |
|
943 | If specified, IPython will respect the environment variable ``QT_API`` used | |
944 | by ETS. ETS 4.0 also works with both PyQt4 and PySide, but it requires |
|
944 | by ETS. ETS 4.0 also works with both PyQt4 and PySide, but it requires | |
945 | PyQt4 to use its v2 API. So if ``QT_API=pyside`` PySide will be used, |
|
945 | PyQt4 to use its v2 API. So if ``QT_API=pyside`` PySide will be used, | |
946 | and if ``QT_API=pyqt`` then PyQt4 will be used *with the v2 API* for |
|
946 | and if ``QT_API=pyqt`` then PyQt4 will be used *with the v2 API* for | |
947 | QString and QVariant, so ETS codes like MayaVi will also work with IPython. |
|
947 | QString and QVariant, so ETS codes like MayaVi will also work with IPython. | |
948 |
|
948 | |||
949 | If you launch IPython in matplotlib mode with ``ipython --matplotlib=qt``, |
|
949 | If you launch IPython in matplotlib mode with ``ipython --matplotlib=qt``, | |
950 | then IPython will ask matplotlib which Qt library to use (only if QT_API is |
|
950 | then IPython will ask matplotlib which Qt library to use (only if QT_API is | |
951 | *not set*), via the 'backend.qt4' rcParam. If matplotlib is version 1.0.1 or |
|
951 | *not set*), via the 'backend.qt4' rcParam. If matplotlib is version 1.0.1 or | |
952 | older, then IPython will always use PyQt4 without setting the v2 APIs, since |
|
952 | older, then IPython will always use PyQt4 without setting the v2 APIs, since | |
953 | neither v2 PyQt nor PySide work. |
|
953 | neither v2 PyQt nor PySide work. | |
954 |
|
954 | |||
955 | .. warning:: |
|
955 | .. warning:: | |
956 |
|
956 | |||
957 | Note that this means for ETS 4 to work with PyQt4, ``QT_API`` *must* be set |
|
957 | Note that this means for ETS 4 to work with PyQt4, ``QT_API`` *must* be set | |
958 | to work with IPython's qt integration, because otherwise PyQt4 will be |
|
958 | to work with IPython's qt integration, because otherwise PyQt4 will be | |
959 | loaded in an incompatible mode. |
|
959 | loaded in an incompatible mode. | |
960 |
|
960 | |||
961 | It also means that you must *not* have ``QT_API`` set if you want to |
|
961 | It also means that you must *not* have ``QT_API`` set if you want to | |
962 | use ``--gui=qt`` with code that requires PyQt4 API v1. |
|
962 | use ``--gui=qt`` with code that requires PyQt4 API v1. | |
963 |
|
963 | |||
964 |
|
964 | |||
965 | .. _matplotlib_support: |
|
965 | .. _matplotlib_support: | |
966 |
|
966 | |||
967 | Plotting with matplotlib |
|
967 | Plotting with matplotlib | |
968 | ======================== |
|
968 | ======================== | |
969 |
|
969 | |||
970 | matplotlib_ provides high quality 2D and 3D plotting for Python. matplotlib_ |
|
970 | matplotlib_ provides high quality 2D and 3D plotting for Python. matplotlib_ | |
971 | can produce plots on screen using a variety of GUI toolkits, including Tk, |
|
971 | can produce plots on screen using a variety of GUI toolkits, including Tk, | |
972 | PyGTK, PyQt4 and wxPython. It also provides a number of commands useful for |
|
972 | PyGTK, PyQt4 and wxPython. It also provides a number of commands useful for | |
973 | scientific computing, all with a syntax compatible with that of the popular |
|
973 | scientific computing, all with a syntax compatible with that of the popular | |
974 | Matlab program. |
|
974 | Matlab program. | |
975 |
|
975 | |||
976 | To start IPython with matplotlib support, use the ``--matplotlib`` switch. If |
|
976 | To start IPython with matplotlib support, use the ``--matplotlib`` switch. If | |
977 | IPython is already running, you can run the :magic:`matplotlib` magic. If no |
|
977 | IPython is already running, you can run the :magic:`matplotlib` magic. If no | |
978 | arguments are given, IPython will automatically detect your choice of |
|
978 | arguments are given, IPython will automatically detect your choice of | |
979 | matplotlib backend. You can also request a specific backend with |
|
979 | matplotlib backend. You can also request a specific backend with | |
980 | ``%matplotlib backend``, where ``backend`` must be one of: 'tk', 'qt', 'wx', |
|
980 | ``%matplotlib backend``, where ``backend`` must be one of: 'tk', 'qt', 'wx', | |
981 | 'gtk', 'osx'. In the web notebook and Qt console, 'inline' is also a valid |
|
981 | 'gtk', 'osx'. In the web notebook and Qt console, 'inline' is also a valid | |
982 | backend value, which produces static figures inlined inside the application |
|
982 | backend value, which produces static figures inlined inside the application | |
983 | window instead of matplotlib's interactive figures that live in separate |
|
983 | window instead of matplotlib's interactive figures that live in separate | |
984 | windows. |
|
984 | windows. | |
985 |
|
985 | |||
986 | .. _interactive_demos: |
|
986 | .. _interactive_demos: | |
987 |
|
987 | |||
988 | Interactive demos with IPython |
|
988 | Interactive demos with IPython | |
989 | ============================== |
|
989 | ============================== | |
990 |
|
990 | |||
991 | IPython ships with a basic system for running scripts interactively in |
|
991 | IPython ships with a basic system for running scripts interactively in | |
992 | sections, useful when presenting code to audiences. A few tags embedded |
|
992 | sections, useful when presenting code to audiences. A few tags embedded | |
993 | in comments (so that the script remains valid Python code) divide a file |
|
993 | in comments (so that the script remains valid Python code) divide a file | |
994 | into separate blocks, and the demo can be run one block at a time, with |
|
994 | into separate blocks, and the demo can be run one block at a time, with | |
995 | IPython printing (with syntax highlighting) the block before executing |
|
995 | IPython printing (with syntax highlighting) the block before executing | |
996 | it, and returning to the interactive prompt after each block. The |
|
996 | it, and returning to the interactive prompt after each block. The | |
997 | interactive namespace is updated after each block is run with the |
|
997 | interactive namespace is updated after each block is run with the | |
998 | contents of the demo's namespace. |
|
998 | contents of the demo's namespace. | |
999 |
|
999 | |||
1000 | This allows you to show a piece of code, run it and then execute |
|
1000 | This allows you to show a piece of code, run it and then execute | |
1001 | interactively commands based on the variables just created. Once you |
|
1001 | interactively commands based on the variables just created. Once you | |
1002 | want to continue, you simply execute the next block of the demo. The |
|
1002 | want to continue, you simply execute the next block of the demo. The | |
1003 | following listing shows the markup necessary for dividing a script into |
|
1003 | following listing shows the markup necessary for dividing a script into | |
1004 | sections for execution as a demo: |
|
1004 | sections for execution as a demo: | |
1005 |
|
1005 | |||
1006 | .. literalinclude:: ../../../examples/IPython Kernel/example-demo.py |
|
1006 | .. literalinclude:: ../../../examples/IPython Kernel/example-demo.py | |
1007 | :language: python |
|
1007 | :language: python | |
1008 |
|
1008 | |||
1009 | In order to run a file as a demo, you must first make a Demo object out |
|
1009 | In order to run a file as a demo, you must first make a Demo object out | |
1010 | of it. If the file is named myscript.py, the following code will make a |
|
1010 | of it. If the file is named myscript.py, the following code will make a | |
1011 | demo:: |
|
1011 | demo:: | |
1012 |
|
1012 | |||
1013 | from IPython.lib.demo import Demo |
|
1013 | from IPython.lib.demo import Demo | |
1014 |
|
1014 | |||
1015 | mydemo = Demo('myscript.py') |
|
1015 | mydemo = Demo('myscript.py') | |
1016 |
|
1016 | |||
1017 | This creates the mydemo object, whose blocks you run one at a time by |
|
1017 | This creates the mydemo object, whose blocks you run one at a time by | |
1018 | simply calling the object with no arguments. Then call it to run each step |
|
1018 | simply calling the object with no arguments. Then call it to run each step | |
1019 | of the demo:: |
|
1019 | of the demo:: | |
1020 |
|
1020 | |||
1021 | mydemo() |
|
1021 | mydemo() | |
1022 |
|
1022 | |||
1023 | Demo objects can be |
|
1023 | Demo objects can be | |
1024 | restarted, you can move forward or back skipping blocks, re-execute the |
|
1024 | restarted, you can move forward or back skipping blocks, re-execute the | |
1025 | last block, etc. See the :mod:`IPython.lib.demo` module and the |
|
1025 | last block, etc. See the :mod:`IPython.lib.demo` module and the | |
1026 | :class:`~IPython.lib.demo.Demo` class for details. |
|
1026 | :class:`~IPython.lib.demo.Demo` class for details. | |
1027 |
|
1027 | |||
1028 | Limitations: These demos are limited to |
|
1028 | Limitations: These demos are limited to | |
1029 | fairly simple uses. In particular, you cannot break up sections within |
|
1029 | fairly simple uses. In particular, you cannot break up sections within | |
1030 | indented code (loops, if statements, function definitions, etc.) |
|
1030 | indented code (loops, if statements, function definitions, etc.) | |
1031 | Supporting something like this would basically require tracking the |
|
1031 | Supporting something like this would basically require tracking the | |
1032 | internal execution state of the Python interpreter, so only top-level |
|
1032 | internal execution state of the Python interpreter, so only top-level | |
1033 | divisions are allowed. If you want to be able to open an IPython |
|
1033 | divisions are allowed. If you want to be able to open an IPython | |
1034 | instance at an arbitrary point in a program, you can use IPython's |
|
1034 | instance at an arbitrary point in a program, you can use IPython's | |
1035 | :ref:`embedding facilities <Embedding>`. |
|
1035 | :ref:`embedding facilities <Embedding>`. | |
1036 |
|
1036 | |||
1037 | .. include:: ../links.txt |
|
1037 | .. include:: ../links.txt |
General Comments 0
You need to be logged in to leave comments.
Login now