Show More
@@ -1,249 +1,254 b'' | |||||
1 | """Tests for various magic functions. |
|
1 | """Tests for various magic functions. | |
2 |
|
2 | |||
3 | Needs to be run by nose (to make ipython session available). |
|
3 | Needs to be run by nose (to make ipython session available). | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | import os |
|
6 | import os | |
7 | import sys |
|
7 | import sys | |
8 | import tempfile |
|
8 | import tempfile | |
9 | import types |
|
9 | import types | |
10 |
|
10 | |||
11 | import nose.tools as nt |
|
11 | import nose.tools as nt | |
12 |
|
12 | |||
13 | from IPython.utils.platutils import find_cmd, get_long_path_name |
|
13 | from IPython.utils.platutils import find_cmd, get_long_path_name | |
14 | from IPython.testing import decorators as dec |
|
14 | from IPython.testing import decorators as dec | |
15 | from IPython.testing import tools as tt |
|
15 | from IPython.testing import tools as tt | |
16 |
|
16 | |||
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 | # Test functions begin |
|
18 | # Test functions begin | |
19 |
|
19 | |||
20 | def test_rehashx(): |
|
20 | def test_rehashx(): | |
21 | # clear up everything |
|
21 | # clear up everything | |
22 | _ip.IP.alias_table.clear() |
|
22 | _ip.IP.alias_table.clear() | |
23 | del _ip.db['syscmdlist'] |
|
23 | del _ip.db['syscmdlist'] | |
24 |
|
24 | |||
25 | _ip.magic('rehashx') |
|
25 | _ip.magic('rehashx') | |
26 | # Practically ALL ipython development systems will have more than 10 aliases |
|
26 | # Practically ALL ipython development systems will have more than 10 aliases | |
27 |
|
27 | |||
28 | assert len(_ip.IP.alias_table) > 10 |
|
28 | yield (nt.assert_true, len(_ip.IP.alias_table) > 10) | |
29 | for key, val in _ip.IP.alias_table.items(): |
|
29 | for key, val in _ip.IP.alias_table.items(): | |
30 | # we must strip dots from alias names |
|
30 | # we must strip dots from alias names | |
31 |
assert |
|
31 | nt.assert_true('.' not in key) | |
32 |
|
32 | |||
33 | # rehashx must fill up syscmdlist |
|
33 | # rehashx must fill up syscmdlist | |
34 | scoms = _ip.db['syscmdlist'] |
|
34 | scoms = _ip.db['syscmdlist'] | |
35 | assert len(scoms) > 10 |
|
35 | yield (nt.assert_true, len(scoms) > 10) | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | def doctest_hist_f(): |
|
38 | def doctest_hist_f(): | |
39 | """Test %hist -f with temporary filename. |
|
39 | """Test %hist -f with temporary filename. | |
40 |
|
40 | |||
41 | In [9]: import tempfile |
|
41 | In [9]: import tempfile | |
42 |
|
42 | |||
43 | In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-') |
|
43 | In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-') | |
44 |
|
44 | |||
45 |
In [11]: %hist |
|
45 | In [11]: %hist -n -f $tfile 3 | |
|
46 | ||||
46 | """ |
|
47 | """ | |
47 |
|
48 | |||
48 |
|
49 | |||
49 | def doctest_hist_r(): |
|
50 | def doctest_hist_r(): | |
50 | """Test %hist -r |
|
51 | """Test %hist -r | |
51 |
|
52 | |||
52 | XXX - This test is not recording the output correctly. Not sure why... |
|
53 | XXX - This test is not recording the output correctly. Not sure why... | |
53 |
|
54 | |||
|
55 | In [20]: 'hist' in _ip.IP.lsmagic() | |||
|
56 | Out[20]: True | |||
|
57 | ||||
54 | In [6]: x=1 |
|
58 | In [6]: x=1 | |
55 |
|
59 | |||
56 | In [7]: hist -n -r 2 |
|
60 | In [7]: %hist -n -r 2 | |
57 | x=1 # random |
|
61 | x=1 # random | |
58 | hist -n -r 2 # random |
|
62 | hist -n -r 2 # random | |
59 | """ |
|
63 | """ | |
60 |
|
64 | |||
61 | # This test is known to fail on win32. |
|
65 | # This test is known to fail on win32. | |
62 | # See ticket https://bugs.launchpad.net/bugs/366334 |
|
66 | # See ticket https://bugs.launchpad.net/bugs/366334 | |
63 | def test_obj_del(): |
|
67 | def test_obj_del(): | |
64 | """Test that object's __del__ methods are called on exit.""" |
|
68 | """Test that object's __del__ methods are called on exit.""" | |
65 | test_dir = os.path.dirname(__file__) |
|
69 | test_dir = os.path.dirname(__file__) | |
66 | del_file = os.path.join(test_dir,'obj_del.py') |
|
70 | del_file = os.path.join(test_dir,'obj_del.py') | |
67 | ipython_cmd = find_cmd('ipython') |
|
71 | ipython_cmd = find_cmd('ipython') | |
68 | out = _ip.IP.getoutput('%s %s' % (ipython_cmd, del_file)) |
|
72 | out = _ip.IP.getoutput('%s %s' % (ipython_cmd, del_file)) | |
69 | nt.assert_equals(out,'obj_del.py: object A deleted') |
|
73 | nt.assert_equals(out,'obj_del.py: object A deleted') | |
70 |
|
74 | |||
71 |
|
75 | |||
72 | def test_shist(): |
|
76 | def test_shist(): | |
73 | # Simple tests of ShadowHist class - test generator. |
|
77 | # Simple tests of ShadowHist class - test generator. | |
74 | import os, shutil, tempfile |
|
78 | import os, shutil, tempfile | |
75 |
|
79 | |||
76 | from IPython.extensions import pickleshare |
|
80 | from IPython.extensions import pickleshare | |
77 | from IPython.core.history import ShadowHist |
|
81 | from IPython.core.history import ShadowHist | |
78 |
|
82 | |||
79 | tfile = tempfile.mktemp('','tmp-ipython-') |
|
83 | tfile = tempfile.mktemp('','tmp-ipython-') | |
80 |
|
84 | |||
81 | db = pickleshare.PickleShareDB(tfile) |
|
85 | db = pickleshare.PickleShareDB(tfile) | |
82 | s = ShadowHist(db) |
|
86 | s = ShadowHist(db) | |
83 | s.add('hello') |
|
87 | s.add('hello') | |
84 | s.add('world') |
|
88 | s.add('world') | |
85 | s.add('hello') |
|
89 | s.add('hello') | |
86 | s.add('hello') |
|
90 | s.add('hello') | |
87 | s.add('karhu') |
|
91 | s.add('karhu') | |
88 |
|
92 | |||
89 | yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')] |
|
93 | yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')] | |
90 |
|
94 | |||
91 | yield nt.assert_equal,s.get(2),'world' |
|
95 | yield nt.assert_equal,s.get(2),'world' | |
92 |
|
96 | |||
93 | shutil.rmtree(tfile) |
|
97 | shutil.rmtree(tfile) | |
94 |
|
98 | |||
95 | @dec.skipif_not_numpy |
|
99 | @dec.skipif_not_numpy | |
96 | def test_numpy_clear_array_undec(): |
|
100 | def test_numpy_clear_array_undec(): | |
|
101 | from IPython.extensions import clearcmd | |||
|
102 | ||||
97 | _ip.ex('import numpy as np') |
|
103 | _ip.ex('import numpy as np') | |
98 | _ip.ex('a = np.empty(2)') |
|
104 | _ip.ex('a = np.empty(2)') | |
99 |
|
105 | yield (nt.assert_true, 'a' in _ip.user_ns) | ||
100 | yield nt.assert_true,'a' in _ip.user_ns |
|
|||
101 | _ip.magic('clear array') |
|
106 | _ip.magic('clear array') | |
102 | yield nt.assert_false,'a' in _ip.user_ns |
|
107 | yield (nt.assert_false, 'a' in _ip.user_ns) | |
103 |
|
108 | |||
104 |
|
109 | |||
105 | @dec.skip() |
|
110 | @dec.skip() | |
106 | def test_fail_dec(*a,**k): |
|
111 | def test_fail_dec(*a,**k): | |
107 | yield nt.assert_true, False |
|
112 | yield nt.assert_true, False | |
108 |
|
113 | |||
109 | @dec.skip('This one shouldn not run') |
|
114 | @dec.skip('This one shouldn not run') | |
110 | def test_fail_dec2(*a,**k): |
|
115 | def test_fail_dec2(*a,**k): | |
111 | yield nt.assert_true, False |
|
116 | yield nt.assert_true, False | |
112 |
|
117 | |||
113 | @dec.skipknownfailure |
|
118 | @dec.skipknownfailure | |
114 | def test_fail_dec3(*a,**k): |
|
119 | def test_fail_dec3(*a,**k): | |
115 | yield nt.assert_true, False |
|
120 | yield nt.assert_true, False | |
116 |
|
121 | |||
117 |
|
122 | |||
118 | def doctest_refbug(): |
|
123 | def doctest_refbug(): | |
119 | """Very nasty problem with references held by multiple runs of a script. |
|
124 | """Very nasty problem with references held by multiple runs of a script. | |
120 | See: https://bugs.launchpad.net/ipython/+bug/269966 |
|
125 | See: https://bugs.launchpad.net/ipython/+bug/269966 | |
121 |
|
126 | |||
122 | In [1]: _ip.IP.clear_main_mod_cache() |
|
127 | In [1]: _ip.IP.clear_main_mod_cache() | |
123 |
|
128 | |||
124 | In [2]: run refbug |
|
129 | In [2]: run refbug | |
125 |
|
130 | |||
126 | In [3]: call_f() |
|
131 | In [3]: call_f() | |
127 | lowercased: hello |
|
132 | lowercased: hello | |
128 |
|
133 | |||
129 | In [4]: run refbug |
|
134 | In [4]: run refbug | |
130 |
|
135 | |||
131 | In [5]: call_f() |
|
136 | In [5]: call_f() | |
132 | lowercased: hello |
|
137 | lowercased: hello | |
133 | lowercased: hello |
|
138 | lowercased: hello | |
134 | """ |
|
139 | """ | |
135 |
|
140 | |||
136 | #----------------------------------------------------------------------------- |
|
141 | #----------------------------------------------------------------------------- | |
137 | # Tests for %run |
|
142 | # Tests for %run | |
138 | #----------------------------------------------------------------------------- |
|
143 | #----------------------------------------------------------------------------- | |
139 |
|
144 | |||
140 | # %run is critical enough that it's a good idea to have a solid collection of |
|
145 | # %run is critical enough that it's a good idea to have a solid collection of | |
141 | # tests for it, some as doctests and some as normal tests. |
|
146 | # tests for it, some as doctests and some as normal tests. | |
142 |
|
147 | |||
143 | def doctest_run_ns(): |
|
148 | def doctest_run_ns(): | |
144 | """Classes declared %run scripts must be instantiable afterwards. |
|
149 | """Classes declared %run scripts must be instantiable afterwards. | |
145 |
|
150 | |||
146 | In [11]: run tclass foo |
|
151 | In [11]: run tclass foo | |
147 |
|
152 | |||
148 | In [12]: isinstance(f(),foo) |
|
153 | In [12]: isinstance(f(),foo) | |
149 | Out[12]: True |
|
154 | Out[12]: True | |
150 | """ |
|
155 | """ | |
151 |
|
156 | |||
152 |
|
157 | |||
153 | def doctest_run_ns2(): |
|
158 | def doctest_run_ns2(): | |
154 | """Classes declared %run scripts must be instantiable afterwards. |
|
159 | """Classes declared %run scripts must be instantiable afterwards. | |
155 |
|
160 | |||
156 | In [4]: run tclass C-first_pass |
|
161 | In [4]: run tclass C-first_pass | |
157 |
|
162 | |||
158 | In [5]: run tclass C-second_pass |
|
163 | In [5]: run tclass C-second_pass | |
159 | tclass.py: deleting object: C-first_pass |
|
164 | tclass.py: deleting object: C-first_pass | |
160 | """ |
|
165 | """ | |
161 |
|
166 | |||
162 | @dec.skip_win32 |
|
167 | @dec.skip_win32 | |
163 | def doctest_run_builtins(): |
|
168 | def doctest_run_builtins(): | |
164 | """Check that %run doesn't damage __builtins__ via a doctest. |
|
169 | """Check that %run doesn't damage __builtins__ via a doctest. | |
165 |
|
170 | |||
166 | This is similar to the test_run_builtins, but I want *both* forms of the |
|
171 | This is similar to the test_run_builtins, but I want *both* forms of the | |
167 | test to catch any possible glitches in our testing machinery, since that |
|
172 | test to catch any possible glitches in our testing machinery, since that | |
168 | modifies %run somewhat. So for this, we have both a normal test (below) |
|
173 | modifies %run somewhat. So for this, we have both a normal test (below) | |
169 | and a doctest (this one). |
|
174 | and a doctest (this one). | |
170 |
|
175 | |||
171 | In [1]: import tempfile |
|
176 | In [1]: import tempfile | |
172 |
|
177 | |||
173 | In [2]: bid1 = id(__builtins__) |
|
178 | In [2]: bid1 = id(__builtins__) | |
174 |
|
179 | |||
175 | In [3]: f = tempfile.NamedTemporaryFile() |
|
180 | In [3]: f = tempfile.NamedTemporaryFile() | |
176 |
|
181 | |||
177 | In [4]: f.write('pass\\n') |
|
182 | In [4]: f.write('pass\\n') | |
178 |
|
183 | |||
179 | In [5]: f.flush() |
|
184 | In [5]: f.flush() | |
180 |
|
185 | |||
181 | In [6]: print 'B1:',type(__builtins__) |
|
186 | In [6]: print 'B1:',type(__builtins__) | |
182 | B1: <type 'module'> |
|
187 | B1: <type 'module'> | |
183 |
|
188 | |||
184 | In [7]: %run $f.name |
|
189 | In [7]: %run $f.name | |
185 |
|
190 | |||
186 | In [8]: bid2 = id(__builtins__) |
|
191 | In [8]: bid2 = id(__builtins__) | |
187 |
|
192 | |||
188 | In [9]: print 'B2:',type(__builtins__) |
|
193 | In [9]: print 'B2:',type(__builtins__) | |
189 | B2: <type 'module'> |
|
194 | B2: <type 'module'> | |
190 |
|
195 | |||
191 | In [10]: bid1 == bid2 |
|
196 | In [10]: bid1 == bid2 | |
192 | Out[10]: True |
|
197 | Out[10]: True | |
193 | """ |
|
198 | """ | |
194 |
|
199 | |||
195 | # For some tests, it will be handy to organize them in a class with a common |
|
200 | # For some tests, it will be handy to organize them in a class with a common | |
196 | # setup that makes a temp file |
|
201 | # setup that makes a temp file | |
197 |
|
202 | |||
198 | class TestMagicRun(object): |
|
203 | class TestMagicRun(object): | |
199 |
|
204 | |||
200 | def setup(self): |
|
205 | def setup(self): | |
201 | """Make a valid python temp file.""" |
|
206 | """Make a valid python temp file.""" | |
202 | f = tempfile.NamedTemporaryFile() |
|
207 | f = tempfile.NamedTemporaryFile() | |
203 | f.write('pass\n') |
|
208 | f.write('pass\n') | |
204 | f.flush() |
|
209 | f.flush() | |
205 | self.tmpfile = f |
|
210 | self.tmpfile = f | |
206 |
|
211 | |||
207 | def run_tmpfile(self): |
|
212 | def run_tmpfile(self): | |
208 | # This fails on Windows if self.tmpfile.name has spaces or "~" in it. |
|
213 | # This fails on Windows if self.tmpfile.name has spaces or "~" in it. | |
209 | # See below and ticket https://bugs.launchpad.net/bugs/366353 |
|
214 | # See below and ticket https://bugs.launchpad.net/bugs/366353 | |
210 | _ip.magic('run %s' % self.tmpfile.name) |
|
215 | _ip.magic('run %s' % self.tmpfile.name) | |
211 |
|
216 | |||
212 | # See https://bugs.launchpad.net/bugs/366353 |
|
217 | # See https://bugs.launchpad.net/bugs/366353 | |
213 | @dec.skip_if_not_win32 |
|
218 | @dec.skip_if_not_win32 | |
214 | def test_run_tempfile_path(self): |
|
219 | def test_run_tempfile_path(self): | |
215 | tt.assert_equals(True,False,"%run doesn't work with tempfile paths on win32.") |
|
220 | tt.assert_equals(True,False,"%run doesn't work with tempfile paths on win32.") | |
216 |
|
221 | |||
217 | # See https://bugs.launchpad.net/bugs/366353 |
|
222 | # See https://bugs.launchpad.net/bugs/366353 | |
218 | @dec.skip_win32 |
|
223 | @dec.skip_win32 | |
219 | def test_builtins_id(self): |
|
224 | def test_builtins_id(self): | |
220 | """Check that %run doesn't damage __builtins__ """ |
|
225 | """Check that %run doesn't damage __builtins__ """ | |
221 |
|
226 | |||
222 | # Test that the id of __builtins__ is not modified by %run |
|
227 | # Test that the id of __builtins__ is not modified by %run | |
223 | bid1 = id(_ip.user_ns['__builtins__']) |
|
228 | bid1 = id(_ip.user_ns['__builtins__']) | |
224 | self.run_tmpfile() |
|
229 | self.run_tmpfile() | |
225 | bid2 = id(_ip.user_ns['__builtins__']) |
|
230 | bid2 = id(_ip.user_ns['__builtins__']) | |
226 | tt.assert_equals(bid1, bid2) |
|
231 | tt.assert_equals(bid1, bid2) | |
227 |
|
232 | |||
228 | # See https://bugs.launchpad.net/bugs/366353 |
|
233 | # See https://bugs.launchpad.net/bugs/366353 | |
229 | @dec.skip_win32 |
|
234 | @dec.skip_win32 | |
230 | def test_builtins_type(self): |
|
235 | def test_builtins_type(self): | |
231 | """Check that the type of __builtins__ doesn't change with %run. |
|
236 | """Check that the type of __builtins__ doesn't change with %run. | |
232 |
|
237 | |||
233 | However, the above could pass if __builtins__ was already modified to |
|
238 | However, the above could pass if __builtins__ was already modified to | |
234 | be a dict (it should be a module) by a previous use of %run. So we |
|
239 | be a dict (it should be a module) by a previous use of %run. So we | |
235 | also check explicitly that it really is a module: |
|
240 | also check explicitly that it really is a module: | |
236 | """ |
|
241 | """ | |
237 | self.run_tmpfile() |
|
242 | self.run_tmpfile() | |
238 | tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys)) |
|
243 | tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys)) | |
239 |
|
244 | |||
240 | # See https://bugs.launchpad.net/bugs/366353 |
|
245 | # See https://bugs.launchpad.net/bugs/366353 | |
241 | @dec.skip_win32 |
|
246 | @dec.skip_win32 | |
242 | def test_prompts(self): |
|
247 | def test_prompts(self): | |
243 | """Test that prompts correctly generate after %run""" |
|
248 | """Test that prompts correctly generate after %run""" | |
244 | self.run_tmpfile() |
|
249 | self.run_tmpfile() | |
245 | p2 = str(_ip.IP.outputcache.prompt2).strip() |
|
250 | p2 = str(_ip.IP.outputcache.prompt2).strip() | |
246 | nt.assert_equals(p2[:3], '...') |
|
251 | nt.assert_equals(p2[:3], '...') | |
247 |
|
252 | |||
248 | def teardown(self): |
|
253 | def teardown(self): | |
249 | self.tmpfile.close() |
|
254 | self.tmpfile.close() |
@@ -1,1057 +1,1063 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | ultratb.py -- Spice up your tracebacks! |
|
3 | ultratb.py -- Spice up your tracebacks! | |
4 |
|
4 | |||
5 | * ColorTB |
|
5 | * ColorTB | |
6 | I've always found it a bit hard to visually parse tracebacks in Python. The |
|
6 | I've always found it a bit hard to visually parse tracebacks in Python. The | |
7 | ColorTB class is a solution to that problem. It colors the different parts of a |
|
7 | ColorTB class is a solution to that problem. It colors the different parts of a | |
8 | traceback in a manner similar to what you would expect from a syntax-highlighting |
|
8 | traceback in a manner similar to what you would expect from a syntax-highlighting | |
9 | text editor. |
|
9 | text editor. | |
10 |
|
10 | |||
11 | Installation instructions for ColorTB: |
|
11 | Installation instructions for ColorTB: | |
12 | import sys,ultratb |
|
12 | import sys,ultratb | |
13 | sys.excepthook = ultratb.ColorTB() |
|
13 | sys.excepthook = ultratb.ColorTB() | |
14 |
|
14 | |||
15 | * VerboseTB |
|
15 | * VerboseTB | |
16 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds |
|
16 | I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds | |
17 | of useful info when a traceback occurs. Ping originally had it spit out HTML |
|
17 | of useful info when a traceback occurs. Ping originally had it spit out HTML | |
18 | and intended it for CGI programmers, but why should they have all the fun? I |
|
18 | and intended it for CGI programmers, but why should they have all the fun? I | |
19 | altered it to spit out colored text to the terminal. It's a bit overwhelming, |
|
19 | altered it to spit out colored text to the terminal. It's a bit overwhelming, | |
20 | but kind of neat, and maybe useful for long-running programs that you believe |
|
20 | but kind of neat, and maybe useful for long-running programs that you believe | |
21 | are bug-free. If a crash *does* occur in that type of program you want details. |
|
21 | are bug-free. If a crash *does* occur in that type of program you want details. | |
22 | Give it a shot--you'll love it or you'll hate it. |
|
22 | Give it a shot--you'll love it or you'll hate it. | |
23 |
|
23 | |||
24 | Note: |
|
24 | Note: | |
25 |
|
25 | |||
26 | The Verbose mode prints the variables currently visible where the exception |
|
26 | The Verbose mode prints the variables currently visible where the exception | |
27 | happened (shortening their strings if too long). This can potentially be |
|
27 | happened (shortening their strings if too long). This can potentially be | |
28 | very slow, if you happen to have a huge data structure whose string |
|
28 | very slow, if you happen to have a huge data structure whose string | |
29 | representation is complex to compute. Your computer may appear to freeze for |
|
29 | representation is complex to compute. Your computer may appear to freeze for | |
30 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback |
|
30 | a while with cpu usage at 100%. If this occurs, you can cancel the traceback | |
31 | with Ctrl-C (maybe hitting it more than once). |
|
31 | with Ctrl-C (maybe hitting it more than once). | |
32 |
|
32 | |||
33 | If you encounter this kind of situation often, you may want to use the |
|
33 | If you encounter this kind of situation often, you may want to use the | |
34 | Verbose_novars mode instead of the regular Verbose, which avoids formatting |
|
34 | Verbose_novars mode instead of the regular Verbose, which avoids formatting | |
35 | variables (but otherwise includes the information and context given by |
|
35 | variables (but otherwise includes the information and context given by | |
36 | Verbose). |
|
36 | Verbose). | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | Installation instructions for ColorTB: |
|
39 | Installation instructions for ColorTB: | |
40 | import sys,ultratb |
|
40 | import sys,ultratb | |
41 | sys.excepthook = ultratb.VerboseTB() |
|
41 | sys.excepthook = ultratb.VerboseTB() | |
42 |
|
42 | |||
43 | Note: Much of the code in this module was lifted verbatim from the standard |
|
43 | Note: Much of the code in this module was lifted verbatim from the standard | |
44 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. |
|
44 | library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'. | |
45 |
|
45 | |||
46 | * Color schemes |
|
46 | * Color schemes | |
47 | The colors are defined in the class TBTools through the use of the |
|
47 | The colors are defined in the class TBTools through the use of the | |
48 | ColorSchemeTable class. Currently the following exist: |
|
48 | ColorSchemeTable class. Currently the following exist: | |
49 |
|
49 | |||
50 | - NoColor: allows all of this module to be used in any terminal (the color |
|
50 | - NoColor: allows all of this module to be used in any terminal (the color | |
51 | escapes are just dummy blank strings). |
|
51 | escapes are just dummy blank strings). | |
52 |
|
52 | |||
53 | - Linux: is meant to look good in a terminal like the Linux console (black |
|
53 | - Linux: is meant to look good in a terminal like the Linux console (black | |
54 | or very dark background). |
|
54 | or very dark background). | |
55 |
|
55 | |||
56 | - LightBG: similar to Linux but swaps dark/light colors to be more readable |
|
56 | - LightBG: similar to Linux but swaps dark/light colors to be more readable | |
57 | in light background terminals. |
|
57 | in light background terminals. | |
58 |
|
58 | |||
59 | You can implement other color schemes easily, the syntax is fairly |
|
59 | You can implement other color schemes easily, the syntax is fairly | |
60 | self-explanatory. Please send back new schemes you develop to the author for |
|
60 | self-explanatory. Please send back new schemes you develop to the author for | |
61 | possible inclusion in future releases. |
|
61 | possible inclusion in future releases. | |
62 | """ |
|
62 | """ | |
63 |
|
63 | |||
64 | #***************************************************************************** |
|
64 | #***************************************************************************** | |
65 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> |
|
65 | # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu> | |
66 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> |
|
66 | # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu> | |
67 | # |
|
67 | # | |
68 | # Distributed under the terms of the BSD License. The full license is in |
|
68 | # Distributed under the terms of the BSD License. The full license is in | |
69 | # the file COPYING, distributed as part of this software. |
|
69 | # the file COPYING, distributed as part of this software. | |
70 | #***************************************************************************** |
|
70 | #***************************************************************************** | |
71 |
|
71 | |||
72 | # Required modules |
|
72 | # Required modules | |
73 | import inspect |
|
73 | import inspect | |
74 | import keyword |
|
74 | import keyword | |
75 | import linecache |
|
75 | import linecache | |
76 | import os |
|
76 | import os | |
77 | import pydoc |
|
77 | import pydoc | |
78 | import re |
|
78 | import re | |
79 | import string |
|
79 | import string | |
80 | import sys |
|
80 | import sys | |
81 | import time |
|
81 | import time | |
82 | import tokenize |
|
82 | import tokenize | |
83 | import traceback |
|
83 | import traceback | |
84 | import types |
|
84 | import types | |
85 |
|
85 | |||
86 | # For purposes of monkeypatching inspect to fix a bug in it. |
|
86 | # For purposes of monkeypatching inspect to fix a bug in it. | |
87 | from inspect import getsourcefile, getfile, getmodule,\ |
|
87 | from inspect import getsourcefile, getfile, getmodule,\ | |
88 | ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode |
|
88 | ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | # IPython's own modules |
|
91 | # IPython's own modules | |
92 | # Modified pdb which doesn't damage IPython's readline handling |
|
92 | # Modified pdb which doesn't damage IPython's readline handling | |
93 | from IPython.utils import PyColorize |
|
93 | from IPython.utils import PyColorize | |
94 | from IPython.core import debugger |
|
94 | from IPython.core import debugger, ipapi | |
95 | from IPython.utils.ipstruct import Struct |
|
95 | from IPython.utils.ipstruct import Struct | |
96 | from IPython.core.excolors import exception_colors |
|
96 | from IPython.core.excolors import exception_colors | |
97 | from IPython.utils.genutils import Term,uniq_stable,error,info |
|
97 | from IPython.utils.genutils import Term,uniq_stable,error,info | |
98 |
|
98 | |||
99 | # Globals |
|
99 | # Globals | |
100 | # amount of space to put line numbers before verbose tracebacks |
|
100 | # amount of space to put line numbers before verbose tracebacks | |
101 | INDENT_SIZE = 8 |
|
101 | INDENT_SIZE = 8 | |
102 |
|
102 | |||
103 | # Default color scheme. This is used, for example, by the traceback |
|
103 | # Default color scheme. This is used, for example, by the traceback | |
104 | # formatter. When running in an actual IPython instance, the user's rc.colors |
|
104 | # formatter. When running in an actual IPython instance, the user's rc.colors | |
105 | # value is used, but havinga module global makes this functionality available |
|
105 | # value is used, but havinga module global makes this functionality available | |
106 | # to users of ultratb who are NOT running inside ipython. |
|
106 | # to users of ultratb who are NOT running inside ipython. | |
107 | DEFAULT_SCHEME = 'NoColor' |
|
107 | DEFAULT_SCHEME = 'NoColor' | |
108 |
|
108 | |||
109 | #--------------------------------------------------------------------------- |
|
109 | #--------------------------------------------------------------------------- | |
110 | # Code begins |
|
110 | # Code begins | |
111 |
|
111 | |||
112 | # Utility functions |
|
112 | # Utility functions | |
113 | def inspect_error(): |
|
113 | def inspect_error(): | |
114 | """Print a message about internal inspect errors. |
|
114 | """Print a message about internal inspect errors. | |
115 |
|
115 | |||
116 | These are unfortunately quite common.""" |
|
116 | These are unfortunately quite common.""" | |
117 |
|
117 | |||
118 | error('Internal Python error in the inspect module.\n' |
|
118 | error('Internal Python error in the inspect module.\n' | |
119 | 'Below is the traceback from this internal error.\n') |
|
119 | 'Below is the traceback from this internal error.\n') | |
120 |
|
120 | |||
121 |
|
121 | |||
122 | def findsource(object): |
|
122 | def findsource(object): | |
123 | """Return the entire source file and starting line number for an object. |
|
123 | """Return the entire source file and starting line number for an object. | |
124 |
|
124 | |||
125 | The argument may be a module, class, method, function, traceback, frame, |
|
125 | The argument may be a module, class, method, function, traceback, frame, | |
126 | or code object. The source code is returned as a list of all the lines |
|
126 | or code object. The source code is returned as a list of all the lines | |
127 | in the file and the line number indexes a line in that list. An IOError |
|
127 | in the file and the line number indexes a line in that list. An IOError | |
128 | is raised if the source code cannot be retrieved. |
|
128 | is raised if the source code cannot be retrieved. | |
129 |
|
129 | |||
130 | FIXED version with which we monkeypatch the stdlib to work around a bug.""" |
|
130 | FIXED version with which we monkeypatch the stdlib to work around a bug.""" | |
131 |
|
131 | |||
132 | file = getsourcefile(object) or getfile(object) |
|
132 | file = getsourcefile(object) or getfile(object) | |
133 | # If the object is a frame, then trying to get the globals dict from its |
|
133 | # If the object is a frame, then trying to get the globals dict from its | |
134 | # module won't work. Instead, the frame object itself has the globals |
|
134 | # module won't work. Instead, the frame object itself has the globals | |
135 | # dictionary. |
|
135 | # dictionary. | |
136 | globals_dict = None |
|
136 | globals_dict = None | |
137 | if inspect.isframe(object): |
|
137 | if inspect.isframe(object): | |
138 | # XXX: can this ever be false? |
|
138 | # XXX: can this ever be false? | |
139 | globals_dict = object.f_globals |
|
139 | globals_dict = object.f_globals | |
140 | else: |
|
140 | else: | |
141 | module = getmodule(object, file) |
|
141 | module = getmodule(object, file) | |
142 | if module: |
|
142 | if module: | |
143 | globals_dict = module.__dict__ |
|
143 | globals_dict = module.__dict__ | |
144 | lines = linecache.getlines(file, globals_dict) |
|
144 | lines = linecache.getlines(file, globals_dict) | |
145 | if not lines: |
|
145 | if not lines: | |
146 | raise IOError('could not get source code') |
|
146 | raise IOError('could not get source code') | |
147 |
|
147 | |||
148 | if ismodule(object): |
|
148 | if ismodule(object): | |
149 | return lines, 0 |
|
149 | return lines, 0 | |
150 |
|
150 | |||
151 | if isclass(object): |
|
151 | if isclass(object): | |
152 | name = object.__name__ |
|
152 | name = object.__name__ | |
153 | pat = re.compile(r'^(\s*)class\s*' + name + r'\b') |
|
153 | pat = re.compile(r'^(\s*)class\s*' + name + r'\b') | |
154 | # make some effort to find the best matching class definition: |
|
154 | # make some effort to find the best matching class definition: | |
155 | # use the one with the least indentation, which is the one |
|
155 | # use the one with the least indentation, which is the one | |
156 | # that's most probably not inside a function definition. |
|
156 | # that's most probably not inside a function definition. | |
157 | candidates = [] |
|
157 | candidates = [] | |
158 | for i in range(len(lines)): |
|
158 | for i in range(len(lines)): | |
159 | match = pat.match(lines[i]) |
|
159 | match = pat.match(lines[i]) | |
160 | if match: |
|
160 | if match: | |
161 | # if it's at toplevel, it's already the best one |
|
161 | # if it's at toplevel, it's already the best one | |
162 | if lines[i][0] == 'c': |
|
162 | if lines[i][0] == 'c': | |
163 | return lines, i |
|
163 | return lines, i | |
164 | # else add whitespace to candidate list |
|
164 | # else add whitespace to candidate list | |
165 | candidates.append((match.group(1), i)) |
|
165 | candidates.append((match.group(1), i)) | |
166 | if candidates: |
|
166 | if candidates: | |
167 | # this will sort by whitespace, and by line number, |
|
167 | # this will sort by whitespace, and by line number, | |
168 | # less whitespace first |
|
168 | # less whitespace first | |
169 | candidates.sort() |
|
169 | candidates.sort() | |
170 | return lines, candidates[0][1] |
|
170 | return lines, candidates[0][1] | |
171 | else: |
|
171 | else: | |
172 | raise IOError('could not find class definition') |
|
172 | raise IOError('could not find class definition') | |
173 |
|
173 | |||
174 | if ismethod(object): |
|
174 | if ismethod(object): | |
175 | object = object.im_func |
|
175 | object = object.im_func | |
176 | if isfunction(object): |
|
176 | if isfunction(object): | |
177 | object = object.func_code |
|
177 | object = object.func_code | |
178 | if istraceback(object): |
|
178 | if istraceback(object): | |
179 | object = object.tb_frame |
|
179 | object = object.tb_frame | |
180 | if isframe(object): |
|
180 | if isframe(object): | |
181 | object = object.f_code |
|
181 | object = object.f_code | |
182 | if iscode(object): |
|
182 | if iscode(object): | |
183 | if not hasattr(object, 'co_firstlineno'): |
|
183 | if not hasattr(object, 'co_firstlineno'): | |
184 | raise IOError('could not find function definition') |
|
184 | raise IOError('could not find function definition') | |
185 | pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') |
|
185 | pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') | |
186 | pmatch = pat.match |
|
186 | pmatch = pat.match | |
187 | # fperez - fix: sometimes, co_firstlineno can give a number larger than |
|
187 | # fperez - fix: sometimes, co_firstlineno can give a number larger than | |
188 | # the length of lines, which causes an error. Safeguard against that. |
|
188 | # the length of lines, which causes an error. Safeguard against that. | |
189 | lnum = min(object.co_firstlineno,len(lines))-1 |
|
189 | lnum = min(object.co_firstlineno,len(lines))-1 | |
190 | while lnum > 0: |
|
190 | while lnum > 0: | |
191 | if pmatch(lines[lnum]): break |
|
191 | if pmatch(lines[lnum]): break | |
192 | lnum -= 1 |
|
192 | lnum -= 1 | |
193 |
|
193 | |||
194 | return lines, lnum |
|
194 | return lines, lnum | |
195 | raise IOError('could not find code object') |
|
195 | raise IOError('could not find code object') | |
196 |
|
196 | |||
197 | # Monkeypatch inspect to apply our bugfix. This code only works with py25 |
|
197 | # Monkeypatch inspect to apply our bugfix. This code only works with py25 | |
198 | if sys.version_info[:2] >= (2,5): |
|
198 | if sys.version_info[:2] >= (2,5): | |
199 | inspect.findsource = findsource |
|
199 | inspect.findsource = findsource | |
200 |
|
200 | |||
201 | def fix_frame_records_filenames(records): |
|
201 | def fix_frame_records_filenames(records): | |
202 | """Try to fix the filenames in each record from inspect.getinnerframes(). |
|
202 | """Try to fix the filenames in each record from inspect.getinnerframes(). | |
203 |
|
203 | |||
204 | Particularly, modules loaded from within zip files have useless filenames |
|
204 | Particularly, modules loaded from within zip files have useless filenames | |
205 | attached to their code object, and inspect.getinnerframes() just uses it. |
|
205 | attached to their code object, and inspect.getinnerframes() just uses it. | |
206 | """ |
|
206 | """ | |
207 | fixed_records = [] |
|
207 | fixed_records = [] | |
208 | for frame, filename, line_no, func_name, lines, index in records: |
|
208 | for frame, filename, line_no, func_name, lines, index in records: | |
209 | # Look inside the frame's globals dictionary for __file__, which should |
|
209 | # Look inside the frame's globals dictionary for __file__, which should | |
210 | # be better. |
|
210 | # be better. | |
211 | better_fn = frame.f_globals.get('__file__', None) |
|
211 | better_fn = frame.f_globals.get('__file__', None) | |
212 | if isinstance(better_fn, str): |
|
212 | if isinstance(better_fn, str): | |
213 | # Check the type just in case someone did something weird with |
|
213 | # Check the type just in case someone did something weird with | |
214 | # __file__. It might also be None if the error occurred during |
|
214 | # __file__. It might also be None if the error occurred during | |
215 | # import. |
|
215 | # import. | |
216 | filename = better_fn |
|
216 | filename = better_fn | |
217 | fixed_records.append((frame, filename, line_no, func_name, lines, index)) |
|
217 | fixed_records.append((frame, filename, line_no, func_name, lines, index)) | |
218 | return fixed_records |
|
218 | return fixed_records | |
219 |
|
219 | |||
220 |
|
220 | |||
221 | def _fixed_getinnerframes(etb, context=1,tb_offset=0): |
|
221 | def _fixed_getinnerframes(etb, context=1,tb_offset=0): | |
222 | import linecache |
|
222 | import linecache | |
223 | LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 |
|
223 | LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5 | |
224 |
|
224 | |||
225 | records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) |
|
225 | records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) | |
226 |
|
226 | |||
227 | # If the error is at the console, don't build any context, since it would |
|
227 | # If the error is at the console, don't build any context, since it would | |
228 | # otherwise produce 5 blank lines printed out (there is no file at the |
|
228 | # otherwise produce 5 blank lines printed out (there is no file at the | |
229 | # console) |
|
229 | # console) | |
230 | rec_check = records[tb_offset:] |
|
230 | rec_check = records[tb_offset:] | |
231 | try: |
|
231 | try: | |
232 | rname = rec_check[0][1] |
|
232 | rname = rec_check[0][1] | |
233 | if rname == '<ipython console>' or rname.endswith('<string>'): |
|
233 | if rname == '<ipython console>' or rname.endswith('<string>'): | |
234 | return rec_check |
|
234 | return rec_check | |
235 | except IndexError: |
|
235 | except IndexError: | |
236 | pass |
|
236 | pass | |
237 |
|
237 | |||
238 | aux = traceback.extract_tb(etb) |
|
238 | aux = traceback.extract_tb(etb) | |
239 | assert len(records) == len(aux) |
|
239 | assert len(records) == len(aux) | |
240 | for i, (file, lnum, _, _) in zip(range(len(records)), aux): |
|
240 | for i, (file, lnum, _, _) in zip(range(len(records)), aux): | |
241 | maybeStart = lnum-1 - context//2 |
|
241 | maybeStart = lnum-1 - context//2 | |
242 | start = max(maybeStart, 0) |
|
242 | start = max(maybeStart, 0) | |
243 | end = start + context |
|
243 | end = start + context | |
244 | lines = linecache.getlines(file)[start:end] |
|
244 | lines = linecache.getlines(file)[start:end] | |
245 | # pad with empty lines if necessary |
|
245 | # pad with empty lines if necessary | |
246 | if maybeStart < 0: |
|
246 | if maybeStart < 0: | |
247 | lines = (['\n'] * -maybeStart) + lines |
|
247 | lines = (['\n'] * -maybeStart) + lines | |
248 | if len(lines) < context: |
|
248 | if len(lines) < context: | |
249 | lines += ['\n'] * (context - len(lines)) |
|
249 | lines += ['\n'] * (context - len(lines)) | |
250 | buf = list(records[i]) |
|
250 | buf = list(records[i]) | |
251 | buf[LNUM_POS] = lnum |
|
251 | buf[LNUM_POS] = lnum | |
252 | buf[INDEX_POS] = lnum - 1 - start |
|
252 | buf[INDEX_POS] = lnum - 1 - start | |
253 | buf[LINES_POS] = lines |
|
253 | buf[LINES_POS] = lines | |
254 | records[i] = tuple(buf) |
|
254 | records[i] = tuple(buf) | |
255 | return records[tb_offset:] |
|
255 | return records[tb_offset:] | |
256 |
|
256 | |||
257 | # Helper function -- largely belongs to VerboseTB, but we need the same |
|
257 | # Helper function -- largely belongs to VerboseTB, but we need the same | |
258 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they |
|
258 | # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they | |
259 | # can be recognized properly by ipython.el's py-traceback-line-re |
|
259 | # can be recognized properly by ipython.el's py-traceback-line-re | |
260 | # (SyntaxErrors have to be treated specially because they have no traceback) |
|
260 | # (SyntaxErrors have to be treated specially because they have no traceback) | |
261 |
|
261 | |||
262 | _parser = PyColorize.Parser() |
|
262 | _parser = PyColorize.Parser() | |
263 |
|
263 | |||
264 | def _formatTracebackLines(lnum, index, lines, Colors, lvals=None,scheme=None): |
|
264 | def _formatTracebackLines(lnum, index, lines, Colors, lvals=None,scheme=None): | |
265 | numbers_width = INDENT_SIZE - 1 |
|
265 | numbers_width = INDENT_SIZE - 1 | |
266 | res = [] |
|
266 | res = [] | |
267 | i = lnum - index |
|
267 | i = lnum - index | |
268 |
|
268 | |||
269 | # This lets us get fully syntax-highlighted tracebacks. |
|
269 | # This lets us get fully syntax-highlighted tracebacks. | |
270 | if scheme is None: |
|
270 | if scheme is None: | |
271 | try: |
|
271 | ipinst = ipapi.get() | |
272 | scheme = __IPYTHON__.rc.colors |
|
272 | if ipinst is not None: | |
273 | except: |
|
273 | scheme = ipinst.IP.rc.colors | |
|
274 | else: | |||
274 | scheme = DEFAULT_SCHEME |
|
275 | scheme = DEFAULT_SCHEME | |
|
276 | ||||
275 | _line_format = _parser.format2 |
|
277 | _line_format = _parser.format2 | |
276 |
|
278 | |||
277 | for line in lines: |
|
279 | for line in lines: | |
278 | new_line, err = _line_format(line,'str',scheme) |
|
280 | new_line, err = _line_format(line,'str',scheme) | |
279 | if not err: line = new_line |
|
281 | if not err: line = new_line | |
280 |
|
282 | |||
281 | if i == lnum: |
|
283 | if i == lnum: | |
282 | # This is the line with the error |
|
284 | # This is the line with the error | |
283 | pad = numbers_width - len(str(i)) |
|
285 | pad = numbers_width - len(str(i)) | |
284 | if pad >= 3: |
|
286 | if pad >= 3: | |
285 | marker = '-'*(pad-3) + '-> ' |
|
287 | marker = '-'*(pad-3) + '-> ' | |
286 | elif pad == 2: |
|
288 | elif pad == 2: | |
287 | marker = '> ' |
|
289 | marker = '> ' | |
288 | elif pad == 1: |
|
290 | elif pad == 1: | |
289 | marker = '>' |
|
291 | marker = '>' | |
290 | else: |
|
292 | else: | |
291 | marker = '' |
|
293 | marker = '' | |
292 | num = marker + str(i) |
|
294 | num = marker + str(i) | |
293 | line = '%s%s%s %s%s' %(Colors.linenoEm, num, |
|
295 | line = '%s%s%s %s%s' %(Colors.linenoEm, num, | |
294 | Colors.line, line, Colors.Normal) |
|
296 | Colors.line, line, Colors.Normal) | |
295 | else: |
|
297 | else: | |
296 | num = '%*s' % (numbers_width,i) |
|
298 | num = '%*s' % (numbers_width,i) | |
297 | line = '%s%s%s %s' %(Colors.lineno, num, |
|
299 | line = '%s%s%s %s' %(Colors.lineno, num, | |
298 | Colors.Normal, line) |
|
300 | Colors.Normal, line) | |
299 |
|
301 | |||
300 | res.append(line) |
|
302 | res.append(line) | |
301 | if lvals and i == lnum: |
|
303 | if lvals and i == lnum: | |
302 | res.append(lvals + '\n') |
|
304 | res.append(lvals + '\n') | |
303 | i = i + 1 |
|
305 | i = i + 1 | |
304 | return res |
|
306 | return res | |
305 |
|
307 | |||
306 |
|
308 | |||
307 | #--------------------------------------------------------------------------- |
|
309 | #--------------------------------------------------------------------------- | |
308 | # Module classes |
|
310 | # Module classes | |
309 | class TBTools: |
|
311 | class TBTools: | |
310 | """Basic tools used by all traceback printer classes.""" |
|
312 | """Basic tools used by all traceback printer classes.""" | |
311 |
|
313 | |||
312 | def __init__(self,color_scheme = 'NoColor',call_pdb=False): |
|
314 | def __init__(self,color_scheme = 'NoColor',call_pdb=False): | |
313 | # Whether to call the interactive pdb debugger after printing |
|
315 | # Whether to call the interactive pdb debugger after printing | |
314 | # tracebacks or not |
|
316 | # tracebacks or not | |
315 | self.call_pdb = call_pdb |
|
317 | self.call_pdb = call_pdb | |
316 |
|
318 | |||
317 | # Create color table |
|
319 | # Create color table | |
318 | self.color_scheme_table = exception_colors() |
|
320 | self.color_scheme_table = exception_colors() | |
319 |
|
321 | |||
320 | self.set_colors(color_scheme) |
|
322 | self.set_colors(color_scheme) | |
321 | self.old_scheme = color_scheme # save initial value for toggles |
|
323 | self.old_scheme = color_scheme # save initial value for toggles | |
322 |
|
324 | |||
323 | if call_pdb: |
|
325 | if call_pdb: | |
324 | self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name) |
|
326 | self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name) | |
325 | else: |
|
327 | else: | |
326 | self.pdb = None |
|
328 | self.pdb = None | |
327 |
|
329 | |||
328 | def set_colors(self,*args,**kw): |
|
330 | def set_colors(self,*args,**kw): | |
329 | """Shorthand access to the color table scheme selector method.""" |
|
331 | """Shorthand access to the color table scheme selector method.""" | |
330 |
|
332 | |||
331 | # Set own color table |
|
333 | # Set own color table | |
332 | self.color_scheme_table.set_active_scheme(*args,**kw) |
|
334 | self.color_scheme_table.set_active_scheme(*args,**kw) | |
333 | # for convenience, set Colors to the active scheme |
|
335 | # for convenience, set Colors to the active scheme | |
334 | self.Colors = self.color_scheme_table.active_colors |
|
336 | self.Colors = self.color_scheme_table.active_colors | |
335 | # Also set colors of debugger |
|
337 | # Also set colors of debugger | |
336 | if hasattr(self,'pdb') and self.pdb is not None: |
|
338 | if hasattr(self,'pdb') and self.pdb is not None: | |
337 | self.pdb.set_colors(*args,**kw) |
|
339 | self.pdb.set_colors(*args,**kw) | |
338 |
|
340 | |||
339 | def color_toggle(self): |
|
341 | def color_toggle(self): | |
340 | """Toggle between the currently active color scheme and NoColor.""" |
|
342 | """Toggle between the currently active color scheme and NoColor.""" | |
341 |
|
343 | |||
342 | if self.color_scheme_table.active_scheme_name == 'NoColor': |
|
344 | if self.color_scheme_table.active_scheme_name == 'NoColor': | |
343 | self.color_scheme_table.set_active_scheme(self.old_scheme) |
|
345 | self.color_scheme_table.set_active_scheme(self.old_scheme) | |
344 | self.Colors = self.color_scheme_table.active_colors |
|
346 | self.Colors = self.color_scheme_table.active_colors | |
345 | else: |
|
347 | else: | |
346 | self.old_scheme = self.color_scheme_table.active_scheme_name |
|
348 | self.old_scheme = self.color_scheme_table.active_scheme_name | |
347 | self.color_scheme_table.set_active_scheme('NoColor') |
|
349 | self.color_scheme_table.set_active_scheme('NoColor') | |
348 | self.Colors = self.color_scheme_table.active_colors |
|
350 | self.Colors = self.color_scheme_table.active_colors | |
349 |
|
351 | |||
350 | #--------------------------------------------------------------------------- |
|
352 | #--------------------------------------------------------------------------- | |
351 | class ListTB(TBTools): |
|
353 | class ListTB(TBTools): | |
352 | """Print traceback information from a traceback list, with optional color. |
|
354 | """Print traceback information from a traceback list, with optional color. | |
353 |
|
355 | |||
354 | Calling: requires 3 arguments: |
|
356 | Calling: requires 3 arguments: | |
355 | (etype, evalue, elist) |
|
357 | (etype, evalue, elist) | |
356 | as would be obtained by: |
|
358 | as would be obtained by: | |
357 | etype, evalue, tb = sys.exc_info() |
|
359 | etype, evalue, tb = sys.exc_info() | |
358 | if tb: |
|
360 | if tb: | |
359 | elist = traceback.extract_tb(tb) |
|
361 | elist = traceback.extract_tb(tb) | |
360 | else: |
|
362 | else: | |
361 | elist = None |
|
363 | elist = None | |
362 |
|
364 | |||
363 | It can thus be used by programs which need to process the traceback before |
|
365 | It can thus be used by programs which need to process the traceback before | |
364 | printing (such as console replacements based on the code module from the |
|
366 | printing (such as console replacements based on the code module from the | |
365 | standard library). |
|
367 | standard library). | |
366 |
|
368 | |||
367 | Because they are meant to be called without a full traceback (only a |
|
369 | Because they are meant to be called without a full traceback (only a | |
368 | list), instances of this class can't call the interactive pdb debugger.""" |
|
370 | list), instances of this class can't call the interactive pdb debugger.""" | |
369 |
|
371 | |||
370 | def __init__(self,color_scheme = 'NoColor'): |
|
372 | def __init__(self,color_scheme = 'NoColor'): | |
371 | TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0) |
|
373 | TBTools.__init__(self,color_scheme = color_scheme,call_pdb=0) | |
372 |
|
374 | |||
373 | def __call__(self, etype, value, elist): |
|
375 | def __call__(self, etype, value, elist): | |
374 | Term.cout.flush() |
|
376 | Term.cout.flush() | |
375 | print >> Term.cerr, self.text(etype,value,elist) |
|
377 | print >> Term.cerr, self.text(etype,value,elist) | |
376 | Term.cerr.flush() |
|
378 | Term.cerr.flush() | |
377 |
|
379 | |||
378 | def text(self,etype, value, elist,context=5): |
|
380 | def text(self,etype, value, elist,context=5): | |
379 | """Return a color formatted string with the traceback info.""" |
|
381 | """Return a color formatted string with the traceback info.""" | |
380 |
|
382 | |||
381 | Colors = self.Colors |
|
383 | Colors = self.Colors | |
382 | out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)] |
|
384 | out_string = ['%s%s%s\n' % (Colors.topline,'-'*60,Colors.Normal)] | |
383 | if elist: |
|
385 | if elist: | |
384 | out_string.append('Traceback %s(most recent call last)%s:' % \ |
|
386 | out_string.append('Traceback %s(most recent call last)%s:' % \ | |
385 | (Colors.normalEm, Colors.Normal) + '\n') |
|
387 | (Colors.normalEm, Colors.Normal) + '\n') | |
386 | out_string.extend(self._format_list(elist)) |
|
388 | out_string.extend(self._format_list(elist)) | |
387 | lines = self._format_exception_only(etype, value) |
|
389 | lines = self._format_exception_only(etype, value) | |
388 | for line in lines[:-1]: |
|
390 | for line in lines[:-1]: | |
389 | out_string.append(" "+line) |
|
391 | out_string.append(" "+line) | |
390 | out_string.append(lines[-1]) |
|
392 | out_string.append(lines[-1]) | |
391 | return ''.join(out_string) |
|
393 | return ''.join(out_string) | |
392 |
|
394 | |||
393 | def _format_list(self, extracted_list): |
|
395 | def _format_list(self, extracted_list): | |
394 | """Format a list of traceback entry tuples for printing. |
|
396 | """Format a list of traceback entry tuples for printing. | |
395 |
|
397 | |||
396 | Given a list of tuples as returned by extract_tb() or |
|
398 | Given a list of tuples as returned by extract_tb() or | |
397 | extract_stack(), return a list of strings ready for printing. |
|
399 | extract_stack(), return a list of strings ready for printing. | |
398 | Each string in the resulting list corresponds to the item with the |
|
400 | Each string in the resulting list corresponds to the item with the | |
399 | same index in the argument list. Each string ends in a newline; |
|
401 | same index in the argument list. Each string ends in a newline; | |
400 | the strings may contain internal newlines as well, for those items |
|
402 | the strings may contain internal newlines as well, for those items | |
401 | whose source text line is not None. |
|
403 | whose source text line is not None. | |
402 |
|
404 | |||
403 | Lifted almost verbatim from traceback.py |
|
405 | Lifted almost verbatim from traceback.py | |
404 | """ |
|
406 | """ | |
405 |
|
407 | |||
406 | Colors = self.Colors |
|
408 | Colors = self.Colors | |
407 | list = [] |
|
409 | list = [] | |
408 | for filename, lineno, name, line in extracted_list[:-1]: |
|
410 | for filename, lineno, name, line in extracted_list[:-1]: | |
409 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ |
|
411 | item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \ | |
410 | (Colors.filename, filename, Colors.Normal, |
|
412 | (Colors.filename, filename, Colors.Normal, | |
411 | Colors.lineno, lineno, Colors.Normal, |
|
413 | Colors.lineno, lineno, Colors.Normal, | |
412 | Colors.name, name, Colors.Normal) |
|
414 | Colors.name, name, Colors.Normal) | |
413 | if line: |
|
415 | if line: | |
414 | item = item + ' %s\n' % line.strip() |
|
416 | item = item + ' %s\n' % line.strip() | |
415 | list.append(item) |
|
417 | list.append(item) | |
416 | # Emphasize the last entry |
|
418 | # Emphasize the last entry | |
417 | filename, lineno, name, line = extracted_list[-1] |
|
419 | filename, lineno, name, line = extracted_list[-1] | |
418 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ |
|
420 | item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \ | |
419 | (Colors.normalEm, |
|
421 | (Colors.normalEm, | |
420 | Colors.filenameEm, filename, Colors.normalEm, |
|
422 | Colors.filenameEm, filename, Colors.normalEm, | |
421 | Colors.linenoEm, lineno, Colors.normalEm, |
|
423 | Colors.linenoEm, lineno, Colors.normalEm, | |
422 | Colors.nameEm, name, Colors.normalEm, |
|
424 | Colors.nameEm, name, Colors.normalEm, | |
423 | Colors.Normal) |
|
425 | Colors.Normal) | |
424 | if line: |
|
426 | if line: | |
425 | item = item + '%s %s%s\n' % (Colors.line, line.strip(), |
|
427 | item = item + '%s %s%s\n' % (Colors.line, line.strip(), | |
426 | Colors.Normal) |
|
428 | Colors.Normal) | |
427 | list.append(item) |
|
429 | list.append(item) | |
428 | return list |
|
430 | return list | |
429 |
|
431 | |||
430 | def _format_exception_only(self, etype, value): |
|
432 | def _format_exception_only(self, etype, value): | |
431 | """Format the exception part of a traceback. |
|
433 | """Format the exception part of a traceback. | |
432 |
|
434 | |||
433 | The arguments are the exception type and value such as given by |
|
435 | The arguments are the exception type and value such as given by | |
434 | sys.exc_info()[:2]. The return value is a list of strings, each ending |
|
436 | sys.exc_info()[:2]. The return value is a list of strings, each ending | |
435 | in a newline. Normally, the list contains a single string; however, |
|
437 | in a newline. Normally, the list contains a single string; however, | |
436 | for SyntaxError exceptions, it contains several lines that (when |
|
438 | for SyntaxError exceptions, it contains several lines that (when | |
437 | printed) display detailed information about where the syntax error |
|
439 | printed) display detailed information about where the syntax error | |
438 | occurred. The message indicating which exception occurred is the |
|
440 | occurred. The message indicating which exception occurred is the | |
439 | always last string in the list. |
|
441 | always last string in the list. | |
440 |
|
442 | |||
441 | Also lifted nearly verbatim from traceback.py |
|
443 | Also lifted nearly verbatim from traceback.py | |
442 | """ |
|
444 | """ | |
443 |
|
445 | |||
444 | have_filedata = False |
|
446 | have_filedata = False | |
445 | Colors = self.Colors |
|
447 | Colors = self.Colors | |
446 | list = [] |
|
448 | list = [] | |
447 | try: |
|
449 | try: | |
448 | stype = Colors.excName + etype.__name__ + Colors.Normal |
|
450 | stype = Colors.excName + etype.__name__ + Colors.Normal | |
449 | except AttributeError: |
|
451 | except AttributeError: | |
450 | stype = etype # String exceptions don't get special coloring |
|
452 | stype = etype # String exceptions don't get special coloring | |
451 | if value is None: |
|
453 | if value is None: | |
452 | list.append( str(stype) + '\n') |
|
454 | list.append( str(stype) + '\n') | |
453 | else: |
|
455 | else: | |
454 | if etype is SyntaxError: |
|
456 | if etype is SyntaxError: | |
455 | try: |
|
457 | try: | |
456 | msg, (filename, lineno, offset, line) = value |
|
458 | msg, (filename, lineno, offset, line) = value | |
457 | except: |
|
459 | except: | |
458 | have_filedata = False |
|
460 | have_filedata = False | |
459 | else: |
|
461 | else: | |
460 | have_filedata = True |
|
462 | have_filedata = True | |
461 | #print 'filename is',filename # dbg |
|
463 | #print 'filename is',filename # dbg | |
462 | if not filename: filename = "<string>" |
|
464 | if not filename: filename = "<string>" | |
463 | list.append('%s File %s"%s"%s, line %s%d%s\n' % \ |
|
465 | list.append('%s File %s"%s"%s, line %s%d%s\n' % \ | |
464 | (Colors.normalEm, |
|
466 | (Colors.normalEm, | |
465 | Colors.filenameEm, filename, Colors.normalEm, |
|
467 | Colors.filenameEm, filename, Colors.normalEm, | |
466 | Colors.linenoEm, lineno, Colors.Normal )) |
|
468 | Colors.linenoEm, lineno, Colors.Normal )) | |
467 | if line is not None: |
|
469 | if line is not None: | |
468 | i = 0 |
|
470 | i = 0 | |
469 | while i < len(line) and line[i].isspace(): |
|
471 | while i < len(line) and line[i].isspace(): | |
470 | i = i+1 |
|
472 | i = i+1 | |
471 | list.append('%s %s%s\n' % (Colors.line, |
|
473 | list.append('%s %s%s\n' % (Colors.line, | |
472 | line.strip(), |
|
474 | line.strip(), | |
473 | Colors.Normal)) |
|
475 | Colors.Normal)) | |
474 | if offset is not None: |
|
476 | if offset is not None: | |
475 | s = ' ' |
|
477 | s = ' ' | |
476 | for c in line[i:offset-1]: |
|
478 | for c in line[i:offset-1]: | |
477 | if c.isspace(): |
|
479 | if c.isspace(): | |
478 | s = s + c |
|
480 | s = s + c | |
479 | else: |
|
481 | else: | |
480 | s = s + ' ' |
|
482 | s = s + ' ' | |
481 | list.append('%s%s^%s\n' % (Colors.caret, s, |
|
483 | list.append('%s%s^%s\n' % (Colors.caret, s, | |
482 | Colors.Normal) ) |
|
484 | Colors.Normal) ) | |
483 | value = msg |
|
485 | value = msg | |
484 | s = self._some_str(value) |
|
486 | s = self._some_str(value) | |
485 | if s: |
|
487 | if s: | |
486 | list.append('%s%s:%s %s\n' % (str(stype), Colors.excName, |
|
488 | list.append('%s%s:%s %s\n' % (str(stype), Colors.excName, | |
487 | Colors.Normal, s)) |
|
489 | Colors.Normal, s)) | |
488 | else: |
|
490 | else: | |
489 | list.append('%s\n' % str(stype)) |
|
491 | list.append('%s\n' % str(stype)) | |
490 |
|
492 | |||
491 | # vds:>> |
|
493 | # vds:>> | |
492 | if have_filedata: |
|
494 | if have_filedata: | |
493 | __IPYTHON__.hooks.synchronize_with_editor(filename, lineno, 0) |
|
495 | ipinst = ipapi.get() | |
|
496 | if ipinst is not None: | |||
|
497 | ipinst.IP.hooks.synchronize_with_editor(filename, lineno, 0) | |||
494 | # vds:<< |
|
498 | # vds:<< | |
495 |
|
499 | |||
496 | return list |
|
500 | return list | |
497 |
|
501 | |||
498 | def _some_str(self, value): |
|
502 | def _some_str(self, value): | |
499 | # Lifted from traceback.py |
|
503 | # Lifted from traceback.py | |
500 | try: |
|
504 | try: | |
501 | return str(value) |
|
505 | return str(value) | |
502 | except: |
|
506 | except: | |
503 | return '<unprintable %s object>' % type(value).__name__ |
|
507 | return '<unprintable %s object>' % type(value).__name__ | |
504 |
|
508 | |||
505 | #---------------------------------------------------------------------------- |
|
509 | #---------------------------------------------------------------------------- | |
506 | class VerboseTB(TBTools): |
|
510 | class VerboseTB(TBTools): | |
507 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead |
|
511 | """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead | |
508 | of HTML. Requires inspect and pydoc. Crazy, man. |
|
512 | of HTML. Requires inspect and pydoc. Crazy, man. | |
509 |
|
513 | |||
510 | Modified version which optionally strips the topmost entries from the |
|
514 | Modified version which optionally strips the topmost entries from the | |
511 | traceback, to be used with alternate interpreters (because their own code |
|
515 | traceback, to be used with alternate interpreters (because their own code | |
512 | would appear in the traceback).""" |
|
516 | would appear in the traceback).""" | |
513 |
|
517 | |||
514 | def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0, |
|
518 | def __init__(self,color_scheme = 'Linux',tb_offset=0,long_header=0, | |
515 | call_pdb = 0, include_vars=1): |
|
519 | call_pdb = 0, include_vars=1): | |
516 | """Specify traceback offset, headers and color scheme. |
|
520 | """Specify traceback offset, headers and color scheme. | |
517 |
|
521 | |||
518 | Define how many frames to drop from the tracebacks. Calling it with |
|
522 | Define how many frames to drop from the tracebacks. Calling it with | |
519 | tb_offset=1 allows use of this handler in interpreters which will have |
|
523 | tb_offset=1 allows use of this handler in interpreters which will have | |
520 | their own code at the top of the traceback (VerboseTB will first |
|
524 | their own code at the top of the traceback (VerboseTB will first | |
521 | remove that frame before printing the traceback info).""" |
|
525 | remove that frame before printing the traceback info).""" | |
522 | TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb) |
|
526 | TBTools.__init__(self,color_scheme=color_scheme,call_pdb=call_pdb) | |
523 | self.tb_offset = tb_offset |
|
527 | self.tb_offset = tb_offset | |
524 | self.long_header = long_header |
|
528 | self.long_header = long_header | |
525 | self.include_vars = include_vars |
|
529 | self.include_vars = include_vars | |
526 |
|
530 | |||
527 | def text(self, etype, evalue, etb, context=5): |
|
531 | def text(self, etype, evalue, etb, context=5): | |
528 | """Return a nice text document describing the traceback.""" |
|
532 | """Return a nice text document describing the traceback.""" | |
529 |
|
533 | |||
530 | # some locals |
|
534 | # some locals | |
531 | try: |
|
535 | try: | |
532 | etype = etype.__name__ |
|
536 | etype = etype.__name__ | |
533 | except AttributeError: |
|
537 | except AttributeError: | |
534 | pass |
|
538 | pass | |
535 | Colors = self.Colors # just a shorthand + quicker name lookup |
|
539 | Colors = self.Colors # just a shorthand + quicker name lookup | |
536 | ColorsNormal = Colors.Normal # used a lot |
|
540 | ColorsNormal = Colors.Normal # used a lot | |
537 | col_scheme = self.color_scheme_table.active_scheme_name |
|
541 | col_scheme = self.color_scheme_table.active_scheme_name | |
538 | indent = ' '*INDENT_SIZE |
|
542 | indent = ' '*INDENT_SIZE | |
539 | em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal) |
|
543 | em_normal = '%s\n%s%s' % (Colors.valEm, indent,ColorsNormal) | |
540 | undefined = '%sundefined%s' % (Colors.em, ColorsNormal) |
|
544 | undefined = '%sundefined%s' % (Colors.em, ColorsNormal) | |
541 | exc = '%s%s%s' % (Colors.excName,etype,ColorsNormal) |
|
545 | exc = '%s%s%s' % (Colors.excName,etype,ColorsNormal) | |
542 |
|
546 | |||
543 | # some internal-use functions |
|
547 | # some internal-use functions | |
544 | def text_repr(value): |
|
548 | def text_repr(value): | |
545 | """Hopefully pretty robust repr equivalent.""" |
|
549 | """Hopefully pretty robust repr equivalent.""" | |
546 | # this is pretty horrible but should always return *something* |
|
550 | # this is pretty horrible but should always return *something* | |
547 | try: |
|
551 | try: | |
548 | return pydoc.text.repr(value) |
|
552 | return pydoc.text.repr(value) | |
549 | except KeyboardInterrupt: |
|
553 | except KeyboardInterrupt: | |
550 | raise |
|
554 | raise | |
551 | except: |
|
555 | except: | |
552 | try: |
|
556 | try: | |
553 | return repr(value) |
|
557 | return repr(value) | |
554 | except KeyboardInterrupt: |
|
558 | except KeyboardInterrupt: | |
555 | raise |
|
559 | raise | |
556 | except: |
|
560 | except: | |
557 | try: |
|
561 | try: | |
558 | # all still in an except block so we catch |
|
562 | # all still in an except block so we catch | |
559 | # getattr raising |
|
563 | # getattr raising | |
560 | name = getattr(value, '__name__', None) |
|
564 | name = getattr(value, '__name__', None) | |
561 | if name: |
|
565 | if name: | |
562 | # ick, recursion |
|
566 | # ick, recursion | |
563 | return text_repr(name) |
|
567 | return text_repr(name) | |
564 | klass = getattr(value, '__class__', None) |
|
568 | klass = getattr(value, '__class__', None) | |
565 | if klass: |
|
569 | if klass: | |
566 | return '%s instance' % text_repr(klass) |
|
570 | return '%s instance' % text_repr(klass) | |
567 | except KeyboardInterrupt: |
|
571 | except KeyboardInterrupt: | |
568 | raise |
|
572 | raise | |
569 | except: |
|
573 | except: | |
570 | return 'UNRECOVERABLE REPR FAILURE' |
|
574 | return 'UNRECOVERABLE REPR FAILURE' | |
571 | def eqrepr(value, repr=text_repr): return '=%s' % repr(value) |
|
575 | def eqrepr(value, repr=text_repr): return '=%s' % repr(value) | |
572 | def nullrepr(value, repr=text_repr): return '' |
|
576 | def nullrepr(value, repr=text_repr): return '' | |
573 |
|
577 | |||
574 | # meat of the code begins |
|
578 | # meat of the code begins | |
575 | try: |
|
579 | try: | |
576 | etype = etype.__name__ |
|
580 | etype = etype.__name__ | |
577 | except AttributeError: |
|
581 | except AttributeError: | |
578 | pass |
|
582 | pass | |
579 |
|
583 | |||
580 | if self.long_header: |
|
584 | if self.long_header: | |
581 | # Header with the exception type, python version, and date |
|
585 | # Header with the exception type, python version, and date | |
582 | pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable |
|
586 | pyver = 'Python ' + string.split(sys.version)[0] + ': ' + sys.executable | |
583 | date = time.ctime(time.time()) |
|
587 | date = time.ctime(time.time()) | |
584 |
|
588 | |||
585 | head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal, |
|
589 | head = '%s%s%s\n%s%s%s\n%s' % (Colors.topline, '-'*75, ColorsNormal, | |
586 | exc, ' '*(75-len(str(etype))-len(pyver)), |
|
590 | exc, ' '*(75-len(str(etype))-len(pyver)), | |
587 | pyver, string.rjust(date, 75) ) |
|
591 | pyver, string.rjust(date, 75) ) | |
588 | head += "\nA problem occured executing Python code. Here is the sequence of function"\ |
|
592 | head += "\nA problem occured executing Python code. Here is the sequence of function"\ | |
589 | "\ncalls leading up to the error, with the most recent (innermost) call last." |
|
593 | "\ncalls leading up to the error, with the most recent (innermost) call last." | |
590 | else: |
|
594 | else: | |
591 | # Simplified header |
|
595 | # Simplified header | |
592 | head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc, |
|
596 | head = '%s%s%s\n%s%s' % (Colors.topline, '-'*75, ColorsNormal,exc, | |
593 | string.rjust('Traceback (most recent call last)', |
|
597 | string.rjust('Traceback (most recent call last)', | |
594 | 75 - len(str(etype)) ) ) |
|
598 | 75 - len(str(etype)) ) ) | |
595 | frames = [] |
|
599 | frames = [] | |
596 | # Flush cache before calling inspect. This helps alleviate some of the |
|
600 | # Flush cache before calling inspect. This helps alleviate some of the | |
597 | # problems with python 2.3's inspect.py. |
|
601 | # problems with python 2.3's inspect.py. | |
598 | linecache.checkcache() |
|
602 | linecache.checkcache() | |
599 | # Drop topmost frames if requested |
|
603 | # Drop topmost frames if requested | |
600 | try: |
|
604 | try: | |
601 | # Try the default getinnerframes and Alex's: Alex's fixes some |
|
605 | # Try the default getinnerframes and Alex's: Alex's fixes some | |
602 | # problems, but it generates empty tracebacks for console errors |
|
606 | # problems, but it generates empty tracebacks for console errors | |
603 | # (5 blanks lines) where none should be returned. |
|
607 | # (5 blanks lines) where none should be returned. | |
604 | #records = inspect.getinnerframes(etb, context)[self.tb_offset:] |
|
608 | #records = inspect.getinnerframes(etb, context)[self.tb_offset:] | |
605 | #print 'python records:', records # dbg |
|
609 | #print 'python records:', records # dbg | |
606 | records = _fixed_getinnerframes(etb, context,self.tb_offset) |
|
610 | records = _fixed_getinnerframes(etb, context,self.tb_offset) | |
607 | #print 'alex records:', records # dbg |
|
611 | #print 'alex records:', records # dbg | |
608 | except: |
|
612 | except: | |
609 |
|
613 | |||
610 | # FIXME: I've been getting many crash reports from python 2.3 |
|
614 | # FIXME: I've been getting many crash reports from python 2.3 | |
611 | # users, traceable to inspect.py. If I can find a small test-case |
|
615 | # users, traceable to inspect.py. If I can find a small test-case | |
612 | # to reproduce this, I should either write a better workaround or |
|
616 | # to reproduce this, I should either write a better workaround or | |
613 | # file a bug report against inspect (if that's the real problem). |
|
617 | # file a bug report against inspect (if that's the real problem). | |
614 | # So far, I haven't been able to find an isolated example to |
|
618 | # So far, I haven't been able to find an isolated example to | |
615 | # reproduce the problem. |
|
619 | # reproduce the problem. | |
616 | inspect_error() |
|
620 | inspect_error() | |
617 | traceback.print_exc(file=Term.cerr) |
|
621 | traceback.print_exc(file=Term.cerr) | |
618 | info('\nUnfortunately, your original traceback can not be constructed.\n') |
|
622 | info('\nUnfortunately, your original traceback can not be constructed.\n') | |
619 | return '' |
|
623 | return '' | |
620 |
|
624 | |||
621 | # build some color string templates outside these nested loops |
|
625 | # build some color string templates outside these nested loops | |
622 | tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal) |
|
626 | tpl_link = '%s%%s%s' % (Colors.filenameEm,ColorsNormal) | |
623 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, |
|
627 | tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm, | |
624 | ColorsNormal) |
|
628 | ColorsNormal) | |
625 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ |
|
629 | tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \ | |
626 | (Colors.vName, Colors.valEm, ColorsNormal) |
|
630 | (Colors.vName, Colors.valEm, ColorsNormal) | |
627 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) |
|
631 | tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal) | |
628 | tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, |
|
632 | tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal, | |
629 | Colors.vName, ColorsNormal) |
|
633 | Colors.vName, ColorsNormal) | |
630 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) |
|
634 | tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal) | |
631 | tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal) |
|
635 | tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal) | |
632 | tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line, |
|
636 | tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm,Colors.line, | |
633 | ColorsNormal) |
|
637 | ColorsNormal) | |
634 |
|
638 | |||
635 | # now, loop over all records printing context and info |
|
639 | # now, loop over all records printing context and info | |
636 | abspath = os.path.abspath |
|
640 | abspath = os.path.abspath | |
637 | for frame, file, lnum, func, lines, index in records: |
|
641 | for frame, file, lnum, func, lines, index in records: | |
638 | #print '*** record:',file,lnum,func,lines,index # dbg |
|
642 | #print '*** record:',file,lnum,func,lines,index # dbg | |
639 | try: |
|
643 | try: | |
640 | file = file and abspath(file) or '?' |
|
644 | file = file and abspath(file) or '?' | |
641 | except OSError: |
|
645 | except OSError: | |
642 | # if file is '<console>' or something not in the filesystem, |
|
646 | # if file is '<console>' or something not in the filesystem, | |
643 | # the abspath call will throw an OSError. Just ignore it and |
|
647 | # the abspath call will throw an OSError. Just ignore it and | |
644 | # keep the original file string. |
|
648 | # keep the original file string. | |
645 | pass |
|
649 | pass | |
646 | link = tpl_link % file |
|
650 | link = tpl_link % file | |
647 | try: |
|
651 | try: | |
648 | args, varargs, varkw, locals = inspect.getargvalues(frame) |
|
652 | args, varargs, varkw, locals = inspect.getargvalues(frame) | |
649 | except: |
|
653 | except: | |
650 | # This can happen due to a bug in python2.3. We should be |
|
654 | # This can happen due to a bug in python2.3. We should be | |
651 | # able to remove this try/except when 2.4 becomes a |
|
655 | # able to remove this try/except when 2.4 becomes a | |
652 | # requirement. Bug details at http://python.org/sf/1005466 |
|
656 | # requirement. Bug details at http://python.org/sf/1005466 | |
653 | inspect_error() |
|
657 | inspect_error() | |
654 | traceback.print_exc(file=Term.cerr) |
|
658 | traceback.print_exc(file=Term.cerr) | |
655 | info("\nIPython's exception reporting continues...\n") |
|
659 | info("\nIPython's exception reporting continues...\n") | |
656 |
|
660 | |||
657 | if func == '?': |
|
661 | if func == '?': | |
658 | call = '' |
|
662 | call = '' | |
659 | else: |
|
663 | else: | |
660 | # Decide whether to include variable details or not |
|
664 | # Decide whether to include variable details or not | |
661 | var_repr = self.include_vars and eqrepr or nullrepr |
|
665 | var_repr = self.include_vars and eqrepr or nullrepr | |
662 | try: |
|
666 | try: | |
663 | call = tpl_call % (func,inspect.formatargvalues(args, |
|
667 | call = tpl_call % (func,inspect.formatargvalues(args, | |
664 | varargs, varkw, |
|
668 | varargs, varkw, | |
665 | locals,formatvalue=var_repr)) |
|
669 | locals,formatvalue=var_repr)) | |
666 | except KeyError: |
|
670 | except KeyError: | |
667 | # Very odd crash from inspect.formatargvalues(). The |
|
671 | # Very odd crash from inspect.formatargvalues(). The | |
668 | # scenario under which it appeared was a call to |
|
672 | # scenario under which it appeared was a call to | |
669 | # view(array,scale) in NumTut.view.view(), where scale had |
|
673 | # view(array,scale) in NumTut.view.view(), where scale had | |
670 | # been defined as a scalar (it should be a tuple). Somehow |
|
674 | # been defined as a scalar (it should be a tuple). Somehow | |
671 | # inspect messes up resolving the argument list of view() |
|
675 | # inspect messes up resolving the argument list of view() | |
672 | # and barfs out. At some point I should dig into this one |
|
676 | # and barfs out. At some point I should dig into this one | |
673 | # and file a bug report about it. |
|
677 | # and file a bug report about it. | |
674 | inspect_error() |
|
678 | inspect_error() | |
675 | traceback.print_exc(file=Term.cerr) |
|
679 | traceback.print_exc(file=Term.cerr) | |
676 | info("\nIPython's exception reporting continues...\n") |
|
680 | info("\nIPython's exception reporting continues...\n") | |
677 | call = tpl_call_fail % func |
|
681 | call = tpl_call_fail % func | |
678 |
|
682 | |||
679 | # Initialize a list of names on the current line, which the |
|
683 | # Initialize a list of names on the current line, which the | |
680 | # tokenizer below will populate. |
|
684 | # tokenizer below will populate. | |
681 | names = [] |
|
685 | names = [] | |
682 |
|
686 | |||
683 | def tokeneater(token_type, token, start, end, line): |
|
687 | def tokeneater(token_type, token, start, end, line): | |
684 | """Stateful tokeneater which builds dotted names. |
|
688 | """Stateful tokeneater which builds dotted names. | |
685 |
|
689 | |||
686 | The list of names it appends to (from the enclosing scope) can |
|
690 | The list of names it appends to (from the enclosing scope) can | |
687 | contain repeated composite names. This is unavoidable, since |
|
691 | contain repeated composite names. This is unavoidable, since | |
688 | there is no way to disambguate partial dotted structures until |
|
692 | there is no way to disambguate partial dotted structures until | |
689 | the full list is known. The caller is responsible for pruning |
|
693 | the full list is known. The caller is responsible for pruning | |
690 | the final list of duplicates before using it.""" |
|
694 | the final list of duplicates before using it.""" | |
691 |
|
695 | |||
692 | # build composite names |
|
696 | # build composite names | |
693 | if token == '.': |
|
697 | if token == '.': | |
694 | try: |
|
698 | try: | |
695 | names[-1] += '.' |
|
699 | names[-1] += '.' | |
696 | # store state so the next token is added for x.y.z names |
|
700 | # store state so the next token is added for x.y.z names | |
697 | tokeneater.name_cont = True |
|
701 | tokeneater.name_cont = True | |
698 | return |
|
702 | return | |
699 | except IndexError: |
|
703 | except IndexError: | |
700 | pass |
|
704 | pass | |
701 | if token_type == tokenize.NAME and token not in keyword.kwlist: |
|
705 | if token_type == tokenize.NAME and token not in keyword.kwlist: | |
702 | if tokeneater.name_cont: |
|
706 | if tokeneater.name_cont: | |
703 | # Dotted names |
|
707 | # Dotted names | |
704 | names[-1] += token |
|
708 | names[-1] += token | |
705 | tokeneater.name_cont = False |
|
709 | tokeneater.name_cont = False | |
706 | else: |
|
710 | else: | |
707 | # Regular new names. We append everything, the caller |
|
711 | # Regular new names. We append everything, the caller | |
708 | # will be responsible for pruning the list later. It's |
|
712 | # will be responsible for pruning the list later. It's | |
709 | # very tricky to try to prune as we go, b/c composite |
|
713 | # very tricky to try to prune as we go, b/c composite | |
710 | # names can fool us. The pruning at the end is easy |
|
714 | # names can fool us. The pruning at the end is easy | |
711 | # to do (or the caller can print a list with repeated |
|
715 | # to do (or the caller can print a list with repeated | |
712 | # names if so desired. |
|
716 | # names if so desired. | |
713 | names.append(token) |
|
717 | names.append(token) | |
714 | elif token_type == tokenize.NEWLINE: |
|
718 | elif token_type == tokenize.NEWLINE: | |
715 | raise IndexError |
|
719 | raise IndexError | |
716 | # we need to store a bit of state in the tokenizer to build |
|
720 | # we need to store a bit of state in the tokenizer to build | |
717 | # dotted names |
|
721 | # dotted names | |
718 | tokeneater.name_cont = False |
|
722 | tokeneater.name_cont = False | |
719 |
|
723 | |||
720 | def linereader(file=file, lnum=[lnum], getline=linecache.getline): |
|
724 | def linereader(file=file, lnum=[lnum], getline=linecache.getline): | |
721 | line = getline(file, lnum[0]) |
|
725 | line = getline(file, lnum[0]) | |
722 | lnum[0] += 1 |
|
726 | lnum[0] += 1 | |
723 | return line |
|
727 | return line | |
724 |
|
728 | |||
725 | # Build the list of names on this line of code where the exception |
|
729 | # Build the list of names on this line of code where the exception | |
726 | # occurred. |
|
730 | # occurred. | |
727 | try: |
|
731 | try: | |
728 | # This builds the names list in-place by capturing it from the |
|
732 | # This builds the names list in-place by capturing it from the | |
729 | # enclosing scope. |
|
733 | # enclosing scope. | |
730 | tokenize.tokenize(linereader, tokeneater) |
|
734 | tokenize.tokenize(linereader, tokeneater) | |
731 | except IndexError: |
|
735 | except IndexError: | |
732 | # signals exit of tokenizer |
|
736 | # signals exit of tokenizer | |
733 | pass |
|
737 | pass | |
734 | except tokenize.TokenError,msg: |
|
738 | except tokenize.TokenError,msg: | |
735 | _m = ("An unexpected error occurred while tokenizing input\n" |
|
739 | _m = ("An unexpected error occurred while tokenizing input\n" | |
736 | "The following traceback may be corrupted or invalid\n" |
|
740 | "The following traceback may be corrupted or invalid\n" | |
737 | "The error message is: %s\n" % msg) |
|
741 | "The error message is: %s\n" % msg) | |
738 | error(_m) |
|
742 | error(_m) | |
739 |
|
743 | |||
740 | # prune names list of duplicates, but keep the right order |
|
744 | # prune names list of duplicates, but keep the right order | |
741 | unique_names = uniq_stable(names) |
|
745 | unique_names = uniq_stable(names) | |
742 |
|
746 | |||
743 | # Start loop over vars |
|
747 | # Start loop over vars | |
744 | lvals = [] |
|
748 | lvals = [] | |
745 | if self.include_vars: |
|
749 | if self.include_vars: | |
746 | for name_full in unique_names: |
|
750 | for name_full in unique_names: | |
747 | name_base = name_full.split('.',1)[0] |
|
751 | name_base = name_full.split('.',1)[0] | |
748 | if name_base in frame.f_code.co_varnames: |
|
752 | if name_base in frame.f_code.co_varnames: | |
749 | if locals.has_key(name_base): |
|
753 | if locals.has_key(name_base): | |
750 | try: |
|
754 | try: | |
751 | value = repr(eval(name_full,locals)) |
|
755 | value = repr(eval(name_full,locals)) | |
752 | except: |
|
756 | except: | |
753 | value = undefined |
|
757 | value = undefined | |
754 | else: |
|
758 | else: | |
755 | value = undefined |
|
759 | value = undefined | |
756 | name = tpl_local_var % name_full |
|
760 | name = tpl_local_var % name_full | |
757 | else: |
|
761 | else: | |
758 | if frame.f_globals.has_key(name_base): |
|
762 | if frame.f_globals.has_key(name_base): | |
759 | try: |
|
763 | try: | |
760 | value = repr(eval(name_full,frame.f_globals)) |
|
764 | value = repr(eval(name_full,frame.f_globals)) | |
761 | except: |
|
765 | except: | |
762 | value = undefined |
|
766 | value = undefined | |
763 | else: |
|
767 | else: | |
764 | value = undefined |
|
768 | value = undefined | |
765 | name = tpl_global_var % name_full |
|
769 | name = tpl_global_var % name_full | |
766 | lvals.append(tpl_name_val % (name,value)) |
|
770 | lvals.append(tpl_name_val % (name,value)) | |
767 | if lvals: |
|
771 | if lvals: | |
768 | lvals = '%s%s' % (indent,em_normal.join(lvals)) |
|
772 | lvals = '%s%s' % (indent,em_normal.join(lvals)) | |
769 | else: |
|
773 | else: | |
770 | lvals = '' |
|
774 | lvals = '' | |
771 |
|
775 | |||
772 | level = '%s %s\n' % (link,call) |
|
776 | level = '%s %s\n' % (link,call) | |
773 |
|
777 | |||
774 | if index is None: |
|
778 | if index is None: | |
775 | frames.append(level) |
|
779 | frames.append(level) | |
776 | else: |
|
780 | else: | |
777 | frames.append('%s%s' % (level,''.join( |
|
781 | frames.append('%s%s' % (level,''.join( | |
778 | _formatTracebackLines(lnum,index,lines,Colors,lvals, |
|
782 | _formatTracebackLines(lnum,index,lines,Colors,lvals, | |
779 | col_scheme)))) |
|
783 | col_scheme)))) | |
780 |
|
784 | |||
781 | # Get (safely) a string form of the exception info |
|
785 | # Get (safely) a string form of the exception info | |
782 | try: |
|
786 | try: | |
783 | etype_str,evalue_str = map(str,(etype,evalue)) |
|
787 | etype_str,evalue_str = map(str,(etype,evalue)) | |
784 | except: |
|
788 | except: | |
785 | # User exception is improperly defined. |
|
789 | # User exception is improperly defined. | |
786 | etype,evalue = str,sys.exc_info()[:2] |
|
790 | etype,evalue = str,sys.exc_info()[:2] | |
787 | etype_str,evalue_str = map(str,(etype,evalue)) |
|
791 | etype_str,evalue_str = map(str,(etype,evalue)) | |
788 | # ... and format it |
|
792 | # ... and format it | |
789 | exception = ['%s%s%s: %s' % (Colors.excName, etype_str, |
|
793 | exception = ['%s%s%s: %s' % (Colors.excName, etype_str, | |
790 | ColorsNormal, evalue_str)] |
|
794 | ColorsNormal, evalue_str)] | |
791 | if type(evalue) is types.InstanceType: |
|
795 | if type(evalue) is types.InstanceType: | |
792 | try: |
|
796 | try: | |
793 | names = [w for w in dir(evalue) if isinstance(w, basestring)] |
|
797 | names = [w for w in dir(evalue) if isinstance(w, basestring)] | |
794 | except: |
|
798 | except: | |
795 | # Every now and then, an object with funny inernals blows up |
|
799 | # Every now and then, an object with funny inernals blows up | |
796 | # when dir() is called on it. We do the best we can to report |
|
800 | # when dir() is called on it. We do the best we can to report | |
797 | # the problem and continue |
|
801 | # the problem and continue | |
798 | _m = '%sException reporting error (object with broken dir())%s:' |
|
802 | _m = '%sException reporting error (object with broken dir())%s:' | |
799 | exception.append(_m % (Colors.excName,ColorsNormal)) |
|
803 | exception.append(_m % (Colors.excName,ColorsNormal)) | |
800 | etype_str,evalue_str = map(str,sys.exc_info()[:2]) |
|
804 | etype_str,evalue_str = map(str,sys.exc_info()[:2]) | |
801 | exception.append('%s%s%s: %s' % (Colors.excName,etype_str, |
|
805 | exception.append('%s%s%s: %s' % (Colors.excName,etype_str, | |
802 | ColorsNormal, evalue_str)) |
|
806 | ColorsNormal, evalue_str)) | |
803 | names = [] |
|
807 | names = [] | |
804 | for name in names: |
|
808 | for name in names: | |
805 | value = text_repr(getattr(evalue, name)) |
|
809 | value = text_repr(getattr(evalue, name)) | |
806 | exception.append('\n%s%s = %s' % (indent, name, value)) |
|
810 | exception.append('\n%s%s = %s' % (indent, name, value)) | |
807 |
|
811 | |||
808 | # vds: >> |
|
812 | # vds: >> | |
809 | if records: |
|
813 | if records: | |
810 | filepath, lnum = records[-1][1:3] |
|
814 | filepath, lnum = records[-1][1:3] | |
811 | #print "file:", str(file), "linenb", str(lnum) # dbg |
|
815 | #print "file:", str(file), "linenb", str(lnum) # dbg | |
812 | filepath = os.path.abspath(filepath) |
|
816 | filepath = os.path.abspath(filepath) | |
813 | __IPYTHON__.hooks.synchronize_with_editor(filepath, lnum, 0) |
|
817 | ipinst = ipapi.get() | |
|
818 | if ipinst is not None: | |||
|
819 | ipinst.IP.hooks.synchronize_with_editor(filepath, lnum, 0) | |||
814 | # vds: << |
|
820 | # vds: << | |
815 |
|
821 | |||
816 | # return all our info assembled as a single string |
|
822 | # return all our info assembled as a single string | |
817 | return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) ) |
|
823 | return '%s\n\n%s\n%s' % (head,'\n'.join(frames),''.join(exception[0]) ) | |
818 |
|
824 | |||
819 | def debugger(self,force=False): |
|
825 | def debugger(self,force=False): | |
820 | """Call up the pdb debugger if desired, always clean up the tb |
|
826 | """Call up the pdb debugger if desired, always clean up the tb | |
821 | reference. |
|
827 | reference. | |
822 |
|
828 | |||
823 | Keywords: |
|
829 | Keywords: | |
824 |
|
830 | |||
825 | - force(False): by default, this routine checks the instance call_pdb |
|
831 | - force(False): by default, this routine checks the instance call_pdb | |
826 | flag and does not actually invoke the debugger if the flag is false. |
|
832 | flag and does not actually invoke the debugger if the flag is false. | |
827 | The 'force' option forces the debugger to activate even if the flag |
|
833 | The 'force' option forces the debugger to activate even if the flag | |
828 | is false. |
|
834 | is false. | |
829 |
|
835 | |||
830 | If the call_pdb flag is set, the pdb interactive debugger is |
|
836 | If the call_pdb flag is set, the pdb interactive debugger is | |
831 | invoked. In all cases, the self.tb reference to the current traceback |
|
837 | invoked. In all cases, the self.tb reference to the current traceback | |
832 | is deleted to prevent lingering references which hamper memory |
|
838 | is deleted to prevent lingering references which hamper memory | |
833 | management. |
|
839 | management. | |
834 |
|
840 | |||
835 | Note that each call to pdb() does an 'import readline', so if your app |
|
841 | Note that each call to pdb() does an 'import readline', so if your app | |
836 | requires a special setup for the readline completers, you'll have to |
|
842 | requires a special setup for the readline completers, you'll have to | |
837 | fix that by hand after invoking the exception handler.""" |
|
843 | fix that by hand after invoking the exception handler.""" | |
838 |
|
844 | |||
839 | if force or self.call_pdb: |
|
845 | if force or self.call_pdb: | |
840 | if self.pdb is None: |
|
846 | if self.pdb is None: | |
841 | self.pdb = debugger.Pdb( |
|
847 | self.pdb = debugger.Pdb( | |
842 | self.color_scheme_table.active_scheme_name) |
|
848 | self.color_scheme_table.active_scheme_name) | |
843 | # the system displayhook may have changed, restore the original |
|
849 | # the system displayhook may have changed, restore the original | |
844 | # for pdb |
|
850 | # for pdb | |
845 | dhook = sys.displayhook |
|
851 | dhook = sys.displayhook | |
846 | sys.displayhook = sys.__displayhook__ |
|
852 | sys.displayhook = sys.__displayhook__ | |
847 | self.pdb.reset() |
|
853 | self.pdb.reset() | |
848 | # Find the right frame so we don't pop up inside ipython itself |
|
854 | # Find the right frame so we don't pop up inside ipython itself | |
849 | if hasattr(self,'tb'): |
|
855 | if hasattr(self,'tb'): | |
850 | etb = self.tb |
|
856 | etb = self.tb | |
851 | else: |
|
857 | else: | |
852 | etb = self.tb = sys.last_traceback |
|
858 | etb = self.tb = sys.last_traceback | |
853 | while self.tb.tb_next is not None: |
|
859 | while self.tb.tb_next is not None: | |
854 | self.tb = self.tb.tb_next |
|
860 | self.tb = self.tb.tb_next | |
855 | try: |
|
861 | try: | |
856 | if etb and etb.tb_next: |
|
862 | if etb and etb.tb_next: | |
857 | etb = etb.tb_next |
|
863 | etb = etb.tb_next | |
858 | self.pdb.botframe = etb.tb_frame |
|
864 | self.pdb.botframe = etb.tb_frame | |
859 | self.pdb.interaction(self.tb.tb_frame, self.tb) |
|
865 | self.pdb.interaction(self.tb.tb_frame, self.tb) | |
860 | finally: |
|
866 | finally: | |
861 | sys.displayhook = dhook |
|
867 | sys.displayhook = dhook | |
862 |
|
868 | |||
863 | if hasattr(self,'tb'): |
|
869 | if hasattr(self,'tb'): | |
864 | del self.tb |
|
870 | del self.tb | |
865 |
|
871 | |||
866 | def handler(self, info=None): |
|
872 | def handler(self, info=None): | |
867 | (etype, evalue, etb) = info or sys.exc_info() |
|
873 | (etype, evalue, etb) = info or sys.exc_info() | |
868 | self.tb = etb |
|
874 | self.tb = etb | |
869 | Term.cout.flush() |
|
875 | Term.cout.flush() | |
870 | print >> Term.cerr, self.text(etype, evalue, etb) |
|
876 | print >> Term.cerr, self.text(etype, evalue, etb) | |
871 | Term.cerr.flush() |
|
877 | Term.cerr.flush() | |
872 |
|
878 | |||
873 | # Changed so an instance can just be called as VerboseTB_inst() and print |
|
879 | # Changed so an instance can just be called as VerboseTB_inst() and print | |
874 | # out the right info on its own. |
|
880 | # out the right info on its own. | |
875 | def __call__(self, etype=None, evalue=None, etb=None): |
|
881 | def __call__(self, etype=None, evalue=None, etb=None): | |
876 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" |
|
882 | """This hook can replace sys.excepthook (for Python 2.1 or higher).""" | |
877 | if etb is None: |
|
883 | if etb is None: | |
878 | self.handler() |
|
884 | self.handler() | |
879 | else: |
|
885 | else: | |
880 | self.handler((etype, evalue, etb)) |
|
886 | self.handler((etype, evalue, etb)) | |
881 | try: |
|
887 | try: | |
882 | self.debugger() |
|
888 | self.debugger() | |
883 | except KeyboardInterrupt: |
|
889 | except KeyboardInterrupt: | |
884 | print "\nKeyboardInterrupt" |
|
890 | print "\nKeyboardInterrupt" | |
885 |
|
891 | |||
886 | #---------------------------------------------------------------------------- |
|
892 | #---------------------------------------------------------------------------- | |
887 | class FormattedTB(VerboseTB,ListTB): |
|
893 | class FormattedTB(VerboseTB,ListTB): | |
888 | """Subclass ListTB but allow calling with a traceback. |
|
894 | """Subclass ListTB but allow calling with a traceback. | |
889 |
|
895 | |||
890 | It can thus be used as a sys.excepthook for Python > 2.1. |
|
896 | It can thus be used as a sys.excepthook for Python > 2.1. | |
891 |
|
897 | |||
892 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. |
|
898 | Also adds 'Context' and 'Verbose' modes, not available in ListTB. | |
893 |
|
899 | |||
894 | Allows a tb_offset to be specified. This is useful for situations where |
|
900 | Allows a tb_offset to be specified. This is useful for situations where | |
895 | one needs to remove a number of topmost frames from the traceback (such as |
|
901 | one needs to remove a number of topmost frames from the traceback (such as | |
896 | occurs with python programs that themselves execute other python code, |
|
902 | occurs with python programs that themselves execute other python code, | |
897 | like Python shells). """ |
|
903 | like Python shells). """ | |
898 |
|
904 | |||
899 | def __init__(self, mode = 'Plain', color_scheme='Linux', |
|
905 | def __init__(self, mode = 'Plain', color_scheme='Linux', | |
900 | tb_offset = 0,long_header=0,call_pdb=0,include_vars=0): |
|
906 | tb_offset = 0,long_header=0,call_pdb=0,include_vars=0): | |
901 |
|
907 | |||
902 | # NEVER change the order of this list. Put new modes at the end: |
|
908 | # NEVER change the order of this list. Put new modes at the end: | |
903 | self.valid_modes = ['Plain','Context','Verbose'] |
|
909 | self.valid_modes = ['Plain','Context','Verbose'] | |
904 | self.verbose_modes = self.valid_modes[1:3] |
|
910 | self.verbose_modes = self.valid_modes[1:3] | |
905 |
|
911 | |||
906 | VerboseTB.__init__(self,color_scheme,tb_offset,long_header, |
|
912 | VerboseTB.__init__(self,color_scheme,tb_offset,long_header, | |
907 | call_pdb=call_pdb,include_vars=include_vars) |
|
913 | call_pdb=call_pdb,include_vars=include_vars) | |
908 | self.set_mode(mode) |
|
914 | self.set_mode(mode) | |
909 |
|
915 | |||
910 | def _extract_tb(self,tb): |
|
916 | def _extract_tb(self,tb): | |
911 | if tb: |
|
917 | if tb: | |
912 | return traceback.extract_tb(tb) |
|
918 | return traceback.extract_tb(tb) | |
913 | else: |
|
919 | else: | |
914 | return None |
|
920 | return None | |
915 |
|
921 | |||
916 | def text(self, etype, value, tb,context=5,mode=None): |
|
922 | def text(self, etype, value, tb,context=5,mode=None): | |
917 | """Return formatted traceback. |
|
923 | """Return formatted traceback. | |
918 |
|
924 | |||
919 | If the optional mode parameter is given, it overrides the current |
|
925 | If the optional mode parameter is given, it overrides the current | |
920 | mode.""" |
|
926 | mode.""" | |
921 |
|
927 | |||
922 | if mode is None: |
|
928 | if mode is None: | |
923 | mode = self.mode |
|
929 | mode = self.mode | |
924 | if mode in self.verbose_modes: |
|
930 | if mode in self.verbose_modes: | |
925 | # verbose modes need a full traceback |
|
931 | # verbose modes need a full traceback | |
926 | return VerboseTB.text(self,etype, value, tb,context=5) |
|
932 | return VerboseTB.text(self,etype, value, tb,context=5) | |
927 | else: |
|
933 | else: | |
928 | # We must check the source cache because otherwise we can print |
|
934 | # We must check the source cache because otherwise we can print | |
929 | # out-of-date source code. |
|
935 | # out-of-date source code. | |
930 | linecache.checkcache() |
|
936 | linecache.checkcache() | |
931 | # Now we can extract and format the exception |
|
937 | # Now we can extract and format the exception | |
932 | elist = self._extract_tb(tb) |
|
938 | elist = self._extract_tb(tb) | |
933 | if len(elist) > self.tb_offset: |
|
939 | if len(elist) > self.tb_offset: | |
934 | del elist[:self.tb_offset] |
|
940 | del elist[:self.tb_offset] | |
935 | return ListTB.text(self,etype,value,elist) |
|
941 | return ListTB.text(self,etype,value,elist) | |
936 |
|
942 | |||
937 | def set_mode(self,mode=None): |
|
943 | def set_mode(self,mode=None): | |
938 | """Switch to the desired mode. |
|
944 | """Switch to the desired mode. | |
939 |
|
945 | |||
940 | If mode is not specified, cycles through the available modes.""" |
|
946 | If mode is not specified, cycles through the available modes.""" | |
941 |
|
947 | |||
942 | if not mode: |
|
948 | if not mode: | |
943 | new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \ |
|
949 | new_idx = ( self.valid_modes.index(self.mode) + 1 ) % \ | |
944 | len(self.valid_modes) |
|
950 | len(self.valid_modes) | |
945 | self.mode = self.valid_modes[new_idx] |
|
951 | self.mode = self.valid_modes[new_idx] | |
946 | elif mode not in self.valid_modes: |
|
952 | elif mode not in self.valid_modes: | |
947 | raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\ |
|
953 | raise ValueError, 'Unrecognized mode in FormattedTB: <'+mode+'>\n'\ | |
948 | 'Valid modes: '+str(self.valid_modes) |
|
954 | 'Valid modes: '+str(self.valid_modes) | |
949 | else: |
|
955 | else: | |
950 | self.mode = mode |
|
956 | self.mode = mode | |
951 | # include variable details only in 'Verbose' mode |
|
957 | # include variable details only in 'Verbose' mode | |
952 | self.include_vars = (self.mode == self.valid_modes[2]) |
|
958 | self.include_vars = (self.mode == self.valid_modes[2]) | |
953 |
|
959 | |||
954 | # some convenient shorcuts |
|
960 | # some convenient shorcuts | |
955 | def plain(self): |
|
961 | def plain(self): | |
956 | self.set_mode(self.valid_modes[0]) |
|
962 | self.set_mode(self.valid_modes[0]) | |
957 |
|
963 | |||
958 | def context(self): |
|
964 | def context(self): | |
959 | self.set_mode(self.valid_modes[1]) |
|
965 | self.set_mode(self.valid_modes[1]) | |
960 |
|
966 | |||
961 | def verbose(self): |
|
967 | def verbose(self): | |
962 | self.set_mode(self.valid_modes[2]) |
|
968 | self.set_mode(self.valid_modes[2]) | |
963 |
|
969 | |||
964 | #---------------------------------------------------------------------------- |
|
970 | #---------------------------------------------------------------------------- | |
965 | class AutoFormattedTB(FormattedTB): |
|
971 | class AutoFormattedTB(FormattedTB): | |
966 | """A traceback printer which can be called on the fly. |
|
972 | """A traceback printer which can be called on the fly. | |
967 |
|
973 | |||
968 | It will find out about exceptions by itself. |
|
974 | It will find out about exceptions by itself. | |
969 |
|
975 | |||
970 | A brief example: |
|
976 | A brief example: | |
971 |
|
977 | |||
972 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') |
|
978 | AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux') | |
973 | try: |
|
979 | try: | |
974 | ... |
|
980 | ... | |
975 | except: |
|
981 | except: | |
976 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object |
|
982 | AutoTB() # or AutoTB(out=logfile) where logfile is an open file object | |
977 | """ |
|
983 | """ | |
978 | def __call__(self,etype=None,evalue=None,etb=None, |
|
984 | def __call__(self,etype=None,evalue=None,etb=None, | |
979 | out=None,tb_offset=None): |
|
985 | out=None,tb_offset=None): | |
980 | """Print out a formatted exception traceback. |
|
986 | """Print out a formatted exception traceback. | |
981 |
|
987 | |||
982 | Optional arguments: |
|
988 | Optional arguments: | |
983 | - out: an open file-like object to direct output to. |
|
989 | - out: an open file-like object to direct output to. | |
984 |
|
990 | |||
985 | - tb_offset: the number of frames to skip over in the stack, on a |
|
991 | - tb_offset: the number of frames to skip over in the stack, on a | |
986 | per-call basis (this overrides temporarily the instance's tb_offset |
|
992 | per-call basis (this overrides temporarily the instance's tb_offset | |
987 | given at initialization time. """ |
|
993 | given at initialization time. """ | |
988 |
|
994 | |||
989 | if out is None: |
|
995 | if out is None: | |
990 | out = Term.cerr |
|
996 | out = Term.cerr | |
991 | Term.cout.flush() |
|
997 | Term.cout.flush() | |
992 | if tb_offset is not None: |
|
998 | if tb_offset is not None: | |
993 | tb_offset, self.tb_offset = self.tb_offset, tb_offset |
|
999 | tb_offset, self.tb_offset = self.tb_offset, tb_offset | |
994 | print >> out, self.text(etype, evalue, etb) |
|
1000 | print >> out, self.text(etype, evalue, etb) | |
995 | self.tb_offset = tb_offset |
|
1001 | self.tb_offset = tb_offset | |
996 | else: |
|
1002 | else: | |
997 | print >> out, self.text(etype, evalue, etb) |
|
1003 | print >> out, self.text(etype, evalue, etb) | |
998 | out.flush() |
|
1004 | out.flush() | |
999 | try: |
|
1005 | try: | |
1000 | self.debugger() |
|
1006 | self.debugger() | |
1001 | except KeyboardInterrupt: |
|
1007 | except KeyboardInterrupt: | |
1002 | print "\nKeyboardInterrupt" |
|
1008 | print "\nKeyboardInterrupt" | |
1003 |
|
1009 | |||
1004 | def text(self,etype=None,value=None,tb=None,context=5,mode=None): |
|
1010 | def text(self,etype=None,value=None,tb=None,context=5,mode=None): | |
1005 | if etype is None: |
|
1011 | if etype is None: | |
1006 | etype,value,tb = sys.exc_info() |
|
1012 | etype,value,tb = sys.exc_info() | |
1007 | self.tb = tb |
|
1013 | self.tb = tb | |
1008 | return FormattedTB.text(self,etype,value,tb,context=5,mode=mode) |
|
1014 | return FormattedTB.text(self,etype,value,tb,context=5,mode=mode) | |
1009 |
|
1015 | |||
1010 | #--------------------------------------------------------------------------- |
|
1016 | #--------------------------------------------------------------------------- | |
1011 | # A simple class to preserve Nathan's original functionality. |
|
1017 | # A simple class to preserve Nathan's original functionality. | |
1012 | class ColorTB(FormattedTB): |
|
1018 | class ColorTB(FormattedTB): | |
1013 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" |
|
1019 | """Shorthand to initialize a FormattedTB in Linux colors mode.""" | |
1014 | def __init__(self,color_scheme='Linux',call_pdb=0): |
|
1020 | def __init__(self,color_scheme='Linux',call_pdb=0): | |
1015 | FormattedTB.__init__(self,color_scheme=color_scheme, |
|
1021 | FormattedTB.__init__(self,color_scheme=color_scheme, | |
1016 | call_pdb=call_pdb) |
|
1022 | call_pdb=call_pdb) | |
1017 |
|
1023 | |||
1018 | #---------------------------------------------------------------------------- |
|
1024 | #---------------------------------------------------------------------------- | |
1019 | # module testing (minimal) |
|
1025 | # module testing (minimal) | |
1020 | if __name__ == "__main__": |
|
1026 | if __name__ == "__main__": | |
1021 | def spam(c, (d, e)): |
|
1027 | def spam(c, (d, e)): | |
1022 | x = c + d |
|
1028 | x = c + d | |
1023 | y = c * d |
|
1029 | y = c * d | |
1024 | foo(x, y) |
|
1030 | foo(x, y) | |
1025 |
|
1031 | |||
1026 | def foo(a, b, bar=1): |
|
1032 | def foo(a, b, bar=1): | |
1027 | eggs(a, b + bar) |
|
1033 | eggs(a, b + bar) | |
1028 |
|
1034 | |||
1029 | def eggs(f, g, z=globals()): |
|
1035 | def eggs(f, g, z=globals()): | |
1030 | h = f + g |
|
1036 | h = f + g | |
1031 | i = f - g |
|
1037 | i = f - g | |
1032 | return h / i |
|
1038 | return h / i | |
1033 |
|
1039 | |||
1034 | print '' |
|
1040 | print '' | |
1035 | print '*** Before ***' |
|
1041 | print '*** Before ***' | |
1036 | try: |
|
1042 | try: | |
1037 | print spam(1, (2, 3)) |
|
1043 | print spam(1, (2, 3)) | |
1038 | except: |
|
1044 | except: | |
1039 | traceback.print_exc() |
|
1045 | traceback.print_exc() | |
1040 | print '' |
|
1046 | print '' | |
1041 |
|
1047 | |||
1042 | handler = ColorTB() |
|
1048 | handler = ColorTB() | |
1043 | print '*** ColorTB ***' |
|
1049 | print '*** ColorTB ***' | |
1044 | try: |
|
1050 | try: | |
1045 | print spam(1, (2, 3)) |
|
1051 | print spam(1, (2, 3)) | |
1046 | except: |
|
1052 | except: | |
1047 | apply(handler, sys.exc_info() ) |
|
1053 | apply(handler, sys.exc_info() ) | |
1048 | print '' |
|
1054 | print '' | |
1049 |
|
1055 | |||
1050 | handler = VerboseTB() |
|
1056 | handler = VerboseTB() | |
1051 | print '*** VerboseTB ***' |
|
1057 | print '*** VerboseTB ***' | |
1052 | try: |
|
1058 | try: | |
1053 | print spam(1, (2, 3)) |
|
1059 | print spam(1, (2, 3)) | |
1054 | except: |
|
1060 | except: | |
1055 | apply(handler, sys.exc_info() ) |
|
1061 | apply(handler, sys.exc_info() ) | |
1056 | print '' |
|
1062 | print '' | |
1057 |
|
1063 |
@@ -1,252 +1,266 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | Test process execution and IO redirection. |
|
3 | Test process execution and IO redirection. | |
4 | """ |
|
4 | """ | |
5 |
|
5 | |||
6 | __docformat__ = "restructuredtext en" |
|
6 | __docformat__ = "restructuredtext en" | |
7 |
|
7 | |||
8 | #------------------------------------------------------------------------------- |
|
8 | #------------------------------------------------------------------------------- | |
9 | # Copyright (C) 2008 The IPython Development Team |
|
9 | # Copyright (C) 2008 The IPython Development Team | |
10 | # |
|
10 | # | |
11 | # Distributed under the terms of the BSD License. The full license is |
|
11 | # Distributed under the terms of the BSD License. The full license is | |
12 | # in the file COPYING, distributed as part of this software. |
|
12 | # in the file COPYING, distributed as part of this software. | |
13 | #------------------------------------------------------------------------------- |
|
13 | #------------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | from copy import copy, deepcopy |
|
15 | from copy import copy, deepcopy | |
16 | from cStringIO import StringIO |
|
16 | from cStringIO import StringIO | |
17 | import string |
|
17 | import string | |
|
18 | import sys | |||
18 |
|
19 | |||
19 | from nose.tools import assert_equal |
|
20 | from nose.tools import assert_equal | |
20 |
|
21 | |||
21 | from IPython.frontend.prefilterfrontend import PrefilterFrontEnd |
|
22 | from IPython.frontend.prefilterfrontend import PrefilterFrontEnd | |
22 | from IPython.core.ipapi import get as get_ipython0 |
|
23 | from IPython.core.ipapi import get as get_ipython0 | |
23 | from IPython.testing.plugin.ipdoctest import default_argv |
|
24 | from IPython.testing.plugin.ipdoctest import default_argv | |
24 |
|
25 | |||
25 |
|
26 | |||
26 | def safe_deepcopy(d): |
|
|||
27 | """ Deep copy every key of the given dict, when possible. Elsewhere |
|
|||
28 | do a copy. |
|
|||
29 | """ |
|
|||
30 | copied_d = dict() |
|
|||
31 | for key, value in d.iteritems(): |
|
|||
32 | try: |
|
|||
33 | copied_d[key] = deepcopy(value) |
|
|||
34 | except: |
|
|||
35 | try: |
|
|||
36 | copied_d[key] = copy(value) |
|
|||
37 | except: |
|
|||
38 | copied_d[key] = value |
|
|||
39 | return copied_d |
|
|||
40 |
|
||||
41 |
|
||||
42 | class TestPrefilterFrontEnd(PrefilterFrontEnd): |
|
27 | class TestPrefilterFrontEnd(PrefilterFrontEnd): | |
43 |
|
28 | |||
44 | input_prompt_template = string.Template('') |
|
29 | input_prompt_template = string.Template('') | |
45 | output_prompt_template = string.Template('') |
|
30 | output_prompt_template = string.Template('') | |
46 | banner = '' |
|
31 | banner = '' | |
47 |
|
32 | |||
48 | def __init__(self): |
|
33 | def __init__(self): | |
49 | self.out = StringIO() |
|
34 | self.out = StringIO() | |
50 | PrefilterFrontEnd.__init__(self,argv=default_argv()) |
|
35 | PrefilterFrontEnd.__init__(self,argv=default_argv()) | |
51 | # Some more code for isolation (yeah, crazy) |
|
36 | # Some more code for isolation (yeah, crazy) | |
52 | self._on_enter() |
|
37 | self._on_enter() | |
53 | self.out.flush() |
|
38 | self.out.flush() | |
54 | self.out.reset() |
|
39 | self.out.reset() | |
55 | self.out.truncate() |
|
40 | self.out.truncate() | |
56 |
|
41 | |||
57 | def write(self, string, *args, **kwargs): |
|
42 | def write(self, string, *args, **kwargs): | |
58 | self.out.write(string) |
|
43 | self.out.write(string) | |
59 |
|
44 | |||
60 | def _on_enter(self): |
|
45 | def _on_enter(self): | |
61 | self.input_buffer += '\n' |
|
46 | self.input_buffer += '\n' | |
62 | PrefilterFrontEnd._on_enter(self) |
|
47 | PrefilterFrontEnd._on_enter(self) | |
63 |
|
48 | |||
64 |
|
49 | |||
65 | def isolate_ipython0(func): |
|
50 | def isolate_ipython0(func): | |
66 | """ Decorator to isolate execution that involves an iptyhon0. |
|
51 | """ Decorator to isolate execution that involves an iptyhon0. | |
67 |
|
52 | |||
68 | Notes |
|
53 | Notes | |
69 | ----- |
|
54 | ----- | |
70 |
|
55 | |||
71 | Apply only to functions with no arguments. Nose skips functions |
|
56 | Apply only to functions with no arguments. Nose skips functions | |
72 | with arguments. |
|
57 | with arguments. | |
73 | """ |
|
58 | """ | |
74 | def my_func(): |
|
59 | def my_func(): | |
75 |
ip |
|
60 | ip0 = get_ipython0() | |
76 |
if ip |
|
61 | if ip0 is None: | |
77 | return func() |
|
62 | return func() | |
78 | ipython0 = iplib.IP |
|
63 | # We have a real ipython running... | |
79 | global_ns = safe_deepcopy(ipython0.user_global_ns) |
|
64 | user_ns = ip0.IP.user_ns | |
80 |
user_ns = |
|
65 | user_global_ns = ip0.IP.user_global_ns | |
|
66 | ||||
|
67 | # Previously the isolation was attempted with a deep copy of the user | |||
|
68 | # dicts, but we found cases where this didn't work correctly. I'm not | |||
|
69 | # quite sure why, but basically it did damage the user namespace, such | |||
|
70 | # that later tests stopped working correctly. Instead we use a simpler | |||
|
71 | # approach, just computing the list of added keys to the namespace and | |||
|
72 | # eliminating those afterwards. Existing keys that may have been | |||
|
73 | # modified remain modified. So far this has proven to be robust. | |||
|
74 | ||||
|
75 | # Compute set of old local/global keys | |||
|
76 | old_locals = set(user_ns.keys()) | |||
|
77 | old_globals = set(user_global_ns.keys()) | |||
81 | try: |
|
78 | try: | |
82 | out = func() |
|
79 | out = func() | |
83 | finally: |
|
80 | finally: | |
84 | ipython0.user_ns = user_ns |
|
81 | # Find new keys, and if any, remove them | |
85 | ipython0.user_global_ns = global_ns |
|
82 | new_locals = set(user_ns.keys()) - old_locals | |
|
83 | new_globals = set(user_global_ns.keys()) - old_globals | |||
|
84 | for k in new_locals: | |||
|
85 | del user_ns[k] | |||
|
86 | for k in new_globals: | |||
|
87 | del user_global_ns[k] | |||
86 | # Undo the hack at creation of PrefilterFrontEnd |
|
88 | # Undo the hack at creation of PrefilterFrontEnd | |
87 | from IPython.core import iplib |
|
89 | from IPython.core import iplib | |
88 | iplib.InteractiveShell.isthreaded = False |
|
90 | iplib.InteractiveShell.isthreaded = False | |
89 | return out |
|
91 | return out | |
90 |
|
92 | |||
91 | my_func.__name__ = func.__name__ |
|
93 | my_func.__name__ = func.__name__ | |
92 | return my_func |
|
94 | return my_func | |
93 |
|
95 | |||
94 |
|
96 | |||
95 | @isolate_ipython0 |
|
97 | @isolate_ipython0 | |
96 | def test_execution(): |
|
98 | def test_execution(): | |
97 | """ Test execution of a command. |
|
99 | """ Test execution of a command. | |
98 | """ |
|
100 | """ | |
99 | f = TestPrefilterFrontEnd() |
|
101 | f = TestPrefilterFrontEnd() | |
100 |
f.input_buffer = 'print |
|
102 | f.input_buffer = 'print(1)' | |
101 | f._on_enter() |
|
103 | f._on_enter() | |
102 | out_value = f.out.getvalue() |
|
104 | out_value = f.out.getvalue() | |
103 | assert_equal(out_value, '1\n') |
|
105 | assert_equal(out_value, '1\n') | |
104 |
|
106 | |||
105 |
|
107 | |||
106 | @isolate_ipython0 |
|
108 | @isolate_ipython0 | |
107 | def test_multiline(): |
|
109 | def test_multiline(): | |
108 | """ Test execution of a multiline command. |
|
110 | """ Test execution of a multiline command. | |
109 | """ |
|
111 | """ | |
110 | f = TestPrefilterFrontEnd() |
|
112 | f = TestPrefilterFrontEnd() | |
111 | f.input_buffer = 'if True:' |
|
113 | f.input_buffer = 'if True:' | |
112 | f._on_enter() |
|
114 | f._on_enter() | |
113 | f.input_buffer += 'print 1' |
|
115 | f.input_buffer += 'print 1' | |
114 | f._on_enter() |
|
116 | f._on_enter() | |
115 | out_value = f.out.getvalue() |
|
117 | out_value = f.out.getvalue() | |
116 | yield assert_equal, out_value, '' |
|
118 | yield assert_equal, out_value, '' | |
117 | f._on_enter() |
|
119 | f._on_enter() | |
118 | out_value = f.out.getvalue() |
|
120 | out_value = f.out.getvalue() | |
119 | yield assert_equal, out_value, '1\n' |
|
121 | yield assert_equal, out_value, '1\n' | |
120 | f = TestPrefilterFrontEnd() |
|
122 | f = TestPrefilterFrontEnd() | |
121 | f.input_buffer='(1 +' |
|
123 | f.input_buffer='(1 +' | |
122 | f._on_enter() |
|
124 | f._on_enter() | |
123 | f.input_buffer += '0)' |
|
125 | f.input_buffer += '0)' | |
124 | f._on_enter() |
|
126 | f._on_enter() | |
125 | out_value = f.out.getvalue() |
|
127 | out_value = f.out.getvalue() | |
126 | yield assert_equal, out_value, '' |
|
128 | yield assert_equal, out_value, '' | |
127 | f._on_enter() |
|
129 | f._on_enter() | |
128 | out_value = f.out.getvalue() |
|
130 | out_value = f.out.getvalue() | |
129 | yield assert_equal, out_value, '1\n' |
|
131 | yield assert_equal, out_value, '1\n' | |
130 |
|
132 | |||
131 |
|
133 | |||
132 | @isolate_ipython0 |
|
134 | @isolate_ipython0 | |
133 | def test_capture(): |
|
135 | def test_capture(): | |
134 | """ Test the capture of output in different channels. |
|
136 | """ Test the capture of output in different channels. | |
135 | """ |
|
137 | """ | |
136 | # Test on the OS-level stdout, stderr. |
|
138 | # Test on the OS-level stdout, stderr. | |
137 | f = TestPrefilterFrontEnd() |
|
139 | f = TestPrefilterFrontEnd() | |
138 | f.input_buffer = \ |
|
140 | f.input_buffer = \ | |
139 | 'import os; out=os.fdopen(1, "w"); out.write("1") ; out.flush()' |
|
141 | 'import os; out=os.fdopen(1, "w"); out.write("1") ; out.flush()' | |
140 | f._on_enter() |
|
142 | f._on_enter() | |
141 | out_value = f.out.getvalue() |
|
143 | out_value = f.out.getvalue() | |
142 | yield assert_equal, out_value, '1' |
|
144 | yield assert_equal, out_value, '1' | |
143 | f = TestPrefilterFrontEnd() |
|
145 | f = TestPrefilterFrontEnd() | |
144 | f.input_buffer = \ |
|
146 | f.input_buffer = \ | |
145 | 'import os; out=os.fdopen(2, "w"); out.write("1") ; out.flush()' |
|
147 | 'import os; out=os.fdopen(2, "w"); out.write("1") ; out.flush()' | |
146 | f._on_enter() |
|
148 | f._on_enter() | |
147 | out_value = f.out.getvalue() |
|
149 | out_value = f.out.getvalue() | |
148 | yield assert_equal, out_value, '1' |
|
150 | yield assert_equal, out_value, '1' | |
149 |
|
151 | |||
150 |
|
152 | |||
151 | @isolate_ipython0 |
|
153 | @isolate_ipython0 | |
152 | def test_magic(): |
|
154 | def test_magic(): | |
153 | """ Test the magic expansion and history. |
|
155 | """ Test the magic expansion and history. | |
154 |
|
156 | |||
155 | This test is fairly fragile and will break when magics change. |
|
157 | This test is fairly fragile and will break when magics change. | |
156 | """ |
|
158 | """ | |
157 | f = TestPrefilterFrontEnd() |
|
159 | f = TestPrefilterFrontEnd() | |
158 | # Before checking the interactive namespace, make sure it's clear (it can |
|
160 | # Before checking the interactive namespace, make sure it's clear (it can | |
159 | # otherwise pick up things stored in the user's local db) |
|
161 | # otherwise pick up things stored in the user's local db) | |
160 | f.input_buffer += '%reset -f' |
|
162 | f.input_buffer += '%reset -f' | |
161 | f._on_enter() |
|
163 | f._on_enter() | |
162 | f.complete_current_input() |
|
164 | f.complete_current_input() | |
163 | # Now, run the %who magic and check output |
|
165 | # Now, run the %who magic and check output | |
164 | f.input_buffer += '%who' |
|
166 | f.input_buffer += '%who' | |
165 | f._on_enter() |
|
167 | f._on_enter() | |
166 | out_value = f.out.getvalue() |
|
168 | out_value = f.out.getvalue() | |
167 | assert_equal(out_value, 'Interactive namespace is empty.\n') |
|
169 | assert_equal(out_value, 'Interactive namespace is empty.\n') | |
168 |
|
170 | |||
169 |
|
171 | |||
170 | @isolate_ipython0 |
|
172 | @isolate_ipython0 | |
171 | def test_help(): |
|
173 | def test_help(): | |
172 | """ Test object inspection. |
|
174 | """ Test object inspection. | |
173 | """ |
|
175 | """ | |
174 | f = TestPrefilterFrontEnd() |
|
176 | f = TestPrefilterFrontEnd() | |
175 | f.input_buffer += "def f():" |
|
177 | f.input_buffer += "def f():" | |
176 | f._on_enter() |
|
178 | f._on_enter() | |
177 | f.input_buffer += "'foobar'" |
|
179 | f.input_buffer += "'foobar'" | |
178 | f._on_enter() |
|
180 | f._on_enter() | |
179 | f.input_buffer += "pass" |
|
181 | f.input_buffer += "pass" | |
180 | f._on_enter() |
|
182 | f._on_enter() | |
181 | f._on_enter() |
|
183 | f._on_enter() | |
182 | f.input_buffer += "f?" |
|
184 | f.input_buffer += "f?" | |
183 | f._on_enter() |
|
185 | f._on_enter() | |
184 | assert 'traceback' not in f.last_result |
|
186 | assert 'traceback' not in f.last_result | |
185 | ## XXX: ipython doctest magic breaks this. I have no clue why |
|
187 | ## XXX: ipython doctest magic breaks this. I have no clue why | |
186 | #out_value = f.out.getvalue() |
|
188 | #out_value = f.out.getvalue() | |
187 | #assert out_value.split()[-1] == 'foobar' |
|
189 | #assert out_value.split()[-1] == 'foobar' | |
188 |
|
190 | |||
189 |
|
191 | |||
190 | @isolate_ipython0 |
|
192 | @isolate_ipython0 | |
191 | def test_completion_simple(): |
|
193 | def test_completion_simple(): | |
192 | """ Test command-line completion on trivial examples. |
|
194 | """ Test command-line completion on trivial examples. | |
193 | """ |
|
195 | """ | |
194 | f = TestPrefilterFrontEnd() |
|
196 | f = TestPrefilterFrontEnd() | |
195 | f.input_buffer = 'zzza = 1' |
|
197 | f.input_buffer = 'zzza = 1' | |
196 | f._on_enter() |
|
198 | f._on_enter() | |
197 | f.input_buffer = 'zzzb = 2' |
|
199 | f.input_buffer = 'zzzb = 2' | |
198 | f._on_enter() |
|
200 | f._on_enter() | |
199 | f.input_buffer = 'zz' |
|
201 | f.input_buffer = 'zz' | |
200 | f.complete_current_input() |
|
202 | f.complete_current_input() | |
201 | out_value = f.out.getvalue() |
|
203 | out_value = f.out.getvalue() | |
202 | yield assert_equal, out_value, '\nzzza zzzb ' |
|
204 | yield assert_equal, out_value, '\nzzza zzzb ' | |
203 | yield assert_equal, f.input_buffer, 'zzz' |
|
205 | yield assert_equal, f.input_buffer, 'zzz' | |
204 |
|
206 | |||
205 |
|
207 | |||
206 | @isolate_ipython0 |
|
208 | @isolate_ipython0 | |
207 | def test_completion_parenthesis(): |
|
209 | def test_completion_parenthesis(): | |
208 | """ Test command-line completion when a parenthesis is open. |
|
210 | """ Test command-line completion when a parenthesis is open. | |
209 | """ |
|
211 | """ | |
210 | f = TestPrefilterFrontEnd() |
|
212 | f = TestPrefilterFrontEnd() | |
211 | f.input_buffer = 'zzza = 1' |
|
213 | f.input_buffer = 'zzza = 1' | |
212 | f._on_enter() |
|
214 | f._on_enter() | |
213 | f.input_buffer = 'zzzb = 2' |
|
215 | f.input_buffer = 'zzzb = 2' | |
214 | f._on_enter() |
|
216 | f._on_enter() | |
215 | f.input_buffer = 'map(zz' |
|
217 | f.input_buffer = 'map(zz' | |
216 | f.complete_current_input() |
|
218 | f.complete_current_input() | |
217 | out_value = f.out.getvalue() |
|
219 | out_value = f.out.getvalue() | |
218 | yield assert_equal, out_value, '\nzzza zzzb ' |
|
220 | yield assert_equal, out_value, '\nzzza zzzb ' | |
219 | yield assert_equal, f.input_buffer, 'map(zzz' |
|
221 | yield assert_equal, f.input_buffer, 'map(zzz' | |
220 |
|
222 | |||
221 |
|
223 | |||
222 | @isolate_ipython0 |
|
224 | @isolate_ipython0 | |
223 | def test_completion_indexing(): |
|
225 | def test_completion_indexing(): | |
224 | """ Test command-line completion when indexing on objects. |
|
226 | """ Test command-line completion when indexing on objects. | |
225 | """ |
|
227 | """ | |
226 | f = TestPrefilterFrontEnd() |
|
228 | f = TestPrefilterFrontEnd() | |
227 | f.input_buffer = 'a = [0]' |
|
229 | f.input_buffer = 'a = [0]' | |
228 | f._on_enter() |
|
230 | f._on_enter() | |
229 | f.input_buffer = 'a[0].' |
|
231 | f.input_buffer = 'a[0].' | |
230 | f.complete_current_input() |
|
232 | f.complete_current_input() | |
231 | assert_equal(f.input_buffer, 'a[0].__') |
|
233 | ||
|
234 | if sys.version_info[:2] >= (2,6): | |||
|
235 | # In Python 2.6, ints picked up a few non __ methods, so now there are | |||
|
236 | # no completions. | |||
|
237 | assert_equal(f.input_buffer, 'a[0].') | |||
|
238 | else: | |||
|
239 | # Right answer for 2.4/2.5 | |||
|
240 | assert_equal(f.input_buffer, 'a[0].__') | |||
232 |
|
241 | |||
233 |
|
242 | |||
234 | @isolate_ipython0 |
|
243 | @isolate_ipython0 | |
235 | def test_completion_equal(): |
|
244 | def test_completion_equal(): | |
236 | """ Test command-line completion when the delimiter is "=", not " ". |
|
245 | """ Test command-line completion when the delimiter is "=", not " ". | |
237 | """ |
|
246 | """ | |
238 | f = TestPrefilterFrontEnd() |
|
247 | f = TestPrefilterFrontEnd() | |
239 | f.input_buffer = 'a=1.' |
|
248 | f.input_buffer = 'a=1.' | |
240 | f.complete_current_input() |
|
249 | f.complete_current_input() | |
241 | assert_equal(f.input_buffer, 'a=1.__') |
|
250 | if sys.version_info[:2] >= (2,6): | |
242 |
|
251 | # In Python 2.6, ints picked up a few non __ methods, so now there are | ||
|
252 | # no completions. | |||
|
253 | assert_equal(f.input_buffer, 'a=1.') | |||
|
254 | else: | |||
|
255 | # Right answer for 2.4/2.5 | |||
|
256 | assert_equal(f.input_buffer, 'a=1.__') | |||
243 |
|
257 | |||
244 |
|
258 | |||
245 | if __name__ == '__main__': |
|
259 | if __name__ == '__main__': | |
246 | test_magic() |
|
260 | test_magic() | |
247 | test_help() |
|
261 | test_help() | |
248 | test_execution() |
|
262 | test_execution() | |
249 | test_multiline() |
|
263 | test_multiline() | |
250 | test_capture() |
|
264 | test_capture() | |
251 | test_completion_simple() |
|
265 | test_completion_simple() | |
252 | test_completion_complex() |
|
266 | test_completion_complex() |
@@ -1,141 +1,141 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | # -*- test-case-name: IPython.kernel.test.test_contexts -*- |
|
2 | # -*- test-case-name: IPython.kernel.test.test_contexts -*- | |
3 | """Context managers for IPython. |
|
3 | """Context managers for IPython. | |
4 |
|
4 | |||
5 | Python 2.5 introduced the `with` statement, which is based on the context |
|
5 | Python 2.5 introduced the `with` statement, which is based on the context | |
6 | manager protocol. This module offers a few context managers for common cases, |
|
6 | manager protocol. This module offers a few context managers for common cases, | |
7 | which can also be useful as templates for writing new, application-specific |
|
7 | which can also be useful as templates for writing new, application-specific | |
8 | managers. |
|
8 | managers. | |
9 | """ |
|
9 | """ | |
10 |
|
10 | |||
11 | __docformat__ = "restructuredtext en" |
|
11 | __docformat__ = "restructuredtext en" | |
12 |
|
12 | |||
13 | #------------------------------------------------------------------------------- |
|
13 | #------------------------------------------------------------------------------- | |
14 | # Copyright (C) 2008 The IPython Development Team |
|
14 | # Copyright (C) 2008 The IPython Development Team | |
15 | # |
|
15 | # | |
16 | # Distributed under the terms of the BSD License. The full license is in |
|
16 | # Distributed under the terms of the BSD License. The full license is in | |
17 | # the file COPYING, distributed as part of this software. |
|
17 | # the file COPYING, distributed as part of this software. | |
18 | #------------------------------------------------------------------------------- |
|
18 | #------------------------------------------------------------------------------- | |
19 |
|
19 | |||
20 | #------------------------------------------------------------------------------- |
|
20 | #------------------------------------------------------------------------------- | |
21 | # Imports |
|
21 | # Imports | |
22 | #------------------------------------------------------------------------------- |
|
22 | #------------------------------------------------------------------------------- | |
23 |
|
23 | |||
24 | import linecache |
|
24 | import linecache | |
25 | import sys |
|
25 | import sys | |
26 |
|
26 | |||
27 | from twisted.internet.error import ConnectionRefusedError |
|
27 | from twisted.internet.error import ConnectionRefusedError | |
28 |
|
28 | |||
29 |
from IPython. |
|
29 | from IPython.core.ultratb import _fixed_getinnerframes, findsource | |
30 | from IPython.core import ipapi |
|
30 | from IPython.core import ipapi | |
31 |
|
31 | |||
32 | from IPython.kernel import error |
|
32 | from IPython.kernel import error | |
33 |
|
33 | |||
34 | #--------------------------------------------------------------------------- |
|
34 | #--------------------------------------------------------------------------- | |
35 | # Utility functions needed by all context managers. |
|
35 | # Utility functions needed by all context managers. | |
36 | #--------------------------------------------------------------------------- |
|
36 | #--------------------------------------------------------------------------- | |
37 |
|
37 | |||
38 | def remote(): |
|
38 | def remote(): | |
39 | """Raises a special exception meant to be caught by context managers. |
|
39 | """Raises a special exception meant to be caught by context managers. | |
40 | """ |
|
40 | """ | |
41 | m = 'Special exception to stop local execution of parallel code.' |
|
41 | m = 'Special exception to stop local execution of parallel code.' | |
42 | raise error.StopLocalExecution(m) |
|
42 | raise error.StopLocalExecution(m) | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | def strip_whitespace(source,require_remote=True): |
|
45 | def strip_whitespace(source,require_remote=True): | |
46 | """strip leading whitespace from input source. |
|
46 | """strip leading whitespace from input source. | |
47 |
|
47 | |||
48 | :Parameters: |
|
48 | :Parameters: | |
49 |
|
49 | |||
50 | """ |
|
50 | """ | |
51 | remote_mark = 'remote()' |
|
51 | remote_mark = 'remote()' | |
52 | # Expand tabs to avoid any confusion. |
|
52 | # Expand tabs to avoid any confusion. | |
53 | wsource = [l.expandtabs(4) for l in source] |
|
53 | wsource = [l.expandtabs(4) for l in source] | |
54 | # Detect the indentation level |
|
54 | # Detect the indentation level | |
55 | done = False |
|
55 | done = False | |
56 | for line in wsource: |
|
56 | for line in wsource: | |
57 | if line.isspace(): |
|
57 | if line.isspace(): | |
58 | continue |
|
58 | continue | |
59 | for col,char in enumerate(line): |
|
59 | for col,char in enumerate(line): | |
60 | if char != ' ': |
|
60 | if char != ' ': | |
61 | done = True |
|
61 | done = True | |
62 | break |
|
62 | break | |
63 | if done: |
|
63 | if done: | |
64 | break |
|
64 | break | |
65 | # Now we know how much leading space there is in the code. Next, we |
|
65 | # Now we know how much leading space there is in the code. Next, we | |
66 | # extract up to the first line that has less indentation. |
|
66 | # extract up to the first line that has less indentation. | |
67 | # WARNINGS: we skip comments that may be misindented, but we do NOT yet |
|
67 | # WARNINGS: we skip comments that may be misindented, but we do NOT yet | |
68 | # detect triple quoted strings that may have flush left text. |
|
68 | # detect triple quoted strings that may have flush left text. | |
69 | for lno,line in enumerate(wsource): |
|
69 | for lno,line in enumerate(wsource): | |
70 | lead = line[:col] |
|
70 | lead = line[:col] | |
71 | if lead.isspace(): |
|
71 | if lead.isspace(): | |
72 | continue |
|
72 | continue | |
73 | else: |
|
73 | else: | |
74 | if not lead.lstrip().startswith('#'): |
|
74 | if not lead.lstrip().startswith('#'): | |
75 | break |
|
75 | break | |
76 | # The real 'with' source is up to lno |
|
76 | # The real 'with' source is up to lno | |
77 | src_lines = [l[col:] for l in wsource[:lno+1]] |
|
77 | src_lines = [l[col:] for l in wsource[:lno+1]] | |
78 |
|
78 | |||
79 | # Finally, check that the source's first non-comment line begins with the |
|
79 | # Finally, check that the source's first non-comment line begins with the | |
80 | # special call 'remote()' |
|
80 | # special call 'remote()' | |
81 | if require_remote: |
|
81 | if require_remote: | |
82 | for nline,line in enumerate(src_lines): |
|
82 | for nline,line in enumerate(src_lines): | |
83 | if line.isspace() or line.startswith('#'): |
|
83 | if line.isspace() or line.startswith('#'): | |
84 | continue |
|
84 | continue | |
85 | if line.startswith(remote_mark): |
|
85 | if line.startswith(remote_mark): | |
86 | break |
|
86 | break | |
87 | else: |
|
87 | else: | |
88 | raise ValueError('%s call missing at the start of code' % |
|
88 | raise ValueError('%s call missing at the start of code' % | |
89 | remote_mark) |
|
89 | remote_mark) | |
90 | out_lines = src_lines[nline+1:] |
|
90 | out_lines = src_lines[nline+1:] | |
91 | else: |
|
91 | else: | |
92 | # If the user specified that the remote() call wasn't mandatory |
|
92 | # If the user specified that the remote() call wasn't mandatory | |
93 | out_lines = src_lines |
|
93 | out_lines = src_lines | |
94 |
|
94 | |||
95 | # src = ''.join(out_lines) # dbg |
|
95 | # src = ''.join(out_lines) # dbg | |
96 | #print 'SRC:\n<<<<<<<>>>>>>>\n%s<<<<<>>>>>>' % src # dbg |
|
96 | #print 'SRC:\n<<<<<<<>>>>>>>\n%s<<<<<>>>>>>' % src # dbg | |
97 | return ''.join(out_lines) |
|
97 | return ''.join(out_lines) | |
98 |
|
98 | |||
99 | class RemoteContextBase(object): |
|
99 | class RemoteContextBase(object): | |
100 | def __init__(self): |
|
100 | def __init__(self): | |
101 | self.ip = ipapi.get() |
|
101 | self.ip = ipapi.get() | |
102 |
|
102 | |||
103 | def _findsource_file(self,f): |
|
103 | def _findsource_file(self,f): | |
104 | linecache.checkcache() |
|
104 | linecache.checkcache() | |
105 | s = findsource(f.f_code) |
|
105 | s = findsource(f.f_code) | |
106 | lnum = f.f_lineno |
|
106 | lnum = f.f_lineno | |
107 | wsource = s[0][f.f_lineno:] |
|
107 | wsource = s[0][f.f_lineno:] | |
108 | return strip_whitespace(wsource) |
|
108 | return strip_whitespace(wsource) | |
109 |
|
109 | |||
110 | def _findsource_ipython(self,f): |
|
110 | def _findsource_ipython(self,f): | |
111 | from IPython.core import ipapi |
|
111 | from IPython.core import ipapi | |
112 | self.ip = ipapi.get() |
|
112 | self.ip = ipapi.get() | |
113 | buf = self.ip.IP.input_hist_raw[-1].splitlines()[1:] |
|
113 | buf = self.ip.IP.input_hist_raw[-1].splitlines()[1:] | |
114 | wsource = [l+'\n' for l in buf ] |
|
114 | wsource = [l+'\n' for l in buf ] | |
115 |
|
115 | |||
116 | return strip_whitespace(wsource) |
|
116 | return strip_whitespace(wsource) | |
117 |
|
117 | |||
118 | def findsource(self,frame): |
|
118 | def findsource(self,frame): | |
119 | local_ns = frame.f_locals |
|
119 | local_ns = frame.f_locals | |
120 | global_ns = frame.f_globals |
|
120 | global_ns = frame.f_globals | |
121 | if frame.f_code.co_filename == '<ipython console>': |
|
121 | if frame.f_code.co_filename == '<ipython console>': | |
122 | src = self._findsource_ipython(frame) |
|
122 | src = self._findsource_ipython(frame) | |
123 | else: |
|
123 | else: | |
124 | src = self._findsource_file(frame) |
|
124 | src = self._findsource_file(frame) | |
125 | return src |
|
125 | return src | |
126 |
|
126 | |||
127 | def __enter__(self): |
|
127 | def __enter__(self): | |
128 | raise NotImplementedError |
|
128 | raise NotImplementedError | |
129 |
|
129 | |||
130 | def __exit__ (self, etype, value, tb): |
|
130 | def __exit__ (self, etype, value, tb): | |
131 | if issubclass(etype,error.StopLocalExecution): |
|
131 | if issubclass(etype,error.StopLocalExecution): | |
132 | return True |
|
132 | return True | |
133 |
|
133 | |||
134 | class RemoteMultiEngine(RemoteContextBase): |
|
134 | class RemoteMultiEngine(RemoteContextBase): | |
135 | def __init__(self,mec): |
|
135 | def __init__(self,mec): | |
136 | self.mec = mec |
|
136 | self.mec = mec | |
137 | RemoteContextBase.__init__(self) |
|
137 | RemoteContextBase.__init__(self) | |
138 |
|
138 | |||
139 | def __enter__(self): |
|
139 | def __enter__(self): | |
140 | src = self.findsource(sys._getframe(1)) |
|
140 | src = self.findsource(sys._getframe(1)) | |
141 | return self.mec.execute(src) |
|
141 | return self.mec.execute(src) |
@@ -1,761 +1,761 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 |
|
2 | |||
3 | """Central interpreter object for an IPython engine. |
|
3 | """Central interpreter object for an IPython engine. | |
4 |
|
4 | |||
5 | The interpreter is the object whose job is to process lines of user input and |
|
5 | The interpreter is the object whose job is to process lines of user input and | |
6 | actually execute them in the user's namespace. |
|
6 | actually execute them in the user's namespace. | |
7 | """ |
|
7 | """ | |
8 |
|
8 | |||
9 | __docformat__ = "restructuredtext en" |
|
9 | __docformat__ = "restructuredtext en" | |
10 |
|
10 | |||
11 | #------------------------------------------------------------------------------- |
|
11 | #------------------------------------------------------------------------------- | |
12 | # Copyright (C) 2008 The IPython Development Team |
|
12 | # Copyright (C) 2008 The IPython Development Team | |
13 | # |
|
13 | # | |
14 | # Distributed under the terms of the BSD License. The full license is in |
|
14 | # Distributed under the terms of the BSD License. The full license is in | |
15 | # the file COPYING, distributed as part of this software. |
|
15 | # the file COPYING, distributed as part of this software. | |
16 | #------------------------------------------------------------------------------- |
|
16 | #------------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | #------------------------------------------------------------------------------- |
|
18 | #------------------------------------------------------------------------------- | |
19 | # Imports |
|
19 | # Imports | |
20 | #------------------------------------------------------------------------------- |
|
20 | #------------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | # Standard library imports. |
|
22 | # Standard library imports. | |
23 | from types import FunctionType |
|
23 | from types import FunctionType | |
24 |
|
24 | |||
25 | import __builtin__ |
|
25 | import __builtin__ | |
26 | import codeop |
|
26 | import codeop | |
27 | import compiler |
|
27 | import compiler | |
28 | import sys |
|
28 | import sys | |
29 | import traceback |
|
29 | import traceback | |
30 |
|
30 | |||
31 | # Local imports. |
|
31 | # Local imports. | |
32 |
from IPython. |
|
32 | from IPython.core import ultratb | |
33 | from IPython.kernel.core.display_trap import DisplayTrap |
|
33 | from IPython.kernel.core.display_trap import DisplayTrap | |
34 | from IPython.kernel.core.macro import Macro |
|
34 | from IPython.kernel.core.macro import Macro | |
35 | from IPython.kernel.core.prompts import CachedOutput |
|
35 | from IPython.kernel.core.prompts import CachedOutput | |
36 | from IPython.kernel.core.traceback_trap import TracebackTrap |
|
36 | from IPython.kernel.core.traceback_trap import TracebackTrap | |
37 | from IPython.kernel.core.util import Bunch, system_shell |
|
37 | from IPython.kernel.core.util import Bunch, system_shell | |
38 | from IPython.external.Itpl import ItplNS |
|
38 | from IPython.external.Itpl import ItplNS | |
39 |
|
39 | |||
40 | # Global constants |
|
40 | # Global constants | |
41 | COMPILER_ERROR = 'error' |
|
41 | COMPILER_ERROR = 'error' | |
42 | INCOMPLETE_INPUT = 'incomplete' |
|
42 | INCOMPLETE_INPUT = 'incomplete' | |
43 | COMPLETE_INPUT = 'complete' |
|
43 | COMPLETE_INPUT = 'complete' | |
44 |
|
44 | |||
45 | ############################################################################## |
|
45 | ############################################################################## | |
46 | # TEMPORARY!!! fake configuration, while we decide whether to use tconfig or |
|
46 | # TEMPORARY!!! fake configuration, while we decide whether to use tconfig or | |
47 | # not |
|
47 | # not | |
48 |
|
48 | |||
49 | rc = Bunch() |
|
49 | rc = Bunch() | |
50 | rc.cache_size = 100 |
|
50 | rc.cache_size = 100 | |
51 | rc.pprint = True |
|
51 | rc.pprint = True | |
52 | rc.separate_in = '\n' |
|
52 | rc.separate_in = '\n' | |
53 | rc.separate_out = '\n' |
|
53 | rc.separate_out = '\n' | |
54 | rc.separate_out2 = '' |
|
54 | rc.separate_out2 = '' | |
55 | rc.prompt_in1 = r'In [\#]: ' |
|
55 | rc.prompt_in1 = r'In [\#]: ' | |
56 | rc.prompt_in2 = r' .\\D.: ' |
|
56 | rc.prompt_in2 = r' .\\D.: ' | |
57 | rc.prompt_out = '' |
|
57 | rc.prompt_out = '' | |
58 | rc.prompts_pad_left = False |
|
58 | rc.prompts_pad_left = False | |
59 |
|
59 | |||
60 | ############################################################################## |
|
60 | ############################################################################## | |
61 |
|
61 | |||
62 | # Top-level utilities |
|
62 | # Top-level utilities | |
63 | def default_display_formatters(): |
|
63 | def default_display_formatters(): | |
64 | """ Return a list of default display formatters. |
|
64 | """ Return a list of default display formatters. | |
65 | """ |
|
65 | """ | |
66 |
|
66 | |||
67 | from display_formatter import PPrintDisplayFormatter, ReprDisplayFormatter |
|
67 | from display_formatter import PPrintDisplayFormatter, ReprDisplayFormatter | |
68 | return [PPrintDisplayFormatter(), ReprDisplayFormatter()] |
|
68 | return [PPrintDisplayFormatter(), ReprDisplayFormatter()] | |
69 |
|
69 | |||
70 | def default_traceback_formatters(): |
|
70 | def default_traceback_formatters(): | |
71 | """ Return a list of default traceback formatters. |
|
71 | """ Return a list of default traceback formatters. | |
72 | """ |
|
72 | """ | |
73 |
|
73 | |||
74 | from traceback_formatter import PlainTracebackFormatter |
|
74 | from traceback_formatter import PlainTracebackFormatter | |
75 | return [PlainTracebackFormatter()] |
|
75 | return [PlainTracebackFormatter()] | |
76 |
|
76 | |||
77 | # Top-level classes |
|
77 | # Top-level classes | |
78 | class NotDefined(object): pass |
|
78 | class NotDefined(object): pass | |
79 |
|
79 | |||
80 | class Interpreter(object): |
|
80 | class Interpreter(object): | |
81 | """ An interpreter object. |
|
81 | """ An interpreter object. | |
82 |
|
82 | |||
83 | fixme: needs to negotiate available formatters with frontends. |
|
83 | fixme: needs to negotiate available formatters with frontends. | |
84 |
|
84 | |||
85 | Important: the interpeter should be built so that it exposes a method |
|
85 | Important: the interpeter should be built so that it exposes a method | |
86 | for each attribute/method of its sub-object. This way it can be |
|
86 | for each attribute/method of its sub-object. This way it can be | |
87 | replaced by a network adapter. |
|
87 | replaced by a network adapter. | |
88 | """ |
|
88 | """ | |
89 |
|
89 | |||
90 | def __init__(self, user_ns=None, global_ns=None,translator=None, |
|
90 | def __init__(self, user_ns=None, global_ns=None,translator=None, | |
91 | magic=None, display_formatters=None, |
|
91 | magic=None, display_formatters=None, | |
92 | traceback_formatters=None, output_trap=None, history=None, |
|
92 | traceback_formatters=None, output_trap=None, history=None, | |
93 | message_cache=None, filename='<string>', config=None): |
|
93 | message_cache=None, filename='<string>', config=None): | |
94 |
|
94 | |||
95 | # The local/global namespaces for code execution |
|
95 | # The local/global namespaces for code execution | |
96 | local_ns = user_ns # compatibility name |
|
96 | local_ns = user_ns # compatibility name | |
97 | if local_ns is None: |
|
97 | if local_ns is None: | |
98 | local_ns = {} |
|
98 | local_ns = {} | |
99 | self.user_ns = local_ns |
|
99 | self.user_ns = local_ns | |
100 | # The local namespace |
|
100 | # The local namespace | |
101 | if global_ns is None: |
|
101 | if global_ns is None: | |
102 | global_ns = {} |
|
102 | global_ns = {} | |
103 | self.user_global_ns = global_ns |
|
103 | self.user_global_ns = global_ns | |
104 |
|
104 | |||
105 | # An object that will translate commands into executable Python. |
|
105 | # An object that will translate commands into executable Python. | |
106 | # The current translator does not work properly so for now we are going |
|
106 | # The current translator does not work properly so for now we are going | |
107 | # without! |
|
107 | # without! | |
108 | # if translator is None: |
|
108 | # if translator is None: | |
109 | # from IPython.kernel.core.translator import IPythonTranslator |
|
109 | # from IPython.kernel.core.translator import IPythonTranslator | |
110 | # translator = IPythonTranslator() |
|
110 | # translator = IPythonTranslator() | |
111 | self.translator = translator |
|
111 | self.translator = translator | |
112 |
|
112 | |||
113 | # An object that maintains magic commands. |
|
113 | # An object that maintains magic commands. | |
114 | if magic is None: |
|
114 | if magic is None: | |
115 | from IPython.kernel.core.magic import Magic |
|
115 | from IPython.kernel.core.magic import Magic | |
116 | magic = Magic(self) |
|
116 | magic = Magic(self) | |
117 | self.magic = magic |
|
117 | self.magic = magic | |
118 |
|
118 | |||
119 | # A list of formatters for the displayhook. |
|
119 | # A list of formatters for the displayhook. | |
120 | if display_formatters is None: |
|
120 | if display_formatters is None: | |
121 | display_formatters = default_display_formatters() |
|
121 | display_formatters = default_display_formatters() | |
122 | self.display_formatters = display_formatters |
|
122 | self.display_formatters = display_formatters | |
123 |
|
123 | |||
124 | # A list of formatters for tracebacks. |
|
124 | # A list of formatters for tracebacks. | |
125 | if traceback_formatters is None: |
|
125 | if traceback_formatters is None: | |
126 | traceback_formatters = default_traceback_formatters() |
|
126 | traceback_formatters = default_traceback_formatters() | |
127 | self.traceback_formatters = traceback_formatters |
|
127 | self.traceback_formatters = traceback_formatters | |
128 |
|
128 | |||
129 | # The object trapping stdout/stderr. |
|
129 | # The object trapping stdout/stderr. | |
130 | if output_trap is None: |
|
130 | if output_trap is None: | |
131 | from IPython.kernel.core.output_trap import OutputTrap |
|
131 | from IPython.kernel.core.output_trap import OutputTrap | |
132 | output_trap = OutputTrap() |
|
132 | output_trap = OutputTrap() | |
133 | self.output_trap = output_trap |
|
133 | self.output_trap = output_trap | |
134 |
|
134 | |||
135 | # An object that manages the history. |
|
135 | # An object that manages the history. | |
136 | if history is None: |
|
136 | if history is None: | |
137 | from IPython.kernel.core.history import InterpreterHistory |
|
137 | from IPython.kernel.core.history import InterpreterHistory | |
138 | history = InterpreterHistory() |
|
138 | history = InterpreterHistory() | |
139 | self.history = history |
|
139 | self.history = history | |
140 | self.get_history_item = history.get_history_item |
|
140 | self.get_history_item = history.get_history_item | |
141 | self.get_history_input_cache = history.get_input_cache |
|
141 | self.get_history_input_cache = history.get_input_cache | |
142 | self.get_history_input_after = history.get_input_after |
|
142 | self.get_history_input_after = history.get_input_after | |
143 |
|
143 | |||
144 | # An object that caches all of the return messages. |
|
144 | # An object that caches all of the return messages. | |
145 | if message_cache is None: |
|
145 | if message_cache is None: | |
146 | from IPython.kernel.core.message_cache import SimpleMessageCache |
|
146 | from IPython.kernel.core.message_cache import SimpleMessageCache | |
147 | message_cache = SimpleMessageCache() |
|
147 | message_cache = SimpleMessageCache() | |
148 | self.message_cache = message_cache |
|
148 | self.message_cache = message_cache | |
149 |
|
149 | |||
150 | # The "filename" of the code that is executed in this interpreter. |
|
150 | # The "filename" of the code that is executed in this interpreter. | |
151 | self.filename = filename |
|
151 | self.filename = filename | |
152 |
|
152 | |||
153 | # An object that contains much configuration information. |
|
153 | # An object that contains much configuration information. | |
154 | if config is None: |
|
154 | if config is None: | |
155 | # fixme: Move this constant elsewhere! |
|
155 | # fixme: Move this constant elsewhere! | |
156 | config = Bunch(ESC_MAGIC='%') |
|
156 | config = Bunch(ESC_MAGIC='%') | |
157 | self.config = config |
|
157 | self.config = config | |
158 |
|
158 | |||
159 | # Hook managers. |
|
159 | # Hook managers. | |
160 | # fixme: make the display callbacks configurable. In the meantime, |
|
160 | # fixme: make the display callbacks configurable. In the meantime, | |
161 | # enable macros. |
|
161 | # enable macros. | |
162 | self.display_trap = DisplayTrap( |
|
162 | self.display_trap = DisplayTrap( | |
163 | formatters=self.display_formatters, |
|
163 | formatters=self.display_formatters, | |
164 | callbacks=[self._possible_macro], |
|
164 | callbacks=[self._possible_macro], | |
165 | ) |
|
165 | ) | |
166 | self.traceback_trap = TracebackTrap( |
|
166 | self.traceback_trap = TracebackTrap( | |
167 | formatters=self.traceback_formatters) |
|
167 | formatters=self.traceback_formatters) | |
168 |
|
168 | |||
169 | # This is used temporarily for reformating exceptions in certain |
|
169 | # This is used temporarily for reformating exceptions in certain | |
170 |
# cases. It will go away once the ultra |
|
170 | # cases. It will go away once the ultratb stuff is ported | |
171 | # to ipython1 |
|
171 | # to ipython1 | |
172 |
self.tbHandler = ultra |
|
172 | self.tbHandler = ultratb.FormattedTB(color_scheme='NoColor', | |
173 | mode='Context', |
|
173 | mode='Context', | |
174 | tb_offset=2) |
|
174 | tb_offset=2) | |
175 |
|
175 | |||
176 | # An object that can compile commands and remember __future__ |
|
176 | # An object that can compile commands and remember __future__ | |
177 | # statements. |
|
177 | # statements. | |
178 | self.command_compiler = codeop.CommandCompiler() |
|
178 | self.command_compiler = codeop.CommandCompiler() | |
179 |
|
179 | |||
180 | # A replacement for the raw_input() and input() builtins. Change these |
|
180 | # A replacement for the raw_input() and input() builtins. Change these | |
181 | # attributes later to configure them. |
|
181 | # attributes later to configure them. | |
182 | self.raw_input_builtin = raw_input |
|
182 | self.raw_input_builtin = raw_input | |
183 | self.input_builtin = input |
|
183 | self.input_builtin = input | |
184 |
|
184 | |||
185 | # The number of the current cell. |
|
185 | # The number of the current cell. | |
186 | self.current_cell_number = 1 |
|
186 | self.current_cell_number = 1 | |
187 |
|
187 | |||
188 | # Initialize cache, set in/out prompts and printing system |
|
188 | # Initialize cache, set in/out prompts and printing system | |
189 | self.outputcache = CachedOutput(self, |
|
189 | self.outputcache = CachedOutput(self, | |
190 | rc.cache_size, |
|
190 | rc.cache_size, | |
191 | rc.pprint, |
|
191 | rc.pprint, | |
192 | input_sep = rc.separate_in, |
|
192 | input_sep = rc.separate_in, | |
193 | output_sep = rc.separate_out, |
|
193 | output_sep = rc.separate_out, | |
194 | output_sep2 = rc.separate_out2, |
|
194 | output_sep2 = rc.separate_out2, | |
195 | ps1 = rc.prompt_in1, |
|
195 | ps1 = rc.prompt_in1, | |
196 | ps2 = rc.prompt_in2, |
|
196 | ps2 = rc.prompt_in2, | |
197 | ps_out = rc.prompt_out, |
|
197 | ps_out = rc.prompt_out, | |
198 | pad_left = rc.prompts_pad_left) |
|
198 | pad_left = rc.prompts_pad_left) | |
199 |
|
199 | |||
200 | # Need to decide later if this is the right approach, but clients |
|
200 | # Need to decide later if this is the right approach, but clients | |
201 | # commonly use sys.ps1/2, so it may be best to just set them here |
|
201 | # commonly use sys.ps1/2, so it may be best to just set them here | |
202 | sys.ps1 = self.outputcache.prompt1.p_str |
|
202 | sys.ps1 = self.outputcache.prompt1.p_str | |
203 | sys.ps2 = self.outputcache.prompt2.p_str |
|
203 | sys.ps2 = self.outputcache.prompt2.p_str | |
204 |
|
204 | |||
205 | # This is the message dictionary assigned temporarily when running the |
|
205 | # This is the message dictionary assigned temporarily when running the | |
206 | # code. |
|
206 | # code. | |
207 | self.message = None |
|
207 | self.message = None | |
208 |
|
208 | |||
209 | self.setup_namespace() |
|
209 | self.setup_namespace() | |
210 |
|
210 | |||
211 |
|
211 | |||
212 | #### Public 'Interpreter' interface ######################################## |
|
212 | #### Public 'Interpreter' interface ######################################## | |
213 |
|
213 | |||
214 | def formatTraceback(self, et, ev, tb, message=''): |
|
214 | def formatTraceback(self, et, ev, tb, message=''): | |
215 | """Put a formatted version of the traceback into value and reraise. |
|
215 | """Put a formatted version of the traceback into value and reraise. | |
216 |
|
216 | |||
217 | When exceptions have to be sent over the network, the traceback |
|
217 | When exceptions have to be sent over the network, the traceback | |
218 | needs to be put into the value of the exception in a nicely |
|
218 | needs to be put into the value of the exception in a nicely | |
219 | formatted way. The method takes the type, value and tb of an |
|
219 | formatted way. The method takes the type, value and tb of an | |
220 | exception and puts a string representation of the tb into the |
|
220 | exception and puts a string representation of the tb into the | |
221 | value of the exception and reraises it. |
|
221 | value of the exception and reraises it. | |
222 |
|
222 | |||
223 | Currently this method uses the ultraTb formatter from IPython trunk. |
|
223 | Currently this method uses the ultraTb formatter from IPython trunk. | |
224 | Eventually it should simply use the traceback formatters in core |
|
224 | Eventually it should simply use the traceback formatters in core | |
225 | that are loaded into self.tracback_trap.formatters. |
|
225 | that are loaded into self.tracback_trap.formatters. | |
226 | """ |
|
226 | """ | |
227 | tbinfo = self.tbHandler.text(et,ev,tb) |
|
227 | tbinfo = self.tbHandler.text(et,ev,tb) | |
228 | ev._ipython_traceback_text = tbinfo |
|
228 | ev._ipython_traceback_text = tbinfo | |
229 | return et, ev, tb |
|
229 | return et, ev, tb | |
230 |
|
230 | |||
231 | def execute(self, commands, raiseException=True): |
|
231 | def execute(self, commands, raiseException=True): | |
232 | """ Execute some IPython commands. |
|
232 | """ Execute some IPython commands. | |
233 |
|
233 | |||
234 | 1. Translate them into Python. |
|
234 | 1. Translate them into Python. | |
235 | 2. Run them. |
|
235 | 2. Run them. | |
236 | 3. Trap stdout/stderr. |
|
236 | 3. Trap stdout/stderr. | |
237 | 4. Trap sys.displayhook(). |
|
237 | 4. Trap sys.displayhook(). | |
238 | 5. Trap exceptions. |
|
238 | 5. Trap exceptions. | |
239 | 6. Return a message object. |
|
239 | 6. Return a message object. | |
240 |
|
240 | |||
241 | Parameters |
|
241 | Parameters | |
242 | ---------- |
|
242 | ---------- | |
243 | commands : str |
|
243 | commands : str | |
244 | The raw commands that the user typed into the prompt. |
|
244 | The raw commands that the user typed into the prompt. | |
245 |
|
245 | |||
246 | Returns |
|
246 | Returns | |
247 | ------- |
|
247 | ------- | |
248 | message : dict |
|
248 | message : dict | |
249 | The dictionary of responses. See the README.txt in this directory |
|
249 | The dictionary of responses. See the README.txt in this directory | |
250 | for an explanation of the format. |
|
250 | for an explanation of the format. | |
251 | """ |
|
251 | """ | |
252 |
|
252 | |||
253 | # Create a message dictionary with all of the information we will be |
|
253 | # Create a message dictionary with all of the information we will be | |
254 | # returning to the frontend and other listeners. |
|
254 | # returning to the frontend and other listeners. | |
255 | message = self.setup_message() |
|
255 | message = self.setup_message() | |
256 |
|
256 | |||
257 | # Massage the input and store the raw and translated commands into |
|
257 | # Massage the input and store the raw and translated commands into | |
258 | # a dict. |
|
258 | # a dict. | |
259 | user_input = dict(raw=commands) |
|
259 | user_input = dict(raw=commands) | |
260 | if self.translator is not None: |
|
260 | if self.translator is not None: | |
261 | python = self.translator(commands, message) |
|
261 | python = self.translator(commands, message) | |
262 | if python is None: |
|
262 | if python is None: | |
263 | # Something went wrong with the translation. The translator |
|
263 | # Something went wrong with the translation. The translator | |
264 | # should have added an appropriate entry to the message object. |
|
264 | # should have added an appropriate entry to the message object. | |
265 | return message |
|
265 | return message | |
266 | else: |
|
266 | else: | |
267 | python = commands |
|
267 | python = commands | |
268 | user_input['translated'] = python |
|
268 | user_input['translated'] = python | |
269 | message['input'] = user_input |
|
269 | message['input'] = user_input | |
270 |
|
270 | |||
271 | # Set the message object so that any magics executed in the code have |
|
271 | # Set the message object so that any magics executed in the code have | |
272 | # access. |
|
272 | # access. | |
273 | self.message = message |
|
273 | self.message = message | |
274 |
|
274 | |||
275 | # Set all of the output/exception traps. |
|
275 | # Set all of the output/exception traps. | |
276 | self.set_traps() |
|
276 | self.set_traps() | |
277 |
|
277 | |||
278 | # Actually execute the Python code. |
|
278 | # Actually execute the Python code. | |
279 | status = self.execute_python(python) |
|
279 | status = self.execute_python(python) | |
280 |
|
280 | |||
281 | # Unset all of the traps. |
|
281 | # Unset all of the traps. | |
282 | self.unset_traps() |
|
282 | self.unset_traps() | |
283 |
|
283 | |||
284 | # Unset the message object. |
|
284 | # Unset the message object. | |
285 | self.message = None |
|
285 | self.message = None | |
286 |
|
286 | |||
287 | # Update the history variables in the namespace. |
|
287 | # Update the history variables in the namespace. | |
288 | # E.g. In, Out, _, __, ___ |
|
288 | # E.g. In, Out, _, __, ___ | |
289 | if self.history is not None: |
|
289 | if self.history is not None: | |
290 | self.history.update_history(self, python) |
|
290 | self.history.update_history(self, python) | |
291 |
|
291 | |||
292 | # Let all of the traps contribute to the message and then clear their |
|
292 | # Let all of the traps contribute to the message and then clear their | |
293 | # stored information. |
|
293 | # stored information. | |
294 | self.output_trap.add_to_message(message) |
|
294 | self.output_trap.add_to_message(message) | |
295 | self.output_trap.clear() |
|
295 | self.output_trap.clear() | |
296 | self.display_trap.add_to_message(message) |
|
296 | self.display_trap.add_to_message(message) | |
297 | self.display_trap.clear() |
|
297 | self.display_trap.clear() | |
298 | self.traceback_trap.add_to_message(message) |
|
298 | self.traceback_trap.add_to_message(message) | |
299 | # Pull out the type, value and tb of the current exception |
|
299 | # Pull out the type, value and tb of the current exception | |
300 | # before clearing it. |
|
300 | # before clearing it. | |
301 | einfo = self.traceback_trap.args |
|
301 | einfo = self.traceback_trap.args | |
302 | self.traceback_trap.clear() |
|
302 | self.traceback_trap.clear() | |
303 |
|
303 | |||
304 | # Cache the message. |
|
304 | # Cache the message. | |
305 | self.message_cache.add_message(self.current_cell_number, message) |
|
305 | self.message_cache.add_message(self.current_cell_number, message) | |
306 |
|
306 | |||
307 | # Bump the number. |
|
307 | # Bump the number. | |
308 | self.current_cell_number += 1 |
|
308 | self.current_cell_number += 1 | |
309 |
|
309 | |||
310 | # This conditional lets the execute method either raise any |
|
310 | # This conditional lets the execute method either raise any | |
311 | # exception that has occured in user code OR return the message |
|
311 | # exception that has occured in user code OR return the message | |
312 | # dict containing the traceback and other useful info. |
|
312 | # dict containing the traceback and other useful info. | |
313 | if raiseException and einfo: |
|
313 | if raiseException and einfo: | |
314 | raise einfo[0],einfo[1],einfo[2] |
|
314 | raise einfo[0],einfo[1],einfo[2] | |
315 | else: |
|
315 | else: | |
316 | return message |
|
316 | return message | |
317 |
|
317 | |||
318 | def generate_prompt(self, is_continuation): |
|
318 | def generate_prompt(self, is_continuation): | |
319 | """Calculate and return a string with the prompt to display. |
|
319 | """Calculate and return a string with the prompt to display. | |
320 |
|
320 | |||
321 | :Parameters: |
|
321 | :Parameters: | |
322 | is_continuation : bool |
|
322 | is_continuation : bool | |
323 | Whether the input line is continuing multiline input or not, so |
|
323 | Whether the input line is continuing multiline input or not, so | |
324 | that a proper continuation prompt can be computed.""" |
|
324 | that a proper continuation prompt can be computed.""" | |
325 |
|
325 | |||
326 | if is_continuation: |
|
326 | if is_continuation: | |
327 | return str(self.outputcache.prompt2) |
|
327 | return str(self.outputcache.prompt2) | |
328 | else: |
|
328 | else: | |
329 | return str(self.outputcache.prompt1) |
|
329 | return str(self.outputcache.prompt1) | |
330 |
|
330 | |||
331 | def execute_python(self, python): |
|
331 | def execute_python(self, python): | |
332 | """ Actually run the Python code in the namespace. |
|
332 | """ Actually run the Python code in the namespace. | |
333 |
|
333 | |||
334 | :Parameters: |
|
334 | :Parameters: | |
335 |
|
335 | |||
336 | python : str |
|
336 | python : str | |
337 | Pure, exec'able Python code. Special IPython commands should have |
|
337 | Pure, exec'able Python code. Special IPython commands should have | |
338 | already been translated into pure Python. |
|
338 | already been translated into pure Python. | |
339 | """ |
|
339 | """ | |
340 |
|
340 | |||
341 | # We use a CommandCompiler instance to compile the code so as to keep |
|
341 | # We use a CommandCompiler instance to compile the code so as to keep | |
342 | # track of __future__ imports. |
|
342 | # track of __future__ imports. | |
343 | try: |
|
343 | try: | |
344 | commands = self.split_commands(python) |
|
344 | commands = self.split_commands(python) | |
345 | except (SyntaxError, IndentationError), e: |
|
345 | except (SyntaxError, IndentationError), e: | |
346 | # Save the exc_info so compilation related exceptions can be |
|
346 | # Save the exc_info so compilation related exceptions can be | |
347 | # reraised |
|
347 | # reraised | |
348 | self.traceback_trap.args = sys.exc_info() |
|
348 | self.traceback_trap.args = sys.exc_info() | |
349 | self.pack_exception(self.message,e) |
|
349 | self.pack_exception(self.message,e) | |
350 | return None |
|
350 | return None | |
351 |
|
351 | |||
352 | for cmd in commands: |
|
352 | for cmd in commands: | |
353 | try: |
|
353 | try: | |
354 | code = self.command_compiler(cmd, self.filename, 'single') |
|
354 | code = self.command_compiler(cmd, self.filename, 'single') | |
355 | except (SyntaxError, OverflowError, ValueError), e: |
|
355 | except (SyntaxError, OverflowError, ValueError), e: | |
356 | self.traceback_trap.args = sys.exc_info() |
|
356 | self.traceback_trap.args = sys.exc_info() | |
357 | self.pack_exception(self.message,e) |
|
357 | self.pack_exception(self.message,e) | |
358 | # No point in continuing if one block raised |
|
358 | # No point in continuing if one block raised | |
359 | return None |
|
359 | return None | |
360 | else: |
|
360 | else: | |
361 | self.execute_block(code) |
|
361 | self.execute_block(code) | |
362 |
|
362 | |||
363 | def execute_block(self,code): |
|
363 | def execute_block(self,code): | |
364 | """Execute a single block of code in the user namespace. |
|
364 | """Execute a single block of code in the user namespace. | |
365 |
|
365 | |||
366 | Return value: a flag indicating whether the code to be run completed |
|
366 | Return value: a flag indicating whether the code to be run completed | |
367 | successfully: |
|
367 | successfully: | |
368 |
|
368 | |||
369 | - 0: successful execution. |
|
369 | - 0: successful execution. | |
370 | - 1: an error occurred. |
|
370 | - 1: an error occurred. | |
371 | """ |
|
371 | """ | |
372 |
|
372 | |||
373 | outflag = 1 # start by assuming error, success will reset it |
|
373 | outflag = 1 # start by assuming error, success will reset it | |
374 | try: |
|
374 | try: | |
375 | exec code in self.user_ns |
|
375 | exec code in self.user_ns | |
376 | outflag = 0 |
|
376 | outflag = 0 | |
377 | except SystemExit: |
|
377 | except SystemExit: | |
378 | self.resetbuffer() |
|
378 | self.resetbuffer() | |
379 | self.traceback_trap.args = sys.exc_info() |
|
379 | self.traceback_trap.args = sys.exc_info() | |
380 | except: |
|
380 | except: | |
381 | self.traceback_trap.args = sys.exc_info() |
|
381 | self.traceback_trap.args = sys.exc_info() | |
382 |
|
382 | |||
383 | return outflag |
|
383 | return outflag | |
384 |
|
384 | |||
385 | def execute_macro(self, macro): |
|
385 | def execute_macro(self, macro): | |
386 | """ Execute the value of a macro. |
|
386 | """ Execute the value of a macro. | |
387 |
|
387 | |||
388 | Parameters |
|
388 | Parameters | |
389 | ---------- |
|
389 | ---------- | |
390 | macro : Macro |
|
390 | macro : Macro | |
391 | """ |
|
391 | """ | |
392 |
|
392 | |||
393 | python = macro.value |
|
393 | python = macro.value | |
394 | if self.translator is not None: |
|
394 | if self.translator is not None: | |
395 | python = self.translator(python) |
|
395 | python = self.translator(python) | |
396 | self.execute_python(python) |
|
396 | self.execute_python(python) | |
397 |
|
397 | |||
398 | def getCommand(self, i=None): |
|
398 | def getCommand(self, i=None): | |
399 | """Gets the ith message in the message_cache. |
|
399 | """Gets the ith message in the message_cache. | |
400 |
|
400 | |||
401 | This is implemented here for compatibility with the old ipython1 shell |
|
401 | This is implemented here for compatibility with the old ipython1 shell | |
402 | I am not sure we need this though. I even seem to remember that we |
|
402 | I am not sure we need this though. I even seem to remember that we | |
403 | were going to get rid of it. |
|
403 | were going to get rid of it. | |
404 | """ |
|
404 | """ | |
405 | return self.message_cache.get_message(i) |
|
405 | return self.message_cache.get_message(i) | |
406 |
|
406 | |||
407 | def reset(self): |
|
407 | def reset(self): | |
408 | """Reset the interpreter. |
|
408 | """Reset the interpreter. | |
409 |
|
409 | |||
410 | Currently this only resets the users variables in the namespace. |
|
410 | Currently this only resets the users variables in the namespace. | |
411 | In the future we might want to also reset the other stateful |
|
411 | In the future we might want to also reset the other stateful | |
412 | things like that the Interpreter has, like In, Out, etc. |
|
412 | things like that the Interpreter has, like In, Out, etc. | |
413 | """ |
|
413 | """ | |
414 | self.user_ns.clear() |
|
414 | self.user_ns.clear() | |
415 | self.setup_namespace() |
|
415 | self.setup_namespace() | |
416 |
|
416 | |||
417 | def complete(self,line,text=None, pos=None): |
|
417 | def complete(self,line,text=None, pos=None): | |
418 | """Complete the given text. |
|
418 | """Complete the given text. | |
419 |
|
419 | |||
420 | :Parameters: |
|
420 | :Parameters: | |
421 |
|
421 | |||
422 | text : str |
|
422 | text : str | |
423 | Text fragment to be completed on. Typically this is |
|
423 | Text fragment to be completed on. Typically this is | |
424 | """ |
|
424 | """ | |
425 | # fixme: implement |
|
425 | # fixme: implement | |
426 | raise NotImplementedError |
|
426 | raise NotImplementedError | |
427 |
|
427 | |||
428 | def push(self, ns): |
|
428 | def push(self, ns): | |
429 | """ Put value into the namespace with name key. |
|
429 | """ Put value into the namespace with name key. | |
430 |
|
430 | |||
431 | Parameters |
|
431 | Parameters | |
432 | ---------- |
|
432 | ---------- | |
433 | **kwds |
|
433 | **kwds | |
434 | """ |
|
434 | """ | |
435 |
|
435 | |||
436 | self.user_ns.update(ns) |
|
436 | self.user_ns.update(ns) | |
437 |
|
437 | |||
438 | def push_function(self, ns): |
|
438 | def push_function(self, ns): | |
439 | # First set the func_globals for all functions to self.user_ns |
|
439 | # First set the func_globals for all functions to self.user_ns | |
440 | new_kwds = {} |
|
440 | new_kwds = {} | |
441 | for k, v in ns.iteritems(): |
|
441 | for k, v in ns.iteritems(): | |
442 | if not isinstance(v, FunctionType): |
|
442 | if not isinstance(v, FunctionType): | |
443 | raise TypeError("function object expected") |
|
443 | raise TypeError("function object expected") | |
444 | new_kwds[k] = FunctionType(v.func_code, self.user_ns) |
|
444 | new_kwds[k] = FunctionType(v.func_code, self.user_ns) | |
445 | self.user_ns.update(new_kwds) |
|
445 | self.user_ns.update(new_kwds) | |
446 |
|
446 | |||
447 | def pack_exception(self,message,exc): |
|
447 | def pack_exception(self,message,exc): | |
448 | message['exception'] = exc.__class__ |
|
448 | message['exception'] = exc.__class__ | |
449 | message['exception_value'] = \ |
|
449 | message['exception_value'] = \ | |
450 | traceback.format_exception_only(exc.__class__, exc) |
|
450 | traceback.format_exception_only(exc.__class__, exc) | |
451 |
|
451 | |||
452 | def feed_block(self, source, filename='<input>', symbol='single'): |
|
452 | def feed_block(self, source, filename='<input>', symbol='single'): | |
453 | """Compile some source in the interpreter. |
|
453 | """Compile some source in the interpreter. | |
454 |
|
454 | |||
455 | One several things can happen: |
|
455 | One several things can happen: | |
456 |
|
456 | |||
457 | 1) The input is incorrect; compile_command() raised an |
|
457 | 1) The input is incorrect; compile_command() raised an | |
458 | exception (SyntaxError or OverflowError). |
|
458 | exception (SyntaxError or OverflowError). | |
459 |
|
459 | |||
460 | 2) The input is incomplete, and more input is required; |
|
460 | 2) The input is incomplete, and more input is required; | |
461 | compile_command() returned None. Nothing happens. |
|
461 | compile_command() returned None. Nothing happens. | |
462 |
|
462 | |||
463 | 3) The input is complete; compile_command() returned a code |
|
463 | 3) The input is complete; compile_command() returned a code | |
464 | object. The code is executed by calling self.runcode() (which |
|
464 | object. The code is executed by calling self.runcode() (which | |
465 | also handles run-time exceptions, except for SystemExit). |
|
465 | also handles run-time exceptions, except for SystemExit). | |
466 |
|
466 | |||
467 | The return value is: |
|
467 | The return value is: | |
468 |
|
468 | |||
469 | - True in case 2 |
|
469 | - True in case 2 | |
470 |
|
470 | |||
471 | - False in the other cases, unless an exception is raised, where |
|
471 | - False in the other cases, unless an exception is raised, where | |
472 | None is returned instead. This can be used by external callers to |
|
472 | None is returned instead. This can be used by external callers to | |
473 | know whether to continue feeding input or not. |
|
473 | know whether to continue feeding input or not. | |
474 |
|
474 | |||
475 | The return value can be used to decide whether to use sys.ps1 or |
|
475 | The return value can be used to decide whether to use sys.ps1 or | |
476 | sys.ps2 to prompt the next line.""" |
|
476 | sys.ps2 to prompt the next line.""" | |
477 |
|
477 | |||
478 | self.message = self.setup_message() |
|
478 | self.message = self.setup_message() | |
479 |
|
479 | |||
480 | try: |
|
480 | try: | |
481 | code = self.command_compiler(source,filename,symbol) |
|
481 | code = self.command_compiler(source,filename,symbol) | |
482 | except (OverflowError, SyntaxError, IndentationError, ValueError ), e: |
|
482 | except (OverflowError, SyntaxError, IndentationError, ValueError ), e: | |
483 | # Case 1 |
|
483 | # Case 1 | |
484 | self.traceback_trap.args = sys.exc_info() |
|
484 | self.traceback_trap.args = sys.exc_info() | |
485 | self.pack_exception(self.message,e) |
|
485 | self.pack_exception(self.message,e) | |
486 | return COMPILER_ERROR,False |
|
486 | return COMPILER_ERROR,False | |
487 |
|
487 | |||
488 | if code is None: |
|
488 | if code is None: | |
489 | # Case 2: incomplete input. This means that the input can span |
|
489 | # Case 2: incomplete input. This means that the input can span | |
490 | # multiple lines. But we still need to decide when to actually |
|
490 | # multiple lines. But we still need to decide when to actually | |
491 | # stop taking user input. Later we'll add auto-indentation support |
|
491 | # stop taking user input. Later we'll add auto-indentation support | |
492 | # somehow. In the meantime, we'll just stop if there are two lines |
|
492 | # somehow. In the meantime, we'll just stop if there are two lines | |
493 | # of pure whitespace at the end. |
|
493 | # of pure whitespace at the end. | |
494 | last_two = source.rsplit('\n',2)[-2:] |
|
494 | last_two = source.rsplit('\n',2)[-2:] | |
495 | print 'last two:',last_two # dbg |
|
495 | print 'last two:',last_two # dbg | |
496 | if len(last_two)==2 and all(s.isspace() for s in last_two): |
|
496 | if len(last_two)==2 and all(s.isspace() for s in last_two): | |
497 | return COMPLETE_INPUT,False |
|
497 | return COMPLETE_INPUT,False | |
498 | else: |
|
498 | else: | |
499 | return INCOMPLETE_INPUT, True |
|
499 | return INCOMPLETE_INPUT, True | |
500 | else: |
|
500 | else: | |
501 | # Case 3 |
|
501 | # Case 3 | |
502 | return COMPLETE_INPUT, False |
|
502 | return COMPLETE_INPUT, False | |
503 |
|
503 | |||
504 | def pull(self, keys): |
|
504 | def pull(self, keys): | |
505 | """ Get an item out of the namespace by key. |
|
505 | """ Get an item out of the namespace by key. | |
506 |
|
506 | |||
507 | Parameters |
|
507 | Parameters | |
508 | ---------- |
|
508 | ---------- | |
509 | key : str |
|
509 | key : str | |
510 |
|
510 | |||
511 | Returns |
|
511 | Returns | |
512 | ------- |
|
512 | ------- | |
513 | value : object |
|
513 | value : object | |
514 |
|
514 | |||
515 | Raises |
|
515 | Raises | |
516 | ------ |
|
516 | ------ | |
517 | TypeError if the key is not a string. |
|
517 | TypeError if the key is not a string. | |
518 | NameError if the object doesn't exist. |
|
518 | NameError if the object doesn't exist. | |
519 | """ |
|
519 | """ | |
520 |
|
520 | |||
521 | if isinstance(keys, str): |
|
521 | if isinstance(keys, str): | |
522 | result = self.user_ns.get(keys, NotDefined()) |
|
522 | result = self.user_ns.get(keys, NotDefined()) | |
523 | if isinstance(result, NotDefined): |
|
523 | if isinstance(result, NotDefined): | |
524 | raise NameError('name %s is not defined' % keys) |
|
524 | raise NameError('name %s is not defined' % keys) | |
525 | elif isinstance(keys, (list, tuple)): |
|
525 | elif isinstance(keys, (list, tuple)): | |
526 | result = [] |
|
526 | result = [] | |
527 | for key in keys: |
|
527 | for key in keys: | |
528 | if not isinstance(key, str): |
|
528 | if not isinstance(key, str): | |
529 | raise TypeError("objects must be keyed by strings.") |
|
529 | raise TypeError("objects must be keyed by strings.") | |
530 | else: |
|
530 | else: | |
531 | r = self.user_ns.get(key, NotDefined()) |
|
531 | r = self.user_ns.get(key, NotDefined()) | |
532 | if isinstance(r, NotDefined): |
|
532 | if isinstance(r, NotDefined): | |
533 | raise NameError('name %s is not defined' % key) |
|
533 | raise NameError('name %s is not defined' % key) | |
534 | else: |
|
534 | else: | |
535 | result.append(r) |
|
535 | result.append(r) | |
536 | if len(keys)==1: |
|
536 | if len(keys)==1: | |
537 | result = result[0] |
|
537 | result = result[0] | |
538 | else: |
|
538 | else: | |
539 | raise TypeError("keys must be a strong or a list/tuple of strings") |
|
539 | raise TypeError("keys must be a strong or a list/tuple of strings") | |
540 | return result |
|
540 | return result | |
541 |
|
541 | |||
542 | def pull_function(self, keys): |
|
542 | def pull_function(self, keys): | |
543 | return self.pull(keys) |
|
543 | return self.pull(keys) | |
544 |
|
544 | |||
545 | #### Interactive user API ################################################## |
|
545 | #### Interactive user API ################################################## | |
546 |
|
546 | |||
547 | def ipsystem(self, command): |
|
547 | def ipsystem(self, command): | |
548 | """ Execute a command in a system shell while expanding variables in the |
|
548 | """ Execute a command in a system shell while expanding variables in the | |
549 | current namespace. |
|
549 | current namespace. | |
550 |
|
550 | |||
551 | Parameters |
|
551 | Parameters | |
552 | ---------- |
|
552 | ---------- | |
553 | command : str |
|
553 | command : str | |
554 | """ |
|
554 | """ | |
555 |
|
555 | |||
556 | # Expand $variables. |
|
556 | # Expand $variables. | |
557 | command = self.var_expand(command) |
|
557 | command = self.var_expand(command) | |
558 |
|
558 | |||
559 | system_shell(command, |
|
559 | system_shell(command, | |
560 | header='IPython system call: ', |
|
560 | header='IPython system call: ', | |
561 | verbose=self.rc.system_verbose, |
|
561 | verbose=self.rc.system_verbose, | |
562 | ) |
|
562 | ) | |
563 |
|
563 | |||
564 | def ipmagic(self, arg_string): |
|
564 | def ipmagic(self, arg_string): | |
565 | """ Call a magic function by name. |
|
565 | """ Call a magic function by name. | |
566 |
|
566 | |||
567 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython |
|
567 | ipmagic('name -opt foo bar') is equivalent to typing at the ipython | |
568 | prompt: |
|
568 | prompt: | |
569 |
|
569 | |||
570 | In[1]: %name -opt foo bar |
|
570 | In[1]: %name -opt foo bar | |
571 |
|
571 | |||
572 | To call a magic without arguments, simply use ipmagic('name'). |
|
572 | To call a magic without arguments, simply use ipmagic('name'). | |
573 |
|
573 | |||
574 | This provides a proper Python function to call IPython's magics in any |
|
574 | This provides a proper Python function to call IPython's magics in any | |
575 | valid Python code you can type at the interpreter, including loops and |
|
575 | valid Python code you can type at the interpreter, including loops and | |
576 | compound statements. It is added by IPython to the Python builtin |
|
576 | compound statements. It is added by IPython to the Python builtin | |
577 | namespace upon initialization. |
|
577 | namespace upon initialization. | |
578 |
|
578 | |||
579 | Parameters |
|
579 | Parameters | |
580 | ---------- |
|
580 | ---------- | |
581 | arg_string : str |
|
581 | arg_string : str | |
582 | A string containing the name of the magic function to call and any |
|
582 | A string containing the name of the magic function to call and any | |
583 | additional arguments to be passed to the magic. |
|
583 | additional arguments to be passed to the magic. | |
584 |
|
584 | |||
585 | Returns |
|
585 | Returns | |
586 | ------- |
|
586 | ------- | |
587 | something : object |
|
587 | something : object | |
588 | The return value of the actual object. |
|
588 | The return value of the actual object. | |
589 | """ |
|
589 | """ | |
590 |
|
590 | |||
591 | # Taken from IPython. |
|
591 | # Taken from IPython. | |
592 | raise NotImplementedError('Not ported yet') |
|
592 | raise NotImplementedError('Not ported yet') | |
593 |
|
593 | |||
594 | args = arg_string.split(' ', 1) |
|
594 | args = arg_string.split(' ', 1) | |
595 | magic_name = args[0] |
|
595 | magic_name = args[0] | |
596 | magic_name = magic_name.lstrip(self.config.ESC_MAGIC) |
|
596 | magic_name = magic_name.lstrip(self.config.ESC_MAGIC) | |
597 |
|
597 | |||
598 | try: |
|
598 | try: | |
599 | magic_args = args[1] |
|
599 | magic_args = args[1] | |
600 | except IndexError: |
|
600 | except IndexError: | |
601 | magic_args = '' |
|
601 | magic_args = '' | |
602 | fn = getattr(self.magic, 'magic_'+magic_name, None) |
|
602 | fn = getattr(self.magic, 'magic_'+magic_name, None) | |
603 | if fn is None: |
|
603 | if fn is None: | |
604 | self.error("Magic function `%s` not found." % magic_name) |
|
604 | self.error("Magic function `%s` not found." % magic_name) | |
605 | else: |
|
605 | else: | |
606 | magic_args = self.var_expand(magic_args) |
|
606 | magic_args = self.var_expand(magic_args) | |
607 | return fn(magic_args) |
|
607 | return fn(magic_args) | |
608 |
|
608 | |||
609 |
|
609 | |||
610 | #### Private 'Interpreter' interface ####################################### |
|
610 | #### Private 'Interpreter' interface ####################################### | |
611 |
|
611 | |||
612 | def setup_message(self): |
|
612 | def setup_message(self): | |
613 | """Return a message object. |
|
613 | """Return a message object. | |
614 |
|
614 | |||
615 | This method prepares and returns a message dictionary. This dict |
|
615 | This method prepares and returns a message dictionary. This dict | |
616 | contains the various fields that are used to transfer information about |
|
616 | contains the various fields that are used to transfer information about | |
617 | execution, results, tracebacks, etc, to clients (either in or out of |
|
617 | execution, results, tracebacks, etc, to clients (either in or out of | |
618 | process ones). Because of the need to work with possibly out of |
|
618 | process ones). Because of the need to work with possibly out of | |
619 | process clients, this dict MUST contain strictly pickle-safe values. |
|
619 | process clients, this dict MUST contain strictly pickle-safe values. | |
620 | """ |
|
620 | """ | |
621 |
|
621 | |||
622 | return dict(number=self.current_cell_number) |
|
622 | return dict(number=self.current_cell_number) | |
623 |
|
623 | |||
624 | def setup_namespace(self): |
|
624 | def setup_namespace(self): | |
625 | """ Add things to the namespace. |
|
625 | """ Add things to the namespace. | |
626 | """ |
|
626 | """ | |
627 |
|
627 | |||
628 | self.user_ns.setdefault('__name__', '__main__') |
|
628 | self.user_ns.setdefault('__name__', '__main__') | |
629 | self.user_ns.setdefault('__builtins__', __builtin__) |
|
629 | self.user_ns.setdefault('__builtins__', __builtin__) | |
630 | self.user_ns['__IP'] = self |
|
630 | self.user_ns['__IP'] = self | |
631 | if self.raw_input_builtin is not None: |
|
631 | if self.raw_input_builtin is not None: | |
632 | self.user_ns['raw_input'] = self.raw_input_builtin |
|
632 | self.user_ns['raw_input'] = self.raw_input_builtin | |
633 | if self.input_builtin is not None: |
|
633 | if self.input_builtin is not None: | |
634 | self.user_ns['input'] = self.input_builtin |
|
634 | self.user_ns['input'] = self.input_builtin | |
635 |
|
635 | |||
636 | builtin_additions = dict( |
|
636 | builtin_additions = dict( | |
637 | ipmagic=self.ipmagic, |
|
637 | ipmagic=self.ipmagic, | |
638 | ) |
|
638 | ) | |
639 | __builtin__.__dict__.update(builtin_additions) |
|
639 | __builtin__.__dict__.update(builtin_additions) | |
640 |
|
640 | |||
641 | if self.history is not None: |
|
641 | if self.history is not None: | |
642 | self.history.setup_namespace(self.user_ns) |
|
642 | self.history.setup_namespace(self.user_ns) | |
643 |
|
643 | |||
644 | def set_traps(self): |
|
644 | def set_traps(self): | |
645 | """ Set all of the output, display, and traceback traps. |
|
645 | """ Set all of the output, display, and traceback traps. | |
646 | """ |
|
646 | """ | |
647 |
|
647 | |||
648 | self.output_trap.set() |
|
648 | self.output_trap.set() | |
649 | self.display_trap.set() |
|
649 | self.display_trap.set() | |
650 | self.traceback_trap.set() |
|
650 | self.traceback_trap.set() | |
651 |
|
651 | |||
652 | def unset_traps(self): |
|
652 | def unset_traps(self): | |
653 | """ Unset all of the output, display, and traceback traps. |
|
653 | """ Unset all of the output, display, and traceback traps. | |
654 | """ |
|
654 | """ | |
655 |
|
655 | |||
656 | self.output_trap.unset() |
|
656 | self.output_trap.unset() | |
657 | self.display_trap.unset() |
|
657 | self.display_trap.unset() | |
658 | self.traceback_trap.unset() |
|
658 | self.traceback_trap.unset() | |
659 |
|
659 | |||
660 | def split_commands(self, python): |
|
660 | def split_commands(self, python): | |
661 | """ Split multiple lines of code into discrete commands that can be |
|
661 | """ Split multiple lines of code into discrete commands that can be | |
662 | executed singly. |
|
662 | executed singly. | |
663 |
|
663 | |||
664 | Parameters |
|
664 | Parameters | |
665 | ---------- |
|
665 | ---------- | |
666 | python : str |
|
666 | python : str | |
667 | Pure, exec'able Python code. |
|
667 | Pure, exec'able Python code. | |
668 |
|
668 | |||
669 | Returns |
|
669 | Returns | |
670 | ------- |
|
670 | ------- | |
671 | commands : list of str |
|
671 | commands : list of str | |
672 | Separate commands that can be exec'ed independently. |
|
672 | Separate commands that can be exec'ed independently. | |
673 | """ |
|
673 | """ | |
674 |
|
674 | |||
675 | # compiler.parse treats trailing spaces after a newline as a |
|
675 | # compiler.parse treats trailing spaces after a newline as a | |
676 | # SyntaxError. This is different than codeop.CommandCompiler, which |
|
676 | # SyntaxError. This is different than codeop.CommandCompiler, which | |
677 | # will compile the trailng spaces just fine. We simply strip any |
|
677 | # will compile the trailng spaces just fine. We simply strip any | |
678 | # trailing whitespace off. Passing a string with trailing whitespace |
|
678 | # trailing whitespace off. Passing a string with trailing whitespace | |
679 | # to exec will fail however. There seems to be some inconsistency in |
|
679 | # to exec will fail however. There seems to be some inconsistency in | |
680 | # how trailing whitespace is handled, but this seems to work. |
|
680 | # how trailing whitespace is handled, but this seems to work. | |
681 | python = python.strip() |
|
681 | python = python.strip() | |
682 |
|
682 | |||
683 | # The compiler module does not like unicode. We need to convert |
|
683 | # The compiler module does not like unicode. We need to convert | |
684 | # it encode it: |
|
684 | # it encode it: | |
685 | if isinstance(python, unicode): |
|
685 | if isinstance(python, unicode): | |
686 | # Use the utf-8-sig BOM so the compiler detects this a UTF-8 |
|
686 | # Use the utf-8-sig BOM so the compiler detects this a UTF-8 | |
687 | # encode string. |
|
687 | # encode string. | |
688 | python = '\xef\xbb\xbf' + python.encode('utf-8') |
|
688 | python = '\xef\xbb\xbf' + python.encode('utf-8') | |
689 |
|
689 | |||
690 | # The compiler module will parse the code into an abstract syntax tree. |
|
690 | # The compiler module will parse the code into an abstract syntax tree. | |
691 | # This has a bug with str("a\nb"), but not str("""a\nb""")!!! |
|
691 | # This has a bug with str("a\nb"), but not str("""a\nb""")!!! | |
692 | ast = compiler.parse(python) |
|
692 | ast = compiler.parse(python) | |
693 |
|
693 | |||
694 | # Uncomment to help debug the ast tree |
|
694 | # Uncomment to help debug the ast tree | |
695 | # for n in ast.node: |
|
695 | # for n in ast.node: | |
696 | # print n.lineno,'->',n |
|
696 | # print n.lineno,'->',n | |
697 |
|
697 | |||
698 | # Each separate command is available by iterating over ast.node. The |
|
698 | # Each separate command is available by iterating over ast.node. The | |
699 | # lineno attribute is the line number (1-indexed) beginning the commands |
|
699 | # lineno attribute is the line number (1-indexed) beginning the commands | |
700 | # suite. |
|
700 | # suite. | |
701 | # lines ending with ";" yield a Discard Node that doesn't have a lineno |
|
701 | # lines ending with ";" yield a Discard Node that doesn't have a lineno | |
702 | # attribute. These nodes can and should be discarded. But there are |
|
702 | # attribute. These nodes can and should be discarded. But there are | |
703 | # other situations that cause Discard nodes that shouldn't be discarded. |
|
703 | # other situations that cause Discard nodes that shouldn't be discarded. | |
704 | # We might eventually discover other cases where lineno is None and have |
|
704 | # We might eventually discover other cases where lineno is None and have | |
705 | # to put in a more sophisticated test. |
|
705 | # to put in a more sophisticated test. | |
706 | linenos = [x.lineno-1 for x in ast.node if x.lineno is not None] |
|
706 | linenos = [x.lineno-1 for x in ast.node if x.lineno is not None] | |
707 |
|
707 | |||
708 | # When we finally get the slices, we will need to slice all the way to |
|
708 | # When we finally get the slices, we will need to slice all the way to | |
709 | # the end even though we don't have a line number for it. Fortunately, |
|
709 | # the end even though we don't have a line number for it. Fortunately, | |
710 | # None does the job nicely. |
|
710 | # None does the job nicely. | |
711 | linenos.append(None) |
|
711 | linenos.append(None) | |
712 |
|
712 | |||
713 | # Same problem at the other end: sometimes the ast tree has its |
|
713 | # Same problem at the other end: sometimes the ast tree has its | |
714 | # first complete statement not starting on line 0. In this case |
|
714 | # first complete statement not starting on line 0. In this case | |
715 | # we might miss part of it. This fixes ticket 266993. Thanks Gael! |
|
715 | # we might miss part of it. This fixes ticket 266993. Thanks Gael! | |
716 | linenos[0] = 0 |
|
716 | linenos[0] = 0 | |
717 |
|
717 | |||
718 | lines = python.splitlines() |
|
718 | lines = python.splitlines() | |
719 |
|
719 | |||
720 | # Create a list of atomic commands. |
|
720 | # Create a list of atomic commands. | |
721 | cmds = [] |
|
721 | cmds = [] | |
722 | for i, j in zip(linenos[:-1], linenos[1:]): |
|
722 | for i, j in zip(linenos[:-1], linenos[1:]): | |
723 | cmd = lines[i:j] |
|
723 | cmd = lines[i:j] | |
724 | if cmd: |
|
724 | if cmd: | |
725 | cmds.append('\n'.join(cmd)+'\n') |
|
725 | cmds.append('\n'.join(cmd)+'\n') | |
726 |
|
726 | |||
727 | return cmds |
|
727 | return cmds | |
728 |
|
728 | |||
729 | def error(self, text): |
|
729 | def error(self, text): | |
730 | """ Pass an error message back to the shell. |
|
730 | """ Pass an error message back to the shell. | |
731 |
|
731 | |||
732 | Preconditions |
|
732 | Preconditions | |
733 | ------------- |
|
733 | ------------- | |
734 | This should only be called when self.message is set. In other words, |
|
734 | This should only be called when self.message is set. In other words, | |
735 | when code is being executed. |
|
735 | when code is being executed. | |
736 |
|
736 | |||
737 | Parameters |
|
737 | Parameters | |
738 | ---------- |
|
738 | ---------- | |
739 | text : str |
|
739 | text : str | |
740 | """ |
|
740 | """ | |
741 |
|
741 | |||
742 | errors = self.message.get('IPYTHON_ERROR', []) |
|
742 | errors = self.message.get('IPYTHON_ERROR', []) | |
743 | errors.append(text) |
|
743 | errors.append(text) | |
744 |
|
744 | |||
745 | def var_expand(self, template): |
|
745 | def var_expand(self, template): | |
746 | """ Expand $variables in the current namespace using Itpl. |
|
746 | """ Expand $variables in the current namespace using Itpl. | |
747 |
|
747 | |||
748 | Parameters |
|
748 | Parameters | |
749 | ---------- |
|
749 | ---------- | |
750 | template : str |
|
750 | template : str | |
751 | """ |
|
751 | """ | |
752 |
|
752 | |||
753 | return str(ItplNS(template, self.user_ns)) |
|
753 | return str(ItplNS(template, self.user_ns)) | |
754 |
|
754 | |||
755 | def _possible_macro(self, obj): |
|
755 | def _possible_macro(self, obj): | |
756 | """ If the object is a macro, execute it. |
|
756 | """ If the object is a macro, execute it. | |
757 | """ |
|
757 | """ | |
758 |
|
758 | |||
759 | if isinstance(obj, Macro): |
|
759 | if isinstance(obj, Macro): | |
760 | self.execute_macro(obj) |
|
760 | self.execute_macro(obj) | |
761 |
|
761 |
@@ -1,279 +1,285 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """IPython Test Suite Runner. |
|
2 | """IPython Test Suite Runner. | |
3 |
|
3 | |||
4 | This module provides a main entry point to a user script to test IPython |
|
4 | This module provides a main entry point to a user script to test IPython | |
5 | itself from the command line. There are two ways of running this script: |
|
5 | itself from the command line. There are two ways of running this script: | |
6 |
|
6 | |||
7 | 1. With the syntax `iptest all`. This runs our entire test suite by |
|
7 | 1. With the syntax `iptest all`. This runs our entire test suite by | |
8 | calling this script (with different arguments) or trial recursively. This |
|
8 | calling this script (with different arguments) or trial recursively. This | |
9 | causes modules and package to be tested in different processes, using nose |
|
9 | causes modules and package to be tested in different processes, using nose | |
10 | or trial where appropriate. |
|
10 | or trial where appropriate. | |
11 | 2. With the regular nose syntax, like `iptest -vvs IPython`. In this form |
|
11 | 2. With the regular nose syntax, like `iptest -vvs IPython`. In this form | |
12 | the script simply calls nose, but with special command line flags and |
|
12 | the script simply calls nose, but with special command line flags and | |
13 | plugins loaded. |
|
13 | plugins loaded. | |
14 |
|
14 | |||
15 | For now, this script requires that both nose and twisted are installed. This |
|
15 | For now, this script requires that both nose and twisted are installed. This | |
16 | will change in the future. |
|
16 | will change in the future. | |
17 | """ |
|
17 | """ | |
18 |
|
18 | |||
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 | # Module imports |
|
20 | # Module imports | |
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 |
|
22 | |||
23 | import os |
|
23 | import os | |
24 | import os.path as path |
|
24 | import os.path as path | |
25 | import sys |
|
25 | import sys | |
26 | import subprocess |
|
26 | import subprocess | |
27 | import time |
|
27 | import time | |
28 | import warnings |
|
28 | import warnings | |
29 |
|
29 | |||
30 | import nose.plugins.builtin |
|
30 | import nose.plugins.builtin | |
31 | from nose.core import TestProgram |
|
31 | from nose.core import TestProgram | |
32 |
|
32 | |||
33 | from IPython.utils.platutils import find_cmd |
|
33 | from IPython.utils.platutils import find_cmd | |
34 | from IPython.testing.plugin.ipdoctest import IPythonDoctest |
|
34 | from IPython.testing.plugin.ipdoctest import IPythonDoctest | |
35 |
|
35 | |||
36 | pjoin = path.join |
|
36 | pjoin = path.join | |
37 |
|
37 | |||
38 | #----------------------------------------------------------------------------- |
|
38 | #----------------------------------------------------------------------------- | |
39 | # Logic for skipping doctests |
|
39 | # Logic for skipping doctests | |
40 | #----------------------------------------------------------------------------- |
|
40 | #----------------------------------------------------------------------------- | |
41 |
|
41 | |||
42 | def test_for(mod): |
|
42 | def test_for(mod): | |
43 | """Test to see if mod is importable.""" |
|
43 | """Test to see if mod is importable.""" | |
44 | try: |
|
44 | try: | |
45 | __import__(mod) |
|
45 | __import__(mod) | |
46 | except ImportError: |
|
46 | except ImportError: | |
47 | return False |
|
47 | return False | |
48 | else: |
|
48 | else: | |
49 | return True |
|
49 | return True | |
50 |
|
50 | |||
51 | have_curses = test_for('_curses') |
|
51 | have_curses = test_for('_curses') | |
52 | have_wx = test_for('wx') |
|
52 | have_wx = test_for('wx') | |
|
53 | have_wx_aui = test_for('wx.aui') | |||
53 | have_zi = test_for('zope.interface') |
|
54 | have_zi = test_for('zope.interface') | |
54 | have_twisted = test_for('twisted') |
|
55 | have_twisted = test_for('twisted') | |
55 | have_foolscap = test_for('foolscap') |
|
56 | have_foolscap = test_for('foolscap') | |
56 | have_objc = test_for('objc') |
|
57 | have_objc = test_for('objc') | |
57 | have_pexpect = test_for('pexpect') |
|
58 | have_pexpect = test_for('pexpect') | |
58 |
|
59 | |||
59 |
|
60 | |||
60 | def make_exclude(): |
|
61 | def make_exclude(): | |
61 |
|
62 | |||
62 | # For the IPythonDoctest plugin, we need to exclude certain patterns that cause |
|
63 | # For the IPythonDoctest plugin, we need to exclude certain patterns that cause | |
63 | # testing problems. We should strive to minimize the number of skipped |
|
64 | # testing problems. We should strive to minimize the number of skipped | |
64 | # modules, since this means untested code. As the testing machinery |
|
65 | # modules, since this means untested code. As the testing machinery | |
65 | # solidifies, this list should eventually become empty. |
|
66 | # solidifies, this list should eventually become empty. | |
66 | EXCLUDE = [pjoin('IPython', 'external'), |
|
67 | EXCLUDE = [pjoin('IPython', 'external'), | |
67 | pjoin('IPython', 'frontend', 'process', 'winprocess.py'), |
|
68 | pjoin('IPython', 'frontend', 'process', 'winprocess.py'), | |
68 | pjoin('IPython_doctest_plugin'), |
|
69 | pjoin('IPython_doctest_plugin'), | |
69 | pjoin('IPython', 'extensions', 'ipy_'), |
|
70 | pjoin('IPython', 'extensions', 'ipy_'), | |
70 |
pjoin('IPython', 'extensions', ' |
|
71 | pjoin('IPython', 'extensions', 'PhysicalQInput'), | |
71 | pjoin('IPython', 'extensions', 'PhysicalQInteractive'), |
|
72 | pjoin('IPython', 'extensions', 'PhysicalQInteractive'), | |
|
73 | pjoin('IPython', 'extensions', 'InterpreterPasteInput'), | |||
72 | pjoin('IPython', 'extensions', 'scitedirector'), |
|
74 | pjoin('IPython', 'extensions', 'scitedirector'), | |
73 | pjoin('IPython', 'extensions', 'numeric_formats'), |
|
75 | pjoin('IPython', 'extensions', 'numeric_formats'), | |
74 | pjoin('IPython', 'testing', 'attic'), |
|
76 | pjoin('IPython', 'testing', 'attic'), | |
75 | pjoin('IPython', 'testing', 'tools'), |
|
77 | pjoin('IPython', 'testing', 'tools'), | |
76 | pjoin('IPython', 'testing', 'mkdoctests') |
|
78 | pjoin('IPython', 'testing', 'mkdoctests') | |
77 | ] |
|
79 | ] | |
78 |
|
80 | |||
79 | if not have_wx: |
|
81 | if not have_wx: | |
80 | EXCLUDE.append(pjoin('IPython', 'extensions', 'igrid')) |
|
82 | EXCLUDE.append(pjoin('IPython', 'extensions', 'igrid')) | |
81 | EXCLUDE.append(pjoin('IPython', 'gui')) |
|
83 | EXCLUDE.append(pjoin('IPython', 'gui')) | |
82 | EXCLUDE.append(pjoin('IPython', 'frontend', 'wx')) |
|
84 | EXCLUDE.append(pjoin('IPython', 'frontend', 'wx')) | |
83 |
|
85 | |||
|
86 | if not have_wx_aui: | |||
|
87 | EXCLUDE.append(pjoin('IPython', 'gui', 'wx', 'wxIPython')) | |||
|
88 | ||||
84 | if not have_objc: |
|
89 | if not have_objc: | |
85 | EXCLUDE.append(pjoin('IPython', 'frontend', 'cocoa')) |
|
90 | EXCLUDE.append(pjoin('IPython', 'frontend', 'cocoa')) | |
86 |
|
91 | |||
87 | if not have_curses: |
|
92 | if not have_curses: | |
88 | EXCLUDE.append(pjoin('IPython', 'extensions', 'ibrowse')) |
|
93 | EXCLUDE.append(pjoin('IPython', 'extensions', 'ibrowse')) | |
89 |
|
94 | |||
90 | if not sys.platform == 'win32': |
|
95 | if not sys.platform == 'win32': | |
91 | EXCLUDE.append(pjoin('IPython', 'utils', 'platutils_win32')) |
|
96 | EXCLUDE.append(pjoin('IPython', 'utils', 'platutils_win32')) | |
92 |
|
97 | |||
93 | # These have to be skipped on win32 because the use echo, rm, cd, etc. |
|
98 | # These have to be skipped on win32 because the use echo, rm, cd, etc. | |
94 | # See ticket https://bugs.launchpad.net/bugs/366982 |
|
99 | # See ticket https://bugs.launchpad.net/bugs/366982 | |
95 | if sys.platform == 'win32': |
|
100 | if sys.platform == 'win32': | |
96 | EXCLUDE.append(pjoin('IPython', 'testing', 'plugin', 'test_exampleip')) |
|
101 | EXCLUDE.append(pjoin('IPython', 'testing', 'plugin', 'test_exampleip')) | |
97 | EXCLUDE.append(pjoin('IPython', 'testing', 'plugin', 'dtexample')) |
|
102 | EXCLUDE.append(pjoin('IPython', 'testing', 'plugin', 'dtexample')) | |
98 |
|
103 | |||
99 | if not os.name == 'posix': |
|
104 | if not os.name == 'posix': | |
100 | EXCLUDE.append(pjoin('IPython', 'utils', 'platutils_posix')) |
|
105 | EXCLUDE.append(pjoin('IPython', 'utils', 'platutils_posix')) | |
101 |
|
106 | |||
102 | if not have_pexpect: |
|
107 | if not have_pexpect: | |
103 | EXCLUDE.append(pjoin('IPython', 'scripts', 'irunner')) |
|
108 | EXCLUDE.append(pjoin('IPython', 'scripts', 'irunner')) | |
104 |
|
109 | |||
105 | # Skip shell always because of a bug in FakeModule. |
|
110 | # Skip shell always because of a bug in FakeModule. | |
106 | EXCLUDE.append(pjoin('IPython', 'core', 'shell')) |
|
111 | EXCLUDE.append(pjoin('IPython', 'core', 'shell')) | |
107 |
|
112 | |||
108 | # This is needed for the reg-exp to match on win32 in the ipdoctest plugin. |
|
113 | # This is needed for the reg-exp to match on win32 in the ipdoctest plugin. | |
109 | if sys.platform == 'win32': |
|
114 | if sys.platform == 'win32': | |
110 | EXCLUDE = [s.replace('\\','\\\\') for s in EXCLUDE] |
|
115 | EXCLUDE = [s.replace('\\','\\\\') for s in EXCLUDE] | |
111 |
|
116 | |||
112 | return EXCLUDE |
|
117 | return EXCLUDE | |
113 |
|
118 | |||
|
119 | ||||
114 | #----------------------------------------------------------------------------- |
|
120 | #----------------------------------------------------------------------------- | |
115 | # Functions and classes |
|
121 | # Functions and classes | |
116 | #----------------------------------------------------------------------------- |
|
122 | #----------------------------------------------------------------------------- | |
117 |
|
123 | |||
118 | def run_iptest(): |
|
124 | def run_iptest(): | |
119 | """Run the IPython test suite using nose. |
|
125 | """Run the IPython test suite using nose. | |
120 |
|
126 | |||
121 | This function is called when this script is **not** called with the form |
|
127 | This function is called when this script is **not** called with the form | |
122 | `iptest all`. It simply calls nose with appropriate command line flags |
|
128 | `iptest all`. It simply calls nose with appropriate command line flags | |
123 | and accepts all of the standard nose arguments. |
|
129 | and accepts all of the standard nose arguments. | |
124 | """ |
|
130 | """ | |
125 |
|
131 | |||
126 | warnings.filterwarnings('ignore', |
|
132 | warnings.filterwarnings('ignore', | |
127 | 'This will be removed soon. Use IPython.testing.util instead') |
|
133 | 'This will be removed soon. Use IPython.testing.util instead') | |
128 |
|
134 | |||
129 | argv = sys.argv + [ |
|
135 | argv = sys.argv + [ | |
130 | # Loading ipdoctest causes problems with Twisted. |
|
136 | # Loading ipdoctest causes problems with Twisted. | |
131 | # I am removing this as a temporary fix to get the |
|
137 | # I am removing this as a temporary fix to get the | |
132 | # test suite back into working shape. Our nose |
|
138 | # test suite back into working shape. Our nose | |
133 | # plugin needs to be gone through with a fine |
|
139 | # plugin needs to be gone through with a fine | |
134 | # toothed comb to find what is causing the problem. |
|
140 | # toothed comb to find what is causing the problem. | |
135 | '--with-ipdoctest', |
|
141 | '--with-ipdoctest', | |
136 | '--ipdoctest-tests','--ipdoctest-extension=txt', |
|
142 | '--ipdoctest-tests','--ipdoctest-extension=txt', | |
137 | '--detailed-errors', |
|
143 | '--detailed-errors', | |
138 |
|
144 | |||
139 | # We add --exe because of setuptools' imbecility (it |
|
145 | # We add --exe because of setuptools' imbecility (it | |
140 | # blindly does chmod +x on ALL files). Nose does the |
|
146 | # blindly does chmod +x on ALL files). Nose does the | |
141 | # right thing and it tries to avoid executables, |
|
147 | # right thing and it tries to avoid executables, | |
142 | # setuptools unfortunately forces our hand here. This |
|
148 | # setuptools unfortunately forces our hand here. This | |
143 | # has been discussed on the distutils list and the |
|
149 | # has been discussed on the distutils list and the | |
144 | # setuptools devs refuse to fix this problem! |
|
150 | # setuptools devs refuse to fix this problem! | |
145 | '--exe', |
|
151 | '--exe', | |
146 | ] |
|
152 | ] | |
147 |
|
153 | |||
148 | # Detect if any tests were required by explicitly calling an IPython |
|
154 | # Detect if any tests were required by explicitly calling an IPython | |
149 | # submodule or giving a specific path |
|
155 | # submodule or giving a specific path | |
150 | has_tests = False |
|
156 | has_tests = False | |
151 | for arg in sys.argv: |
|
157 | for arg in sys.argv: | |
152 | if 'IPython' in arg or arg.endswith('.py') or \ |
|
158 | if 'IPython' in arg or arg.endswith('.py') or \ | |
153 | (':' in arg and '.py' in arg): |
|
159 | (':' in arg and '.py' in arg): | |
154 | has_tests = True |
|
160 | has_tests = True | |
155 | break |
|
161 | break | |
156 |
|
162 | |||
157 | # If nothing was specifically requested, test full IPython |
|
163 | # If nothing was specifically requested, test full IPython | |
158 | if not has_tests: |
|
164 | if not has_tests: | |
159 | argv.append('IPython') |
|
165 | argv.append('IPython') | |
160 |
|
166 | |||
161 | # Construct list of plugins, omitting the existing doctest plugin, which |
|
167 | # Construct list of plugins, omitting the existing doctest plugin, which | |
162 | # ours replaces (and extends). |
|
168 | # ours replaces (and extends). | |
163 | EXCLUDE = make_exclude() |
|
169 | EXCLUDE = make_exclude() | |
164 | plugins = [IPythonDoctest(EXCLUDE)] |
|
170 | plugins = [IPythonDoctest(EXCLUDE)] | |
165 | for p in nose.plugins.builtin.plugins: |
|
171 | for p in nose.plugins.builtin.plugins: | |
166 | plug = p() |
|
172 | plug = p() | |
167 | if plug.name == 'doctest': |
|
173 | if plug.name == 'doctest': | |
168 | continue |
|
174 | continue | |
169 | plugins.append(plug) |
|
175 | plugins.append(plug) | |
170 |
|
176 | |||
171 | TestProgram(argv=argv,plugins=plugins) |
|
177 | TestProgram(argv=argv,plugins=plugins) | |
172 |
|
178 | |||
173 |
|
179 | |||
174 | class IPTester(object): |
|
180 | class IPTester(object): | |
175 | """Call that calls iptest or trial in a subprocess. |
|
181 | """Call that calls iptest or trial in a subprocess. | |
176 | """ |
|
182 | """ | |
177 | def __init__(self,runner='iptest',params=None): |
|
183 | def __init__(self,runner='iptest',params=None): | |
178 | """ """ |
|
184 | """ """ | |
179 | if runner == 'iptest': |
|
185 | if runner == 'iptest': | |
180 | self.runner = ['iptest','-v'] |
|
186 | self.runner = ['iptest','-v'] | |
181 | else: |
|
187 | else: | |
182 | self.runner = [find_cmd('trial')] |
|
188 | self.runner = [find_cmd('trial')] | |
183 | if params is None: |
|
189 | if params is None: | |
184 | params = [] |
|
190 | params = [] | |
185 | if isinstance(params,str): |
|
191 | if isinstance(params,str): | |
186 | params = [params] |
|
192 | params = [params] | |
187 | self.params = params |
|
193 | self.params = params | |
188 |
|
194 | |||
189 | # Assemble call |
|
195 | # Assemble call | |
190 | self.call_args = self.runner+self.params |
|
196 | self.call_args = self.runner+self.params | |
191 |
|
197 | |||
192 | def run(self): |
|
198 | def run(self): | |
193 | """Run the stored commands""" |
|
199 | """Run the stored commands""" | |
194 | return subprocess.call(self.call_args) |
|
200 | return subprocess.call(self.call_args) | |
195 |
|
201 | |||
196 |
|
202 | |||
197 | def make_runners(): |
|
203 | def make_runners(): | |
198 | """Define the top-level packages that need to be tested. |
|
204 | """Define the top-level packages that need to be tested. | |
199 | """ |
|
205 | """ | |
200 |
|
206 | |||
201 | nose_packages = ['config', 'core', 'extensions', |
|
207 | nose_packages = ['config', 'core', 'extensions', | |
202 | 'frontend', 'lib', 'quarantine', |
|
208 | 'frontend', 'lib', 'quarantine', | |
203 | 'scripts', 'testing', 'utils'] |
|
209 | 'scripts', 'testing', 'utils'] | |
204 | trial_packages = ['kernel'] |
|
210 | trial_packages = ['kernel'] | |
205 |
|
211 | |||
206 | if have_wx: |
|
212 | if have_wx: | |
207 | nose_packages.append('gui') |
|
213 | nose_packages.append('gui') | |
208 |
|
214 | |||
209 | nose_packages = ['IPython.%s' % m for m in nose_packages ] |
|
215 | nose_packages = ['IPython.%s' % m for m in nose_packages ] | |
210 | trial_packages = ['IPython.%s' % m for m in trial_packages ] |
|
216 | trial_packages = ['IPython.%s' % m for m in trial_packages ] | |
211 |
|
217 | |||
212 | # Make runners |
|
218 | # Make runners | |
213 | runners = dict() |
|
219 | runners = dict() | |
214 |
|
220 | |||
215 | nose_runners = dict(zip(nose_packages, [IPTester(params=v) for v in nose_packages])) |
|
221 | nose_runners = dict(zip(nose_packages, [IPTester(params=v) for v in nose_packages])) | |
216 | if have_zi and have_twisted and have_foolscap: |
|
222 | if have_zi and have_twisted and have_foolscap: | |
217 | trial_runners = dict(zip(trial_packages, [IPTester('trial',params=v) for v in trial_packages])) |
|
223 | trial_runners = dict(zip(trial_packages, [IPTester('trial',params=v) for v in trial_packages])) | |
218 | runners.update(nose_runners) |
|
224 | runners.update(nose_runners) | |
219 | runners.update(trial_runners) |
|
225 | runners.update(trial_runners) | |
220 |
|
226 | |||
221 | return runners |
|
227 | return runners | |
222 |
|
228 | |||
223 |
|
229 | |||
224 | def run_iptestall(): |
|
230 | def run_iptestall(): | |
225 | """Run the entire IPython test suite by calling nose and trial. |
|
231 | """Run the entire IPython test suite by calling nose and trial. | |
226 |
|
232 | |||
227 | This function constructs :class:`IPTester` instances for all IPython |
|
233 | This function constructs :class:`IPTester` instances for all IPython | |
228 | modules and package and then runs each of them. This causes the modules |
|
234 | modules and package and then runs each of them. This causes the modules | |
229 | and packages of IPython to be tested each in their own subprocess using |
|
235 | and packages of IPython to be tested each in their own subprocess using | |
230 | nose or twisted.trial appropriately. |
|
236 | nose or twisted.trial appropriately. | |
231 | """ |
|
237 | """ | |
232 |
|
238 | |||
233 | runners = make_runners() |
|
239 | runners = make_runners() | |
234 |
|
240 | |||
235 | # Run all test runners, tracking execution time |
|
241 | # Run all test runners, tracking execution time | |
236 | failed = {} |
|
242 | failed = {} | |
237 | t_start = time.time() |
|
243 | t_start = time.time() | |
238 | for name,runner in runners.iteritems(): |
|
244 | for name,runner in runners.iteritems(): | |
239 | print '*'*77 |
|
245 | print '*'*77 | |
240 |
print 'IPython test |
|
246 | print 'IPython test group:',name | |
241 | res = runner.run() |
|
247 | res = runner.run() | |
242 | if res: |
|
248 | if res: | |
243 | failed[name] = res |
|
249 | failed[name] = res | |
244 | t_end = time.time() |
|
250 | t_end = time.time() | |
245 | t_tests = t_end - t_start |
|
251 | t_tests = t_end - t_start | |
246 | nrunners = len(runners) |
|
252 | nrunners = len(runners) | |
247 | nfail = len(failed) |
|
253 | nfail = len(failed) | |
248 | # summarize results |
|
254 | # summarize results | |
249 |
|
255 | |||
250 | print '*'*77 |
|
256 | print '*'*77 | |
251 |
print 'Ran %s test |
|
257 | print 'Ran %s test groups in %.3fs' % (nrunners, t_tests) | |
252 |
|
258 | |||
253 | if not failed: |
|
259 | if not failed: | |
254 | print 'OK' |
|
260 | print 'OK' | |
255 | else: |
|
261 | else: | |
256 | # If anything went wrong, point out what command to rerun manually to |
|
262 | # If anything went wrong, point out what command to rerun manually to | |
257 | # see the actual errors and individual summary |
|
263 | # see the actual errors and individual summary | |
258 |
print 'ERROR - %s out of %s test |
|
264 | print 'ERROR - %s out of %s test groups failed.' % (nfail, nrunners) | |
259 | for name in failed: |
|
265 | for name in failed: | |
260 | failed_runner = runners[name] |
|
266 | failed_runner = runners[name] | |
261 | print '-'*40 |
|
267 | print '-'*40 | |
262 | print 'Runner failed:',name |
|
268 | print 'Runner failed:',name | |
263 | print 'You may wish to rerun this one individually, with:' |
|
269 | print 'You may wish to rerun this one individually, with:' | |
264 | print ' '.join(failed_runner.call_args) |
|
270 | print ' '.join(failed_runner.call_args) | |
265 |
|
271 | |||
266 |
|
272 | |||
267 |
|
273 | |||
268 | def main(): |
|
274 | def main(): | |
269 | if len(sys.argv) == 1: |
|
275 | if len(sys.argv) == 1: | |
270 | run_iptestall() |
|
276 | run_iptestall() | |
271 | else: |
|
277 | else: | |
272 | if sys.argv[1] == 'all': |
|
278 | if sys.argv[1] == 'all': | |
273 | run_iptestall() |
|
279 | run_iptestall() | |
274 | else: |
|
280 | else: | |
275 | run_iptest() |
|
281 | run_iptest() | |
276 |
|
282 | |||
277 |
|
283 | |||
278 | if __name__ == '__main__': |
|
284 | if __name__ == '__main__': | |
279 | main() No newline at end of file |
|
285 | main() |
@@ -1,909 +1,918 b'' | |||||
1 | """Nose Plugin that supports IPython doctests. |
|
1 | """Nose Plugin that supports IPython doctests. | |
2 |
|
2 | |||
3 | Limitations: |
|
3 | Limitations: | |
4 |
|
4 | |||
5 | - When generating examples for use as doctests, make sure that you have |
|
5 | - When generating examples for use as doctests, make sure that you have | |
6 | pretty-printing OFF. This can be done either by starting ipython with the |
|
6 | pretty-printing OFF. This can be done either by starting ipython with the | |
7 | flag '--nopprint', by setting pprint to 0 in your ipythonrc file, or by |
|
7 | flag '--nopprint', by setting pprint to 0 in your ipythonrc file, or by | |
8 | interactively disabling it with %Pprint. This is required so that IPython |
|
8 | interactively disabling it with %Pprint. This is required so that IPython | |
9 | output matches that of normal Python, which is used by doctest for internal |
|
9 | output matches that of normal Python, which is used by doctest for internal | |
10 | execution. |
|
10 | execution. | |
11 |
|
11 | |||
12 | - Do not rely on specific prompt numbers for results (such as using |
|
12 | - Do not rely on specific prompt numbers for results (such as using | |
13 | '_34==True', for example). For IPython tests run via an external process the |
|
13 | '_34==True', for example). For IPython tests run via an external process the | |
14 | prompt numbers may be different, and IPython tests run as normal python code |
|
14 | prompt numbers may be different, and IPython tests run as normal python code | |
15 | won't even have these special _NN variables set at all. |
|
15 | won't even have these special _NN variables set at all. | |
16 | """ |
|
16 | """ | |
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Module imports |
|
19 | # Module imports | |
20 |
|
20 | |||
21 | # From the standard library |
|
21 | # From the standard library | |
22 | import __builtin__ |
|
22 | import __builtin__ | |
23 | import commands |
|
23 | import commands | |
24 | import doctest |
|
24 | import doctest | |
25 | import inspect |
|
25 | import inspect | |
26 | import logging |
|
26 | import logging | |
27 | import os |
|
27 | import os | |
28 | import re |
|
28 | import re | |
29 | import sys |
|
29 | import sys | |
30 | import traceback |
|
30 | import traceback | |
31 | import unittest |
|
31 | import unittest | |
32 |
|
32 | |||
33 | from inspect import getmodule |
|
33 | from inspect import getmodule | |
34 | from StringIO import StringIO |
|
34 | from StringIO import StringIO | |
35 |
|
35 | |||
36 | # We are overriding the default doctest runner, so we need to import a few |
|
36 | # We are overriding the default doctest runner, so we need to import a few | |
37 | # things from doctest directly |
|
37 | # things from doctest directly | |
38 | from doctest import (REPORTING_FLAGS, REPORT_ONLY_FIRST_FAILURE, |
|
38 | from doctest import (REPORTING_FLAGS, REPORT_ONLY_FIRST_FAILURE, | |
39 | _unittest_reportflags, DocTestRunner, |
|
39 | _unittest_reportflags, DocTestRunner, | |
40 | _extract_future_flags, pdb, _OutputRedirectingPdb, |
|
40 | _extract_future_flags, pdb, _OutputRedirectingPdb, | |
41 | _exception_traceback, |
|
41 | _exception_traceback, | |
42 | linecache) |
|
42 | linecache) | |
43 |
|
43 | |||
44 | # Third-party modules |
|
44 | # Third-party modules | |
45 | import nose.core |
|
45 | import nose.core | |
46 |
|
46 | |||
47 | from nose.plugins import doctests, Plugin |
|
47 | from nose.plugins import doctests, Plugin | |
48 | from nose.util import anyp, getpackage, test_address, resolve_name, tolist |
|
48 | from nose.util import anyp, getpackage, test_address, resolve_name, tolist | |
49 |
|
49 | |||
50 | #----------------------------------------------------------------------------- |
|
50 | #----------------------------------------------------------------------------- | |
51 | # Module globals and other constants |
|
51 | # Module globals and other constants | |
52 |
|
52 | |||
53 | log = logging.getLogger(__name__) |
|
53 | log = logging.getLogger(__name__) | |
54 |
|
54 | |||
55 | ########################################################################### |
|
55 | ########################################################################### | |
56 | # *** HACK *** |
|
56 | # *** HACK *** | |
57 | # We must start our own ipython object and heavily muck with it so that all the |
|
57 | # We must start our own ipython object and heavily muck with it so that all the | |
58 | # modifications IPython makes to system behavior don't send the doctest |
|
58 | # modifications IPython makes to system behavior don't send the doctest | |
59 | # machinery into a fit. This code should be considered a gross hack, but it |
|
59 | # machinery into a fit. This code should be considered a gross hack, but it | |
60 | # gets the job done. |
|
60 | # gets the job done. | |
61 |
|
61 | |||
62 | def default_argv(): |
|
62 | def default_argv(): | |
63 | """Return a valid default argv for creating testing instances of ipython""" |
|
63 | """Return a valid default argv for creating testing instances of ipython""" | |
64 |
|
64 | |||
65 | # Get the install directory for the user configuration and tell ipython to |
|
65 | # Get the install directory for the user configuration and tell ipython to | |
66 | # use the default profile from there. |
|
66 | # use the default profile from there. | |
67 | from IPython.config import userconfig |
|
67 | from IPython.config import userconfig | |
68 | ipcdir = os.path.dirname(userconfig.__file__) |
|
68 | ipcdir = os.path.dirname(userconfig.__file__) | |
69 | #ipconf = os.path.join(ipcdir,'ipy_user_conf.py') |
|
69 | #ipconf = os.path.join(ipcdir,'ipy_user_conf.py') | |
70 | ipconf = os.path.join(ipcdir,'ipythonrc') |
|
70 | ipconf = os.path.join(ipcdir,'ipythonrc') | |
71 | #print 'conf:',ipconf # dbg |
|
71 | #print 'conf:',ipconf # dbg | |
72 |
|
72 | |||
73 | return ['--colors=NoColor','--noterm_title','-rcfile=%s' % ipconf] |
|
73 | return ['--colors=NoColor','--noterm_title','-rcfile=%s' % ipconf] | |
74 |
|
74 | |||
75 |
|
75 | |||
76 | # Hack to modify the %run command so we can sync the user's namespace with the |
|
76 | # Hack to modify the %run command so we can sync the user's namespace with the | |
77 | # test globals. Once we move over to a clean magic system, this will be done |
|
77 | # test globals. Once we move over to a clean magic system, this will be done | |
78 | # with much less ugliness. |
|
78 | # with much less ugliness. | |
79 |
|
79 | |||
80 | class py_file_finder(object): |
|
80 | class py_file_finder(object): | |
81 | def __init__(self,test_filename): |
|
81 | def __init__(self,test_filename): | |
82 | self.test_filename = test_filename |
|
82 | self.test_filename = test_filename | |
83 |
|
83 | |||
84 | def __call__(self,name): |
|
84 | def __call__(self,name): | |
85 | from IPython.utils.genutils import get_py_filename |
|
85 | from IPython.utils.genutils import get_py_filename | |
86 | try: |
|
86 | try: | |
87 | return get_py_filename(name) |
|
87 | return get_py_filename(name) | |
88 | except IOError: |
|
88 | except IOError: | |
89 | test_dir = os.path.dirname(self.test_filename) |
|
89 | test_dir = os.path.dirname(self.test_filename) | |
90 | new_path = os.path.join(test_dir,name) |
|
90 | new_path = os.path.join(test_dir,name) | |
91 | return get_py_filename(new_path) |
|
91 | return get_py_filename(new_path) | |
92 |
|
92 | |||
93 |
|
93 | |||
94 | def _run_ns_sync(self,arg_s,runner=None): |
|
94 | def _run_ns_sync(self,arg_s,runner=None): | |
95 | """Modified version of %run that syncs testing namespaces. |
|
95 | """Modified version of %run that syncs testing namespaces. | |
96 |
|
96 | |||
97 | This is strictly needed for running doctests that call %run. |
|
97 | This is strictly needed for running doctests that call %run. | |
98 | """ |
|
98 | """ | |
99 |
|
99 | |||
100 | # When tests call %run directly (not via doctest) these function attributes |
|
100 | # When tests call %run directly (not via doctest) these function attributes | |
101 | # are not set |
|
101 | # are not set | |
102 | try: |
|
102 | try: | |
103 | fname = _run_ns_sync.test_filename |
|
103 | fname = _run_ns_sync.test_filename | |
104 | except AttributeError: |
|
104 | except AttributeError: | |
105 | fname = arg_s |
|
105 | fname = arg_s | |
106 |
|
106 | |||
107 | finder = py_file_finder(fname) |
|
107 | finder = py_file_finder(fname) | |
108 | out = _ip.IP.magic_run_ori(arg_s,runner,finder) |
|
108 | out = _ip.IP.magic_run_ori(arg_s,runner,finder) | |
109 |
|
109 | |||
110 | # Simliarly, there is no test_globs when a test is NOT a doctest |
|
110 | # Simliarly, there is no test_globs when a test is NOT a doctest | |
111 | if hasattr(_run_ns_sync,'test_globs'): |
|
111 | if hasattr(_run_ns_sync,'test_globs'): | |
112 | _run_ns_sync.test_globs.update(_ip.user_ns) |
|
112 | _run_ns_sync.test_globs.update(_ip.user_ns) | |
113 | return out |
|
113 | return out | |
114 |
|
114 | |||
115 |
|
115 | |||
116 | class ipnsdict(dict): |
|
116 | class ipnsdict(dict): | |
117 | """A special subclass of dict for use as an IPython namespace in doctests. |
|
117 | """A special subclass of dict for use as an IPython namespace in doctests. | |
118 |
|
118 | |||
119 | This subclass adds a simple checkpointing capability so that when testing |
|
119 | This subclass adds a simple checkpointing capability so that when testing | |
120 | machinery clears it (we use it as the test execution context), it doesn't |
|
120 | machinery clears it (we use it as the test execution context), it doesn't | |
121 | get completely destroyed. |
|
121 | get completely destroyed. | |
122 | """ |
|
122 | """ | |
123 |
|
123 | |||
124 | def __init__(self,*a): |
|
124 | def __init__(self,*a): | |
125 | dict.__init__(self,*a) |
|
125 | dict.__init__(self,*a) | |
126 | self._savedict = {} |
|
126 | self._savedict = {} | |
127 |
|
127 | |||
128 | def clear(self): |
|
128 | def clear(self): | |
129 | dict.clear(self) |
|
129 | dict.clear(self) | |
130 | self.update(self._savedict) |
|
130 | self.update(self._savedict) | |
131 |
|
131 | |||
132 | def _checkpoint(self): |
|
132 | def _checkpoint(self): | |
133 | self._savedict.clear() |
|
133 | self._savedict.clear() | |
134 | self._savedict.update(self) |
|
134 | self._savedict.update(self) | |
135 |
|
135 | |||
136 | def update(self,other): |
|
136 | def update(self,other): | |
137 | self._checkpoint() |
|
137 | self._checkpoint() | |
138 | dict.update(self,other) |
|
138 | dict.update(self,other) | |
139 |
|
139 | |||
140 | # If '_' is in the namespace, python won't set it when executing code, |
|
140 | # If '_' is in the namespace, python won't set it when executing code, | |
141 | # and we have examples that test it. So we ensure that the namespace |
|
141 | # and we have examples that test it. So we ensure that the namespace | |
142 | # is always 'clean' of it before it's used for test code execution. |
|
142 | # is always 'clean' of it before it's used for test code execution. | |
143 | self.pop('_',None) |
|
143 | self.pop('_',None) | |
144 |
|
144 | |||
145 | # The builtins namespace must *always* be the real __builtin__ module, |
|
145 | # The builtins namespace must *always* be the real __builtin__ module, | |
146 | # else weird stuff happens. The main ipython code does have provisions |
|
146 | # else weird stuff happens. The main ipython code does have provisions | |
147 | # to ensure this after %run, but since in this class we do some |
|
147 | # to ensure this after %run, but since in this class we do some | |
148 | # aggressive low-level cleaning of the execution namespace, we need to |
|
148 | # aggressive low-level cleaning of the execution namespace, we need to | |
149 | # correct for that ourselves, to ensure consitency with the 'real' |
|
149 | # correct for that ourselves, to ensure consitency with the 'real' | |
150 | # ipython. |
|
150 | # ipython. | |
151 | self['__builtins__'] = __builtin__ |
|
151 | self['__builtins__'] = __builtin__ | |
152 |
|
152 | |||
153 |
|
153 | |||
154 | def start_ipython(): |
|
154 | def start_ipython(): | |
155 | """Start a global IPython shell, which we need for IPython-specific syntax. |
|
155 | """Start a global IPython shell, which we need for IPython-specific syntax. | |
156 | """ |
|
156 | """ | |
157 |
|
157 | |||
158 | # This function should only ever run once! |
|
158 | # This function should only ever run once! | |
159 | if hasattr(start_ipython,'already_called'): |
|
159 | if hasattr(start_ipython,'already_called'): | |
160 | return |
|
160 | return | |
161 | start_ipython.already_called = True |
|
161 | start_ipython.already_called = True | |
162 |
|
162 | |||
163 | # Ok, first time we're called, go ahead |
|
163 | # Ok, first time we're called, go ahead | |
164 | import new |
|
164 | import new | |
165 |
|
165 | |||
166 | import IPython |
|
166 | import IPython | |
167 | from IPython.core import ipapi |
|
167 | from IPython.core import ipapi | |
168 |
|
168 | |||
169 | def xsys(cmd): |
|
169 | def xsys(cmd): | |
170 | """Execute a command and print its output. |
|
170 | """Execute a command and print its output. | |
171 |
|
171 | |||
172 | This is just a convenience function to replace the IPython system call |
|
172 | This is just a convenience function to replace the IPython system call | |
173 | with one that is more doctest-friendly. |
|
173 | with one that is more doctest-friendly. | |
174 | """ |
|
174 | """ | |
175 | cmd = _ip.IP.var_expand(cmd,depth=1) |
|
175 | cmd = _ip.IP.var_expand(cmd,depth=1) | |
176 | sys.stdout.write(commands.getoutput(cmd)) |
|
176 | sys.stdout.write(commands.getoutput(cmd)) | |
177 | sys.stdout.flush() |
|
177 | sys.stdout.flush() | |
178 |
|
178 | |||
179 | # Store certain global objects that IPython modifies |
|
179 | # Store certain global objects that IPython modifies | |
180 | _displayhook = sys.displayhook |
|
180 | _displayhook = sys.displayhook | |
181 | _excepthook = sys.excepthook |
|
181 | _excepthook = sys.excepthook | |
182 | _main = sys.modules.get('__main__') |
|
182 | _main = sys.modules.get('__main__') | |
183 |
|
183 | |||
184 | argv = default_argv() |
|
184 | argv = default_argv() | |
185 |
|
185 | |||
186 | # Start IPython instance. We customize it to start with minimal frills. |
|
186 | # Start IPython instance. We customize it to start with minimal frills. | |
187 | user_ns,global_ns = ipapi.make_user_namespaces(ipnsdict(),dict()) |
|
187 | user_ns,global_ns = ipapi.make_user_namespaces(ipnsdict(),dict()) | |
188 | IPython.shell.IPShell(argv,user_ns,global_ns) |
|
188 | IPython.shell.IPShell(argv,user_ns,global_ns) | |
189 |
|
189 | |||
190 | # Deactivate the various python system hooks added by ipython for |
|
190 | # Deactivate the various python system hooks added by ipython for | |
191 | # interactive convenience so we don't confuse the doctest system |
|
191 | # interactive convenience so we don't confuse the doctest system | |
192 | sys.modules['__main__'] = _main |
|
192 | sys.modules['__main__'] = _main | |
193 | sys.displayhook = _displayhook |
|
193 | sys.displayhook = _displayhook | |
194 | sys.excepthook = _excepthook |
|
194 | sys.excepthook = _excepthook | |
195 |
|
195 | |||
196 | # So that ipython magics and aliases can be doctested (they work by making |
|
196 | # So that ipython magics and aliases can be doctested (they work by making | |
197 | # a call into a global _ip object) |
|
197 | # a call into a global _ip object) | |
198 | _ip = ipapi.get() |
|
198 | _ip = ipapi.get() | |
199 | __builtin__._ip = _ip |
|
199 | __builtin__._ip = _ip | |
200 |
|
200 | |||
201 | # Modify the IPython system call with one that uses getoutput, so that we |
|
201 | # Modify the IPython system call with one that uses getoutput, so that we | |
202 | # can capture subcommands and print them to Python's stdout, otherwise the |
|
202 | # can capture subcommands and print them to Python's stdout, otherwise the | |
203 | # doctest machinery would miss them. |
|
203 | # doctest machinery would miss them. | |
204 | _ip.system = xsys |
|
204 | _ip.system = xsys | |
205 |
|
205 | |||
206 | # Also patch our %run function in. |
|
206 | # Also patch our %run function in. | |
207 | im = new.instancemethod(_run_ns_sync,_ip.IP, _ip.IP.__class__) |
|
207 | im = new.instancemethod(_run_ns_sync,_ip.IP, _ip.IP.__class__) | |
208 | _ip.IP.magic_run_ori = _ip.IP.magic_run |
|
208 | _ip.IP.magic_run_ori = _ip.IP.magic_run | |
209 | _ip.IP.magic_run = im |
|
209 | _ip.IP.magic_run = im | |
210 |
|
210 | |||
|
211 | # XXX - For some very bizarre reason, the loading of %history by default is | |||
|
212 | # failing. This needs to be fixed later, but for now at least this ensures | |||
|
213 | # that tests that use %hist run to completion. | |||
|
214 | from IPython.core import history | |||
|
215 | history.init_ipython(_ip) | |||
|
216 | if not hasattr(_ip.IP,'magic_history'): | |||
|
217 | raise RuntimeError("Can't load magics, aborting") | |||
|
218 | ||||
|
219 | ||||
211 | # The start call MUST be made here. I'm not sure yet why it doesn't work if |
|
220 | # The start call MUST be made here. I'm not sure yet why it doesn't work if | |
212 | # it is made later, at plugin initialization time, but in all my tests, that's |
|
221 | # it is made later, at plugin initialization time, but in all my tests, that's | |
213 | # the case. |
|
222 | # the case. | |
214 | start_ipython() |
|
223 | start_ipython() | |
215 |
|
224 | |||
216 | # *** END HACK *** |
|
225 | # *** END HACK *** | |
217 | ########################################################################### |
|
226 | ########################################################################### | |
218 |
|
227 | |||
219 | # Classes and functions |
|
228 | # Classes and functions | |
220 |
|
229 | |||
221 | def is_extension_module(filename): |
|
230 | def is_extension_module(filename): | |
222 | """Return whether the given filename is an extension module. |
|
231 | """Return whether the given filename is an extension module. | |
223 |
|
232 | |||
224 | This simply checks that the extension is either .so or .pyd. |
|
233 | This simply checks that the extension is either .so or .pyd. | |
225 | """ |
|
234 | """ | |
226 | return os.path.splitext(filename)[1].lower() in ('.so','.pyd') |
|
235 | return os.path.splitext(filename)[1].lower() in ('.so','.pyd') | |
227 |
|
236 | |||
228 |
|
237 | |||
229 | class DocTestSkip(object): |
|
238 | class DocTestSkip(object): | |
230 | """Object wrapper for doctests to be skipped.""" |
|
239 | """Object wrapper for doctests to be skipped.""" | |
231 |
|
240 | |||
232 | ds_skip = """Doctest to skip. |
|
241 | ds_skip = """Doctest to skip. | |
233 | >>> 1 #doctest: +SKIP |
|
242 | >>> 1 #doctest: +SKIP | |
234 | """ |
|
243 | """ | |
235 |
|
244 | |||
236 | def __init__(self,obj): |
|
245 | def __init__(self,obj): | |
237 | self.obj = obj |
|
246 | self.obj = obj | |
238 |
|
247 | |||
239 | def __getattribute__(self,key): |
|
248 | def __getattribute__(self,key): | |
240 | if key == '__doc__': |
|
249 | if key == '__doc__': | |
241 | return DocTestSkip.ds_skip |
|
250 | return DocTestSkip.ds_skip | |
242 | else: |
|
251 | else: | |
243 | return getattr(object.__getattribute__(self,'obj'),key) |
|
252 | return getattr(object.__getattribute__(self,'obj'),key) | |
244 |
|
253 | |||
245 | # Modified version of the one in the stdlib, that fixes a python bug (doctests |
|
254 | # Modified version of the one in the stdlib, that fixes a python bug (doctests | |
246 | # not found in extension modules, http://bugs.python.org/issue3158) |
|
255 | # not found in extension modules, http://bugs.python.org/issue3158) | |
247 | class DocTestFinder(doctest.DocTestFinder): |
|
256 | class DocTestFinder(doctest.DocTestFinder): | |
248 |
|
257 | |||
249 | def _from_module(self, module, object): |
|
258 | def _from_module(self, module, object): | |
250 | """ |
|
259 | """ | |
251 | Return true if the given object is defined in the given |
|
260 | Return true if the given object is defined in the given | |
252 | module. |
|
261 | module. | |
253 | """ |
|
262 | """ | |
254 | if module is None: |
|
263 | if module is None: | |
255 | return True |
|
264 | return True | |
256 | elif inspect.isfunction(object): |
|
265 | elif inspect.isfunction(object): | |
257 | return module.__dict__ is object.func_globals |
|
266 | return module.__dict__ is object.func_globals | |
258 | elif inspect.isbuiltin(object): |
|
267 | elif inspect.isbuiltin(object): | |
259 | return module.__name__ == object.__module__ |
|
268 | return module.__name__ == object.__module__ | |
260 | elif inspect.isclass(object): |
|
269 | elif inspect.isclass(object): | |
261 | return module.__name__ == object.__module__ |
|
270 | return module.__name__ == object.__module__ | |
262 | elif inspect.ismethod(object): |
|
271 | elif inspect.ismethod(object): | |
263 | # This one may be a bug in cython that fails to correctly set the |
|
272 | # This one may be a bug in cython that fails to correctly set the | |
264 | # __module__ attribute of methods, but since the same error is easy |
|
273 | # __module__ attribute of methods, but since the same error is easy | |
265 | # to make by extension code writers, having this safety in place |
|
274 | # to make by extension code writers, having this safety in place | |
266 | # isn't such a bad idea |
|
275 | # isn't such a bad idea | |
267 | return module.__name__ == object.im_class.__module__ |
|
276 | return module.__name__ == object.im_class.__module__ | |
268 | elif inspect.getmodule(object) is not None: |
|
277 | elif inspect.getmodule(object) is not None: | |
269 | return module is inspect.getmodule(object) |
|
278 | return module is inspect.getmodule(object) | |
270 | elif hasattr(object, '__module__'): |
|
279 | elif hasattr(object, '__module__'): | |
271 | return module.__name__ == object.__module__ |
|
280 | return module.__name__ == object.__module__ | |
272 | elif isinstance(object, property): |
|
281 | elif isinstance(object, property): | |
273 | return True # [XX] no way not be sure. |
|
282 | return True # [XX] no way not be sure. | |
274 | else: |
|
283 | else: | |
275 | raise ValueError("object must be a class or function") |
|
284 | raise ValueError("object must be a class or function") | |
276 |
|
285 | |||
277 | def _find(self, tests, obj, name, module, source_lines, globs, seen): |
|
286 | def _find(self, tests, obj, name, module, source_lines, globs, seen): | |
278 | """ |
|
287 | """ | |
279 | Find tests for the given object and any contained objects, and |
|
288 | Find tests for the given object and any contained objects, and | |
280 | add them to `tests`. |
|
289 | add them to `tests`. | |
281 | """ |
|
290 | """ | |
282 |
|
291 | |||
283 | if hasattr(obj,"skip_doctest"): |
|
292 | if hasattr(obj,"skip_doctest"): | |
284 | #print 'SKIPPING DOCTEST FOR:',obj # dbg |
|
293 | #print 'SKIPPING DOCTEST FOR:',obj # dbg | |
285 | obj = DocTestSkip(obj) |
|
294 | obj = DocTestSkip(obj) | |
286 |
|
295 | |||
287 | doctest.DocTestFinder._find(self,tests, obj, name, module, |
|
296 | doctest.DocTestFinder._find(self,tests, obj, name, module, | |
288 | source_lines, globs, seen) |
|
297 | source_lines, globs, seen) | |
289 |
|
298 | |||
290 | # Below we re-run pieces of the above method with manual modifications, |
|
299 | # Below we re-run pieces of the above method with manual modifications, | |
291 | # because the original code is buggy and fails to correctly identify |
|
300 | # because the original code is buggy and fails to correctly identify | |
292 | # doctests in extension modules. |
|
301 | # doctests in extension modules. | |
293 |
|
302 | |||
294 | # Local shorthands |
|
303 | # Local shorthands | |
295 | from inspect import isroutine, isclass, ismodule |
|
304 | from inspect import isroutine, isclass, ismodule | |
296 |
|
305 | |||
297 | # Look for tests in a module's contained objects. |
|
306 | # Look for tests in a module's contained objects. | |
298 | if inspect.ismodule(obj) and self._recurse: |
|
307 | if inspect.ismodule(obj) and self._recurse: | |
299 | for valname, val in obj.__dict__.items(): |
|
308 | for valname, val in obj.__dict__.items(): | |
300 | valname1 = '%s.%s' % (name, valname) |
|
309 | valname1 = '%s.%s' % (name, valname) | |
301 | if ( (isroutine(val) or isclass(val)) |
|
310 | if ( (isroutine(val) or isclass(val)) | |
302 | and self._from_module(module, val) ): |
|
311 | and self._from_module(module, val) ): | |
303 |
|
312 | |||
304 | self._find(tests, val, valname1, module, source_lines, |
|
313 | self._find(tests, val, valname1, module, source_lines, | |
305 | globs, seen) |
|
314 | globs, seen) | |
306 |
|
315 | |||
307 | # Look for tests in a class's contained objects. |
|
316 | # Look for tests in a class's contained objects. | |
308 | if inspect.isclass(obj) and self._recurse: |
|
317 | if inspect.isclass(obj) and self._recurse: | |
309 | #print 'RECURSE into class:',obj # dbg |
|
318 | #print 'RECURSE into class:',obj # dbg | |
310 | for valname, val in obj.__dict__.items(): |
|
319 | for valname, val in obj.__dict__.items(): | |
311 | # Special handling for staticmethod/classmethod. |
|
320 | # Special handling for staticmethod/classmethod. | |
312 | if isinstance(val, staticmethod): |
|
321 | if isinstance(val, staticmethod): | |
313 | val = getattr(obj, valname) |
|
322 | val = getattr(obj, valname) | |
314 | if isinstance(val, classmethod): |
|
323 | if isinstance(val, classmethod): | |
315 | val = getattr(obj, valname).im_func |
|
324 | val = getattr(obj, valname).im_func | |
316 |
|
325 | |||
317 | # Recurse to methods, properties, and nested classes. |
|
326 | # Recurse to methods, properties, and nested classes. | |
318 | if ((inspect.isfunction(val) or inspect.isclass(val) or |
|
327 | if ((inspect.isfunction(val) or inspect.isclass(val) or | |
319 | inspect.ismethod(val) or |
|
328 | inspect.ismethod(val) or | |
320 | isinstance(val, property)) and |
|
329 | isinstance(val, property)) and | |
321 | self._from_module(module, val)): |
|
330 | self._from_module(module, val)): | |
322 | valname = '%s.%s' % (name, valname) |
|
331 | valname = '%s.%s' % (name, valname) | |
323 | self._find(tests, val, valname, module, source_lines, |
|
332 | self._find(tests, val, valname, module, source_lines, | |
324 | globs, seen) |
|
333 | globs, seen) | |
325 |
|
334 | |||
326 |
|
335 | |||
327 | class IPDoctestOutputChecker(doctest.OutputChecker): |
|
336 | class IPDoctestOutputChecker(doctest.OutputChecker): | |
328 | """Second-chance checker with support for random tests. |
|
337 | """Second-chance checker with support for random tests. | |
329 |
|
338 | |||
330 | If the default comparison doesn't pass, this checker looks in the expected |
|
339 | If the default comparison doesn't pass, this checker looks in the expected | |
331 | output string for flags that tell us to ignore the output. |
|
340 | output string for flags that tell us to ignore the output. | |
332 | """ |
|
341 | """ | |
333 |
|
342 | |||
334 | random_re = re.compile(r'#\s*random\s+') |
|
343 | random_re = re.compile(r'#\s*random\s+') | |
335 |
|
344 | |||
336 | def check_output(self, want, got, optionflags): |
|
345 | def check_output(self, want, got, optionflags): | |
337 | """Check output, accepting special markers embedded in the output. |
|
346 | """Check output, accepting special markers embedded in the output. | |
338 |
|
347 | |||
339 | If the output didn't pass the default validation but the special string |
|
348 | If the output didn't pass the default validation but the special string | |
340 | '#random' is included, we accept it.""" |
|
349 | '#random' is included, we accept it.""" | |
341 |
|
350 | |||
342 | # Let the original tester verify first, in case people have valid tests |
|
351 | # Let the original tester verify first, in case people have valid tests | |
343 | # that happen to have a comment saying '#random' embedded in. |
|
352 | # that happen to have a comment saying '#random' embedded in. | |
344 | ret = doctest.OutputChecker.check_output(self, want, got, |
|
353 | ret = doctest.OutputChecker.check_output(self, want, got, | |
345 | optionflags) |
|
354 | optionflags) | |
346 | if not ret and self.random_re.search(want): |
|
355 | if not ret and self.random_re.search(want): | |
347 | #print >> sys.stderr, 'RANDOM OK:',want # dbg |
|
356 | #print >> sys.stderr, 'RANDOM OK:',want # dbg | |
348 | return True |
|
357 | return True | |
349 |
|
358 | |||
350 | return ret |
|
359 | return ret | |
351 |
|
360 | |||
352 |
|
361 | |||
353 | class DocTestCase(doctests.DocTestCase): |
|
362 | class DocTestCase(doctests.DocTestCase): | |
354 | """Proxy for DocTestCase: provides an address() method that |
|
363 | """Proxy for DocTestCase: provides an address() method that | |
355 | returns the correct address for the doctest case. Otherwise |
|
364 | returns the correct address for the doctest case. Otherwise | |
356 | acts as a proxy to the test case. To provide hints for address(), |
|
365 | acts as a proxy to the test case. To provide hints for address(), | |
357 | an obj may also be passed -- this will be used as the test object |
|
366 | an obj may also be passed -- this will be used as the test object | |
358 | for purposes of determining the test address, if it is provided. |
|
367 | for purposes of determining the test address, if it is provided. | |
359 | """ |
|
368 | """ | |
360 |
|
369 | |||
361 | # Note: this method was taken from numpy's nosetester module. |
|
370 | # Note: this method was taken from numpy's nosetester module. | |
362 |
|
371 | |||
363 | # Subclass nose.plugins.doctests.DocTestCase to work around a bug in |
|
372 | # Subclass nose.plugins.doctests.DocTestCase to work around a bug in | |
364 | # its constructor that blocks non-default arguments from being passed |
|
373 | # its constructor that blocks non-default arguments from being passed | |
365 | # down into doctest.DocTestCase |
|
374 | # down into doctest.DocTestCase | |
366 |
|
375 | |||
367 | def __init__(self, test, optionflags=0, setUp=None, tearDown=None, |
|
376 | def __init__(self, test, optionflags=0, setUp=None, tearDown=None, | |
368 | checker=None, obj=None, result_var='_'): |
|
377 | checker=None, obj=None, result_var='_'): | |
369 | self._result_var = result_var |
|
378 | self._result_var = result_var | |
370 | doctests.DocTestCase.__init__(self, test, |
|
379 | doctests.DocTestCase.__init__(self, test, | |
371 | optionflags=optionflags, |
|
380 | optionflags=optionflags, | |
372 | setUp=setUp, tearDown=tearDown, |
|
381 | setUp=setUp, tearDown=tearDown, | |
373 | checker=checker) |
|
382 | checker=checker) | |
374 | # Now we must actually copy the original constructor from the stdlib |
|
383 | # Now we must actually copy the original constructor from the stdlib | |
375 | # doctest class, because we can't call it directly and a bug in nose |
|
384 | # doctest class, because we can't call it directly and a bug in nose | |
376 | # means it never gets passed the right arguments. |
|
385 | # means it never gets passed the right arguments. | |
377 |
|
386 | |||
378 | self._dt_optionflags = optionflags |
|
387 | self._dt_optionflags = optionflags | |
379 | self._dt_checker = checker |
|
388 | self._dt_checker = checker | |
380 | self._dt_test = test |
|
389 | self._dt_test = test | |
381 | self._dt_setUp = setUp |
|
390 | self._dt_setUp = setUp | |
382 | self._dt_tearDown = tearDown |
|
391 | self._dt_tearDown = tearDown | |
383 |
|
392 | |||
384 | # XXX - store this runner once in the object! |
|
393 | # XXX - store this runner once in the object! | |
385 | runner = IPDocTestRunner(optionflags=optionflags, |
|
394 | runner = IPDocTestRunner(optionflags=optionflags, | |
386 | checker=checker, verbose=False) |
|
395 | checker=checker, verbose=False) | |
387 | self._dt_runner = runner |
|
396 | self._dt_runner = runner | |
388 |
|
397 | |||
389 |
|
398 | |||
390 | # Each doctest should remember what directory it was loaded from... |
|
399 | # Each doctest should remember what directory it was loaded from... | |
391 | self._ori_dir = os.getcwd() |
|
400 | self._ori_dir = os.getcwd() | |
392 |
|
401 | |||
393 | # Modified runTest from the default stdlib |
|
402 | # Modified runTest from the default stdlib | |
394 | def runTest(self): |
|
403 | def runTest(self): | |
395 | test = self._dt_test |
|
404 | test = self._dt_test | |
396 | runner = self._dt_runner |
|
405 | runner = self._dt_runner | |
397 |
|
406 | |||
398 | old = sys.stdout |
|
407 | old = sys.stdout | |
399 | new = StringIO() |
|
408 | new = StringIO() | |
400 | optionflags = self._dt_optionflags |
|
409 | optionflags = self._dt_optionflags | |
401 |
|
410 | |||
402 | if not (optionflags & REPORTING_FLAGS): |
|
411 | if not (optionflags & REPORTING_FLAGS): | |
403 | # The option flags don't include any reporting flags, |
|
412 | # The option flags don't include any reporting flags, | |
404 | # so add the default reporting flags |
|
413 | # so add the default reporting flags | |
405 | optionflags |= _unittest_reportflags |
|
414 | optionflags |= _unittest_reportflags | |
406 |
|
415 | |||
407 | try: |
|
416 | try: | |
408 | # Save our current directory and switch out to the one where the |
|
417 | # Save our current directory and switch out to the one where the | |
409 | # test was originally created, in case another doctest did a |
|
418 | # test was originally created, in case another doctest did a | |
410 | # directory change. We'll restore this in the finally clause. |
|
419 | # directory change. We'll restore this in the finally clause. | |
411 | curdir = os.getcwd() |
|
420 | curdir = os.getcwd() | |
412 | os.chdir(self._ori_dir) |
|
421 | os.chdir(self._ori_dir) | |
413 |
|
422 | |||
414 | runner.DIVIDER = "-"*70 |
|
423 | runner.DIVIDER = "-"*70 | |
415 | failures, tries = runner.run(test,out=new.write, |
|
424 | failures, tries = runner.run(test,out=new.write, | |
416 | clear_globs=False) |
|
425 | clear_globs=False) | |
417 | finally: |
|
426 | finally: | |
418 | sys.stdout = old |
|
427 | sys.stdout = old | |
419 | os.chdir(curdir) |
|
428 | os.chdir(curdir) | |
420 |
|
429 | |||
421 | if failures: |
|
430 | if failures: | |
422 | raise self.failureException(self.format_failure(new.getvalue())) |
|
431 | raise self.failureException(self.format_failure(new.getvalue())) | |
423 |
|
432 | |||
424 | def setUp(self): |
|
433 | def setUp(self): | |
425 | """Modified test setup that syncs with ipython namespace""" |
|
434 | """Modified test setup that syncs with ipython namespace""" | |
426 |
|
435 | |||
427 | if isinstance(self._dt_test.examples[0],IPExample): |
|
436 | if isinstance(self._dt_test.examples[0],IPExample): | |
428 | # for IPython examples *only*, we swap the globals with the ipython |
|
437 | # for IPython examples *only*, we swap the globals with the ipython | |
429 | # namespace, after updating it with the globals (which doctest |
|
438 | # namespace, after updating it with the globals (which doctest | |
430 | # fills with the necessary info from the module being tested). |
|
439 | # fills with the necessary info from the module being tested). | |
431 | _ip.IP.user_ns.update(self._dt_test.globs) |
|
440 | _ip.IP.user_ns.update(self._dt_test.globs) | |
432 | self._dt_test.globs = _ip.IP.user_ns |
|
441 | self._dt_test.globs = _ip.IP.user_ns | |
433 |
|
442 | |||
434 | doctests.DocTestCase.setUp(self) |
|
443 | doctests.DocTestCase.setUp(self) | |
435 |
|
444 | |||
436 |
|
445 | |||
437 | # A simple subclassing of the original with a different class name, so we can |
|
446 | # A simple subclassing of the original with a different class name, so we can | |
438 | # distinguish and treat differently IPython examples from pure python ones. |
|
447 | # distinguish and treat differently IPython examples from pure python ones. | |
439 | class IPExample(doctest.Example): pass |
|
448 | class IPExample(doctest.Example): pass | |
440 |
|
449 | |||
441 |
|
450 | |||
442 | class IPExternalExample(doctest.Example): |
|
451 | class IPExternalExample(doctest.Example): | |
443 | """Doctest examples to be run in an external process.""" |
|
452 | """Doctest examples to be run in an external process.""" | |
444 |
|
453 | |||
445 | def __init__(self, source, want, exc_msg=None, lineno=0, indent=0, |
|
454 | def __init__(self, source, want, exc_msg=None, lineno=0, indent=0, | |
446 | options=None): |
|
455 | options=None): | |
447 | # Parent constructor |
|
456 | # Parent constructor | |
448 | doctest.Example.__init__(self,source,want,exc_msg,lineno,indent,options) |
|
457 | doctest.Example.__init__(self,source,want,exc_msg,lineno,indent,options) | |
449 |
|
458 | |||
450 | # An EXTRA newline is needed to prevent pexpect hangs |
|
459 | # An EXTRA newline is needed to prevent pexpect hangs | |
451 | self.source += '\n' |
|
460 | self.source += '\n' | |
452 |
|
461 | |||
453 |
|
462 | |||
454 | class IPDocTestParser(doctest.DocTestParser): |
|
463 | class IPDocTestParser(doctest.DocTestParser): | |
455 | """ |
|
464 | """ | |
456 | A class used to parse strings containing doctest examples. |
|
465 | A class used to parse strings containing doctest examples. | |
457 |
|
466 | |||
458 | Note: This is a version modified to properly recognize IPython input and |
|
467 | Note: This is a version modified to properly recognize IPython input and | |
459 | convert any IPython examples into valid Python ones. |
|
468 | convert any IPython examples into valid Python ones. | |
460 | """ |
|
469 | """ | |
461 | # This regular expression is used to find doctest examples in a |
|
470 | # This regular expression is used to find doctest examples in a | |
462 | # string. It defines three groups: `source` is the source code |
|
471 | # string. It defines three groups: `source` is the source code | |
463 | # (including leading indentation and prompts); `indent` is the |
|
472 | # (including leading indentation and prompts); `indent` is the | |
464 | # indentation of the first (PS1) line of the source code; and |
|
473 | # indentation of the first (PS1) line of the source code; and | |
465 | # `want` is the expected output (including leading indentation). |
|
474 | # `want` is the expected output (including leading indentation). | |
466 |
|
475 | |||
467 | # Classic Python prompts or default IPython ones |
|
476 | # Classic Python prompts or default IPython ones | |
468 | _PS1_PY = r'>>>' |
|
477 | _PS1_PY = r'>>>' | |
469 | _PS2_PY = r'\.\.\.' |
|
478 | _PS2_PY = r'\.\.\.' | |
470 |
|
479 | |||
471 | _PS1_IP = r'In\ \[\d+\]:' |
|
480 | _PS1_IP = r'In\ \[\d+\]:' | |
472 | _PS2_IP = r'\ \ \ \.\.\.+:' |
|
481 | _PS2_IP = r'\ \ \ \.\.\.+:' | |
473 |
|
482 | |||
474 | _RE_TPL = r''' |
|
483 | _RE_TPL = r''' | |
475 | # Source consists of a PS1 line followed by zero or more PS2 lines. |
|
484 | # Source consists of a PS1 line followed by zero or more PS2 lines. | |
476 | (?P<source> |
|
485 | (?P<source> | |
477 | (?:^(?P<indent> [ ]*) (?P<ps1> %s) .*) # PS1 line |
|
486 | (?:^(?P<indent> [ ]*) (?P<ps1> %s) .*) # PS1 line | |
478 | (?:\n [ ]* (?P<ps2> %s) .*)*) # PS2 lines |
|
487 | (?:\n [ ]* (?P<ps2> %s) .*)*) # PS2 lines | |
479 | \n? # a newline |
|
488 | \n? # a newline | |
480 | # Want consists of any non-blank lines that do not start with PS1. |
|
489 | # Want consists of any non-blank lines that do not start with PS1. | |
481 | (?P<want> (?:(?![ ]*$) # Not a blank line |
|
490 | (?P<want> (?:(?![ ]*$) # Not a blank line | |
482 | (?![ ]*%s) # Not a line starting with PS1 |
|
491 | (?![ ]*%s) # Not a line starting with PS1 | |
483 | (?![ ]*%s) # Not a line starting with PS2 |
|
492 | (?![ ]*%s) # Not a line starting with PS2 | |
484 | .*$\n? # But any other line |
|
493 | .*$\n? # But any other line | |
485 | )*) |
|
494 | )*) | |
486 | ''' |
|
495 | ''' | |
487 |
|
496 | |||
488 | _EXAMPLE_RE_PY = re.compile( _RE_TPL % (_PS1_PY,_PS2_PY,_PS1_PY,_PS2_PY), |
|
497 | _EXAMPLE_RE_PY = re.compile( _RE_TPL % (_PS1_PY,_PS2_PY,_PS1_PY,_PS2_PY), | |
489 | re.MULTILINE | re.VERBOSE) |
|
498 | re.MULTILINE | re.VERBOSE) | |
490 |
|
499 | |||
491 | _EXAMPLE_RE_IP = re.compile( _RE_TPL % (_PS1_IP,_PS2_IP,_PS1_IP,_PS2_IP), |
|
500 | _EXAMPLE_RE_IP = re.compile( _RE_TPL % (_PS1_IP,_PS2_IP,_PS1_IP,_PS2_IP), | |
492 | re.MULTILINE | re.VERBOSE) |
|
501 | re.MULTILINE | re.VERBOSE) | |
493 |
|
502 | |||
494 | # Mark a test as being fully random. In this case, we simply append the |
|
503 | # Mark a test as being fully random. In this case, we simply append the | |
495 | # random marker ('#random') to each individual example's output. This way |
|
504 | # random marker ('#random') to each individual example's output. This way | |
496 | # we don't need to modify any other code. |
|
505 | # we don't need to modify any other code. | |
497 | _RANDOM_TEST = re.compile(r'#\s*all-random\s+') |
|
506 | _RANDOM_TEST = re.compile(r'#\s*all-random\s+') | |
498 |
|
507 | |||
499 | # Mark tests to be executed in an external process - currently unsupported. |
|
508 | # Mark tests to be executed in an external process - currently unsupported. | |
500 | _EXTERNAL_IP = re.compile(r'#\s*ipdoctest:\s*EXTERNAL') |
|
509 | _EXTERNAL_IP = re.compile(r'#\s*ipdoctest:\s*EXTERNAL') | |
501 |
|
510 | |||
502 | def ip2py(self,source): |
|
511 | def ip2py(self,source): | |
503 | """Convert input IPython source into valid Python.""" |
|
512 | """Convert input IPython source into valid Python.""" | |
504 | out = [] |
|
513 | out = [] | |
505 | newline = out.append |
|
514 | newline = out.append | |
506 | #print 'IPSRC:\n',source,'\n###' # dbg |
|
515 | #print 'IPSRC:\n',source,'\n###' # dbg | |
507 | # The input source must be first stripped of all bracketing whitespace |
|
516 | # The input source must be first stripped of all bracketing whitespace | |
508 | # and turned into lines, so it looks to the parser like regular user |
|
517 | # and turned into lines, so it looks to the parser like regular user | |
509 | # input |
|
518 | # input | |
510 | for lnum,line in enumerate(source.strip().splitlines()): |
|
519 | for lnum,line in enumerate(source.strip().splitlines()): | |
511 | newline(_ip.IP.prefilter(line,lnum>0)) |
|
520 | newline(_ip.IP.prefilter(line,lnum>0)) | |
512 | newline('') # ensure a closing newline, needed by doctest |
|
521 | newline('') # ensure a closing newline, needed by doctest | |
513 | #print "PYSRC:", '\n'.join(out) # dbg |
|
522 | #print "PYSRC:", '\n'.join(out) # dbg | |
514 | return '\n'.join(out) |
|
523 | return '\n'.join(out) | |
515 |
|
524 | |||
516 | def parse(self, string, name='<string>'): |
|
525 | def parse(self, string, name='<string>'): | |
517 | """ |
|
526 | """ | |
518 | Divide the given string into examples and intervening text, |
|
527 | Divide the given string into examples and intervening text, | |
519 | and return them as a list of alternating Examples and strings. |
|
528 | and return them as a list of alternating Examples and strings. | |
520 | Line numbers for the Examples are 0-based. The optional |
|
529 | Line numbers for the Examples are 0-based. The optional | |
521 | argument `name` is a name identifying this string, and is only |
|
530 | argument `name` is a name identifying this string, and is only | |
522 | used for error messages. |
|
531 | used for error messages. | |
523 | """ |
|
532 | """ | |
524 |
|
533 | |||
525 | #print 'Parse string:\n',string # dbg |
|
534 | #print 'Parse string:\n',string # dbg | |
526 |
|
535 | |||
527 | string = string.expandtabs() |
|
536 | string = string.expandtabs() | |
528 | # If all lines begin with the same indentation, then strip it. |
|
537 | # If all lines begin with the same indentation, then strip it. | |
529 | min_indent = self._min_indent(string) |
|
538 | min_indent = self._min_indent(string) | |
530 | if min_indent > 0: |
|
539 | if min_indent > 0: | |
531 | string = '\n'.join([l[min_indent:] for l in string.split('\n')]) |
|
540 | string = '\n'.join([l[min_indent:] for l in string.split('\n')]) | |
532 |
|
541 | |||
533 | output = [] |
|
542 | output = [] | |
534 | charno, lineno = 0, 0 |
|
543 | charno, lineno = 0, 0 | |
535 |
|
544 | |||
536 | # We make 'all random' tests by adding the '# random' mark to every |
|
545 | # We make 'all random' tests by adding the '# random' mark to every | |
537 | # block of output in the test. |
|
546 | # block of output in the test. | |
538 | if self._RANDOM_TEST.search(string): |
|
547 | if self._RANDOM_TEST.search(string): | |
539 | random_marker = '\n# random' |
|
548 | random_marker = '\n# random' | |
540 | else: |
|
549 | else: | |
541 | random_marker = '' |
|
550 | random_marker = '' | |
542 |
|
551 | |||
543 | # Whether to convert the input from ipython to python syntax |
|
552 | # Whether to convert the input from ipython to python syntax | |
544 | ip2py = False |
|
553 | ip2py = False | |
545 | # Find all doctest examples in the string. First, try them as Python |
|
554 | # Find all doctest examples in the string. First, try them as Python | |
546 | # examples, then as IPython ones |
|
555 | # examples, then as IPython ones | |
547 | terms = list(self._EXAMPLE_RE_PY.finditer(string)) |
|
556 | terms = list(self._EXAMPLE_RE_PY.finditer(string)) | |
548 | if terms: |
|
557 | if terms: | |
549 | # Normal Python example |
|
558 | # Normal Python example | |
550 | #print '-'*70 # dbg |
|
559 | #print '-'*70 # dbg | |
551 | #print 'PyExample, Source:\n',string # dbg |
|
560 | #print 'PyExample, Source:\n',string # dbg | |
552 | #print '-'*70 # dbg |
|
561 | #print '-'*70 # dbg | |
553 | Example = doctest.Example |
|
562 | Example = doctest.Example | |
554 | else: |
|
563 | else: | |
555 | # It's an ipython example. Note that IPExamples are run |
|
564 | # It's an ipython example. Note that IPExamples are run | |
556 | # in-process, so their syntax must be turned into valid python. |
|
565 | # in-process, so their syntax must be turned into valid python. | |
557 | # IPExternalExamples are run out-of-process (via pexpect) so they |
|
566 | # IPExternalExamples are run out-of-process (via pexpect) so they | |
558 | # don't need any filtering (a real ipython will be executing them). |
|
567 | # don't need any filtering (a real ipython will be executing them). | |
559 | terms = list(self._EXAMPLE_RE_IP.finditer(string)) |
|
568 | terms = list(self._EXAMPLE_RE_IP.finditer(string)) | |
560 | if self._EXTERNAL_IP.search(string): |
|
569 | if self._EXTERNAL_IP.search(string): | |
561 | #print '-'*70 # dbg |
|
570 | #print '-'*70 # dbg | |
562 | #print 'IPExternalExample, Source:\n',string # dbg |
|
571 | #print 'IPExternalExample, Source:\n',string # dbg | |
563 | #print '-'*70 # dbg |
|
572 | #print '-'*70 # dbg | |
564 | Example = IPExternalExample |
|
573 | Example = IPExternalExample | |
565 | else: |
|
574 | else: | |
566 | #print '-'*70 # dbg |
|
575 | #print '-'*70 # dbg | |
567 | #print 'IPExample, Source:\n',string # dbg |
|
576 | #print 'IPExample, Source:\n',string # dbg | |
568 | #print '-'*70 # dbg |
|
577 | #print '-'*70 # dbg | |
569 | Example = IPExample |
|
578 | Example = IPExample | |
570 | ip2py = True |
|
579 | ip2py = True | |
571 |
|
580 | |||
572 | for m in terms: |
|
581 | for m in terms: | |
573 | # Add the pre-example text to `output`. |
|
582 | # Add the pre-example text to `output`. | |
574 | output.append(string[charno:m.start()]) |
|
583 | output.append(string[charno:m.start()]) | |
575 | # Update lineno (lines before this example) |
|
584 | # Update lineno (lines before this example) | |
576 | lineno += string.count('\n', charno, m.start()) |
|
585 | lineno += string.count('\n', charno, m.start()) | |
577 | # Extract info from the regexp match. |
|
586 | # Extract info from the regexp match. | |
578 | (source, options, want, exc_msg) = \ |
|
587 | (source, options, want, exc_msg) = \ | |
579 | self._parse_example(m, name, lineno,ip2py) |
|
588 | self._parse_example(m, name, lineno,ip2py) | |
580 |
|
589 | |||
581 | # Append the random-output marker (it defaults to empty in most |
|
590 | # Append the random-output marker (it defaults to empty in most | |
582 | # cases, it's only non-empty for 'all-random' tests): |
|
591 | # cases, it's only non-empty for 'all-random' tests): | |
583 | want += random_marker |
|
592 | want += random_marker | |
584 |
|
593 | |||
585 | if Example is IPExternalExample: |
|
594 | if Example is IPExternalExample: | |
586 | options[doctest.NORMALIZE_WHITESPACE] = True |
|
595 | options[doctest.NORMALIZE_WHITESPACE] = True | |
587 | want += '\n' |
|
596 | want += '\n' | |
588 |
|
597 | |||
589 | # Create an Example, and add it to the list. |
|
598 | # Create an Example, and add it to the list. | |
590 | if not self._IS_BLANK_OR_COMMENT(source): |
|
599 | if not self._IS_BLANK_OR_COMMENT(source): | |
591 | output.append(Example(source, want, exc_msg, |
|
600 | output.append(Example(source, want, exc_msg, | |
592 | lineno=lineno, |
|
601 | lineno=lineno, | |
593 | indent=min_indent+len(m.group('indent')), |
|
602 | indent=min_indent+len(m.group('indent')), | |
594 | options=options)) |
|
603 | options=options)) | |
595 | # Update lineno (lines inside this example) |
|
604 | # Update lineno (lines inside this example) | |
596 | lineno += string.count('\n', m.start(), m.end()) |
|
605 | lineno += string.count('\n', m.start(), m.end()) | |
597 | # Update charno. |
|
606 | # Update charno. | |
598 | charno = m.end() |
|
607 | charno = m.end() | |
599 | # Add any remaining post-example text to `output`. |
|
608 | # Add any remaining post-example text to `output`. | |
600 | output.append(string[charno:]) |
|
609 | output.append(string[charno:]) | |
601 | return output |
|
610 | return output | |
602 |
|
611 | |||
603 | def _parse_example(self, m, name, lineno,ip2py=False): |
|
612 | def _parse_example(self, m, name, lineno,ip2py=False): | |
604 | """ |
|
613 | """ | |
605 | Given a regular expression match from `_EXAMPLE_RE` (`m`), |
|
614 | Given a regular expression match from `_EXAMPLE_RE` (`m`), | |
606 | return a pair `(source, want)`, where `source` is the matched |
|
615 | return a pair `(source, want)`, where `source` is the matched | |
607 | example's source code (with prompts and indentation stripped); |
|
616 | example's source code (with prompts and indentation stripped); | |
608 | and `want` is the example's expected output (with indentation |
|
617 | and `want` is the example's expected output (with indentation | |
609 | stripped). |
|
618 | stripped). | |
610 |
|
619 | |||
611 | `name` is the string's name, and `lineno` is the line number |
|
620 | `name` is the string's name, and `lineno` is the line number | |
612 | where the example starts; both are used for error messages. |
|
621 | where the example starts; both are used for error messages. | |
613 |
|
622 | |||
614 | Optional: |
|
623 | Optional: | |
615 | `ip2py`: if true, filter the input via IPython to convert the syntax |
|
624 | `ip2py`: if true, filter the input via IPython to convert the syntax | |
616 | into valid python. |
|
625 | into valid python. | |
617 | """ |
|
626 | """ | |
618 |
|
627 | |||
619 | # Get the example's indentation level. |
|
628 | # Get the example's indentation level. | |
620 | indent = len(m.group('indent')) |
|
629 | indent = len(m.group('indent')) | |
621 |
|
630 | |||
622 | # Divide source into lines; check that they're properly |
|
631 | # Divide source into lines; check that they're properly | |
623 | # indented; and then strip their indentation & prompts. |
|
632 | # indented; and then strip their indentation & prompts. | |
624 | source_lines = m.group('source').split('\n') |
|
633 | source_lines = m.group('source').split('\n') | |
625 |
|
634 | |||
626 | # We're using variable-length input prompts |
|
635 | # We're using variable-length input prompts | |
627 | ps1 = m.group('ps1') |
|
636 | ps1 = m.group('ps1') | |
628 | ps2 = m.group('ps2') |
|
637 | ps2 = m.group('ps2') | |
629 | ps1_len = len(ps1) |
|
638 | ps1_len = len(ps1) | |
630 |
|
639 | |||
631 | self._check_prompt_blank(source_lines, indent, name, lineno,ps1_len) |
|
640 | self._check_prompt_blank(source_lines, indent, name, lineno,ps1_len) | |
632 | if ps2: |
|
641 | if ps2: | |
633 | self._check_prefix(source_lines[1:], ' '*indent + ps2, name, lineno) |
|
642 | self._check_prefix(source_lines[1:], ' '*indent + ps2, name, lineno) | |
634 |
|
643 | |||
635 | source = '\n'.join([sl[indent+ps1_len+1:] for sl in source_lines]) |
|
644 | source = '\n'.join([sl[indent+ps1_len+1:] for sl in source_lines]) | |
636 |
|
645 | |||
637 | if ip2py: |
|
646 | if ip2py: | |
638 | # Convert source input from IPython into valid Python syntax |
|
647 | # Convert source input from IPython into valid Python syntax | |
639 | source = self.ip2py(source) |
|
648 | source = self.ip2py(source) | |
640 |
|
649 | |||
641 | # Divide want into lines; check that it's properly indented; and |
|
650 | # Divide want into lines; check that it's properly indented; and | |
642 | # then strip the indentation. Spaces before the last newline should |
|
651 | # then strip the indentation. Spaces before the last newline should | |
643 | # be preserved, so plain rstrip() isn't good enough. |
|
652 | # be preserved, so plain rstrip() isn't good enough. | |
644 | want = m.group('want') |
|
653 | want = m.group('want') | |
645 | want_lines = want.split('\n') |
|
654 | want_lines = want.split('\n') | |
646 | if len(want_lines) > 1 and re.match(r' *$', want_lines[-1]): |
|
655 | if len(want_lines) > 1 and re.match(r' *$', want_lines[-1]): | |
647 | del want_lines[-1] # forget final newline & spaces after it |
|
656 | del want_lines[-1] # forget final newline & spaces after it | |
648 | self._check_prefix(want_lines, ' '*indent, name, |
|
657 | self._check_prefix(want_lines, ' '*indent, name, | |
649 | lineno + len(source_lines)) |
|
658 | lineno + len(source_lines)) | |
650 |
|
659 | |||
651 | # Remove ipython output prompt that might be present in the first line |
|
660 | # Remove ipython output prompt that might be present in the first line | |
652 | want_lines[0] = re.sub(r'Out\[\d+\]: \s*?\n?','',want_lines[0]) |
|
661 | want_lines[0] = re.sub(r'Out\[\d+\]: \s*?\n?','',want_lines[0]) | |
653 |
|
662 | |||
654 | want = '\n'.join([wl[indent:] for wl in want_lines]) |
|
663 | want = '\n'.join([wl[indent:] for wl in want_lines]) | |
655 |
|
664 | |||
656 | # If `want` contains a traceback message, then extract it. |
|
665 | # If `want` contains a traceback message, then extract it. | |
657 | m = self._EXCEPTION_RE.match(want) |
|
666 | m = self._EXCEPTION_RE.match(want) | |
658 | if m: |
|
667 | if m: | |
659 | exc_msg = m.group('msg') |
|
668 | exc_msg = m.group('msg') | |
660 | else: |
|
669 | else: | |
661 | exc_msg = None |
|
670 | exc_msg = None | |
662 |
|
671 | |||
663 | # Extract options from the source. |
|
672 | # Extract options from the source. | |
664 | options = self._find_options(source, name, lineno) |
|
673 | options = self._find_options(source, name, lineno) | |
665 |
|
674 | |||
666 | return source, options, want, exc_msg |
|
675 | return source, options, want, exc_msg | |
667 |
|
676 | |||
668 | def _check_prompt_blank(self, lines, indent, name, lineno, ps1_len): |
|
677 | def _check_prompt_blank(self, lines, indent, name, lineno, ps1_len): | |
669 | """ |
|
678 | """ | |
670 | Given the lines of a source string (including prompts and |
|
679 | Given the lines of a source string (including prompts and | |
671 | leading indentation), check to make sure that every prompt is |
|
680 | leading indentation), check to make sure that every prompt is | |
672 | followed by a space character. If any line is not followed by |
|
681 | followed by a space character. If any line is not followed by | |
673 | a space character, then raise ValueError. |
|
682 | a space character, then raise ValueError. | |
674 |
|
683 | |||
675 | Note: IPython-modified version which takes the input prompt length as a |
|
684 | Note: IPython-modified version which takes the input prompt length as a | |
676 | parameter, so that prompts of variable length can be dealt with. |
|
685 | parameter, so that prompts of variable length can be dealt with. | |
677 | """ |
|
686 | """ | |
678 | space_idx = indent+ps1_len |
|
687 | space_idx = indent+ps1_len | |
679 | min_len = space_idx+1 |
|
688 | min_len = space_idx+1 | |
680 | for i, line in enumerate(lines): |
|
689 | for i, line in enumerate(lines): | |
681 | if len(line) >= min_len and line[space_idx] != ' ': |
|
690 | if len(line) >= min_len and line[space_idx] != ' ': | |
682 | raise ValueError('line %r of the docstring for %s ' |
|
691 | raise ValueError('line %r of the docstring for %s ' | |
683 | 'lacks blank after %s: %r' % |
|
692 | 'lacks blank after %s: %r' % | |
684 | (lineno+i+1, name, |
|
693 | (lineno+i+1, name, | |
685 | line[indent:space_idx], line)) |
|
694 | line[indent:space_idx], line)) | |
686 |
|
695 | |||
687 |
|
696 | |||
688 | SKIP = doctest.register_optionflag('SKIP') |
|
697 | SKIP = doctest.register_optionflag('SKIP') | |
689 |
|
698 | |||
690 |
|
699 | |||
691 | class IPDocTestRunner(doctest.DocTestRunner,object): |
|
700 | class IPDocTestRunner(doctest.DocTestRunner,object): | |
692 | """Test runner that synchronizes the IPython namespace with test globals. |
|
701 | """Test runner that synchronizes the IPython namespace with test globals. | |
693 | """ |
|
702 | """ | |
694 |
|
703 | |||
695 | def run(self, test, compileflags=None, out=None, clear_globs=True): |
|
704 | def run(self, test, compileflags=None, out=None, clear_globs=True): | |
696 |
|
705 | |||
697 | # Hack: ipython needs access to the execution context of the example, |
|
706 | # Hack: ipython needs access to the execution context of the example, | |
698 | # so that it can propagate user variables loaded by %run into |
|
707 | # so that it can propagate user variables loaded by %run into | |
699 | # test.globs. We put them here into our modified %run as a function |
|
708 | # test.globs. We put them here into our modified %run as a function | |
700 | # attribute. Our new %run will then only make the namespace update |
|
709 | # attribute. Our new %run will then only make the namespace update | |
701 | # when called (rather than unconconditionally updating test.globs here |
|
710 | # when called (rather than unconconditionally updating test.globs here | |
702 | # for all examples, most of which won't be calling %run anyway). |
|
711 | # for all examples, most of which won't be calling %run anyway). | |
703 | _run_ns_sync.test_globs = test.globs |
|
712 | _run_ns_sync.test_globs = test.globs | |
704 | _run_ns_sync.test_filename = test.filename |
|
713 | _run_ns_sync.test_filename = test.filename | |
705 |
|
714 | |||
706 | return super(IPDocTestRunner,self).run(test, |
|
715 | return super(IPDocTestRunner,self).run(test, | |
707 | compileflags,out,clear_globs) |
|
716 | compileflags,out,clear_globs) | |
708 |
|
717 | |||
709 |
|
718 | |||
710 | class DocFileCase(doctest.DocFileCase): |
|
719 | class DocFileCase(doctest.DocFileCase): | |
711 | """Overrides to provide filename |
|
720 | """Overrides to provide filename | |
712 | """ |
|
721 | """ | |
713 | def address(self): |
|
722 | def address(self): | |
714 | return (self._dt_test.filename, None, None) |
|
723 | return (self._dt_test.filename, None, None) | |
715 |
|
724 | |||
716 |
|
725 | |||
717 | class ExtensionDoctest(doctests.Doctest): |
|
726 | class ExtensionDoctest(doctests.Doctest): | |
718 | """Nose Plugin that supports doctests in extension modules. |
|
727 | """Nose Plugin that supports doctests in extension modules. | |
719 | """ |
|
728 | """ | |
720 | name = 'extdoctest' # call nosetests with --with-extdoctest |
|
729 | name = 'extdoctest' # call nosetests with --with-extdoctest | |
721 | enabled = True |
|
730 | enabled = True | |
722 |
|
731 | |||
723 | def __init__(self,exclude_patterns=None): |
|
732 | def __init__(self,exclude_patterns=None): | |
724 | """Create a new ExtensionDoctest plugin. |
|
733 | """Create a new ExtensionDoctest plugin. | |
725 |
|
734 | |||
726 | Parameters |
|
735 | Parameters | |
727 | ---------- |
|
736 | ---------- | |
728 |
|
737 | |||
729 | exclude_patterns : sequence of strings, optional |
|
738 | exclude_patterns : sequence of strings, optional | |
730 | These patterns are compiled as regular expressions, subsequently used |
|
739 | These patterns are compiled as regular expressions, subsequently used | |
731 | to exclude any filename which matches them from inclusion in the test |
|
740 | to exclude any filename which matches them from inclusion in the test | |
732 | suite (using pattern.search(), NOT pattern.match() ). |
|
741 | suite (using pattern.search(), NOT pattern.match() ). | |
733 | """ |
|
742 | """ | |
734 |
|
743 | |||
735 | if exclude_patterns is None: |
|
744 | if exclude_patterns is None: | |
736 | exclude_patterns = [] |
|
745 | exclude_patterns = [] | |
737 | self.exclude_patterns = map(re.compile,exclude_patterns) |
|
746 | self.exclude_patterns = map(re.compile,exclude_patterns) | |
738 | doctests.Doctest.__init__(self) |
|
747 | doctests.Doctest.__init__(self) | |
739 |
|
748 | |||
740 | def options(self, parser, env=os.environ): |
|
749 | def options(self, parser, env=os.environ): | |
741 | Plugin.options(self, parser, env) |
|
750 | Plugin.options(self, parser, env) | |
742 | parser.add_option('--doctest-tests', action='store_true', |
|
751 | parser.add_option('--doctest-tests', action='store_true', | |
743 | dest='doctest_tests', |
|
752 | dest='doctest_tests', | |
744 | default=env.get('NOSE_DOCTEST_TESTS',True), |
|
753 | default=env.get('NOSE_DOCTEST_TESTS',True), | |
745 | help="Also look for doctests in test modules. " |
|
754 | help="Also look for doctests in test modules. " | |
746 | "Note that classes, methods and functions should " |
|
755 | "Note that classes, methods and functions should " | |
747 | "have either doctests or non-doctest tests, " |
|
756 | "have either doctests or non-doctest tests, " | |
748 | "not both. [NOSE_DOCTEST_TESTS]") |
|
757 | "not both. [NOSE_DOCTEST_TESTS]") | |
749 | parser.add_option('--doctest-extension', action="append", |
|
758 | parser.add_option('--doctest-extension', action="append", | |
750 | dest="doctestExtension", |
|
759 | dest="doctestExtension", | |
751 | help="Also look for doctests in files with " |
|
760 | help="Also look for doctests in files with " | |
752 | "this extension [NOSE_DOCTEST_EXTENSION]") |
|
761 | "this extension [NOSE_DOCTEST_EXTENSION]") | |
753 | # Set the default as a list, if given in env; otherwise |
|
762 | # Set the default as a list, if given in env; otherwise | |
754 | # an additional value set on the command line will cause |
|
763 | # an additional value set on the command line will cause | |
755 | # an error. |
|
764 | # an error. | |
756 | env_setting = env.get('NOSE_DOCTEST_EXTENSION') |
|
765 | env_setting = env.get('NOSE_DOCTEST_EXTENSION') | |
757 | if env_setting is not None: |
|
766 | if env_setting is not None: | |
758 | parser.set_defaults(doctestExtension=tolist(env_setting)) |
|
767 | parser.set_defaults(doctestExtension=tolist(env_setting)) | |
759 |
|
768 | |||
760 |
|
769 | |||
761 | def configure(self, options, config): |
|
770 | def configure(self, options, config): | |
762 | Plugin.configure(self, options, config) |
|
771 | Plugin.configure(self, options, config) | |
763 | self.doctest_tests = options.doctest_tests |
|
772 | self.doctest_tests = options.doctest_tests | |
764 | self.extension = tolist(options.doctestExtension) |
|
773 | self.extension = tolist(options.doctestExtension) | |
765 |
|
774 | |||
766 | self.parser = doctest.DocTestParser() |
|
775 | self.parser = doctest.DocTestParser() | |
767 | self.finder = DocTestFinder() |
|
776 | self.finder = DocTestFinder() | |
768 | self.checker = IPDoctestOutputChecker() |
|
777 | self.checker = IPDoctestOutputChecker() | |
769 | self.globs = None |
|
778 | self.globs = None | |
770 | self.extraglobs = None |
|
779 | self.extraglobs = None | |
771 |
|
780 | |||
772 |
|
781 | |||
773 | def loadTestsFromExtensionModule(self,filename): |
|
782 | def loadTestsFromExtensionModule(self,filename): | |
774 | bpath,mod = os.path.split(filename) |
|
783 | bpath,mod = os.path.split(filename) | |
775 | modname = os.path.splitext(mod)[0] |
|
784 | modname = os.path.splitext(mod)[0] | |
776 | try: |
|
785 | try: | |
777 | sys.path.append(bpath) |
|
786 | sys.path.append(bpath) | |
778 | module = __import__(modname) |
|
787 | module = __import__(modname) | |
779 | tests = list(self.loadTestsFromModule(module)) |
|
788 | tests = list(self.loadTestsFromModule(module)) | |
780 | finally: |
|
789 | finally: | |
781 | sys.path.pop() |
|
790 | sys.path.pop() | |
782 | return tests |
|
791 | return tests | |
783 |
|
792 | |||
784 | # NOTE: the method below is almost a copy of the original one in nose, with |
|
793 | # NOTE: the method below is almost a copy of the original one in nose, with | |
785 | # a few modifications to control output checking. |
|
794 | # a few modifications to control output checking. | |
786 |
|
795 | |||
787 | def loadTestsFromModule(self, module): |
|
796 | def loadTestsFromModule(self, module): | |
788 | #print '*** ipdoctest - lTM',module # dbg |
|
797 | #print '*** ipdoctest - lTM',module # dbg | |
789 |
|
798 | |||
790 | if not self.matches(module.__name__): |
|
799 | if not self.matches(module.__name__): | |
791 | log.debug("Doctest doesn't want module %s", module) |
|
800 | log.debug("Doctest doesn't want module %s", module) | |
792 | return |
|
801 | return | |
793 |
|
802 | |||
794 | tests = self.finder.find(module,globs=self.globs, |
|
803 | tests = self.finder.find(module,globs=self.globs, | |
795 | extraglobs=self.extraglobs) |
|
804 | extraglobs=self.extraglobs) | |
796 | if not tests: |
|
805 | if not tests: | |
797 | return |
|
806 | return | |
798 |
|
807 | |||
799 | # always use whitespace and ellipsis options |
|
808 | # always use whitespace and ellipsis options | |
800 | optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS |
|
809 | optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS | |
801 |
|
810 | |||
802 | tests.sort() |
|
811 | tests.sort() | |
803 | module_file = module.__file__ |
|
812 | module_file = module.__file__ | |
804 | if module_file[-4:] in ('.pyc', '.pyo'): |
|
813 | if module_file[-4:] in ('.pyc', '.pyo'): | |
805 | module_file = module_file[:-1] |
|
814 | module_file = module_file[:-1] | |
806 | for test in tests: |
|
815 | for test in tests: | |
807 | if not test.examples: |
|
816 | if not test.examples: | |
808 | continue |
|
817 | continue | |
809 | if not test.filename: |
|
818 | if not test.filename: | |
810 | test.filename = module_file |
|
819 | test.filename = module_file | |
811 |
|
820 | |||
812 | yield DocTestCase(test, |
|
821 | yield DocTestCase(test, | |
813 | optionflags=optionflags, |
|
822 | optionflags=optionflags, | |
814 | checker=self.checker) |
|
823 | checker=self.checker) | |
815 |
|
824 | |||
816 |
|
825 | |||
817 | def loadTestsFromFile(self, filename): |
|
826 | def loadTestsFromFile(self, filename): | |
818 | if is_extension_module(filename): |
|
827 | if is_extension_module(filename): | |
819 | for t in self.loadTestsFromExtensionModule(filename): |
|
828 | for t in self.loadTestsFromExtensionModule(filename): | |
820 | yield t |
|
829 | yield t | |
821 | else: |
|
830 | else: | |
822 | if self.extension and anyp(filename.endswith, self.extension): |
|
831 | if self.extension and anyp(filename.endswith, self.extension): | |
823 | name = os.path.basename(filename) |
|
832 | name = os.path.basename(filename) | |
824 | dh = open(filename) |
|
833 | dh = open(filename) | |
825 | try: |
|
834 | try: | |
826 | doc = dh.read() |
|
835 | doc = dh.read() | |
827 | finally: |
|
836 | finally: | |
828 | dh.close() |
|
837 | dh.close() | |
829 | test = self.parser.get_doctest( |
|
838 | test = self.parser.get_doctest( | |
830 | doc, globs={'__file__': filename}, name=name, |
|
839 | doc, globs={'__file__': filename}, name=name, | |
831 | filename=filename, lineno=0) |
|
840 | filename=filename, lineno=0) | |
832 | if test.examples: |
|
841 | if test.examples: | |
833 | #print 'FileCase:',test.examples # dbg |
|
842 | #print 'FileCase:',test.examples # dbg | |
834 | yield DocFileCase(test) |
|
843 | yield DocFileCase(test) | |
835 | else: |
|
844 | else: | |
836 | yield False # no tests to load |
|
845 | yield False # no tests to load | |
837 |
|
846 | |||
838 | def wantFile(self,filename): |
|
847 | def wantFile(self,filename): | |
839 | """Return whether the given filename should be scanned for tests. |
|
848 | """Return whether the given filename should be scanned for tests. | |
840 |
|
849 | |||
841 | Modified version that accepts extension modules as valid containers for |
|
850 | Modified version that accepts extension modules as valid containers for | |
842 | doctests. |
|
851 | doctests. | |
843 | """ |
|
852 | """ | |
844 | # print '*** ipdoctest- wantFile:',filename # dbg |
|
853 | # print '*** ipdoctest- wantFile:',filename # dbg | |
845 |
|
854 | |||
846 | for pat in self.exclude_patterns: |
|
855 | for pat in self.exclude_patterns: | |
847 | if pat.search(filename): |
|
856 | if pat.search(filename): | |
848 | # print '###>>> SKIP:',filename # dbg |
|
857 | # print '###>>> SKIP:',filename # dbg | |
849 | return False |
|
858 | return False | |
850 |
|
859 | |||
851 | if is_extension_module(filename): |
|
860 | if is_extension_module(filename): | |
852 | return True |
|
861 | return True | |
853 | else: |
|
862 | else: | |
854 | return doctests.Doctest.wantFile(self,filename) |
|
863 | return doctests.Doctest.wantFile(self,filename) | |
855 |
|
864 | |||
856 |
|
865 | |||
857 | class IPythonDoctest(ExtensionDoctest): |
|
866 | class IPythonDoctest(ExtensionDoctest): | |
858 | """Nose Plugin that supports doctests in extension modules. |
|
867 | """Nose Plugin that supports doctests in extension modules. | |
859 | """ |
|
868 | """ | |
860 | name = 'ipdoctest' # call nosetests with --with-ipdoctest |
|
869 | name = 'ipdoctest' # call nosetests with --with-ipdoctest | |
861 | enabled = True |
|
870 | enabled = True | |
862 |
|
871 | |||
863 | def makeTest(self, obj, parent): |
|
872 | def makeTest(self, obj, parent): | |
864 | """Look for doctests in the given object, which will be a |
|
873 | """Look for doctests in the given object, which will be a | |
865 | function, method or class. |
|
874 | function, method or class. | |
866 | """ |
|
875 | """ | |
867 | # always use whitespace and ellipsis options |
|
876 | # always use whitespace and ellipsis options | |
868 | optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS |
|
877 | optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS | |
869 |
|
878 | |||
870 | doctests = self.finder.find(obj, module=getmodule(parent)) |
|
879 | doctests = self.finder.find(obj, module=getmodule(parent)) | |
871 | if doctests: |
|
880 | if doctests: | |
872 | for test in doctests: |
|
881 | for test in doctests: | |
873 | if len(test.examples) == 0: |
|
882 | if len(test.examples) == 0: | |
874 | continue |
|
883 | continue | |
875 |
|
884 | |||
876 | yield DocTestCase(test, obj=obj, |
|
885 | yield DocTestCase(test, obj=obj, | |
877 | optionflags=optionflags, |
|
886 | optionflags=optionflags, | |
878 | checker=self.checker) |
|
887 | checker=self.checker) | |
879 |
|
888 | |||
880 | def options(self, parser, env=os.environ): |
|
889 | def options(self, parser, env=os.environ): | |
881 | Plugin.options(self, parser, env) |
|
890 | Plugin.options(self, parser, env) | |
882 | parser.add_option('--ipdoctest-tests', action='store_true', |
|
891 | parser.add_option('--ipdoctest-tests', action='store_true', | |
883 | dest='ipdoctest_tests', |
|
892 | dest='ipdoctest_tests', | |
884 | default=env.get('NOSE_IPDOCTEST_TESTS',True), |
|
893 | default=env.get('NOSE_IPDOCTEST_TESTS',True), | |
885 | help="Also look for doctests in test modules. " |
|
894 | help="Also look for doctests in test modules. " | |
886 | "Note that classes, methods and functions should " |
|
895 | "Note that classes, methods and functions should " | |
887 | "have either doctests or non-doctest tests, " |
|
896 | "have either doctests or non-doctest tests, " | |
888 | "not both. [NOSE_IPDOCTEST_TESTS]") |
|
897 | "not both. [NOSE_IPDOCTEST_TESTS]") | |
889 | parser.add_option('--ipdoctest-extension', action="append", |
|
898 | parser.add_option('--ipdoctest-extension', action="append", | |
890 | dest="ipdoctest_extension", |
|
899 | dest="ipdoctest_extension", | |
891 | help="Also look for doctests in files with " |
|
900 | help="Also look for doctests in files with " | |
892 | "this extension [NOSE_IPDOCTEST_EXTENSION]") |
|
901 | "this extension [NOSE_IPDOCTEST_EXTENSION]") | |
893 | # Set the default as a list, if given in env; otherwise |
|
902 | # Set the default as a list, if given in env; otherwise | |
894 | # an additional value set on the command line will cause |
|
903 | # an additional value set on the command line will cause | |
895 | # an error. |
|
904 | # an error. | |
896 | env_setting = env.get('NOSE_IPDOCTEST_EXTENSION') |
|
905 | env_setting = env.get('NOSE_IPDOCTEST_EXTENSION') | |
897 | if env_setting is not None: |
|
906 | if env_setting is not None: | |
898 | parser.set_defaults(ipdoctest_extension=tolist(env_setting)) |
|
907 | parser.set_defaults(ipdoctest_extension=tolist(env_setting)) | |
899 |
|
908 | |||
900 | def configure(self, options, config): |
|
909 | def configure(self, options, config): | |
901 | Plugin.configure(self, options, config) |
|
910 | Plugin.configure(self, options, config) | |
902 | self.doctest_tests = options.ipdoctest_tests |
|
911 | self.doctest_tests = options.ipdoctest_tests | |
903 | self.extension = tolist(options.ipdoctest_extension) |
|
912 | self.extension = tolist(options.ipdoctest_extension) | |
904 |
|
913 | |||
905 | self.parser = IPDocTestParser() |
|
914 | self.parser = IPDocTestParser() | |
906 | self.finder = DocTestFinder(parser=self.parser) |
|
915 | self.finder = DocTestFinder(parser=self.parser) | |
907 | self.checker = IPDoctestOutputChecker() |
|
916 | self.checker = IPDoctestOutputChecker() | |
908 | self.globs = None |
|
917 | self.globs = None | |
909 | self.extraglobs = None |
|
918 | self.extraglobs = None |
@@ -1,2261 +1,2261 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """General purpose utilities. |
|
2 | """General purpose utilities. | |
3 |
|
3 | |||
4 | This is a grab-bag of stuff I find useful in most programs I write. Some of |
|
4 | This is a grab-bag of stuff I find useful in most programs I write. Some of | |
5 | these things are also convenient when working at the command line. |
|
5 | these things are also convenient when working at the command line. | |
6 | """ |
|
6 | """ | |
7 |
|
7 | |||
8 | #***************************************************************************** |
|
8 | #***************************************************************************** | |
9 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> |
|
9 | # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu> | |
10 | # |
|
10 | # | |
11 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
12 | # the file COPYING, distributed as part of this software. |
|
12 | # the file COPYING, distributed as part of this software. | |
13 | #***************************************************************************** |
|
13 | #***************************************************************************** | |
14 |
|
14 | |||
15 | #**************************************************************************** |
|
15 | #**************************************************************************** | |
16 | # required modules from the Python standard library |
|
16 | # required modules from the Python standard library | |
17 | import __main__ |
|
17 | import __main__ | |
18 | import commands |
|
18 | import commands | |
19 | try: |
|
19 | try: | |
20 | import doctest |
|
20 | import doctest | |
21 | except ImportError: |
|
21 | except ImportError: | |
22 | pass |
|
22 | pass | |
23 | import os |
|
23 | import os | |
24 | import platform |
|
24 | import platform | |
25 | import re |
|
25 | import re | |
26 | import shlex |
|
26 | import shlex | |
27 | import shutil |
|
27 | import shutil | |
28 | import subprocess |
|
28 | import subprocess | |
29 | import sys |
|
29 | import sys | |
30 | import tempfile |
|
30 | import tempfile | |
31 | import time |
|
31 | import time | |
32 | import types |
|
32 | import types | |
33 | import warnings |
|
33 | import warnings | |
34 |
|
34 | |||
35 | # Curses and termios are Unix-only modules |
|
35 | # Curses and termios are Unix-only modules | |
36 | try: |
|
36 | try: | |
37 | import curses |
|
37 | import curses | |
38 | # We need termios as well, so if its import happens to raise, we bail on |
|
38 | # We need termios as well, so if its import happens to raise, we bail on | |
39 | # using curses altogether. |
|
39 | # using curses altogether. | |
40 | import termios |
|
40 | import termios | |
41 | except ImportError: |
|
41 | except ImportError: | |
42 | USE_CURSES = False |
|
42 | USE_CURSES = False | |
43 | else: |
|
43 | else: | |
44 | # Curses on Solaris may not be complete, so we can't use it there |
|
44 | # Curses on Solaris may not be complete, so we can't use it there | |
45 | USE_CURSES = hasattr(curses,'initscr') |
|
45 | USE_CURSES = hasattr(curses,'initscr') | |
46 |
|
46 | |||
47 | # Other IPython utilities |
|
47 | # Other IPython utilities | |
48 | import IPython |
|
48 | import IPython | |
49 | from IPython.external.Itpl import Itpl,itpl,printpl |
|
49 | from IPython.external.Itpl import Itpl,itpl,printpl | |
50 | from IPython.utils import platutils |
|
50 | from IPython.utils import platutils | |
51 | from IPython.utils import DPyGetOpt |
|
51 | from IPython.utils import DPyGetOpt | |
52 | from IPython.utils.generics import result_display |
|
52 | from IPython.utils.generics import result_display | |
53 | from IPython.core import ipapi |
|
53 | from IPython.core import ipapi | |
54 | from IPython.external.path import path |
|
54 | from IPython.external.path import path | |
55 | if os.name == "nt": |
|
55 | if os.name == "nt": | |
56 | from IPython.utils.winconsole import get_console_size |
|
56 | from IPython.utils.winconsole import get_console_size | |
57 |
|
57 | |||
58 | try: |
|
58 | try: | |
59 | set |
|
59 | set | |
60 | except: |
|
60 | except: | |
61 | from sets import Set as set |
|
61 | from sets import Set as set | |
62 |
|
62 | |||
63 |
|
63 | |||
64 | #**************************************************************************** |
|
64 | #**************************************************************************** | |
65 | # Exceptions |
|
65 | # Exceptions | |
66 | class Error(Exception): |
|
66 | class Error(Exception): | |
67 | """Base class for exceptions in this module.""" |
|
67 | """Base class for exceptions in this module.""" | |
68 | pass |
|
68 | pass | |
69 |
|
69 | |||
70 | #---------------------------------------------------------------------------- |
|
70 | #---------------------------------------------------------------------------- | |
71 | class IOStream: |
|
71 | class IOStream: | |
72 | def __init__(self,stream,fallback): |
|
72 | def __init__(self,stream,fallback): | |
73 | if not hasattr(stream,'write') or not hasattr(stream,'flush'): |
|
73 | if not hasattr(stream,'write') or not hasattr(stream,'flush'): | |
74 | stream = fallback |
|
74 | stream = fallback | |
75 | self.stream = stream |
|
75 | self.stream = stream | |
76 | self._swrite = stream.write |
|
76 | self._swrite = stream.write | |
77 | self.flush = stream.flush |
|
77 | self.flush = stream.flush | |
78 |
|
78 | |||
79 | def write(self,data): |
|
79 | def write(self,data): | |
80 | try: |
|
80 | try: | |
81 | self._swrite(data) |
|
81 | self._swrite(data) | |
82 | except: |
|
82 | except: | |
83 | try: |
|
83 | try: | |
84 | # print handles some unicode issues which may trip a plain |
|
84 | # print handles some unicode issues which may trip a plain | |
85 | # write() call. Attempt to emulate write() by using a |
|
85 | # write() call. Attempt to emulate write() by using a | |
86 | # trailing comma |
|
86 | # trailing comma | |
87 | print >> self.stream, data, |
|
87 | print >> self.stream, data, | |
88 | except: |
|
88 | except: | |
89 | # if we get here, something is seriously broken. |
|
89 | # if we get here, something is seriously broken. | |
90 | print >> sys.stderr, \ |
|
90 | print >> sys.stderr, \ | |
91 | 'ERROR - failed to write data to stream:', self.stream |
|
91 | 'ERROR - failed to write data to stream:', self.stream | |
92 |
|
92 | |||
93 | def close(self): |
|
93 | def close(self): | |
94 | pass |
|
94 | pass | |
95 |
|
95 | |||
96 |
|
96 | |||
97 | class IOTerm: |
|
97 | class IOTerm: | |
98 | """ Term holds the file or file-like objects for handling I/O operations. |
|
98 | """ Term holds the file or file-like objects for handling I/O operations. | |
99 |
|
99 | |||
100 | These are normally just sys.stdin, sys.stdout and sys.stderr but for |
|
100 | These are normally just sys.stdin, sys.stdout and sys.stderr but for | |
101 | Windows they can can replaced to allow editing the strings before they are |
|
101 | Windows they can can replaced to allow editing the strings before they are | |
102 | displayed.""" |
|
102 | displayed.""" | |
103 |
|
103 | |||
104 | # In the future, having IPython channel all its I/O operations through |
|
104 | # In the future, having IPython channel all its I/O operations through | |
105 | # this class will make it easier to embed it into other environments which |
|
105 | # this class will make it easier to embed it into other environments which | |
106 | # are not a normal terminal (such as a GUI-based shell) |
|
106 | # are not a normal terminal (such as a GUI-based shell) | |
107 | def __init__(self,cin=None,cout=None,cerr=None): |
|
107 | def __init__(self,cin=None,cout=None,cerr=None): | |
108 | self.cin = IOStream(cin,sys.stdin) |
|
108 | self.cin = IOStream(cin,sys.stdin) | |
109 | self.cout = IOStream(cout,sys.stdout) |
|
109 | self.cout = IOStream(cout,sys.stdout) | |
110 | self.cerr = IOStream(cerr,sys.stderr) |
|
110 | self.cerr = IOStream(cerr,sys.stderr) | |
111 |
|
111 | |||
112 | # Global variable to be used for all I/O |
|
112 | # Global variable to be used for all I/O | |
113 | Term = IOTerm() |
|
113 | Term = IOTerm() | |
114 |
|
114 | |||
115 | import IPython.utils.rlineimpl as readline |
|
115 | import IPython.utils.rlineimpl as readline | |
116 | # Remake Term to use the readline i/o facilities |
|
116 | # Remake Term to use the readline i/o facilities | |
117 | if sys.platform == 'win32' and readline.have_readline: |
|
117 | if sys.platform == 'win32' and readline.have_readline: | |
118 |
|
118 | |||
119 | Term = IOTerm(cout=readline._outputfile,cerr=readline._outputfile) |
|
119 | Term = IOTerm(cout=readline._outputfile,cerr=readline._outputfile) | |
120 |
|
120 | |||
121 |
|
121 | |||
122 | #**************************************************************************** |
|
122 | #**************************************************************************** | |
123 | # Generic warning/error printer, used by everything else |
|
123 | # Generic warning/error printer, used by everything else | |
124 | def warn(msg,level=2,exit_val=1): |
|
124 | def warn(msg,level=2,exit_val=1): | |
125 | """Standard warning printer. Gives formatting consistency. |
|
125 | """Standard warning printer. Gives formatting consistency. | |
126 |
|
126 | |||
127 | Output is sent to Term.cerr (sys.stderr by default). |
|
127 | Output is sent to Term.cerr (sys.stderr by default). | |
128 |
|
128 | |||
129 | Options: |
|
129 | Options: | |
130 |
|
130 | |||
131 | -level(2): allows finer control: |
|
131 | -level(2): allows finer control: | |
132 | 0 -> Do nothing, dummy function. |
|
132 | 0 -> Do nothing, dummy function. | |
133 | 1 -> Print message. |
|
133 | 1 -> Print message. | |
134 | 2 -> Print 'WARNING:' + message. (Default level). |
|
134 | 2 -> Print 'WARNING:' + message. (Default level). | |
135 | 3 -> Print 'ERROR:' + message. |
|
135 | 3 -> Print 'ERROR:' + message. | |
136 | 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val). |
|
136 | 4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val). | |
137 |
|
137 | |||
138 | -exit_val (1): exit value returned by sys.exit() for a level 4 |
|
138 | -exit_val (1): exit value returned by sys.exit() for a level 4 | |
139 | warning. Ignored for all other levels.""" |
|
139 | warning. Ignored for all other levels.""" | |
140 |
|
140 | |||
141 | if level>0: |
|
141 | if level>0: | |
142 | header = ['','','WARNING: ','ERROR: ','FATAL ERROR: '] |
|
142 | header = ['','','WARNING: ','ERROR: ','FATAL ERROR: '] | |
143 | print >> Term.cerr, '%s%s' % (header[level],msg) |
|
143 | print >> Term.cerr, '%s%s' % (header[level],msg) | |
144 | if level == 4: |
|
144 | if level == 4: | |
145 | print >> Term.cerr,'Exiting.\n' |
|
145 | print >> Term.cerr,'Exiting.\n' | |
146 | sys.exit(exit_val) |
|
146 | sys.exit(exit_val) | |
147 |
|
147 | |||
148 | def info(msg): |
|
148 | def info(msg): | |
149 | """Equivalent to warn(msg,level=1).""" |
|
149 | """Equivalent to warn(msg,level=1).""" | |
150 |
|
150 | |||
151 | warn(msg,level=1) |
|
151 | warn(msg,level=1) | |
152 |
|
152 | |||
153 | def error(msg): |
|
153 | def error(msg): | |
154 | """Equivalent to warn(msg,level=3).""" |
|
154 | """Equivalent to warn(msg,level=3).""" | |
155 |
|
155 | |||
156 | warn(msg,level=3) |
|
156 | warn(msg,level=3) | |
157 |
|
157 | |||
158 | def fatal(msg,exit_val=1): |
|
158 | def fatal(msg,exit_val=1): | |
159 | """Equivalent to warn(msg,exit_val=exit_val,level=4).""" |
|
159 | """Equivalent to warn(msg,exit_val=exit_val,level=4).""" | |
160 |
|
160 | |||
161 | warn(msg,exit_val=exit_val,level=4) |
|
161 | warn(msg,exit_val=exit_val,level=4) | |
162 |
|
162 | |||
163 | #--------------------------------------------------------------------------- |
|
163 | #--------------------------------------------------------------------------- | |
164 | # Debugging routines |
|
164 | # Debugging routines | |
165 | # |
|
165 | # | |
166 | def debugx(expr,pre_msg=''): |
|
166 | def debugx(expr,pre_msg=''): | |
167 | """Print the value of an expression from the caller's frame. |
|
167 | """Print the value of an expression from the caller's frame. | |
168 |
|
168 | |||
169 | Takes an expression, evaluates it in the caller's frame and prints both |
|
169 | Takes an expression, evaluates it in the caller's frame and prints both | |
170 | the given expression and the resulting value (as well as a debug mark |
|
170 | the given expression and the resulting value (as well as a debug mark | |
171 | indicating the name of the calling function. The input must be of a form |
|
171 | indicating the name of the calling function. The input must be of a form | |
172 | suitable for eval(). |
|
172 | suitable for eval(). | |
173 |
|
173 | |||
174 | An optional message can be passed, which will be prepended to the printed |
|
174 | An optional message can be passed, which will be prepended to the printed | |
175 | expr->value pair.""" |
|
175 | expr->value pair.""" | |
176 |
|
176 | |||
177 | cf = sys._getframe(1) |
|
177 | cf = sys._getframe(1) | |
178 | print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr, |
|
178 | print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr, | |
179 | eval(expr,cf.f_globals,cf.f_locals)) |
|
179 | eval(expr,cf.f_globals,cf.f_locals)) | |
180 |
|
180 | |||
181 | # deactivate it by uncommenting the following line, which makes it a no-op |
|
181 | # deactivate it by uncommenting the following line, which makes it a no-op | |
182 | #def debugx(expr,pre_msg=''): pass |
|
182 | #def debugx(expr,pre_msg=''): pass | |
183 |
|
183 | |||
184 | #---------------------------------------------------------------------------- |
|
184 | #---------------------------------------------------------------------------- | |
185 | StringTypes = types.StringTypes |
|
185 | StringTypes = types.StringTypes | |
186 |
|
186 | |||
187 | # Basic timing functionality |
|
187 | # Basic timing functionality | |
188 |
|
188 | |||
189 | # If possible (Unix), use the resource module instead of time.clock() |
|
189 | # If possible (Unix), use the resource module instead of time.clock() | |
190 | try: |
|
190 | try: | |
191 | import resource |
|
191 | import resource | |
192 | def clocku(): |
|
192 | def clocku(): | |
193 | """clocku() -> floating point number |
|
193 | """clocku() -> floating point number | |
194 |
|
194 | |||
195 | Return the *USER* CPU time in seconds since the start of the process. |
|
195 | Return the *USER* CPU time in seconds since the start of the process. | |
196 | This is done via a call to resource.getrusage, so it avoids the |
|
196 | This is done via a call to resource.getrusage, so it avoids the | |
197 | wraparound problems in time.clock().""" |
|
197 | wraparound problems in time.clock().""" | |
198 |
|
198 | |||
199 | return resource.getrusage(resource.RUSAGE_SELF)[0] |
|
199 | return resource.getrusage(resource.RUSAGE_SELF)[0] | |
200 |
|
200 | |||
201 | def clocks(): |
|
201 | def clocks(): | |
202 | """clocks() -> floating point number |
|
202 | """clocks() -> floating point number | |
203 |
|
203 | |||
204 | Return the *SYSTEM* CPU time in seconds since the start of the process. |
|
204 | Return the *SYSTEM* CPU time in seconds since the start of the process. | |
205 | This is done via a call to resource.getrusage, so it avoids the |
|
205 | This is done via a call to resource.getrusage, so it avoids the | |
206 | wraparound problems in time.clock().""" |
|
206 | wraparound problems in time.clock().""" | |
207 |
|
207 | |||
208 | return resource.getrusage(resource.RUSAGE_SELF)[1] |
|
208 | return resource.getrusage(resource.RUSAGE_SELF)[1] | |
209 |
|
209 | |||
210 | def clock(): |
|
210 | def clock(): | |
211 | """clock() -> floating point number |
|
211 | """clock() -> floating point number | |
212 |
|
212 | |||
213 | Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of |
|
213 | Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of | |
214 | the process. This is done via a call to resource.getrusage, so it |
|
214 | the process. This is done via a call to resource.getrusage, so it | |
215 | avoids the wraparound problems in time.clock().""" |
|
215 | avoids the wraparound problems in time.clock().""" | |
216 |
|
216 | |||
217 | u,s = resource.getrusage(resource.RUSAGE_SELF)[:2] |
|
217 | u,s = resource.getrusage(resource.RUSAGE_SELF)[:2] | |
218 | return u+s |
|
218 | return u+s | |
219 |
|
219 | |||
220 | def clock2(): |
|
220 | def clock2(): | |
221 | """clock2() -> (t_user,t_system) |
|
221 | """clock2() -> (t_user,t_system) | |
222 |
|
222 | |||
223 | Similar to clock(), but return a tuple of user/system times.""" |
|
223 | Similar to clock(), but return a tuple of user/system times.""" | |
224 | return resource.getrusage(resource.RUSAGE_SELF)[:2] |
|
224 | return resource.getrusage(resource.RUSAGE_SELF)[:2] | |
225 |
|
225 | |||
226 | except ImportError: |
|
226 | except ImportError: | |
227 | # There is no distinction of user/system time under windows, so we just use |
|
227 | # There is no distinction of user/system time under windows, so we just use | |
228 | # time.clock() for everything... |
|
228 | # time.clock() for everything... | |
229 | clocku = clocks = clock = time.clock |
|
229 | clocku = clocks = clock = time.clock | |
230 | def clock2(): |
|
230 | def clock2(): | |
231 | """Under windows, system CPU time can't be measured. |
|
231 | """Under windows, system CPU time can't be measured. | |
232 |
|
232 | |||
233 | This just returns clock() and zero.""" |
|
233 | This just returns clock() and zero.""" | |
234 | return time.clock(),0.0 |
|
234 | return time.clock(),0.0 | |
235 |
|
235 | |||
236 | def timings_out(reps,func,*args,**kw): |
|
236 | def timings_out(reps,func,*args,**kw): | |
237 | """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output) |
|
237 | """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output) | |
238 |
|
238 | |||
239 | Execute a function reps times, return a tuple with the elapsed total |
|
239 | Execute a function reps times, return a tuple with the elapsed total | |
240 | CPU time in seconds, the time per call and the function's output. |
|
240 | CPU time in seconds, the time per call and the function's output. | |
241 |
|
241 | |||
242 | Under Unix, the return value is the sum of user+system time consumed by |
|
242 | Under Unix, the return value is the sum of user+system time consumed by | |
243 | the process, computed via the resource module. This prevents problems |
|
243 | the process, computed via the resource module. This prevents problems | |
244 | related to the wraparound effect which the time.clock() function has. |
|
244 | related to the wraparound effect which the time.clock() function has. | |
245 |
|
245 | |||
246 | Under Windows the return value is in wall clock seconds. See the |
|
246 | Under Windows the return value is in wall clock seconds. See the | |
247 | documentation for the time module for more details.""" |
|
247 | documentation for the time module for more details.""" | |
248 |
|
248 | |||
249 | reps = int(reps) |
|
249 | reps = int(reps) | |
250 | assert reps >=1, 'reps must be >= 1' |
|
250 | assert reps >=1, 'reps must be >= 1' | |
251 | if reps==1: |
|
251 | if reps==1: | |
252 | start = clock() |
|
252 | start = clock() | |
253 | out = func(*args,**kw) |
|
253 | out = func(*args,**kw) | |
254 | tot_time = clock()-start |
|
254 | tot_time = clock()-start | |
255 | else: |
|
255 | else: | |
256 | rng = xrange(reps-1) # the last time is executed separately to store output |
|
256 | rng = xrange(reps-1) # the last time is executed separately to store output | |
257 | start = clock() |
|
257 | start = clock() | |
258 | for dummy in rng: func(*args,**kw) |
|
258 | for dummy in rng: func(*args,**kw) | |
259 | out = func(*args,**kw) # one last time |
|
259 | out = func(*args,**kw) # one last time | |
260 | tot_time = clock()-start |
|
260 | tot_time = clock()-start | |
261 | av_time = tot_time / reps |
|
261 | av_time = tot_time / reps | |
262 | return tot_time,av_time,out |
|
262 | return tot_time,av_time,out | |
263 |
|
263 | |||
264 | def timings(reps,func,*args,**kw): |
|
264 | def timings(reps,func,*args,**kw): | |
265 | """timings(reps,func,*args,**kw) -> (t_total,t_per_call) |
|
265 | """timings(reps,func,*args,**kw) -> (t_total,t_per_call) | |
266 |
|
266 | |||
267 | Execute a function reps times, return a tuple with the elapsed total CPU |
|
267 | Execute a function reps times, return a tuple with the elapsed total CPU | |
268 | time in seconds and the time per call. These are just the first two values |
|
268 | time in seconds and the time per call. These are just the first two values | |
269 | in timings_out().""" |
|
269 | in timings_out().""" | |
270 |
|
270 | |||
271 | return timings_out(reps,func,*args,**kw)[0:2] |
|
271 | return timings_out(reps,func,*args,**kw)[0:2] | |
272 |
|
272 | |||
273 | def timing(func,*args,**kw): |
|
273 | def timing(func,*args,**kw): | |
274 | """timing(func,*args,**kw) -> t_total |
|
274 | """timing(func,*args,**kw) -> t_total | |
275 |
|
275 | |||
276 | Execute a function once, return the elapsed total CPU time in |
|
276 | Execute a function once, return the elapsed total CPU time in | |
277 | seconds. This is just the first value in timings_out().""" |
|
277 | seconds. This is just the first value in timings_out().""" | |
278 |
|
278 | |||
279 | return timings_out(1,func,*args,**kw)[0] |
|
279 | return timings_out(1,func,*args,**kw)[0] | |
280 |
|
280 | |||
281 | #**************************************************************************** |
|
281 | #**************************************************************************** | |
282 | # file and system |
|
282 | # file and system | |
283 |
|
283 | |||
284 | def arg_split(s,posix=False): |
|
284 | def arg_split(s,posix=False): | |
285 | """Split a command line's arguments in a shell-like manner. |
|
285 | """Split a command line's arguments in a shell-like manner. | |
286 |
|
286 | |||
287 | This is a modified version of the standard library's shlex.split() |
|
287 | This is a modified version of the standard library's shlex.split() | |
288 | function, but with a default of posix=False for splitting, so that quotes |
|
288 | function, but with a default of posix=False for splitting, so that quotes | |
289 | in inputs are respected.""" |
|
289 | in inputs are respected.""" | |
290 |
|
290 | |||
291 | # XXX - there may be unicode-related problems here!!! I'm not sure that |
|
291 | # XXX - there may be unicode-related problems here!!! I'm not sure that | |
292 | # shlex is truly unicode-safe, so it might be necessary to do |
|
292 | # shlex is truly unicode-safe, so it might be necessary to do | |
293 | # |
|
293 | # | |
294 | # s = s.encode(sys.stdin.encoding) |
|
294 | # s = s.encode(sys.stdin.encoding) | |
295 | # |
|
295 | # | |
296 | # first, to ensure that shlex gets a normal string. Input from anyone who |
|
296 | # first, to ensure that shlex gets a normal string. Input from anyone who | |
297 | # knows more about unicode and shlex than I would be good to have here... |
|
297 | # knows more about unicode and shlex than I would be good to have here... | |
298 | lex = shlex.shlex(s, posix=posix) |
|
298 | lex = shlex.shlex(s, posix=posix) | |
299 | lex.whitespace_split = True |
|
299 | lex.whitespace_split = True | |
300 | return list(lex) |
|
300 | return list(lex) | |
301 |
|
301 | |||
302 | def system(cmd,verbose=0,debug=0,header=''): |
|
302 | def system(cmd,verbose=0,debug=0,header=''): | |
303 | """Execute a system command, return its exit status. |
|
303 | """Execute a system command, return its exit status. | |
304 |
|
304 | |||
305 | Options: |
|
305 | Options: | |
306 |
|
306 | |||
307 | - verbose (0): print the command to be executed. |
|
307 | - verbose (0): print the command to be executed. | |
308 |
|
308 | |||
309 | - debug (0): only print, do not actually execute. |
|
309 | - debug (0): only print, do not actually execute. | |
310 |
|
310 | |||
311 | - header (''): Header to print on screen prior to the executed command (it |
|
311 | - header (''): Header to print on screen prior to the executed command (it | |
312 | is only prepended to the command, no newlines are added). |
|
312 | is only prepended to the command, no newlines are added). | |
313 |
|
313 | |||
314 | Note: a stateful version of this function is available through the |
|
314 | Note: a stateful version of this function is available through the | |
315 | SystemExec class.""" |
|
315 | SystemExec class.""" | |
316 |
|
316 | |||
317 | stat = 0 |
|
317 | stat = 0 | |
318 | if verbose or debug: print header+cmd |
|
318 | if verbose or debug: print header+cmd | |
319 | sys.stdout.flush() |
|
319 | sys.stdout.flush() | |
320 | if not debug: stat = os.system(cmd) |
|
320 | if not debug: stat = os.system(cmd) | |
321 | return stat |
|
321 | return stat | |
322 |
|
322 | |||
323 | def abbrev_cwd(): |
|
323 | def abbrev_cwd(): | |
324 | """ Return abbreviated version of cwd, e.g. d:mydir """ |
|
324 | """ Return abbreviated version of cwd, e.g. d:mydir """ | |
325 | cwd = os.getcwd().replace('\\','/') |
|
325 | cwd = os.getcwd().replace('\\','/') | |
326 | drivepart = '' |
|
326 | drivepart = '' | |
327 | tail = cwd |
|
327 | tail = cwd | |
328 | if sys.platform == 'win32': |
|
328 | if sys.platform == 'win32': | |
329 | if len(cwd) < 4: |
|
329 | if len(cwd) < 4: | |
330 | return cwd |
|
330 | return cwd | |
331 | drivepart,tail = os.path.splitdrive(cwd) |
|
331 | drivepart,tail = os.path.splitdrive(cwd) | |
332 |
|
332 | |||
333 |
|
333 | |||
334 | parts = tail.split('/') |
|
334 | parts = tail.split('/') | |
335 | if len(parts) > 2: |
|
335 | if len(parts) > 2: | |
336 | tail = '/'.join(parts[-2:]) |
|
336 | tail = '/'.join(parts[-2:]) | |
337 |
|
337 | |||
338 | return (drivepart + ( |
|
338 | return (drivepart + ( | |
339 | cwd == '/' and '/' or tail)) |
|
339 | cwd == '/' and '/' or tail)) | |
340 |
|
340 | |||
341 |
|
341 | |||
342 | # This function is used by ipython in a lot of places to make system calls. |
|
342 | # This function is used by ipython in a lot of places to make system calls. | |
343 | # We need it to be slightly different under win32, due to the vagaries of |
|
343 | # We need it to be slightly different under win32, due to the vagaries of | |
344 | # 'network shares'. A win32 override is below. |
|
344 | # 'network shares'. A win32 override is below. | |
345 |
|
345 | |||
346 | def shell(cmd,verbose=0,debug=0,header=''): |
|
346 | def shell(cmd,verbose=0,debug=0,header=''): | |
347 | """Execute a command in the system shell, always return None. |
|
347 | """Execute a command in the system shell, always return None. | |
348 |
|
348 | |||
349 | Options: |
|
349 | Options: | |
350 |
|
350 | |||
351 | - verbose (0): print the command to be executed. |
|
351 | - verbose (0): print the command to be executed. | |
352 |
|
352 | |||
353 | - debug (0): only print, do not actually execute. |
|
353 | - debug (0): only print, do not actually execute. | |
354 |
|
354 | |||
355 | - header (''): Header to print on screen prior to the executed command (it |
|
355 | - header (''): Header to print on screen prior to the executed command (it | |
356 | is only prepended to the command, no newlines are added). |
|
356 | is only prepended to the command, no newlines are added). | |
357 |
|
357 | |||
358 | Note: this is similar to genutils.system(), but it returns None so it can |
|
358 | Note: this is similar to genutils.system(), but it returns None so it can | |
359 | be conveniently used in interactive loops without getting the return value |
|
359 | be conveniently used in interactive loops without getting the return value | |
360 | (typically 0) printed many times.""" |
|
360 | (typically 0) printed many times.""" | |
361 |
|
361 | |||
362 | stat = 0 |
|
362 | stat = 0 | |
363 | if verbose or debug: print header+cmd |
|
363 | if verbose or debug: print header+cmd | |
364 | # flush stdout so we don't mangle python's buffering |
|
364 | # flush stdout so we don't mangle python's buffering | |
365 | sys.stdout.flush() |
|
365 | sys.stdout.flush() | |
366 |
|
366 | |||
367 | if not debug: |
|
367 | if not debug: | |
368 | platutils.set_term_title("IPy " + cmd) |
|
368 | platutils.set_term_title("IPy " + cmd) | |
369 | os.system(cmd) |
|
369 | os.system(cmd) | |
370 | platutils.set_term_title("IPy " + abbrev_cwd()) |
|
370 | platutils.set_term_title("IPy " + abbrev_cwd()) | |
371 |
|
371 | |||
372 | # override shell() for win32 to deal with network shares |
|
372 | # override shell() for win32 to deal with network shares | |
373 | if os.name in ('nt','dos'): |
|
373 | if os.name in ('nt','dos'): | |
374 |
|
374 | |||
375 | shell_ori = shell |
|
375 | shell_ori = shell | |
376 |
|
376 | |||
377 | def shell(cmd,verbose=0,debug=0,header=''): |
|
377 | def shell(cmd,verbose=0,debug=0,header=''): | |
378 | if os.getcwd().startswith(r"\\"): |
|
378 | if os.getcwd().startswith(r"\\"): | |
379 | path = os.getcwd() |
|
379 | path = os.getcwd() | |
380 | # change to c drive (cannot be on UNC-share when issuing os.system, |
|
380 | # change to c drive (cannot be on UNC-share when issuing os.system, | |
381 | # as cmd.exe cannot handle UNC addresses) |
|
381 | # as cmd.exe cannot handle UNC addresses) | |
382 | os.chdir("c:") |
|
382 | os.chdir("c:") | |
383 | # issue pushd to the UNC-share and then run the command |
|
383 | # issue pushd to the UNC-share and then run the command | |
384 | try: |
|
384 | try: | |
385 | shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header) |
|
385 | shell_ori('"pushd %s&&"'%path+cmd,verbose,debug,header) | |
386 | finally: |
|
386 | finally: | |
387 | os.chdir(path) |
|
387 | os.chdir(path) | |
388 | else: |
|
388 | else: | |
389 | shell_ori(cmd,verbose,debug,header) |
|
389 | shell_ori(cmd,verbose,debug,header) | |
390 |
|
390 | |||
391 | shell.__doc__ = shell_ori.__doc__ |
|
391 | shell.__doc__ = shell_ori.__doc__ | |
392 |
|
392 | |||
393 | def getoutput(cmd,verbose=0,debug=0,header='',split=0): |
|
393 | def getoutput(cmd,verbose=0,debug=0,header='',split=0): | |
394 | """Dummy substitute for perl's backquotes. |
|
394 | """Dummy substitute for perl's backquotes. | |
395 |
|
395 | |||
396 | Executes a command and returns the output. |
|
396 | Executes a command and returns the output. | |
397 |
|
397 | |||
398 | Accepts the same arguments as system(), plus: |
|
398 | Accepts the same arguments as system(), plus: | |
399 |
|
399 | |||
400 | - split(0): if true, the output is returned as a list split on newlines. |
|
400 | - split(0): if true, the output is returned as a list split on newlines. | |
401 |
|
401 | |||
402 | Note: a stateful version of this function is available through the |
|
402 | Note: a stateful version of this function is available through the | |
403 | SystemExec class. |
|
403 | SystemExec class. | |
404 |
|
404 | |||
405 | This is pretty much deprecated and rarely used, |
|
405 | This is pretty much deprecated and rarely used, | |
406 | genutils.getoutputerror may be what you need. |
|
406 | genutils.getoutputerror may be what you need. | |
407 |
|
407 | |||
408 | """ |
|
408 | """ | |
409 |
|
409 | |||
410 | if verbose or debug: print header+cmd |
|
410 | if verbose or debug: print header+cmd | |
411 | if not debug: |
|
411 | if not debug: | |
412 | output = os.popen(cmd).read() |
|
412 | output = os.popen(cmd).read() | |
413 | # stipping last \n is here for backwards compat. |
|
413 | # stipping last \n is here for backwards compat. | |
414 | if output.endswith('\n'): |
|
414 | if output.endswith('\n'): | |
415 | output = output[:-1] |
|
415 | output = output[:-1] | |
416 | if split: |
|
416 | if split: | |
417 | return output.split('\n') |
|
417 | return output.split('\n') | |
418 | else: |
|
418 | else: | |
419 | return output |
|
419 | return output | |
420 |
|
420 | |||
421 | def getoutputerror(cmd,verbose=0,debug=0,header='',split=0): |
|
421 | def getoutputerror(cmd,verbose=0,debug=0,header='',split=0): | |
422 | """Return (standard output,standard error) of executing cmd in a shell. |
|
422 | """Return (standard output,standard error) of executing cmd in a shell. | |
423 |
|
423 | |||
424 | Accepts the same arguments as system(), plus: |
|
424 | Accepts the same arguments as system(), plus: | |
425 |
|
425 | |||
426 | - split(0): if true, each of stdout/err is returned as a list split on |
|
426 | - split(0): if true, each of stdout/err is returned as a list split on | |
427 | newlines. |
|
427 | newlines. | |
428 |
|
428 | |||
429 | Note: a stateful version of this function is available through the |
|
429 | Note: a stateful version of this function is available through the | |
430 | SystemExec class.""" |
|
430 | SystemExec class.""" | |
431 |
|
431 | |||
432 | if verbose or debug: print header+cmd |
|
432 | if verbose or debug: print header+cmd | |
433 | if not cmd: |
|
433 | if not cmd: | |
434 | if split: |
|
434 | if split: | |
435 | return [],[] |
|
435 | return [],[] | |
436 | else: |
|
436 | else: | |
437 | return '','' |
|
437 | return '','' | |
438 | if not debug: |
|
438 | if not debug: | |
439 | pin,pout,perr = os.popen3(cmd) |
|
439 | pin,pout,perr = os.popen3(cmd) | |
440 | tout = pout.read().rstrip() |
|
440 | tout = pout.read().rstrip() | |
441 | terr = perr.read().rstrip() |
|
441 | terr = perr.read().rstrip() | |
442 | pin.close() |
|
442 | pin.close() | |
443 | pout.close() |
|
443 | pout.close() | |
444 | perr.close() |
|
444 | perr.close() | |
445 | if split: |
|
445 | if split: | |
446 | return tout.split('\n'),terr.split('\n') |
|
446 | return tout.split('\n'),terr.split('\n') | |
447 | else: |
|
447 | else: | |
448 | return tout,terr |
|
448 | return tout,terr | |
449 |
|
449 | |||
450 | # for compatibility with older naming conventions |
|
450 | # for compatibility with older naming conventions | |
451 | xsys = system |
|
451 | xsys = system | |
452 | bq = getoutput |
|
452 | bq = getoutput | |
453 |
|
453 | |||
454 | class SystemExec: |
|
454 | class SystemExec: | |
455 | """Access the system and getoutput functions through a stateful interface. |
|
455 | """Access the system and getoutput functions through a stateful interface. | |
456 |
|
456 | |||
457 | Note: here we refer to the system and getoutput functions from this |
|
457 | Note: here we refer to the system and getoutput functions from this | |
458 | library, not the ones from the standard python library. |
|
458 | library, not the ones from the standard python library. | |
459 |
|
459 | |||
460 | This class offers the system and getoutput functions as methods, but the |
|
460 | This class offers the system and getoutput functions as methods, but the | |
461 | verbose, debug and header parameters can be set for the instance (at |
|
461 | verbose, debug and header parameters can be set for the instance (at | |
462 | creation time or later) so that they don't need to be specified on each |
|
462 | creation time or later) so that they don't need to be specified on each | |
463 | call. |
|
463 | call. | |
464 |
|
464 | |||
465 | For efficiency reasons, there's no way to override the parameters on a |
|
465 | For efficiency reasons, there's no way to override the parameters on a | |
466 | per-call basis other than by setting instance attributes. If you need |
|
466 | per-call basis other than by setting instance attributes. If you need | |
467 | local overrides, it's best to directly call system() or getoutput(). |
|
467 | local overrides, it's best to directly call system() or getoutput(). | |
468 |
|
468 | |||
469 | The following names are provided as alternate options: |
|
469 | The following names are provided as alternate options: | |
470 | - xsys: alias to system |
|
470 | - xsys: alias to system | |
471 | - bq: alias to getoutput |
|
471 | - bq: alias to getoutput | |
472 |
|
472 | |||
473 | An instance can then be created as: |
|
473 | An instance can then be created as: | |
474 | >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ') |
|
474 | >>> sysexec = SystemExec(verbose=1,debug=0,header='Calling: ') | |
475 | """ |
|
475 | """ | |
476 |
|
476 | |||
477 | def __init__(self,verbose=0,debug=0,header='',split=0): |
|
477 | def __init__(self,verbose=0,debug=0,header='',split=0): | |
478 | """Specify the instance's values for verbose, debug and header.""" |
|
478 | """Specify the instance's values for verbose, debug and header.""" | |
479 | setattr_list(self,'verbose debug header split') |
|
479 | setattr_list(self,'verbose debug header split') | |
480 |
|
480 | |||
481 | def system(self,cmd): |
|
481 | def system(self,cmd): | |
482 | """Stateful interface to system(), with the same keyword parameters.""" |
|
482 | """Stateful interface to system(), with the same keyword parameters.""" | |
483 |
|
483 | |||
484 | system(cmd,self.verbose,self.debug,self.header) |
|
484 | system(cmd,self.verbose,self.debug,self.header) | |
485 |
|
485 | |||
486 | def shell(self,cmd): |
|
486 | def shell(self,cmd): | |
487 | """Stateful interface to shell(), with the same keyword parameters.""" |
|
487 | """Stateful interface to shell(), with the same keyword parameters.""" | |
488 |
|
488 | |||
489 | shell(cmd,self.verbose,self.debug,self.header) |
|
489 | shell(cmd,self.verbose,self.debug,self.header) | |
490 |
|
490 | |||
491 | xsys = system # alias |
|
491 | xsys = system # alias | |
492 |
|
492 | |||
493 | def getoutput(self,cmd): |
|
493 | def getoutput(self,cmd): | |
494 | """Stateful interface to getoutput().""" |
|
494 | """Stateful interface to getoutput().""" | |
495 |
|
495 | |||
496 | return getoutput(cmd,self.verbose,self.debug,self.header,self.split) |
|
496 | return getoutput(cmd,self.verbose,self.debug,self.header,self.split) | |
497 |
|
497 | |||
498 | def getoutputerror(self,cmd): |
|
498 | def getoutputerror(self,cmd): | |
499 | """Stateful interface to getoutputerror().""" |
|
499 | """Stateful interface to getoutputerror().""" | |
500 |
|
500 | |||
501 | return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split) |
|
501 | return getoutputerror(cmd,self.verbose,self.debug,self.header,self.split) | |
502 |
|
502 | |||
503 | bq = getoutput # alias |
|
503 | bq = getoutput # alias | |
504 |
|
504 | |||
505 | #----------------------------------------------------------------------------- |
|
505 | #----------------------------------------------------------------------------- | |
506 | def mutex_opts(dict,ex_op): |
|
506 | def mutex_opts(dict,ex_op): | |
507 | """Check for presence of mutually exclusive keys in a dict. |
|
507 | """Check for presence of mutually exclusive keys in a dict. | |
508 |
|
508 | |||
509 | Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]""" |
|
509 | Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]""" | |
510 | for op1,op2 in ex_op: |
|
510 | for op1,op2 in ex_op: | |
511 | if op1 in dict and op2 in dict: |
|
511 | if op1 in dict and op2 in dict: | |
512 | raise ValueError,'\n*** ERROR in Arguments *** '\ |
|
512 | raise ValueError,'\n*** ERROR in Arguments *** '\ | |
513 | 'Options '+op1+' and '+op2+' are mutually exclusive.' |
|
513 | 'Options '+op1+' and '+op2+' are mutually exclusive.' | |
514 |
|
514 | |||
515 | #----------------------------------------------------------------------------- |
|
515 | #----------------------------------------------------------------------------- | |
516 | def get_py_filename(name): |
|
516 | def get_py_filename(name): | |
517 | """Return a valid python filename in the current directory. |
|
517 | """Return a valid python filename in the current directory. | |
518 |
|
518 | |||
519 | If the given name is not a file, it adds '.py' and searches again. |
|
519 | If the given name is not a file, it adds '.py' and searches again. | |
520 | Raises IOError with an informative message if the file isn't found.""" |
|
520 | Raises IOError with an informative message if the file isn't found.""" | |
521 |
|
521 | |||
522 | name = os.path.expanduser(name) |
|
522 | name = os.path.expanduser(name) | |
523 | if not os.path.isfile(name) and not name.endswith('.py'): |
|
523 | if not os.path.isfile(name) and not name.endswith('.py'): | |
524 | name += '.py' |
|
524 | name += '.py' | |
525 | if os.path.isfile(name): |
|
525 | if os.path.isfile(name): | |
526 | return name |
|
526 | return name | |
527 | else: |
|
527 | else: | |
528 | raise IOError,'File `%s` not found.' % name |
|
528 | raise IOError,'File `%s` not found.' % name | |
529 |
|
529 | |||
530 | #----------------------------------------------------------------------------- |
|
530 | #----------------------------------------------------------------------------- | |
531 | def filefind(fname,alt_dirs = None): |
|
531 | def filefind(fname,alt_dirs = None): | |
532 | """Return the given filename either in the current directory, if it |
|
532 | """Return the given filename either in the current directory, if it | |
533 | exists, or in a specified list of directories. |
|
533 | exists, or in a specified list of directories. | |
534 |
|
534 | |||
535 | ~ expansion is done on all file and directory names. |
|
535 | ~ expansion is done on all file and directory names. | |
536 |
|
536 | |||
537 | Upon an unsuccessful search, raise an IOError exception.""" |
|
537 | Upon an unsuccessful search, raise an IOError exception.""" | |
538 |
|
538 | |||
539 | if alt_dirs is None: |
|
539 | if alt_dirs is None: | |
540 | try: |
|
540 | try: | |
541 | alt_dirs = get_home_dir() |
|
541 | alt_dirs = get_home_dir() | |
542 | except HomeDirError: |
|
542 | except HomeDirError: | |
543 | alt_dirs = os.getcwd() |
|
543 | alt_dirs = os.getcwd() | |
544 | search = [fname] + list_strings(alt_dirs) |
|
544 | search = [fname] + list_strings(alt_dirs) | |
545 | search = map(os.path.expanduser,search) |
|
545 | search = map(os.path.expanduser,search) | |
546 | #print 'search list for',fname,'list:',search # dbg |
|
546 | #print 'search list for',fname,'list:',search # dbg | |
547 | fname = search[0] |
|
547 | fname = search[0] | |
548 | if os.path.isfile(fname): |
|
548 | if os.path.isfile(fname): | |
549 | return fname |
|
549 | return fname | |
550 | for direc in search[1:]: |
|
550 | for direc in search[1:]: | |
551 | testname = os.path.join(direc,fname) |
|
551 | testname = os.path.join(direc,fname) | |
552 | #print 'testname',testname # dbg |
|
552 | #print 'testname',testname # dbg | |
553 | if os.path.isfile(testname): |
|
553 | if os.path.isfile(testname): | |
554 | return testname |
|
554 | return testname | |
555 | raise IOError,'File' + `fname` + \ |
|
555 | raise IOError,'File' + `fname` + \ | |
556 | ' not found in current or supplied directories:' + `alt_dirs` |
|
556 | ' not found in current or supplied directories:' + `alt_dirs` | |
557 |
|
557 | |||
558 | #---------------------------------------------------------------------------- |
|
558 | #---------------------------------------------------------------------------- | |
559 | def file_read(filename): |
|
559 | def file_read(filename): | |
560 | """Read a file and close it. Returns the file source.""" |
|
560 | """Read a file and close it. Returns the file source.""" | |
561 | fobj = open(filename,'r'); |
|
561 | fobj = open(filename,'r'); | |
562 | source = fobj.read(); |
|
562 | source = fobj.read(); | |
563 | fobj.close() |
|
563 | fobj.close() | |
564 | return source |
|
564 | return source | |
565 |
|
565 | |||
566 | def file_readlines(filename): |
|
566 | def file_readlines(filename): | |
567 | """Read a file and close it. Returns the file source using readlines().""" |
|
567 | """Read a file and close it. Returns the file source using readlines().""" | |
568 | fobj = open(filename,'r'); |
|
568 | fobj = open(filename,'r'); | |
569 | lines = fobj.readlines(); |
|
569 | lines = fobj.readlines(); | |
570 | fobj.close() |
|
570 | fobj.close() | |
571 | return lines |
|
571 | return lines | |
572 |
|
572 | |||
573 | #---------------------------------------------------------------------------- |
|
573 | #---------------------------------------------------------------------------- | |
574 | def target_outdated(target,deps): |
|
574 | def target_outdated(target,deps): | |
575 | """Determine whether a target is out of date. |
|
575 | """Determine whether a target is out of date. | |
576 |
|
576 | |||
577 | target_outdated(target,deps) -> 1/0 |
|
577 | target_outdated(target,deps) -> 1/0 | |
578 |
|
578 | |||
579 | deps: list of filenames which MUST exist. |
|
579 | deps: list of filenames which MUST exist. | |
580 | target: single filename which may or may not exist. |
|
580 | target: single filename which may or may not exist. | |
581 |
|
581 | |||
582 | If target doesn't exist or is older than any file listed in deps, return |
|
582 | If target doesn't exist or is older than any file listed in deps, return | |
583 | true, otherwise return false. |
|
583 | true, otherwise return false. | |
584 | """ |
|
584 | """ | |
585 | try: |
|
585 | try: | |
586 | target_time = os.path.getmtime(target) |
|
586 | target_time = os.path.getmtime(target) | |
587 | except os.error: |
|
587 | except os.error: | |
588 | return 1 |
|
588 | return 1 | |
589 | for dep in deps: |
|
589 | for dep in deps: | |
590 | dep_time = os.path.getmtime(dep) |
|
590 | dep_time = os.path.getmtime(dep) | |
591 | if dep_time > target_time: |
|
591 | if dep_time > target_time: | |
592 | #print "For target",target,"Dep failed:",dep # dbg |
|
592 | #print "For target",target,"Dep failed:",dep # dbg | |
593 | #print "times (dep,tar):",dep_time,target_time # dbg |
|
593 | #print "times (dep,tar):",dep_time,target_time # dbg | |
594 | return 1 |
|
594 | return 1 | |
595 | return 0 |
|
595 | return 0 | |
596 |
|
596 | |||
597 | #----------------------------------------------------------------------------- |
|
597 | #----------------------------------------------------------------------------- | |
598 | def target_update(target,deps,cmd): |
|
598 | def target_update(target,deps,cmd): | |
599 | """Update a target with a given command given a list of dependencies. |
|
599 | """Update a target with a given command given a list of dependencies. | |
600 |
|
600 | |||
601 | target_update(target,deps,cmd) -> runs cmd if target is outdated. |
|
601 | target_update(target,deps,cmd) -> runs cmd if target is outdated. | |
602 |
|
602 | |||
603 | This is just a wrapper around target_outdated() which calls the given |
|
603 | This is just a wrapper around target_outdated() which calls the given | |
604 | command if target is outdated.""" |
|
604 | command if target is outdated.""" | |
605 |
|
605 | |||
606 | if target_outdated(target,deps): |
|
606 | if target_outdated(target,deps): | |
607 | xsys(cmd) |
|
607 | xsys(cmd) | |
608 |
|
608 | |||
609 | #---------------------------------------------------------------------------- |
|
609 | #---------------------------------------------------------------------------- | |
610 | def unquote_ends(istr): |
|
610 | def unquote_ends(istr): | |
611 | """Remove a single pair of quotes from the endpoints of a string.""" |
|
611 | """Remove a single pair of quotes from the endpoints of a string.""" | |
612 |
|
612 | |||
613 | if not istr: |
|
613 | if not istr: | |
614 | return istr |
|
614 | return istr | |
615 | if (istr[0]=="'" and istr[-1]=="'") or \ |
|
615 | if (istr[0]=="'" and istr[-1]=="'") or \ | |
616 | (istr[0]=='"' and istr[-1]=='"'): |
|
616 | (istr[0]=='"' and istr[-1]=='"'): | |
617 | return istr[1:-1] |
|
617 | return istr[1:-1] | |
618 | else: |
|
618 | else: | |
619 | return istr |
|
619 | return istr | |
620 |
|
620 | |||
621 | #---------------------------------------------------------------------------- |
|
621 | #---------------------------------------------------------------------------- | |
622 | def process_cmdline(argv,names=[],defaults={},usage=''): |
|
622 | def process_cmdline(argv,names=[],defaults={},usage=''): | |
623 | """ Process command-line options and arguments. |
|
623 | """ Process command-line options and arguments. | |
624 |
|
624 | |||
625 | Arguments: |
|
625 | Arguments: | |
626 |
|
626 | |||
627 | - argv: list of arguments, typically sys.argv. |
|
627 | - argv: list of arguments, typically sys.argv. | |
628 |
|
628 | |||
629 | - names: list of option names. See DPyGetOpt docs for details on options |
|
629 | - names: list of option names. See DPyGetOpt docs for details on options | |
630 | syntax. |
|
630 | syntax. | |
631 |
|
631 | |||
632 | - defaults: dict of default values. |
|
632 | - defaults: dict of default values. | |
633 |
|
633 | |||
634 | - usage: optional usage notice to print if a wrong argument is passed. |
|
634 | - usage: optional usage notice to print if a wrong argument is passed. | |
635 |
|
635 | |||
636 | Return a dict of options and a list of free arguments.""" |
|
636 | Return a dict of options and a list of free arguments.""" | |
637 |
|
637 | |||
638 | getopt = DPyGetOpt.DPyGetOpt() |
|
638 | getopt = DPyGetOpt.DPyGetOpt() | |
639 | getopt.setIgnoreCase(0) |
|
639 | getopt.setIgnoreCase(0) | |
640 | getopt.parseConfiguration(names) |
|
640 | getopt.parseConfiguration(names) | |
641 |
|
641 | |||
642 | try: |
|
642 | try: | |
643 | getopt.processArguments(argv) |
|
643 | getopt.processArguments(argv) | |
644 | except DPyGetOpt.ArgumentError, exc: |
|
644 | except DPyGetOpt.ArgumentError, exc: | |
645 | print usage |
|
645 | print usage | |
646 | warn('"%s"' % exc,level=4) |
|
646 | warn('"%s"' % exc,level=4) | |
647 |
|
647 | |||
648 | defaults.update(getopt.optionValues) |
|
648 | defaults.update(getopt.optionValues) | |
649 | args = getopt.freeValues |
|
649 | args = getopt.freeValues | |
650 |
|
650 | |||
651 | return defaults,args |
|
651 | return defaults,args | |
652 |
|
652 | |||
653 | #---------------------------------------------------------------------------- |
|
653 | #---------------------------------------------------------------------------- | |
654 | def optstr2types(ostr): |
|
654 | def optstr2types(ostr): | |
655 | """Convert a string of option names to a dict of type mappings. |
|
655 | """Convert a string of option names to a dict of type mappings. | |
656 |
|
656 | |||
657 | optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'} |
|
657 | optstr2types(str) -> {None:'string_opts',int:'int_opts',float:'float_opts'} | |
658 |
|
658 | |||
659 | This is used to get the types of all the options in a string formatted |
|
659 | This is used to get the types of all the options in a string formatted | |
660 | with the conventions of DPyGetOpt. The 'type' None is used for options |
|
660 | with the conventions of DPyGetOpt. The 'type' None is used for options | |
661 | which are strings (they need no further conversion). This function's main |
|
661 | which are strings (they need no further conversion). This function's main | |
662 | use is to get a typemap for use with read_dict(). |
|
662 | use is to get a typemap for use with read_dict(). | |
663 | """ |
|
663 | """ | |
664 |
|
664 | |||
665 | typeconv = {None:'',int:'',float:''} |
|
665 | typeconv = {None:'',int:'',float:''} | |
666 | typemap = {'s':None,'i':int,'f':float} |
|
666 | typemap = {'s':None,'i':int,'f':float} | |
667 | opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)') |
|
667 | opt_re = re.compile(r'([\w]*)([^:=]*:?=?)([sif]?)') | |
668 |
|
668 | |||
669 | for w in ostr.split(): |
|
669 | for w in ostr.split(): | |
670 | oname,alias,otype = opt_re.match(w).groups() |
|
670 | oname,alias,otype = opt_re.match(w).groups() | |
671 | if otype == '' or alias == '!': # simple switches are integers too |
|
671 | if otype == '' or alias == '!': # simple switches are integers too | |
672 | otype = 'i' |
|
672 | otype = 'i' | |
673 | typeconv[typemap[otype]] += oname + ' ' |
|
673 | typeconv[typemap[otype]] += oname + ' ' | |
674 | return typeconv |
|
674 | return typeconv | |
675 |
|
675 | |||
676 | #---------------------------------------------------------------------------- |
|
676 | #---------------------------------------------------------------------------- | |
677 | def read_dict(filename,type_conv=None,**opt): |
|
677 | def read_dict(filename,type_conv=None,**opt): | |
678 | r"""Read a dictionary of key=value pairs from an input file, optionally |
|
678 | r"""Read a dictionary of key=value pairs from an input file, optionally | |
679 | performing conversions on the resulting values. |
|
679 | performing conversions on the resulting values. | |
680 |
|
680 | |||
681 | read_dict(filename,type_conv,**opt) -> dict |
|
681 | read_dict(filename,type_conv,**opt) -> dict | |
682 |
|
682 | |||
683 | Only one value per line is accepted, the format should be |
|
683 | Only one value per line is accepted, the format should be | |
684 | # optional comments are ignored |
|
684 | # optional comments are ignored | |
685 | key value\n |
|
685 | key value\n | |
686 |
|
686 | |||
687 | Args: |
|
687 | Args: | |
688 |
|
688 | |||
689 | - type_conv: A dictionary specifying which keys need to be converted to |
|
689 | - type_conv: A dictionary specifying which keys need to be converted to | |
690 | which types. By default all keys are read as strings. This dictionary |
|
690 | which types. By default all keys are read as strings. This dictionary | |
691 | should have as its keys valid conversion functions for strings |
|
691 | should have as its keys valid conversion functions for strings | |
692 | (int,long,float,complex, or your own). The value for each key |
|
692 | (int,long,float,complex, or your own). The value for each key | |
693 | (converter) should be a whitespace separated string containing the names |
|
693 | (converter) should be a whitespace separated string containing the names | |
694 | of all the entries in the file to be converted using that function. For |
|
694 | of all the entries in the file to be converted using that function. For | |
695 | keys to be left alone, use None as the conversion function (only needed |
|
695 | keys to be left alone, use None as the conversion function (only needed | |
696 | with purge=1, see below). |
|
696 | with purge=1, see below). | |
697 |
|
697 | |||
698 | - opt: dictionary with extra options as below (default in parens) |
|
698 | - opt: dictionary with extra options as below (default in parens) | |
699 |
|
699 | |||
700 | purge(0): if set to 1, all keys *not* listed in type_conv are purged out |
|
700 | purge(0): if set to 1, all keys *not* listed in type_conv are purged out | |
701 | of the dictionary to be returned. If purge is going to be used, the |
|
701 | of the dictionary to be returned. If purge is going to be used, the | |
702 | set of keys to be left as strings also has to be explicitly specified |
|
702 | set of keys to be left as strings also has to be explicitly specified | |
703 | using the (non-existent) conversion function None. |
|
703 | using the (non-existent) conversion function None. | |
704 |
|
704 | |||
705 | fs(None): field separator. This is the key/value separator to be used |
|
705 | fs(None): field separator. This is the key/value separator to be used | |
706 | when parsing the file. The None default means any whitespace [behavior |
|
706 | when parsing the file. The None default means any whitespace [behavior | |
707 | of string.split()]. |
|
707 | of string.split()]. | |
708 |
|
708 | |||
709 | strip(0): if 1, strip string values of leading/trailinig whitespace. |
|
709 | strip(0): if 1, strip string values of leading/trailinig whitespace. | |
710 |
|
710 | |||
711 | warn(1): warning level if requested keys are not found in file. |
|
711 | warn(1): warning level if requested keys are not found in file. | |
712 | - 0: silently ignore. |
|
712 | - 0: silently ignore. | |
713 | - 1: inform but proceed. |
|
713 | - 1: inform but proceed. | |
714 | - 2: raise KeyError exception. |
|
714 | - 2: raise KeyError exception. | |
715 |
|
715 | |||
716 | no_empty(0): if 1, remove keys with whitespace strings as a value. |
|
716 | no_empty(0): if 1, remove keys with whitespace strings as a value. | |
717 |
|
717 | |||
718 | unique([]): list of keys (or space separated string) which can't be |
|
718 | unique([]): list of keys (or space separated string) which can't be | |
719 | repeated. If one such key is found in the file, each new instance |
|
719 | repeated. If one such key is found in the file, each new instance | |
720 | overwrites the previous one. For keys not listed here, the behavior is |
|
720 | overwrites the previous one. For keys not listed here, the behavior is | |
721 | to make a list of all appearances. |
|
721 | to make a list of all appearances. | |
722 |
|
722 | |||
723 | Example: |
|
723 | Example: | |
724 |
|
724 | |||
725 | If the input file test.ini contains (we put it in a string to keep the test |
|
725 | If the input file test.ini contains (we put it in a string to keep the test | |
726 | self-contained): |
|
726 | self-contained): | |
727 |
|
727 | |||
728 | >>> test_ini = '''\ |
|
728 | >>> test_ini = '''\ | |
729 | ... i 3 |
|
729 | ... i 3 | |
730 | ... x 4.5 |
|
730 | ... x 4.5 | |
731 | ... y 5.5 |
|
731 | ... y 5.5 | |
732 | ... s hi ho''' |
|
732 | ... s hi ho''' | |
733 |
|
733 | |||
734 | Then we can use it as follows: |
|
734 | Then we can use it as follows: | |
735 | >>> type_conv={int:'i',float:'x',None:'s'} |
|
735 | >>> type_conv={int:'i',float:'x',None:'s'} | |
736 |
|
736 | |||
737 | >>> d = read_dict(test_ini) |
|
737 | >>> d = read_dict(test_ini) | |
738 |
|
738 | |||
739 | >>> sorted(d.items()) |
|
739 | >>> sorted(d.items()) | |
740 | [('i', '3'), ('s', 'hi ho'), ('x', '4.5'), ('y', '5.5')] |
|
740 | [('i', '3'), ('s', 'hi ho'), ('x', '4.5'), ('y', '5.5')] | |
741 |
|
741 | |||
742 | >>> d = read_dict(test_ini,type_conv) |
|
742 | >>> d = read_dict(test_ini,type_conv) | |
743 |
|
743 | |||
744 | >>> sorted(d.items()) |
|
744 | >>> sorted(d.items()) | |
745 | [('i', 3), ('s', 'hi ho'), ('x', 4.5), ('y', '5.5')] |
|
745 | [('i', 3), ('s', 'hi ho'), ('x', 4.5), ('y', '5.5')] | |
746 |
|
746 | |||
747 | >>> d = read_dict(test_ini,type_conv,purge=True) |
|
747 | >>> d = read_dict(test_ini,type_conv,purge=True) | |
748 |
|
748 | |||
749 | >>> sorted(d.items()) |
|
749 | >>> sorted(d.items()) | |
750 | [('i', 3), ('s', 'hi ho'), ('x', 4.5)] |
|
750 | [('i', 3), ('s', 'hi ho'), ('x', 4.5)] | |
751 | """ |
|
751 | """ | |
752 |
|
752 | |||
753 | # starting config |
|
753 | # starting config | |
754 | opt.setdefault('purge',0) |
|
754 | opt.setdefault('purge',0) | |
755 | opt.setdefault('fs',None) # field sep defaults to any whitespace |
|
755 | opt.setdefault('fs',None) # field sep defaults to any whitespace | |
756 | opt.setdefault('strip',0) |
|
756 | opt.setdefault('strip',0) | |
757 | opt.setdefault('warn',1) |
|
757 | opt.setdefault('warn',1) | |
758 | opt.setdefault('no_empty',0) |
|
758 | opt.setdefault('no_empty',0) | |
759 | opt.setdefault('unique','') |
|
759 | opt.setdefault('unique','') | |
760 | if type(opt['unique']) in StringTypes: |
|
760 | if type(opt['unique']) in StringTypes: | |
761 | unique_keys = qw(opt['unique']) |
|
761 | unique_keys = qw(opt['unique']) | |
762 | elif type(opt['unique']) in (types.TupleType,types.ListType): |
|
762 | elif type(opt['unique']) in (types.TupleType,types.ListType): | |
763 | unique_keys = opt['unique'] |
|
763 | unique_keys = opt['unique'] | |
764 | else: |
|
764 | else: | |
765 | raise ValueError, 'Unique keys must be given as a string, List or Tuple' |
|
765 | raise ValueError, 'Unique keys must be given as a string, List or Tuple' | |
766 |
|
766 | |||
767 | dict = {} |
|
767 | dict = {} | |
768 |
|
768 | |||
769 | # first read in table of values as strings |
|
769 | # first read in table of values as strings | |
770 | if '\n' in filename: |
|
770 | if '\n' in filename: | |
771 | lines = filename.splitlines() |
|
771 | lines = filename.splitlines() | |
772 | file = None |
|
772 | file = None | |
773 | else: |
|
773 | else: | |
774 | file = open(filename,'r') |
|
774 | file = open(filename,'r') | |
775 | lines = file.readlines() |
|
775 | lines = file.readlines() | |
776 | for line in lines: |
|
776 | for line in lines: | |
777 | line = line.strip() |
|
777 | line = line.strip() | |
778 | if len(line) and line[0]=='#': continue |
|
778 | if len(line) and line[0]=='#': continue | |
779 | if len(line)>0: |
|
779 | if len(line)>0: | |
780 | lsplit = line.split(opt['fs'],1) |
|
780 | lsplit = line.split(opt['fs'],1) | |
781 | try: |
|
781 | try: | |
782 | key,val = lsplit |
|
782 | key,val = lsplit | |
783 | except ValueError: |
|
783 | except ValueError: | |
784 | key,val = lsplit[0],'' |
|
784 | key,val = lsplit[0],'' | |
785 | key = key.strip() |
|
785 | key = key.strip() | |
786 | if opt['strip']: val = val.strip() |
|
786 | if opt['strip']: val = val.strip() | |
787 | if val == "''" or val == '""': val = '' |
|
787 | if val == "''" or val == '""': val = '' | |
788 | if opt['no_empty'] and (val=='' or val.isspace()): |
|
788 | if opt['no_empty'] and (val=='' or val.isspace()): | |
789 | continue |
|
789 | continue | |
790 | # if a key is found more than once in the file, build a list |
|
790 | # if a key is found more than once in the file, build a list | |
791 | # unless it's in the 'unique' list. In that case, last found in file |
|
791 | # unless it's in the 'unique' list. In that case, last found in file | |
792 | # takes precedence. User beware. |
|
792 | # takes precedence. User beware. | |
793 | try: |
|
793 | try: | |
794 | if dict[key] and key in unique_keys: |
|
794 | if dict[key] and key in unique_keys: | |
795 | dict[key] = val |
|
795 | dict[key] = val | |
796 | elif type(dict[key]) is types.ListType: |
|
796 | elif type(dict[key]) is types.ListType: | |
797 | dict[key].append(val) |
|
797 | dict[key].append(val) | |
798 | else: |
|
798 | else: | |
799 | dict[key] = [dict[key],val] |
|
799 | dict[key] = [dict[key],val] | |
800 | except KeyError: |
|
800 | except KeyError: | |
801 | dict[key] = val |
|
801 | dict[key] = val | |
802 | # purge if requested |
|
802 | # purge if requested | |
803 | if opt['purge']: |
|
803 | if opt['purge']: | |
804 | accepted_keys = qwflat(type_conv.values()) |
|
804 | accepted_keys = qwflat(type_conv.values()) | |
805 | for key in dict.keys(): |
|
805 | for key in dict.keys(): | |
806 | if key in accepted_keys: continue |
|
806 | if key in accepted_keys: continue | |
807 | del(dict[key]) |
|
807 | del(dict[key]) | |
808 | # now convert if requested |
|
808 | # now convert if requested | |
809 | if type_conv==None: return dict |
|
809 | if type_conv==None: return dict | |
810 | conversions = type_conv.keys() |
|
810 | conversions = type_conv.keys() | |
811 | try: conversions.remove(None) |
|
811 | try: conversions.remove(None) | |
812 | except: pass |
|
812 | except: pass | |
813 | for convert in conversions: |
|
813 | for convert in conversions: | |
814 | for val in qw(type_conv[convert]): |
|
814 | for val in qw(type_conv[convert]): | |
815 | try: |
|
815 | try: | |
816 | dict[val] = convert(dict[val]) |
|
816 | dict[val] = convert(dict[val]) | |
817 | except KeyError,e: |
|
817 | except KeyError,e: | |
818 | if opt['warn'] == 0: |
|
818 | if opt['warn'] == 0: | |
819 | pass |
|
819 | pass | |
820 | elif opt['warn'] == 1: |
|
820 | elif opt['warn'] == 1: | |
821 | print >>sys.stderr, 'Warning: key',val,\ |
|
821 | print >>sys.stderr, 'Warning: key',val,\ | |
822 | 'not found in file',filename |
|
822 | 'not found in file',filename | |
823 | elif opt['warn'] == 2: |
|
823 | elif opt['warn'] == 2: | |
824 | raise KeyError,e |
|
824 | raise KeyError,e | |
825 | else: |
|
825 | else: | |
826 | raise ValueError,'Warning level must be 0,1 or 2' |
|
826 | raise ValueError,'Warning level must be 0,1 or 2' | |
827 |
|
827 | |||
828 | return dict |
|
828 | return dict | |
829 |
|
829 | |||
830 | #---------------------------------------------------------------------------- |
|
830 | #---------------------------------------------------------------------------- | |
831 | def flag_calls(func): |
|
831 | def flag_calls(func): | |
832 | """Wrap a function to detect and flag when it gets called. |
|
832 | """Wrap a function to detect and flag when it gets called. | |
833 |
|
833 | |||
834 | This is a decorator which takes a function and wraps it in a function with |
|
834 | This is a decorator which takes a function and wraps it in a function with | |
835 | a 'called' attribute. wrapper.called is initialized to False. |
|
835 | a 'called' attribute. wrapper.called is initialized to False. | |
836 |
|
836 | |||
837 | The wrapper.called attribute is set to False right before each call to the |
|
837 | The wrapper.called attribute is set to False right before each call to the | |
838 | wrapped function, so if the call fails it remains False. After the call |
|
838 | wrapped function, so if the call fails it remains False. After the call | |
839 | completes, wrapper.called is set to True and the output is returned. |
|
839 | completes, wrapper.called is set to True and the output is returned. | |
840 |
|
840 | |||
841 | Testing for truth in wrapper.called allows you to determine if a call to |
|
841 | Testing for truth in wrapper.called allows you to determine if a call to | |
842 | func() was attempted and succeeded.""" |
|
842 | func() was attempted and succeeded.""" | |
843 |
|
843 | |||
844 | def wrapper(*args,**kw): |
|
844 | def wrapper(*args,**kw): | |
845 | wrapper.called = False |
|
845 | wrapper.called = False | |
846 | out = func(*args,**kw) |
|
846 | out = func(*args,**kw) | |
847 | wrapper.called = True |
|
847 | wrapper.called = True | |
848 | return out |
|
848 | return out | |
849 |
|
849 | |||
850 | wrapper.called = False |
|
850 | wrapper.called = False | |
851 | wrapper.__doc__ = func.__doc__ |
|
851 | wrapper.__doc__ = func.__doc__ | |
852 | return wrapper |
|
852 | return wrapper | |
853 |
|
853 | |||
854 | #---------------------------------------------------------------------------- |
|
854 | #---------------------------------------------------------------------------- | |
855 | def dhook_wrap(func,*a,**k): |
|
855 | def dhook_wrap(func,*a,**k): | |
856 | """Wrap a function call in a sys.displayhook controller. |
|
856 | """Wrap a function call in a sys.displayhook controller. | |
857 |
|
857 | |||
858 | Returns a wrapper around func which calls func, with all its arguments and |
|
858 | Returns a wrapper around func which calls func, with all its arguments and | |
859 | keywords unmodified, using the default sys.displayhook. Since IPython |
|
859 | keywords unmodified, using the default sys.displayhook. Since IPython | |
860 | modifies sys.displayhook, it breaks the behavior of certain systems that |
|
860 | modifies sys.displayhook, it breaks the behavior of certain systems that | |
861 | rely on the default behavior, notably doctest. |
|
861 | rely on the default behavior, notably doctest. | |
862 | """ |
|
862 | """ | |
863 |
|
863 | |||
864 | def f(*a,**k): |
|
864 | def f(*a,**k): | |
865 |
|
865 | |||
866 | dhook_s = sys.displayhook |
|
866 | dhook_s = sys.displayhook | |
867 | sys.displayhook = sys.__displayhook__ |
|
867 | sys.displayhook = sys.__displayhook__ | |
868 | try: |
|
868 | try: | |
869 | out = func(*a,**k) |
|
869 | out = func(*a,**k) | |
870 | finally: |
|
870 | finally: | |
871 | sys.displayhook = dhook_s |
|
871 | sys.displayhook = dhook_s | |
872 |
|
872 | |||
873 | return out |
|
873 | return out | |
874 |
|
874 | |||
875 | f.__doc__ = func.__doc__ |
|
875 | f.__doc__ = func.__doc__ | |
876 | return f |
|
876 | return f | |
877 |
|
877 | |||
878 | #---------------------------------------------------------------------------- |
|
878 | #---------------------------------------------------------------------------- | |
879 | def doctest_reload(): |
|
879 | def doctest_reload(): | |
880 | """Properly reload doctest to reuse it interactively. |
|
880 | """Properly reload doctest to reuse it interactively. | |
881 |
|
881 | |||
882 | This routine: |
|
882 | This routine: | |
883 |
|
883 | |||
884 | - reloads doctest |
|
884 | - imports doctest but does NOT reload it (see below). | |
885 |
|
885 | |||
886 | - resets its global 'master' attribute to None, so that multiple uses of |
|
886 | - resets its global 'master' attribute to None, so that multiple uses of | |
887 | the module interactively don't produce cumulative reports. |
|
887 | the module interactively don't produce cumulative reports. | |
888 |
|
888 | |||
889 | - Monkeypatches its core test runner method to protect it from IPython's |
|
889 | - Monkeypatches its core test runner method to protect it from IPython's | |
890 | modified displayhook. Doctest expects the default displayhook behavior |
|
890 | modified displayhook. Doctest expects the default displayhook behavior | |
891 | deep down, so our modification breaks it completely. For this reason, a |
|
891 | deep down, so our modification breaks it completely. For this reason, a | |
892 | hard monkeypatch seems like a reasonable solution rather than asking |
|
892 | hard monkeypatch seems like a reasonable solution rather than asking | |
893 |
users to manually use a different doctest runner when under IPython. |
|
893 | users to manually use a different doctest runner when under IPython. | |
894 |
|
|
894 | ||
895 | import doctest |
|
895 | Note | |
896 | reload(doctest) |
|
896 | ---- | |
897 | doctest.master=None |
|
|||
898 |
|
|
897 | ||
899 | try: |
|
898 | This function *used to* reload doctest, but this has been disabled because | |
900 | doctest.DocTestRunner |
|
899 | reloading doctest unconditionally can cause massive breakage of other | |
901 | except AttributeError: |
|
900 | doctest-dependent modules already in memory, such as those for IPython's | |
902 | # This is only for python 2.3 compatibility, remove once we move to |
|
901 | own testing system. The name wasn't changed to avoid breaking people's | |
903 | # 2.4 only. |
|
902 | code, but the reload call isn't actually made anymore.""" | |
904 | pass |
|
903 | ||
905 | else: |
|
904 | import doctest | |
906 | doctest.DocTestRunner.run = dhook_wrap(doctest.DocTestRunner.run) |
|
905 | doctest.master = None | |
|
906 | doctest.DocTestRunner.run = dhook_wrap(doctest.DocTestRunner.run) | |||
907 |
|
907 | |||
908 | #---------------------------------------------------------------------------- |
|
908 | #---------------------------------------------------------------------------- | |
909 | class HomeDirError(Error): |
|
909 | class HomeDirError(Error): | |
910 | pass |
|
910 | pass | |
911 |
|
911 | |||
912 | def get_home_dir(): |
|
912 | def get_home_dir(): | |
913 | """Return the closest possible equivalent to a 'home' directory. |
|
913 | """Return the closest possible equivalent to a 'home' directory. | |
914 |
|
914 | |||
915 | We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH. |
|
915 | We first try $HOME. Absent that, on NT it's $HOMEDRIVE\$HOMEPATH. | |
916 |
|
916 | |||
917 | Currently only Posix and NT are implemented, a HomeDirError exception is |
|
917 | Currently only Posix and NT are implemented, a HomeDirError exception is | |
918 | raised for all other OSes. """ |
|
918 | raised for all other OSes. """ | |
919 |
|
919 | |||
920 | isdir = os.path.isdir |
|
920 | isdir = os.path.isdir | |
921 | env = os.environ |
|
921 | env = os.environ | |
922 |
|
922 | |||
923 | # first, check py2exe distribution root directory for _ipython. |
|
923 | # first, check py2exe distribution root directory for _ipython. | |
924 | # This overrides all. Normally does not exist. |
|
924 | # This overrides all. Normally does not exist. | |
925 |
|
925 | |||
926 | if hasattr(sys, "frozen"): #Is frozen by py2exe |
|
926 | if hasattr(sys, "frozen"): #Is frozen by py2exe | |
927 | if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file |
|
927 | if '\\library.zip\\' in IPython.__file__.lower():#libraries compressed to zip-file | |
928 | root, rest = IPython.__file__.lower().split('library.zip') |
|
928 | root, rest = IPython.__file__.lower().split('library.zip') | |
929 | else: |
|
929 | else: | |
930 | root=os.path.join(os.path.split(IPython.__file__)[0],"../../") |
|
930 | root=os.path.join(os.path.split(IPython.__file__)[0],"../../") | |
931 | root=os.path.abspath(root).rstrip('\\') |
|
931 | root=os.path.abspath(root).rstrip('\\') | |
932 | if isdir(os.path.join(root, '_ipython')): |
|
932 | if isdir(os.path.join(root, '_ipython')): | |
933 | os.environ["IPYKITROOT"] = root |
|
933 | os.environ["IPYKITROOT"] = root | |
934 | return root |
|
934 | return root | |
935 | try: |
|
935 | try: | |
936 | homedir = env['HOME'] |
|
936 | homedir = env['HOME'] | |
937 | if not isdir(homedir): |
|
937 | if not isdir(homedir): | |
938 | # in case a user stuck some string which does NOT resolve to a |
|
938 | # in case a user stuck some string which does NOT resolve to a | |
939 | # valid path, it's as good as if we hadn't foud it |
|
939 | # valid path, it's as good as if we hadn't foud it | |
940 | raise KeyError |
|
940 | raise KeyError | |
941 | return homedir |
|
941 | return homedir | |
942 | except KeyError: |
|
942 | except KeyError: | |
943 | if os.name == 'posix': |
|
943 | if os.name == 'posix': | |
944 | raise HomeDirError,'undefined $HOME, IPython can not proceed.' |
|
944 | raise HomeDirError,'undefined $HOME, IPython can not proceed.' | |
945 | elif os.name == 'nt': |
|
945 | elif os.name == 'nt': | |
946 | # For some strange reason, win9x returns 'nt' for os.name. |
|
946 | # For some strange reason, win9x returns 'nt' for os.name. | |
947 | try: |
|
947 | try: | |
948 | homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH']) |
|
948 | homedir = os.path.join(env['HOMEDRIVE'],env['HOMEPATH']) | |
949 | if not isdir(homedir): |
|
949 | if not isdir(homedir): | |
950 | homedir = os.path.join(env['USERPROFILE']) |
|
950 | homedir = os.path.join(env['USERPROFILE']) | |
951 | if not isdir(homedir): |
|
951 | if not isdir(homedir): | |
952 | raise HomeDirError |
|
952 | raise HomeDirError | |
953 | return homedir |
|
953 | return homedir | |
954 | except KeyError: |
|
954 | except KeyError: | |
955 | try: |
|
955 | try: | |
956 | # Use the registry to get the 'My Documents' folder. |
|
956 | # Use the registry to get the 'My Documents' folder. | |
957 | import _winreg as wreg |
|
957 | import _winreg as wreg | |
958 | key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, |
|
958 | key = wreg.OpenKey(wreg.HKEY_CURRENT_USER, | |
959 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders") |
|
959 | "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders") | |
960 | homedir = wreg.QueryValueEx(key,'Personal')[0] |
|
960 | homedir = wreg.QueryValueEx(key,'Personal')[0] | |
961 | key.Close() |
|
961 | key.Close() | |
962 | if not isdir(homedir): |
|
962 | if not isdir(homedir): | |
963 | e = ('Invalid "Personal" folder registry key ' |
|
963 | e = ('Invalid "Personal" folder registry key ' | |
964 | 'typically "My Documents".\n' |
|
964 | 'typically "My Documents".\n' | |
965 | 'Value: %s\n' |
|
965 | 'Value: %s\n' | |
966 | 'This is not a valid directory on your system.' % |
|
966 | 'This is not a valid directory on your system.' % | |
967 | homedir) |
|
967 | homedir) | |
968 | raise HomeDirError(e) |
|
968 | raise HomeDirError(e) | |
969 | return homedir |
|
969 | return homedir | |
970 | except HomeDirError: |
|
970 | except HomeDirError: | |
971 | raise |
|
971 | raise | |
972 | except: |
|
972 | except: | |
973 | return 'C:\\' |
|
973 | return 'C:\\' | |
974 | elif os.name == 'dos': |
|
974 | elif os.name == 'dos': | |
975 | # Desperate, may do absurd things in classic MacOS. May work under DOS. |
|
975 | # Desperate, may do absurd things in classic MacOS. May work under DOS. | |
976 | return 'C:\\' |
|
976 | return 'C:\\' | |
977 | else: |
|
977 | else: | |
978 | raise HomeDirError,'support for your operating system not implemented.' |
|
978 | raise HomeDirError,'support for your operating system not implemented.' | |
979 |
|
979 | |||
980 |
|
980 | |||
981 | def get_ipython_dir(): |
|
981 | def get_ipython_dir(): | |
982 | """Get the IPython directory for this platform and user. |
|
982 | """Get the IPython directory for this platform and user. | |
983 |
|
983 | |||
984 | This uses the logic in `get_home_dir` to find the home directory |
|
984 | This uses the logic in `get_home_dir` to find the home directory | |
985 | and the adds either .ipython or _ipython to the end of the path. |
|
985 | and the adds either .ipython or _ipython to the end of the path. | |
986 | """ |
|
986 | """ | |
987 | if os.name == 'posix': |
|
987 | if os.name == 'posix': | |
988 | ipdir_def = '.ipython' |
|
988 | ipdir_def = '.ipython' | |
989 | else: |
|
989 | else: | |
990 | ipdir_def = '_ipython' |
|
990 | ipdir_def = '_ipython' | |
991 | home_dir = get_home_dir() |
|
991 | home_dir = get_home_dir() | |
992 | ipdir = os.path.abspath(os.environ.get('IPYTHONDIR', |
|
992 | ipdir = os.path.abspath(os.environ.get('IPYTHONDIR', | |
993 | os.path.join(home_dir, ipdir_def))) |
|
993 | os.path.join(home_dir, ipdir_def))) | |
994 | return ipdir.decode(sys.getfilesystemencoding()) |
|
994 | return ipdir.decode(sys.getfilesystemencoding()) | |
995 |
|
995 | |||
996 | def get_security_dir(): |
|
996 | def get_security_dir(): | |
997 | """Get the IPython security directory. |
|
997 | """Get the IPython security directory. | |
998 |
|
998 | |||
999 | This directory is the default location for all security related files, |
|
999 | This directory is the default location for all security related files, | |
1000 | including SSL/TLS certificates and FURL files. |
|
1000 | including SSL/TLS certificates and FURL files. | |
1001 |
|
1001 | |||
1002 | If the directory does not exist, it is created with 0700 permissions. |
|
1002 | If the directory does not exist, it is created with 0700 permissions. | |
1003 | If it exists, permissions are set to 0700. |
|
1003 | If it exists, permissions are set to 0700. | |
1004 | """ |
|
1004 | """ | |
1005 | security_dir = os.path.join(get_ipython_dir(), 'security') |
|
1005 | security_dir = os.path.join(get_ipython_dir(), 'security') | |
1006 | if not os.path.isdir(security_dir): |
|
1006 | if not os.path.isdir(security_dir): | |
1007 | os.mkdir(security_dir, 0700) |
|
1007 | os.mkdir(security_dir, 0700) | |
1008 | else: |
|
1008 | else: | |
1009 | os.chmod(security_dir, 0700) |
|
1009 | os.chmod(security_dir, 0700) | |
1010 | return security_dir |
|
1010 | return security_dir | |
1011 |
|
1011 | |||
1012 | def get_log_dir(): |
|
1012 | def get_log_dir(): | |
1013 | """Get the IPython log directory. |
|
1013 | """Get the IPython log directory. | |
1014 |
|
1014 | |||
1015 | If the log directory does not exist, it is created. |
|
1015 | If the log directory does not exist, it is created. | |
1016 | """ |
|
1016 | """ | |
1017 | log_dir = os.path.join(get_ipython_dir(), 'log') |
|
1017 | log_dir = os.path.join(get_ipython_dir(), 'log') | |
1018 | if not os.path.isdir(log_dir): |
|
1018 | if not os.path.isdir(log_dir): | |
1019 | os.mkdir(log_dir, 0777) |
|
1019 | os.mkdir(log_dir, 0777) | |
1020 | return log_dir |
|
1020 | return log_dir | |
1021 |
|
1021 | |||
1022 | #**************************************************************************** |
|
1022 | #**************************************************************************** | |
1023 | # strings and text |
|
1023 | # strings and text | |
1024 |
|
1024 | |||
1025 | class LSString(str): |
|
1025 | class LSString(str): | |
1026 | """String derivative with a special access attributes. |
|
1026 | """String derivative with a special access attributes. | |
1027 |
|
1027 | |||
1028 | These are normal strings, but with the special attributes: |
|
1028 | These are normal strings, but with the special attributes: | |
1029 |
|
1029 | |||
1030 | .l (or .list) : value as list (split on newlines). |
|
1030 | .l (or .list) : value as list (split on newlines). | |
1031 | .n (or .nlstr): original value (the string itself). |
|
1031 | .n (or .nlstr): original value (the string itself). | |
1032 | .s (or .spstr): value as whitespace-separated string. |
|
1032 | .s (or .spstr): value as whitespace-separated string. | |
1033 | .p (or .paths): list of path objects |
|
1033 | .p (or .paths): list of path objects | |
1034 |
|
1034 | |||
1035 | Any values which require transformations are computed only once and |
|
1035 | Any values which require transformations are computed only once and | |
1036 | cached. |
|
1036 | cached. | |
1037 |
|
1037 | |||
1038 | Such strings are very useful to efficiently interact with the shell, which |
|
1038 | Such strings are very useful to efficiently interact with the shell, which | |
1039 | typically only understands whitespace-separated options for commands.""" |
|
1039 | typically only understands whitespace-separated options for commands.""" | |
1040 |
|
1040 | |||
1041 | def get_list(self): |
|
1041 | def get_list(self): | |
1042 | try: |
|
1042 | try: | |
1043 | return self.__list |
|
1043 | return self.__list | |
1044 | except AttributeError: |
|
1044 | except AttributeError: | |
1045 | self.__list = self.split('\n') |
|
1045 | self.__list = self.split('\n') | |
1046 | return self.__list |
|
1046 | return self.__list | |
1047 |
|
1047 | |||
1048 | l = list = property(get_list) |
|
1048 | l = list = property(get_list) | |
1049 |
|
1049 | |||
1050 | def get_spstr(self): |
|
1050 | def get_spstr(self): | |
1051 | try: |
|
1051 | try: | |
1052 | return self.__spstr |
|
1052 | return self.__spstr | |
1053 | except AttributeError: |
|
1053 | except AttributeError: | |
1054 | self.__spstr = self.replace('\n',' ') |
|
1054 | self.__spstr = self.replace('\n',' ') | |
1055 | return self.__spstr |
|
1055 | return self.__spstr | |
1056 |
|
1056 | |||
1057 | s = spstr = property(get_spstr) |
|
1057 | s = spstr = property(get_spstr) | |
1058 |
|
1058 | |||
1059 | def get_nlstr(self): |
|
1059 | def get_nlstr(self): | |
1060 | return self |
|
1060 | return self | |
1061 |
|
1061 | |||
1062 | n = nlstr = property(get_nlstr) |
|
1062 | n = nlstr = property(get_nlstr) | |
1063 |
|
1063 | |||
1064 | def get_paths(self): |
|
1064 | def get_paths(self): | |
1065 | try: |
|
1065 | try: | |
1066 | return self.__paths |
|
1066 | return self.__paths | |
1067 | except AttributeError: |
|
1067 | except AttributeError: | |
1068 | self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)] |
|
1068 | self.__paths = [path(p) for p in self.split('\n') if os.path.exists(p)] | |
1069 | return self.__paths |
|
1069 | return self.__paths | |
1070 |
|
1070 | |||
1071 | p = paths = property(get_paths) |
|
1071 | p = paths = property(get_paths) | |
1072 |
|
1072 | |||
1073 | def print_lsstring(arg): |
|
1073 | def print_lsstring(arg): | |
1074 | """ Prettier (non-repr-like) and more informative printer for LSString """ |
|
1074 | """ Prettier (non-repr-like) and more informative printer for LSString """ | |
1075 | print "LSString (.p, .n, .l, .s available). Value:" |
|
1075 | print "LSString (.p, .n, .l, .s available). Value:" | |
1076 | print arg |
|
1076 | print arg | |
1077 |
|
1077 | |||
1078 | print_lsstring = result_display.when_type(LSString)(print_lsstring) |
|
1078 | print_lsstring = result_display.when_type(LSString)(print_lsstring) | |
1079 |
|
1079 | |||
1080 | #---------------------------------------------------------------------------- |
|
1080 | #---------------------------------------------------------------------------- | |
1081 | class SList(list): |
|
1081 | class SList(list): | |
1082 | """List derivative with a special access attributes. |
|
1082 | """List derivative with a special access attributes. | |
1083 |
|
1083 | |||
1084 | These are normal lists, but with the special attributes: |
|
1084 | These are normal lists, but with the special attributes: | |
1085 |
|
1085 | |||
1086 | .l (or .list) : value as list (the list itself). |
|
1086 | .l (or .list) : value as list (the list itself). | |
1087 | .n (or .nlstr): value as a string, joined on newlines. |
|
1087 | .n (or .nlstr): value as a string, joined on newlines. | |
1088 | .s (or .spstr): value as a string, joined on spaces. |
|
1088 | .s (or .spstr): value as a string, joined on spaces. | |
1089 | .p (or .paths): list of path objects |
|
1089 | .p (or .paths): list of path objects | |
1090 |
|
1090 | |||
1091 | Any values which require transformations are computed only once and |
|
1091 | Any values which require transformations are computed only once and | |
1092 | cached.""" |
|
1092 | cached.""" | |
1093 |
|
1093 | |||
1094 | def get_list(self): |
|
1094 | def get_list(self): | |
1095 | return self |
|
1095 | return self | |
1096 |
|
1096 | |||
1097 | l = list = property(get_list) |
|
1097 | l = list = property(get_list) | |
1098 |
|
1098 | |||
1099 | def get_spstr(self): |
|
1099 | def get_spstr(self): | |
1100 | try: |
|
1100 | try: | |
1101 | return self.__spstr |
|
1101 | return self.__spstr | |
1102 | except AttributeError: |
|
1102 | except AttributeError: | |
1103 | self.__spstr = ' '.join(self) |
|
1103 | self.__spstr = ' '.join(self) | |
1104 | return self.__spstr |
|
1104 | return self.__spstr | |
1105 |
|
1105 | |||
1106 | s = spstr = property(get_spstr) |
|
1106 | s = spstr = property(get_spstr) | |
1107 |
|
1107 | |||
1108 | def get_nlstr(self): |
|
1108 | def get_nlstr(self): | |
1109 | try: |
|
1109 | try: | |
1110 | return self.__nlstr |
|
1110 | return self.__nlstr | |
1111 | except AttributeError: |
|
1111 | except AttributeError: | |
1112 | self.__nlstr = '\n'.join(self) |
|
1112 | self.__nlstr = '\n'.join(self) | |
1113 | return self.__nlstr |
|
1113 | return self.__nlstr | |
1114 |
|
1114 | |||
1115 | n = nlstr = property(get_nlstr) |
|
1115 | n = nlstr = property(get_nlstr) | |
1116 |
|
1116 | |||
1117 | def get_paths(self): |
|
1117 | def get_paths(self): | |
1118 | try: |
|
1118 | try: | |
1119 | return self.__paths |
|
1119 | return self.__paths | |
1120 | except AttributeError: |
|
1120 | except AttributeError: | |
1121 | self.__paths = [path(p) for p in self if os.path.exists(p)] |
|
1121 | self.__paths = [path(p) for p in self if os.path.exists(p)] | |
1122 | return self.__paths |
|
1122 | return self.__paths | |
1123 |
|
1123 | |||
1124 | p = paths = property(get_paths) |
|
1124 | p = paths = property(get_paths) | |
1125 |
|
1125 | |||
1126 | def grep(self, pattern, prune = False, field = None): |
|
1126 | def grep(self, pattern, prune = False, field = None): | |
1127 | """ Return all strings matching 'pattern' (a regex or callable) |
|
1127 | """ Return all strings matching 'pattern' (a regex or callable) | |
1128 |
|
1128 | |||
1129 | This is case-insensitive. If prune is true, return all items |
|
1129 | This is case-insensitive. If prune is true, return all items | |
1130 | NOT matching the pattern. |
|
1130 | NOT matching the pattern. | |
1131 |
|
1131 | |||
1132 | If field is specified, the match must occur in the specified |
|
1132 | If field is specified, the match must occur in the specified | |
1133 | whitespace-separated field. |
|
1133 | whitespace-separated field. | |
1134 |
|
1134 | |||
1135 | Examples:: |
|
1135 | Examples:: | |
1136 |
|
1136 | |||
1137 | a.grep( lambda x: x.startswith('C') ) |
|
1137 | a.grep( lambda x: x.startswith('C') ) | |
1138 | a.grep('Cha.*log', prune=1) |
|
1138 | a.grep('Cha.*log', prune=1) | |
1139 | a.grep('chm', field=-1) |
|
1139 | a.grep('chm', field=-1) | |
1140 | """ |
|
1140 | """ | |
1141 |
|
1141 | |||
1142 | def match_target(s): |
|
1142 | def match_target(s): | |
1143 | if field is None: |
|
1143 | if field is None: | |
1144 | return s |
|
1144 | return s | |
1145 | parts = s.split() |
|
1145 | parts = s.split() | |
1146 | try: |
|
1146 | try: | |
1147 | tgt = parts[field] |
|
1147 | tgt = parts[field] | |
1148 | return tgt |
|
1148 | return tgt | |
1149 | except IndexError: |
|
1149 | except IndexError: | |
1150 | return "" |
|
1150 | return "" | |
1151 |
|
1151 | |||
1152 | if isinstance(pattern, basestring): |
|
1152 | if isinstance(pattern, basestring): | |
1153 | pred = lambda x : re.search(pattern, x, re.IGNORECASE) |
|
1153 | pred = lambda x : re.search(pattern, x, re.IGNORECASE) | |
1154 | else: |
|
1154 | else: | |
1155 | pred = pattern |
|
1155 | pred = pattern | |
1156 | if not prune: |
|
1156 | if not prune: | |
1157 | return SList([el for el in self if pred(match_target(el))]) |
|
1157 | return SList([el for el in self if pred(match_target(el))]) | |
1158 | else: |
|
1158 | else: | |
1159 | return SList([el for el in self if not pred(match_target(el))]) |
|
1159 | return SList([el for el in self if not pred(match_target(el))]) | |
1160 | def fields(self, *fields): |
|
1160 | def fields(self, *fields): | |
1161 | """ Collect whitespace-separated fields from string list |
|
1161 | """ Collect whitespace-separated fields from string list | |
1162 |
|
1162 | |||
1163 | Allows quick awk-like usage of string lists. |
|
1163 | Allows quick awk-like usage of string lists. | |
1164 |
|
1164 | |||
1165 | Example data (in var a, created by 'a = !ls -l'):: |
|
1165 | Example data (in var a, created by 'a = !ls -l'):: | |
1166 | -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog |
|
1166 | -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog | |
1167 | drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython |
|
1167 | drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython | |
1168 |
|
1168 | |||
1169 | a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+'] |
|
1169 | a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+'] | |
1170 | a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+'] |
|
1170 | a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+'] | |
1171 | (note the joining by space). |
|
1171 | (note the joining by space). | |
1172 | a.fields(-1) is ['ChangeLog', 'IPython'] |
|
1172 | a.fields(-1) is ['ChangeLog', 'IPython'] | |
1173 |
|
1173 | |||
1174 | IndexErrors are ignored. |
|
1174 | IndexErrors are ignored. | |
1175 |
|
1175 | |||
1176 | Without args, fields() just split()'s the strings. |
|
1176 | Without args, fields() just split()'s the strings. | |
1177 | """ |
|
1177 | """ | |
1178 | if len(fields) == 0: |
|
1178 | if len(fields) == 0: | |
1179 | return [el.split() for el in self] |
|
1179 | return [el.split() for el in self] | |
1180 |
|
1180 | |||
1181 | res = SList() |
|
1181 | res = SList() | |
1182 | for el in [f.split() for f in self]: |
|
1182 | for el in [f.split() for f in self]: | |
1183 | lineparts = [] |
|
1183 | lineparts = [] | |
1184 |
|
1184 | |||
1185 | for fd in fields: |
|
1185 | for fd in fields: | |
1186 | try: |
|
1186 | try: | |
1187 | lineparts.append(el[fd]) |
|
1187 | lineparts.append(el[fd]) | |
1188 | except IndexError: |
|
1188 | except IndexError: | |
1189 | pass |
|
1189 | pass | |
1190 | if lineparts: |
|
1190 | if lineparts: | |
1191 | res.append(" ".join(lineparts)) |
|
1191 | res.append(" ".join(lineparts)) | |
1192 |
|
1192 | |||
1193 | return res |
|
1193 | return res | |
1194 | def sort(self,field= None, nums = False): |
|
1194 | def sort(self,field= None, nums = False): | |
1195 | """ sort by specified fields (see fields()) |
|
1195 | """ sort by specified fields (see fields()) | |
1196 |
|
1196 | |||
1197 | Example:: |
|
1197 | Example:: | |
1198 | a.sort(1, nums = True) |
|
1198 | a.sort(1, nums = True) | |
1199 |
|
1199 | |||
1200 | Sorts a by second field, in numerical order (so that 21 > 3) |
|
1200 | Sorts a by second field, in numerical order (so that 21 > 3) | |
1201 |
|
1201 | |||
1202 | """ |
|
1202 | """ | |
1203 |
|
1203 | |||
1204 | #decorate, sort, undecorate |
|
1204 | #decorate, sort, undecorate | |
1205 | if field is not None: |
|
1205 | if field is not None: | |
1206 | dsu = [[SList([line]).fields(field), line] for line in self] |
|
1206 | dsu = [[SList([line]).fields(field), line] for line in self] | |
1207 | else: |
|
1207 | else: | |
1208 | dsu = [[line, line] for line in self] |
|
1208 | dsu = [[line, line] for line in self] | |
1209 | if nums: |
|
1209 | if nums: | |
1210 | for i in range(len(dsu)): |
|
1210 | for i in range(len(dsu)): | |
1211 | numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()]) |
|
1211 | numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()]) | |
1212 | try: |
|
1212 | try: | |
1213 | n = int(numstr) |
|
1213 | n = int(numstr) | |
1214 | except ValueError: |
|
1214 | except ValueError: | |
1215 | n = 0; |
|
1215 | n = 0; | |
1216 | dsu[i][0] = n |
|
1216 | dsu[i][0] = n | |
1217 |
|
1217 | |||
1218 |
|
1218 | |||
1219 | dsu.sort() |
|
1219 | dsu.sort() | |
1220 | return SList([t[1] for t in dsu]) |
|
1220 | return SList([t[1] for t in dsu]) | |
1221 |
|
1221 | |||
1222 | def print_slist(arg): |
|
1222 | def print_slist(arg): | |
1223 | """ Prettier (non-repr-like) and more informative printer for SList """ |
|
1223 | """ Prettier (non-repr-like) and more informative printer for SList """ | |
1224 | print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):" |
|
1224 | print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):" | |
1225 | if hasattr(arg, 'hideonce') and arg.hideonce: |
|
1225 | if hasattr(arg, 'hideonce') and arg.hideonce: | |
1226 | arg.hideonce = False |
|
1226 | arg.hideonce = False | |
1227 | return |
|
1227 | return | |
1228 |
|
1228 | |||
1229 | nlprint(arg) |
|
1229 | nlprint(arg) | |
1230 |
|
1230 | |||
1231 | print_slist = result_display.when_type(SList)(print_slist) |
|
1231 | print_slist = result_display.when_type(SList)(print_slist) | |
1232 |
|
1232 | |||
1233 |
|
1233 | |||
1234 |
|
1234 | |||
1235 | #---------------------------------------------------------------------------- |
|
1235 | #---------------------------------------------------------------------------- | |
1236 | def esc_quotes(strng): |
|
1236 | def esc_quotes(strng): | |
1237 | """Return the input string with single and double quotes escaped out""" |
|
1237 | """Return the input string with single and double quotes escaped out""" | |
1238 |
|
1238 | |||
1239 | return strng.replace('"','\\"').replace("'","\\'") |
|
1239 | return strng.replace('"','\\"').replace("'","\\'") | |
1240 |
|
1240 | |||
1241 | #---------------------------------------------------------------------------- |
|
1241 | #---------------------------------------------------------------------------- | |
1242 | def make_quoted_expr(s): |
|
1242 | def make_quoted_expr(s): | |
1243 | """Return string s in appropriate quotes, using raw string if possible. |
|
1243 | """Return string s in appropriate quotes, using raw string if possible. | |
1244 |
|
1244 | |||
1245 | XXX - example removed because it caused encoding errors in documentation |
|
1245 | XXX - example removed because it caused encoding errors in documentation | |
1246 | generation. We need a new example that doesn't contain invalid chars. |
|
1246 | generation. We need a new example that doesn't contain invalid chars. | |
1247 |
|
1247 | |||
1248 | Note the use of raw string and padding at the end to allow trailing |
|
1248 | Note the use of raw string and padding at the end to allow trailing | |
1249 | backslash. |
|
1249 | backslash. | |
1250 | """ |
|
1250 | """ | |
1251 |
|
1251 | |||
1252 | tail = '' |
|
1252 | tail = '' | |
1253 | tailpadding = '' |
|
1253 | tailpadding = '' | |
1254 | raw = '' |
|
1254 | raw = '' | |
1255 | if "\\" in s: |
|
1255 | if "\\" in s: | |
1256 | raw = 'r' |
|
1256 | raw = 'r' | |
1257 | if s.endswith('\\'): |
|
1257 | if s.endswith('\\'): | |
1258 | tail = '[:-1]' |
|
1258 | tail = '[:-1]' | |
1259 | tailpadding = '_' |
|
1259 | tailpadding = '_' | |
1260 | if '"' not in s: |
|
1260 | if '"' not in s: | |
1261 | quote = '"' |
|
1261 | quote = '"' | |
1262 | elif "'" not in s: |
|
1262 | elif "'" not in s: | |
1263 | quote = "'" |
|
1263 | quote = "'" | |
1264 | elif '"""' not in s and not s.endswith('"'): |
|
1264 | elif '"""' not in s and not s.endswith('"'): | |
1265 | quote = '"""' |
|
1265 | quote = '"""' | |
1266 | elif "'''" not in s and not s.endswith("'"): |
|
1266 | elif "'''" not in s and not s.endswith("'"): | |
1267 | quote = "'''" |
|
1267 | quote = "'''" | |
1268 | else: |
|
1268 | else: | |
1269 | # give up, backslash-escaped string will do |
|
1269 | # give up, backslash-escaped string will do | |
1270 | return '"%s"' % esc_quotes(s) |
|
1270 | return '"%s"' % esc_quotes(s) | |
1271 | res = raw + quote + s + tailpadding + quote + tail |
|
1271 | res = raw + quote + s + tailpadding + quote + tail | |
1272 | return res |
|
1272 | return res | |
1273 |
|
1273 | |||
1274 |
|
1274 | |||
1275 | #---------------------------------------------------------------------------- |
|
1275 | #---------------------------------------------------------------------------- | |
1276 | def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'): |
|
1276 | def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'): | |
1277 | """Take multiple lines of input. |
|
1277 | """Take multiple lines of input. | |
1278 |
|
1278 | |||
1279 | A list with each line of input as a separate element is returned when a |
|
1279 | A list with each line of input as a separate element is returned when a | |
1280 | termination string is entered (defaults to a single '.'). Input can also |
|
1280 | termination string is entered (defaults to a single '.'). Input can also | |
1281 | terminate via EOF (^D in Unix, ^Z-RET in Windows). |
|
1281 | terminate via EOF (^D in Unix, ^Z-RET in Windows). | |
1282 |
|
1282 | |||
1283 | Lines of input which end in \\ are joined into single entries (and a |
|
1283 | Lines of input which end in \\ are joined into single entries (and a | |
1284 | secondary continuation prompt is issued as long as the user terminates |
|
1284 | secondary continuation prompt is issued as long as the user terminates | |
1285 | lines with \\). This allows entering very long strings which are still |
|
1285 | lines with \\). This allows entering very long strings which are still | |
1286 | meant to be treated as single entities. |
|
1286 | meant to be treated as single entities. | |
1287 | """ |
|
1287 | """ | |
1288 |
|
1288 | |||
1289 | try: |
|
1289 | try: | |
1290 | if header: |
|
1290 | if header: | |
1291 | header += '\n' |
|
1291 | header += '\n' | |
1292 | lines = [raw_input(header + ps1)] |
|
1292 | lines = [raw_input(header + ps1)] | |
1293 | except EOFError: |
|
1293 | except EOFError: | |
1294 | return [] |
|
1294 | return [] | |
1295 | terminate = [terminate_str] |
|
1295 | terminate = [terminate_str] | |
1296 | try: |
|
1296 | try: | |
1297 | while lines[-1:] != terminate: |
|
1297 | while lines[-1:] != terminate: | |
1298 | new_line = raw_input(ps1) |
|
1298 | new_line = raw_input(ps1) | |
1299 | while new_line.endswith('\\'): |
|
1299 | while new_line.endswith('\\'): | |
1300 | new_line = new_line[:-1] + raw_input(ps2) |
|
1300 | new_line = new_line[:-1] + raw_input(ps2) | |
1301 | lines.append(new_line) |
|
1301 | lines.append(new_line) | |
1302 |
|
1302 | |||
1303 | return lines[:-1] # don't return the termination command |
|
1303 | return lines[:-1] # don't return the termination command | |
1304 | except EOFError: |
|
1304 | except EOFError: | |
1305 |
|
1305 | |||
1306 | return lines |
|
1306 | return lines | |
1307 |
|
1307 | |||
1308 | #---------------------------------------------------------------------------- |
|
1308 | #---------------------------------------------------------------------------- | |
1309 | def raw_input_ext(prompt='', ps2='... '): |
|
1309 | def raw_input_ext(prompt='', ps2='... '): | |
1310 | """Similar to raw_input(), but accepts extended lines if input ends with \\.""" |
|
1310 | """Similar to raw_input(), but accepts extended lines if input ends with \\.""" | |
1311 |
|
1311 | |||
1312 | line = raw_input(prompt) |
|
1312 | line = raw_input(prompt) | |
1313 | while line.endswith('\\'): |
|
1313 | while line.endswith('\\'): | |
1314 | line = line[:-1] + raw_input(ps2) |
|
1314 | line = line[:-1] + raw_input(ps2) | |
1315 | return line |
|
1315 | return line | |
1316 |
|
1316 | |||
1317 | #---------------------------------------------------------------------------- |
|
1317 | #---------------------------------------------------------------------------- | |
1318 | def ask_yes_no(prompt,default=None): |
|
1318 | def ask_yes_no(prompt,default=None): | |
1319 | """Asks a question and returns a boolean (y/n) answer. |
|
1319 | """Asks a question and returns a boolean (y/n) answer. | |
1320 |
|
1320 | |||
1321 | If default is given (one of 'y','n'), it is used if the user input is |
|
1321 | If default is given (one of 'y','n'), it is used if the user input is | |
1322 | empty. Otherwise the question is repeated until an answer is given. |
|
1322 | empty. Otherwise the question is repeated until an answer is given. | |
1323 |
|
1323 | |||
1324 | An EOF is treated as the default answer. If there is no default, an |
|
1324 | An EOF is treated as the default answer. If there is no default, an | |
1325 | exception is raised to prevent infinite loops. |
|
1325 | exception is raised to prevent infinite loops. | |
1326 |
|
1326 | |||
1327 | Valid answers are: y/yes/n/no (match is not case sensitive).""" |
|
1327 | Valid answers are: y/yes/n/no (match is not case sensitive).""" | |
1328 |
|
1328 | |||
1329 | answers = {'y':True,'n':False,'yes':True,'no':False} |
|
1329 | answers = {'y':True,'n':False,'yes':True,'no':False} | |
1330 | ans = None |
|
1330 | ans = None | |
1331 | while ans not in answers.keys(): |
|
1331 | while ans not in answers.keys(): | |
1332 | try: |
|
1332 | try: | |
1333 | ans = raw_input(prompt+' ').lower() |
|
1333 | ans = raw_input(prompt+' ').lower() | |
1334 | if not ans: # response was an empty string |
|
1334 | if not ans: # response was an empty string | |
1335 | ans = default |
|
1335 | ans = default | |
1336 | except KeyboardInterrupt: |
|
1336 | except KeyboardInterrupt: | |
1337 | pass |
|
1337 | pass | |
1338 | except EOFError: |
|
1338 | except EOFError: | |
1339 | if default in answers.keys(): |
|
1339 | if default in answers.keys(): | |
1340 | ans = default |
|
1340 | ans = default | |
1341 |
|
1341 | |||
1342 | else: |
|
1342 | else: | |
1343 | raise |
|
1343 | raise | |
1344 |
|
1344 | |||
1345 | return answers[ans] |
|
1345 | return answers[ans] | |
1346 |
|
1346 | |||
1347 | #---------------------------------------------------------------------------- |
|
1347 | #---------------------------------------------------------------------------- | |
1348 | def marquee(txt='',width=78,mark='*'): |
|
1348 | def marquee(txt='',width=78,mark='*'): | |
1349 | """Return the input string centered in a 'marquee'.""" |
|
1349 | """Return the input string centered in a 'marquee'.""" | |
1350 | if not txt: |
|
1350 | if not txt: | |
1351 | return (mark*width)[:width] |
|
1351 | return (mark*width)[:width] | |
1352 | nmark = (width-len(txt)-2)/len(mark)/2 |
|
1352 | nmark = (width-len(txt)-2)/len(mark)/2 | |
1353 | if nmark < 0: nmark =0 |
|
1353 | if nmark < 0: nmark =0 | |
1354 | marks = mark*nmark |
|
1354 | marks = mark*nmark | |
1355 | return '%s %s %s' % (marks,txt,marks) |
|
1355 | return '%s %s %s' % (marks,txt,marks) | |
1356 |
|
1356 | |||
1357 | #---------------------------------------------------------------------------- |
|
1357 | #---------------------------------------------------------------------------- | |
1358 | class EvalDict: |
|
1358 | class EvalDict: | |
1359 | """ |
|
1359 | """ | |
1360 | Emulate a dict which evaluates its contents in the caller's frame. |
|
1360 | Emulate a dict which evaluates its contents in the caller's frame. | |
1361 |
|
1361 | |||
1362 | Usage: |
|
1362 | Usage: | |
1363 | >>> number = 19 |
|
1363 | >>> number = 19 | |
1364 |
|
1364 | |||
1365 | >>> text = "python" |
|
1365 | >>> text = "python" | |
1366 |
|
1366 | |||
1367 | >>> print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict() |
|
1367 | >>> print "%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict() | |
1368 | Python 2.1 rules! |
|
1368 | Python 2.1 rules! | |
1369 | """ |
|
1369 | """ | |
1370 |
|
1370 | |||
1371 | # This version is due to sismex01@hebmex.com on c.l.py, and is basically a |
|
1371 | # This version is due to sismex01@hebmex.com on c.l.py, and is basically a | |
1372 | # modified (shorter) version of: |
|
1372 | # modified (shorter) version of: | |
1373 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by |
|
1373 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by | |
1374 | # Skip Montanaro (skip@pobox.com). |
|
1374 | # Skip Montanaro (skip@pobox.com). | |
1375 |
|
1375 | |||
1376 | def __getitem__(self, name): |
|
1376 | def __getitem__(self, name): | |
1377 | frame = sys._getframe(1) |
|
1377 | frame = sys._getframe(1) | |
1378 | return eval(name, frame.f_globals, frame.f_locals) |
|
1378 | return eval(name, frame.f_globals, frame.f_locals) | |
1379 |
|
1379 | |||
1380 | EvalString = EvalDict # for backwards compatibility |
|
1380 | EvalString = EvalDict # for backwards compatibility | |
1381 | #---------------------------------------------------------------------------- |
|
1381 | #---------------------------------------------------------------------------- | |
1382 | def qw(words,flat=0,sep=None,maxsplit=-1): |
|
1382 | def qw(words,flat=0,sep=None,maxsplit=-1): | |
1383 | """Similar to Perl's qw() operator, but with some more options. |
|
1383 | """Similar to Perl's qw() operator, but with some more options. | |
1384 |
|
1384 | |||
1385 | qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit) |
|
1385 | qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit) | |
1386 |
|
1386 | |||
1387 | words can also be a list itself, and with flat=1, the output will be |
|
1387 | words can also be a list itself, and with flat=1, the output will be | |
1388 | recursively flattened. |
|
1388 | recursively flattened. | |
1389 |
|
1389 | |||
1390 | Examples: |
|
1390 | Examples: | |
1391 |
|
1391 | |||
1392 | >>> qw('1 2') |
|
1392 | >>> qw('1 2') | |
1393 | ['1', '2'] |
|
1393 | ['1', '2'] | |
1394 |
|
1394 | |||
1395 | >>> qw(['a b','1 2',['m n','p q']]) |
|
1395 | >>> qw(['a b','1 2',['m n','p q']]) | |
1396 | [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]] |
|
1396 | [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]] | |
1397 |
|
1397 | |||
1398 | >>> qw(['a b','1 2',['m n','p q']],flat=1) |
|
1398 | >>> qw(['a b','1 2',['m n','p q']],flat=1) | |
1399 | ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] |
|
1399 | ['a', 'b', '1', '2', 'm', 'n', 'p', 'q'] | |
1400 | """ |
|
1400 | """ | |
1401 |
|
1401 | |||
1402 | if type(words) in StringTypes: |
|
1402 | if type(words) in StringTypes: | |
1403 | return [word.strip() for word in words.split(sep,maxsplit) |
|
1403 | return [word.strip() for word in words.split(sep,maxsplit) | |
1404 | if word and not word.isspace() ] |
|
1404 | if word and not word.isspace() ] | |
1405 | if flat: |
|
1405 | if flat: | |
1406 | return flatten(map(qw,words,[1]*len(words))) |
|
1406 | return flatten(map(qw,words,[1]*len(words))) | |
1407 | return map(qw,words) |
|
1407 | return map(qw,words) | |
1408 |
|
1408 | |||
1409 | #---------------------------------------------------------------------------- |
|
1409 | #---------------------------------------------------------------------------- | |
1410 | def qwflat(words,sep=None,maxsplit=-1): |
|
1410 | def qwflat(words,sep=None,maxsplit=-1): | |
1411 | """Calls qw(words) in flat mode. It's just a convenient shorthand.""" |
|
1411 | """Calls qw(words) in flat mode. It's just a convenient shorthand.""" | |
1412 | return qw(words,1,sep,maxsplit) |
|
1412 | return qw(words,1,sep,maxsplit) | |
1413 |
|
1413 | |||
1414 | #---------------------------------------------------------------------------- |
|
1414 | #---------------------------------------------------------------------------- | |
1415 | def qw_lol(indata): |
|
1415 | def qw_lol(indata): | |
1416 | """qw_lol('a b') -> [['a','b']], |
|
1416 | """qw_lol('a b') -> [['a','b']], | |
1417 | otherwise it's just a call to qw(). |
|
1417 | otherwise it's just a call to qw(). | |
1418 |
|
1418 | |||
1419 | We need this to make sure the modules_some keys *always* end up as a |
|
1419 | We need this to make sure the modules_some keys *always* end up as a | |
1420 | list of lists.""" |
|
1420 | list of lists.""" | |
1421 |
|
1421 | |||
1422 | if type(indata) in StringTypes: |
|
1422 | if type(indata) in StringTypes: | |
1423 | return [qw(indata)] |
|
1423 | return [qw(indata)] | |
1424 | else: |
|
1424 | else: | |
1425 | return qw(indata) |
|
1425 | return qw(indata) | |
1426 |
|
1426 | |||
1427 | #---------------------------------------------------------------------------- |
|
1427 | #---------------------------------------------------------------------------- | |
1428 | def grep(pat,list,case=1): |
|
1428 | def grep(pat,list,case=1): | |
1429 | """Simple minded grep-like function. |
|
1429 | """Simple minded grep-like function. | |
1430 | grep(pat,list) returns occurrences of pat in list, None on failure. |
|
1430 | grep(pat,list) returns occurrences of pat in list, None on failure. | |
1431 |
|
1431 | |||
1432 | It only does simple string matching, with no support for regexps. Use the |
|
1432 | It only does simple string matching, with no support for regexps. Use the | |
1433 | option case=0 for case-insensitive matching.""" |
|
1433 | option case=0 for case-insensitive matching.""" | |
1434 |
|
1434 | |||
1435 | # This is pretty crude. At least it should implement copying only references |
|
1435 | # This is pretty crude. At least it should implement copying only references | |
1436 | # to the original data in case it's big. Now it copies the data for output. |
|
1436 | # to the original data in case it's big. Now it copies the data for output. | |
1437 | out=[] |
|
1437 | out=[] | |
1438 | if case: |
|
1438 | if case: | |
1439 | for term in list: |
|
1439 | for term in list: | |
1440 | if term.find(pat)>-1: out.append(term) |
|
1440 | if term.find(pat)>-1: out.append(term) | |
1441 | else: |
|
1441 | else: | |
1442 | lpat=pat.lower() |
|
1442 | lpat=pat.lower() | |
1443 | for term in list: |
|
1443 | for term in list: | |
1444 | if term.lower().find(lpat)>-1: out.append(term) |
|
1444 | if term.lower().find(lpat)>-1: out.append(term) | |
1445 |
|
1445 | |||
1446 | if len(out): return out |
|
1446 | if len(out): return out | |
1447 | else: return None |
|
1447 | else: return None | |
1448 |
|
1448 | |||
1449 | #---------------------------------------------------------------------------- |
|
1449 | #---------------------------------------------------------------------------- | |
1450 | def dgrep(pat,*opts): |
|
1450 | def dgrep(pat,*opts): | |
1451 | """Return grep() on dir()+dir(__builtins__). |
|
1451 | """Return grep() on dir()+dir(__builtins__). | |
1452 |
|
1452 | |||
1453 | A very common use of grep() when working interactively.""" |
|
1453 | A very common use of grep() when working interactively.""" | |
1454 |
|
1454 | |||
1455 | return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts) |
|
1455 | return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts) | |
1456 |
|
1456 | |||
1457 | #---------------------------------------------------------------------------- |
|
1457 | #---------------------------------------------------------------------------- | |
1458 | def idgrep(pat): |
|
1458 | def idgrep(pat): | |
1459 | """Case-insensitive dgrep()""" |
|
1459 | """Case-insensitive dgrep()""" | |
1460 |
|
1460 | |||
1461 | return dgrep(pat,0) |
|
1461 | return dgrep(pat,0) | |
1462 |
|
1462 | |||
1463 | #---------------------------------------------------------------------------- |
|
1463 | #---------------------------------------------------------------------------- | |
1464 | def igrep(pat,list): |
|
1464 | def igrep(pat,list): | |
1465 | """Synonym for case-insensitive grep.""" |
|
1465 | """Synonym for case-insensitive grep.""" | |
1466 |
|
1466 | |||
1467 | return grep(pat,list,case=0) |
|
1467 | return grep(pat,list,case=0) | |
1468 |
|
1468 | |||
1469 | #---------------------------------------------------------------------------- |
|
1469 | #---------------------------------------------------------------------------- | |
1470 | def indent(str,nspaces=4,ntabs=0): |
|
1470 | def indent(str,nspaces=4,ntabs=0): | |
1471 | """Indent a string a given number of spaces or tabstops. |
|
1471 | """Indent a string a given number of spaces or tabstops. | |
1472 |
|
1472 | |||
1473 | indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces. |
|
1473 | indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces. | |
1474 | """ |
|
1474 | """ | |
1475 | if str is None: |
|
1475 | if str is None: | |
1476 | return |
|
1476 | return | |
1477 | ind = '\t'*ntabs+' '*nspaces |
|
1477 | ind = '\t'*ntabs+' '*nspaces | |
1478 | outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind)) |
|
1478 | outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind)) | |
1479 | if outstr.endswith(os.linesep+ind): |
|
1479 | if outstr.endswith(os.linesep+ind): | |
1480 | return outstr[:-len(ind)] |
|
1480 | return outstr[:-len(ind)] | |
1481 | else: |
|
1481 | else: | |
1482 | return outstr |
|
1482 | return outstr | |
1483 |
|
1483 | |||
1484 | #----------------------------------------------------------------------------- |
|
1484 | #----------------------------------------------------------------------------- | |
1485 | def native_line_ends(filename,backup=1): |
|
1485 | def native_line_ends(filename,backup=1): | |
1486 | """Convert (in-place) a file to line-ends native to the current OS. |
|
1486 | """Convert (in-place) a file to line-ends native to the current OS. | |
1487 |
|
1487 | |||
1488 | If the optional backup argument is given as false, no backup of the |
|
1488 | If the optional backup argument is given as false, no backup of the | |
1489 | original file is left. """ |
|
1489 | original file is left. """ | |
1490 |
|
1490 | |||
1491 | backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'} |
|
1491 | backup_suffixes = {'posix':'~','dos':'.bak','nt':'.bak','mac':'.bak'} | |
1492 |
|
1492 | |||
1493 | bak_filename = filename + backup_suffixes[os.name] |
|
1493 | bak_filename = filename + backup_suffixes[os.name] | |
1494 |
|
1494 | |||
1495 | original = open(filename).read() |
|
1495 | original = open(filename).read() | |
1496 | shutil.copy2(filename,bak_filename) |
|
1496 | shutil.copy2(filename,bak_filename) | |
1497 | try: |
|
1497 | try: | |
1498 | new = open(filename,'wb') |
|
1498 | new = open(filename,'wb') | |
1499 | new.write(os.linesep.join(original.splitlines())) |
|
1499 | new.write(os.linesep.join(original.splitlines())) | |
1500 | new.write(os.linesep) # ALWAYS put an eol at the end of the file |
|
1500 | new.write(os.linesep) # ALWAYS put an eol at the end of the file | |
1501 | new.close() |
|
1501 | new.close() | |
1502 | except: |
|
1502 | except: | |
1503 | os.rename(bak_filename,filename) |
|
1503 | os.rename(bak_filename,filename) | |
1504 | if not backup: |
|
1504 | if not backup: | |
1505 | try: |
|
1505 | try: | |
1506 | os.remove(bak_filename) |
|
1506 | os.remove(bak_filename) | |
1507 | except: |
|
1507 | except: | |
1508 | pass |
|
1508 | pass | |
1509 |
|
1509 | |||
1510 | #---------------------------------------------------------------------------- |
|
1510 | #---------------------------------------------------------------------------- | |
1511 | def get_pager_cmd(pager_cmd = None): |
|
1511 | def get_pager_cmd(pager_cmd = None): | |
1512 | """Return a pager command. |
|
1512 | """Return a pager command. | |
1513 |
|
1513 | |||
1514 | Makes some attempts at finding an OS-correct one.""" |
|
1514 | Makes some attempts at finding an OS-correct one.""" | |
1515 |
|
1515 | |||
1516 | if os.name == 'posix': |
|
1516 | if os.name == 'posix': | |
1517 | default_pager_cmd = 'less -r' # -r for color control sequences |
|
1517 | default_pager_cmd = 'less -r' # -r for color control sequences | |
1518 | elif os.name in ['nt','dos']: |
|
1518 | elif os.name in ['nt','dos']: | |
1519 | default_pager_cmd = 'type' |
|
1519 | default_pager_cmd = 'type' | |
1520 |
|
1520 | |||
1521 | if pager_cmd is None: |
|
1521 | if pager_cmd is None: | |
1522 | try: |
|
1522 | try: | |
1523 | pager_cmd = os.environ['PAGER'] |
|
1523 | pager_cmd = os.environ['PAGER'] | |
1524 | except: |
|
1524 | except: | |
1525 | pager_cmd = default_pager_cmd |
|
1525 | pager_cmd = default_pager_cmd | |
1526 | return pager_cmd |
|
1526 | return pager_cmd | |
1527 |
|
1527 | |||
1528 | #----------------------------------------------------------------------------- |
|
1528 | #----------------------------------------------------------------------------- | |
1529 | def get_pager_start(pager,start): |
|
1529 | def get_pager_start(pager,start): | |
1530 | """Return the string for paging files with an offset. |
|
1530 | """Return the string for paging files with an offset. | |
1531 |
|
1531 | |||
1532 | This is the '+N' argument which less and more (under Unix) accept. |
|
1532 | This is the '+N' argument which less and more (under Unix) accept. | |
1533 | """ |
|
1533 | """ | |
1534 |
|
1534 | |||
1535 | if pager in ['less','more']: |
|
1535 | if pager in ['less','more']: | |
1536 | if start: |
|
1536 | if start: | |
1537 | start_string = '+' + str(start) |
|
1537 | start_string = '+' + str(start) | |
1538 | else: |
|
1538 | else: | |
1539 | start_string = '' |
|
1539 | start_string = '' | |
1540 | else: |
|
1540 | else: | |
1541 | start_string = '' |
|
1541 | start_string = '' | |
1542 | return start_string |
|
1542 | return start_string | |
1543 |
|
1543 | |||
1544 | #---------------------------------------------------------------------------- |
|
1544 | #---------------------------------------------------------------------------- | |
1545 | # (X)emacs on W32 doesn't like to be bypassed with msvcrt.getch() |
|
1545 | # (X)emacs on W32 doesn't like to be bypassed with msvcrt.getch() | |
1546 | if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs': |
|
1546 | if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs': | |
1547 | import msvcrt |
|
1547 | import msvcrt | |
1548 | def page_more(): |
|
1548 | def page_more(): | |
1549 | """ Smart pausing between pages |
|
1549 | """ Smart pausing between pages | |
1550 |
|
1550 | |||
1551 | @return: True if need print more lines, False if quit |
|
1551 | @return: True if need print more lines, False if quit | |
1552 | """ |
|
1552 | """ | |
1553 | Term.cout.write('---Return to continue, q to quit--- ') |
|
1553 | Term.cout.write('---Return to continue, q to quit--- ') | |
1554 | ans = msvcrt.getch() |
|
1554 | ans = msvcrt.getch() | |
1555 | if ans in ("q", "Q"): |
|
1555 | if ans in ("q", "Q"): | |
1556 | result = False |
|
1556 | result = False | |
1557 | else: |
|
1557 | else: | |
1558 | result = True |
|
1558 | result = True | |
1559 | Term.cout.write("\b"*37 + " "*37 + "\b"*37) |
|
1559 | Term.cout.write("\b"*37 + " "*37 + "\b"*37) | |
1560 | return result |
|
1560 | return result | |
1561 | else: |
|
1561 | else: | |
1562 | def page_more(): |
|
1562 | def page_more(): | |
1563 | ans = raw_input('---Return to continue, q to quit--- ') |
|
1563 | ans = raw_input('---Return to continue, q to quit--- ') | |
1564 | if ans.lower().startswith('q'): |
|
1564 | if ans.lower().startswith('q'): | |
1565 | return False |
|
1565 | return False | |
1566 | else: |
|
1566 | else: | |
1567 | return True |
|
1567 | return True | |
1568 |
|
1568 | |||
1569 | esc_re = re.compile(r"(\x1b[^m]+m)") |
|
1569 | esc_re = re.compile(r"(\x1b[^m]+m)") | |
1570 |
|
1570 | |||
1571 | def page_dumb(strng,start=0,screen_lines=25): |
|
1571 | def page_dumb(strng,start=0,screen_lines=25): | |
1572 | """Very dumb 'pager' in Python, for when nothing else works. |
|
1572 | """Very dumb 'pager' in Python, for when nothing else works. | |
1573 |
|
1573 | |||
1574 | Only moves forward, same interface as page(), except for pager_cmd and |
|
1574 | Only moves forward, same interface as page(), except for pager_cmd and | |
1575 | mode.""" |
|
1575 | mode.""" | |
1576 |
|
1576 | |||
1577 | out_ln = strng.splitlines()[start:] |
|
1577 | out_ln = strng.splitlines()[start:] | |
1578 | screens = chop(out_ln,screen_lines-1) |
|
1578 | screens = chop(out_ln,screen_lines-1) | |
1579 | if len(screens) == 1: |
|
1579 | if len(screens) == 1: | |
1580 | print >>Term.cout, os.linesep.join(screens[0]) |
|
1580 | print >>Term.cout, os.linesep.join(screens[0]) | |
1581 | else: |
|
1581 | else: | |
1582 | last_escape = "" |
|
1582 | last_escape = "" | |
1583 | for scr in screens[0:-1]: |
|
1583 | for scr in screens[0:-1]: | |
1584 | hunk = os.linesep.join(scr) |
|
1584 | hunk = os.linesep.join(scr) | |
1585 | print >>Term.cout, last_escape + hunk |
|
1585 | print >>Term.cout, last_escape + hunk | |
1586 | if not page_more(): |
|
1586 | if not page_more(): | |
1587 | return |
|
1587 | return | |
1588 | esc_list = esc_re.findall(hunk) |
|
1588 | esc_list = esc_re.findall(hunk) | |
1589 | if len(esc_list) > 0: |
|
1589 | if len(esc_list) > 0: | |
1590 | last_escape = esc_list[-1] |
|
1590 | last_escape = esc_list[-1] | |
1591 | print >>Term.cout, last_escape + os.linesep.join(screens[-1]) |
|
1591 | print >>Term.cout, last_escape + os.linesep.join(screens[-1]) | |
1592 |
|
1592 | |||
1593 | #---------------------------------------------------------------------------- |
|
1593 | #---------------------------------------------------------------------------- | |
1594 | def page(strng,start=0,screen_lines=0,pager_cmd = None): |
|
1594 | def page(strng,start=0,screen_lines=0,pager_cmd = None): | |
1595 | """Print a string, piping through a pager after a certain length. |
|
1595 | """Print a string, piping through a pager after a certain length. | |
1596 |
|
1596 | |||
1597 | The screen_lines parameter specifies the number of *usable* lines of your |
|
1597 | The screen_lines parameter specifies the number of *usable* lines of your | |
1598 | terminal screen (total lines minus lines you need to reserve to show other |
|
1598 | terminal screen (total lines minus lines you need to reserve to show other | |
1599 | information). |
|
1599 | information). | |
1600 |
|
1600 | |||
1601 | If you set screen_lines to a number <=0, page() will try to auto-determine |
|
1601 | If you set screen_lines to a number <=0, page() will try to auto-determine | |
1602 | your screen size and will only use up to (screen_size+screen_lines) for |
|
1602 | your screen size and will only use up to (screen_size+screen_lines) for | |
1603 | printing, paging after that. That is, if you want auto-detection but need |
|
1603 | printing, paging after that. That is, if you want auto-detection but need | |
1604 | to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for |
|
1604 | to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for | |
1605 | auto-detection without any lines reserved simply use screen_lines = 0. |
|
1605 | auto-detection without any lines reserved simply use screen_lines = 0. | |
1606 |
|
1606 | |||
1607 | If a string won't fit in the allowed lines, it is sent through the |
|
1607 | If a string won't fit in the allowed lines, it is sent through the | |
1608 | specified pager command. If none given, look for PAGER in the environment, |
|
1608 | specified pager command. If none given, look for PAGER in the environment, | |
1609 | and ultimately default to less. |
|
1609 | and ultimately default to less. | |
1610 |
|
1610 | |||
1611 | If no system pager works, the string is sent through a 'dumb pager' |
|
1611 | If no system pager works, the string is sent through a 'dumb pager' | |
1612 | written in python, very simplistic. |
|
1612 | written in python, very simplistic. | |
1613 | """ |
|
1613 | """ | |
1614 |
|
1614 | |||
1615 | # Some routines may auto-compute start offsets incorrectly and pass a |
|
1615 | # Some routines may auto-compute start offsets incorrectly and pass a | |
1616 | # negative value. Offset to 0 for robustness. |
|
1616 | # negative value. Offset to 0 for robustness. | |
1617 | start = max(0,start) |
|
1617 | start = max(0,start) | |
1618 |
|
1618 | |||
1619 | # first, try the hook |
|
1619 | # first, try the hook | |
1620 | ip = ipapi.get() |
|
1620 | ip = ipapi.get() | |
1621 | if ip: |
|
1621 | if ip: | |
1622 | try: |
|
1622 | try: | |
1623 | ip.IP.hooks.show_in_pager(strng) |
|
1623 | ip.IP.hooks.show_in_pager(strng) | |
1624 | return |
|
1624 | return | |
1625 | except ipapi.TryNext: |
|
1625 | except ipapi.TryNext: | |
1626 | pass |
|
1626 | pass | |
1627 |
|
1627 | |||
1628 | # Ugly kludge, but calling curses.initscr() flat out crashes in emacs |
|
1628 | # Ugly kludge, but calling curses.initscr() flat out crashes in emacs | |
1629 | TERM = os.environ.get('TERM','dumb') |
|
1629 | TERM = os.environ.get('TERM','dumb') | |
1630 | if TERM in ['dumb','emacs'] and os.name != 'nt': |
|
1630 | if TERM in ['dumb','emacs'] and os.name != 'nt': | |
1631 | print strng |
|
1631 | print strng | |
1632 | return |
|
1632 | return | |
1633 | # chop off the topmost part of the string we don't want to see |
|
1633 | # chop off the topmost part of the string we don't want to see | |
1634 | str_lines = strng.split(os.linesep)[start:] |
|
1634 | str_lines = strng.split(os.linesep)[start:] | |
1635 | str_toprint = os.linesep.join(str_lines) |
|
1635 | str_toprint = os.linesep.join(str_lines) | |
1636 | num_newlines = len(str_lines) |
|
1636 | num_newlines = len(str_lines) | |
1637 | len_str = len(str_toprint) |
|
1637 | len_str = len(str_toprint) | |
1638 |
|
1638 | |||
1639 | # Dumb heuristics to guesstimate number of on-screen lines the string |
|
1639 | # Dumb heuristics to guesstimate number of on-screen lines the string | |
1640 | # takes. Very basic, but good enough for docstrings in reasonable |
|
1640 | # takes. Very basic, but good enough for docstrings in reasonable | |
1641 | # terminals. If someone later feels like refining it, it's not hard. |
|
1641 | # terminals. If someone later feels like refining it, it's not hard. | |
1642 | numlines = max(num_newlines,int(len_str/80)+1) |
|
1642 | numlines = max(num_newlines,int(len_str/80)+1) | |
1643 |
|
1643 | |||
1644 | if os.name == "nt": |
|
1644 | if os.name == "nt": | |
1645 | screen_lines_def = get_console_size(defaulty=25)[1] |
|
1645 | screen_lines_def = get_console_size(defaulty=25)[1] | |
1646 | else: |
|
1646 | else: | |
1647 | screen_lines_def = 25 # default value if we can't auto-determine |
|
1647 | screen_lines_def = 25 # default value if we can't auto-determine | |
1648 |
|
1648 | |||
1649 | # auto-determine screen size |
|
1649 | # auto-determine screen size | |
1650 | if screen_lines <= 0: |
|
1650 | if screen_lines <= 0: | |
1651 | if TERM=='xterm': |
|
1651 | if TERM=='xterm': | |
1652 | use_curses = USE_CURSES |
|
1652 | use_curses = USE_CURSES | |
1653 | else: |
|
1653 | else: | |
1654 | # curses causes problems on many terminals other than xterm. |
|
1654 | # curses causes problems on many terminals other than xterm. | |
1655 | use_curses = False |
|
1655 | use_curses = False | |
1656 | if use_curses: |
|
1656 | if use_curses: | |
1657 | # There is a bug in curses, where *sometimes* it fails to properly |
|
1657 | # There is a bug in curses, where *sometimes* it fails to properly | |
1658 | # initialize, and then after the endwin() call is made, the |
|
1658 | # initialize, and then after the endwin() call is made, the | |
1659 | # terminal is left in an unusable state. Rather than trying to |
|
1659 | # terminal is left in an unusable state. Rather than trying to | |
1660 | # check everytime for this (by requesting and comparing termios |
|
1660 | # check everytime for this (by requesting and comparing termios | |
1661 | # flags each time), we just save the initial terminal state and |
|
1661 | # flags each time), we just save the initial terminal state and | |
1662 | # unconditionally reset it every time. It's cheaper than making |
|
1662 | # unconditionally reset it every time. It's cheaper than making | |
1663 | # the checks. |
|
1663 | # the checks. | |
1664 | term_flags = termios.tcgetattr(sys.stdout) |
|
1664 | term_flags = termios.tcgetattr(sys.stdout) | |
1665 | scr = curses.initscr() |
|
1665 | scr = curses.initscr() | |
1666 | screen_lines_real,screen_cols = scr.getmaxyx() |
|
1666 | screen_lines_real,screen_cols = scr.getmaxyx() | |
1667 | curses.endwin() |
|
1667 | curses.endwin() | |
1668 | # Restore terminal state in case endwin() didn't. |
|
1668 | # Restore terminal state in case endwin() didn't. | |
1669 | termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags) |
|
1669 | termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags) | |
1670 | # Now we have what we needed: the screen size in rows/columns |
|
1670 | # Now we have what we needed: the screen size in rows/columns | |
1671 | screen_lines += screen_lines_real |
|
1671 | screen_lines += screen_lines_real | |
1672 | #print '***Screen size:',screen_lines_real,'lines x',\ |
|
1672 | #print '***Screen size:',screen_lines_real,'lines x',\ | |
1673 | #screen_cols,'columns.' # dbg |
|
1673 | #screen_cols,'columns.' # dbg | |
1674 | else: |
|
1674 | else: | |
1675 | screen_lines += screen_lines_def |
|
1675 | screen_lines += screen_lines_def | |
1676 |
|
1676 | |||
1677 | #print 'numlines',numlines,'screenlines',screen_lines # dbg |
|
1677 | #print 'numlines',numlines,'screenlines',screen_lines # dbg | |
1678 | if numlines <= screen_lines : |
|
1678 | if numlines <= screen_lines : | |
1679 | #print '*** normal print' # dbg |
|
1679 | #print '*** normal print' # dbg | |
1680 | print >>Term.cout, str_toprint |
|
1680 | print >>Term.cout, str_toprint | |
1681 | else: |
|
1681 | else: | |
1682 | # Try to open pager and default to internal one if that fails. |
|
1682 | # Try to open pager and default to internal one if that fails. | |
1683 | # All failure modes are tagged as 'retval=1', to match the return |
|
1683 | # All failure modes are tagged as 'retval=1', to match the return | |
1684 | # value of a failed system command. If any intermediate attempt |
|
1684 | # value of a failed system command. If any intermediate attempt | |
1685 | # sets retval to 1, at the end we resort to our own page_dumb() pager. |
|
1685 | # sets retval to 1, at the end we resort to our own page_dumb() pager. | |
1686 | pager_cmd = get_pager_cmd(pager_cmd) |
|
1686 | pager_cmd = get_pager_cmd(pager_cmd) | |
1687 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
1687 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) | |
1688 | if os.name == 'nt': |
|
1688 | if os.name == 'nt': | |
1689 | if pager_cmd.startswith('type'): |
|
1689 | if pager_cmd.startswith('type'): | |
1690 | # The default WinXP 'type' command is failing on complex strings. |
|
1690 | # The default WinXP 'type' command is failing on complex strings. | |
1691 | retval = 1 |
|
1691 | retval = 1 | |
1692 | else: |
|
1692 | else: | |
1693 | tmpname = tempfile.mktemp('.txt') |
|
1693 | tmpname = tempfile.mktemp('.txt') | |
1694 | tmpfile = file(tmpname,'wt') |
|
1694 | tmpfile = file(tmpname,'wt') | |
1695 | tmpfile.write(strng) |
|
1695 | tmpfile.write(strng) | |
1696 | tmpfile.close() |
|
1696 | tmpfile.close() | |
1697 | cmd = "%s < %s" % (pager_cmd,tmpname) |
|
1697 | cmd = "%s < %s" % (pager_cmd,tmpname) | |
1698 | if os.system(cmd): |
|
1698 | if os.system(cmd): | |
1699 | retval = 1 |
|
1699 | retval = 1 | |
1700 | else: |
|
1700 | else: | |
1701 | retval = None |
|
1701 | retval = None | |
1702 | os.remove(tmpname) |
|
1702 | os.remove(tmpname) | |
1703 | else: |
|
1703 | else: | |
1704 | try: |
|
1704 | try: | |
1705 | retval = None |
|
1705 | retval = None | |
1706 | # if I use popen4, things hang. No idea why. |
|
1706 | # if I use popen4, things hang. No idea why. | |
1707 | #pager,shell_out = os.popen4(pager_cmd) |
|
1707 | #pager,shell_out = os.popen4(pager_cmd) | |
1708 | pager = os.popen(pager_cmd,'w') |
|
1708 | pager = os.popen(pager_cmd,'w') | |
1709 | pager.write(strng) |
|
1709 | pager.write(strng) | |
1710 | pager.close() |
|
1710 | pager.close() | |
1711 | retval = pager.close() # success returns None |
|
1711 | retval = pager.close() # success returns None | |
1712 | except IOError,msg: # broken pipe when user quits |
|
1712 | except IOError,msg: # broken pipe when user quits | |
1713 | if msg.args == (32,'Broken pipe'): |
|
1713 | if msg.args == (32,'Broken pipe'): | |
1714 | retval = None |
|
1714 | retval = None | |
1715 | else: |
|
1715 | else: | |
1716 | retval = 1 |
|
1716 | retval = 1 | |
1717 | except OSError: |
|
1717 | except OSError: | |
1718 | # Other strange problems, sometimes seen in Win2k/cygwin |
|
1718 | # Other strange problems, sometimes seen in Win2k/cygwin | |
1719 | retval = 1 |
|
1719 | retval = 1 | |
1720 | if retval is not None: |
|
1720 | if retval is not None: | |
1721 | page_dumb(strng,screen_lines=screen_lines) |
|
1721 | page_dumb(strng,screen_lines=screen_lines) | |
1722 |
|
1722 | |||
1723 | #---------------------------------------------------------------------------- |
|
1723 | #---------------------------------------------------------------------------- | |
1724 | def page_file(fname,start = 0, pager_cmd = None): |
|
1724 | def page_file(fname,start = 0, pager_cmd = None): | |
1725 | """Page a file, using an optional pager command and starting line. |
|
1725 | """Page a file, using an optional pager command and starting line. | |
1726 | """ |
|
1726 | """ | |
1727 |
|
1727 | |||
1728 | pager_cmd = get_pager_cmd(pager_cmd) |
|
1728 | pager_cmd = get_pager_cmd(pager_cmd) | |
1729 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) |
|
1729 | pager_cmd += ' ' + get_pager_start(pager_cmd,start) | |
1730 |
|
1730 | |||
1731 | try: |
|
1731 | try: | |
1732 | if os.environ['TERM'] in ['emacs','dumb']: |
|
1732 | if os.environ['TERM'] in ['emacs','dumb']: | |
1733 | raise EnvironmentError |
|
1733 | raise EnvironmentError | |
1734 | xsys(pager_cmd + ' ' + fname) |
|
1734 | xsys(pager_cmd + ' ' + fname) | |
1735 | except: |
|
1735 | except: | |
1736 | try: |
|
1736 | try: | |
1737 | if start > 0: |
|
1737 | if start > 0: | |
1738 | start -= 1 |
|
1738 | start -= 1 | |
1739 | page(open(fname).read(),start) |
|
1739 | page(open(fname).read(),start) | |
1740 | except: |
|
1740 | except: | |
1741 | print 'Unable to show file',`fname` |
|
1741 | print 'Unable to show file',`fname` | |
1742 |
|
1742 | |||
1743 |
|
1743 | |||
1744 | #---------------------------------------------------------------------------- |
|
1744 | #---------------------------------------------------------------------------- | |
1745 | def snip_print(str,width = 75,print_full = 0,header = ''): |
|
1745 | def snip_print(str,width = 75,print_full = 0,header = ''): | |
1746 | """Print a string snipping the midsection to fit in width. |
|
1746 | """Print a string snipping the midsection to fit in width. | |
1747 |
|
1747 | |||
1748 | print_full: mode control: |
|
1748 | print_full: mode control: | |
1749 | - 0: only snip long strings |
|
1749 | - 0: only snip long strings | |
1750 | - 1: send to page() directly. |
|
1750 | - 1: send to page() directly. | |
1751 | - 2: snip long strings and ask for full length viewing with page() |
|
1751 | - 2: snip long strings and ask for full length viewing with page() | |
1752 | Return 1 if snipping was necessary, 0 otherwise.""" |
|
1752 | Return 1 if snipping was necessary, 0 otherwise.""" | |
1753 |
|
1753 | |||
1754 | if print_full == 1: |
|
1754 | if print_full == 1: | |
1755 | page(header+str) |
|
1755 | page(header+str) | |
1756 | return 0 |
|
1756 | return 0 | |
1757 |
|
1757 | |||
1758 | print header, |
|
1758 | print header, | |
1759 | if len(str) < width: |
|
1759 | if len(str) < width: | |
1760 | print str |
|
1760 | print str | |
1761 | snip = 0 |
|
1761 | snip = 0 | |
1762 | else: |
|
1762 | else: | |
1763 | whalf = int((width -5)/2) |
|
1763 | whalf = int((width -5)/2) | |
1764 | print str[:whalf] + ' <...> ' + str[-whalf:] |
|
1764 | print str[:whalf] + ' <...> ' + str[-whalf:] | |
1765 | snip = 1 |
|
1765 | snip = 1 | |
1766 | if snip and print_full == 2: |
|
1766 | if snip and print_full == 2: | |
1767 | if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y': |
|
1767 | if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y': | |
1768 | page(str) |
|
1768 | page(str) | |
1769 | return snip |
|
1769 | return snip | |
1770 |
|
1770 | |||
1771 | #**************************************************************************** |
|
1771 | #**************************************************************************** | |
1772 | # lists, dicts and structures |
|
1772 | # lists, dicts and structures | |
1773 |
|
1773 | |||
1774 | def belong(candidates,checklist): |
|
1774 | def belong(candidates,checklist): | |
1775 | """Check whether a list of items appear in a given list of options. |
|
1775 | """Check whether a list of items appear in a given list of options. | |
1776 |
|
1776 | |||
1777 | Returns a list of 1 and 0, one for each candidate given.""" |
|
1777 | Returns a list of 1 and 0, one for each candidate given.""" | |
1778 |
|
1778 | |||
1779 | return [x in checklist for x in candidates] |
|
1779 | return [x in checklist for x in candidates] | |
1780 |
|
1780 | |||
1781 | #---------------------------------------------------------------------------- |
|
1781 | #---------------------------------------------------------------------------- | |
1782 | def uniq_stable(elems): |
|
1782 | def uniq_stable(elems): | |
1783 | """uniq_stable(elems) -> list |
|
1783 | """uniq_stable(elems) -> list | |
1784 |
|
1784 | |||
1785 | Return from an iterable, a list of all the unique elements in the input, |
|
1785 | Return from an iterable, a list of all the unique elements in the input, | |
1786 | but maintaining the order in which they first appear. |
|
1786 | but maintaining the order in which they first appear. | |
1787 |
|
1787 | |||
1788 | A naive solution to this problem which just makes a dictionary with the |
|
1788 | A naive solution to this problem which just makes a dictionary with the | |
1789 | elements as keys fails to respect the stability condition, since |
|
1789 | elements as keys fails to respect the stability condition, since | |
1790 | dictionaries are unsorted by nature. |
|
1790 | dictionaries are unsorted by nature. | |
1791 |
|
1791 | |||
1792 | Note: All elements in the input must be valid dictionary keys for this |
|
1792 | Note: All elements in the input must be valid dictionary keys for this | |
1793 | routine to work, as it internally uses a dictionary for efficiency |
|
1793 | routine to work, as it internally uses a dictionary for efficiency | |
1794 | reasons.""" |
|
1794 | reasons.""" | |
1795 |
|
1795 | |||
1796 | unique = [] |
|
1796 | unique = [] | |
1797 | unique_dict = {} |
|
1797 | unique_dict = {} | |
1798 | for nn in elems: |
|
1798 | for nn in elems: | |
1799 | if nn not in unique_dict: |
|
1799 | if nn not in unique_dict: | |
1800 | unique.append(nn) |
|
1800 | unique.append(nn) | |
1801 | unique_dict[nn] = None |
|
1801 | unique_dict[nn] = None | |
1802 | return unique |
|
1802 | return unique | |
1803 |
|
1803 | |||
1804 | #---------------------------------------------------------------------------- |
|
1804 | #---------------------------------------------------------------------------- | |
1805 | class NLprinter: |
|
1805 | class NLprinter: | |
1806 | """Print an arbitrarily nested list, indicating index numbers. |
|
1806 | """Print an arbitrarily nested list, indicating index numbers. | |
1807 |
|
1807 | |||
1808 | An instance of this class called nlprint is available and callable as a |
|
1808 | An instance of this class called nlprint is available and callable as a | |
1809 | function. |
|
1809 | function. | |
1810 |
|
1810 | |||
1811 | nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent' |
|
1811 | nlprint(list,indent=' ',sep=': ') -> prints indenting each level by 'indent' | |
1812 | and using 'sep' to separate the index from the value. """ |
|
1812 | and using 'sep' to separate the index from the value. """ | |
1813 |
|
1813 | |||
1814 | def __init__(self): |
|
1814 | def __init__(self): | |
1815 | self.depth = 0 |
|
1815 | self.depth = 0 | |
1816 |
|
1816 | |||
1817 | def __call__(self,lst,pos='',**kw): |
|
1817 | def __call__(self,lst,pos='',**kw): | |
1818 | """Prints the nested list numbering levels.""" |
|
1818 | """Prints the nested list numbering levels.""" | |
1819 | kw.setdefault('indent',' ') |
|
1819 | kw.setdefault('indent',' ') | |
1820 | kw.setdefault('sep',': ') |
|
1820 | kw.setdefault('sep',': ') | |
1821 | kw.setdefault('start',0) |
|
1821 | kw.setdefault('start',0) | |
1822 | kw.setdefault('stop',len(lst)) |
|
1822 | kw.setdefault('stop',len(lst)) | |
1823 | # we need to remove start and stop from kw so they don't propagate |
|
1823 | # we need to remove start and stop from kw so they don't propagate | |
1824 | # into a recursive call for a nested list. |
|
1824 | # into a recursive call for a nested list. | |
1825 | start = kw['start']; del kw['start'] |
|
1825 | start = kw['start']; del kw['start'] | |
1826 | stop = kw['stop']; del kw['stop'] |
|
1826 | stop = kw['stop']; del kw['stop'] | |
1827 | if self.depth == 0 and 'header' in kw.keys(): |
|
1827 | if self.depth == 0 and 'header' in kw.keys(): | |
1828 | print kw['header'] |
|
1828 | print kw['header'] | |
1829 |
|
1829 | |||
1830 | for idx in range(start,stop): |
|
1830 | for idx in range(start,stop): | |
1831 | elem = lst[idx] |
|
1831 | elem = lst[idx] | |
1832 | if type(elem)==type([]): |
|
1832 | if type(elem)==type([]): | |
1833 | self.depth += 1 |
|
1833 | self.depth += 1 | |
1834 | self.__call__(elem,itpl('$pos$idx,'),**kw) |
|
1834 | self.__call__(elem,itpl('$pos$idx,'),**kw) | |
1835 | self.depth -= 1 |
|
1835 | self.depth -= 1 | |
1836 | else: |
|
1836 | else: | |
1837 | printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem') |
|
1837 | printpl(kw['indent']*self.depth+'$pos$idx$kw["sep"]$elem') | |
1838 |
|
1838 | |||
1839 | nlprint = NLprinter() |
|
1839 | nlprint = NLprinter() | |
1840 | #---------------------------------------------------------------------------- |
|
1840 | #---------------------------------------------------------------------------- | |
1841 | def all_belong(candidates,checklist): |
|
1841 | def all_belong(candidates,checklist): | |
1842 | """Check whether a list of items ALL appear in a given list of options. |
|
1842 | """Check whether a list of items ALL appear in a given list of options. | |
1843 |
|
1843 | |||
1844 | Returns a single 1 or 0 value.""" |
|
1844 | Returns a single 1 or 0 value.""" | |
1845 |
|
1845 | |||
1846 | return 1-(0 in [x in checklist for x in candidates]) |
|
1846 | return 1-(0 in [x in checklist for x in candidates]) | |
1847 |
|
1847 | |||
1848 | #---------------------------------------------------------------------------- |
|
1848 | #---------------------------------------------------------------------------- | |
1849 | def sort_compare(lst1,lst2,inplace = 1): |
|
1849 | def sort_compare(lst1,lst2,inplace = 1): | |
1850 | """Sort and compare two lists. |
|
1850 | """Sort and compare two lists. | |
1851 |
|
1851 | |||
1852 | By default it does it in place, thus modifying the lists. Use inplace = 0 |
|
1852 | By default it does it in place, thus modifying the lists. Use inplace = 0 | |
1853 | to avoid that (at the cost of temporary copy creation).""" |
|
1853 | to avoid that (at the cost of temporary copy creation).""" | |
1854 | if not inplace: |
|
1854 | if not inplace: | |
1855 | lst1 = lst1[:] |
|
1855 | lst1 = lst1[:] | |
1856 | lst2 = lst2[:] |
|
1856 | lst2 = lst2[:] | |
1857 | lst1.sort(); lst2.sort() |
|
1857 | lst1.sort(); lst2.sort() | |
1858 | return lst1 == lst2 |
|
1858 | return lst1 == lst2 | |
1859 |
|
1859 | |||
1860 | #---------------------------------------------------------------------------- |
|
1860 | #---------------------------------------------------------------------------- | |
1861 | def list2dict(lst): |
|
1861 | def list2dict(lst): | |
1862 | """Takes a list of (key,value) pairs and turns it into a dict.""" |
|
1862 | """Takes a list of (key,value) pairs and turns it into a dict.""" | |
1863 |
|
1863 | |||
1864 | dic = {} |
|
1864 | dic = {} | |
1865 | for k,v in lst: dic[k] = v |
|
1865 | for k,v in lst: dic[k] = v | |
1866 | return dic |
|
1866 | return dic | |
1867 |
|
1867 | |||
1868 | #---------------------------------------------------------------------------- |
|
1868 | #---------------------------------------------------------------------------- | |
1869 | def list2dict2(lst,default=''): |
|
1869 | def list2dict2(lst,default=''): | |
1870 | """Takes a list and turns it into a dict. |
|
1870 | """Takes a list and turns it into a dict. | |
1871 | Much slower than list2dict, but more versatile. This version can take |
|
1871 | Much slower than list2dict, but more versatile. This version can take | |
1872 | lists with sublists of arbitrary length (including sclars).""" |
|
1872 | lists with sublists of arbitrary length (including sclars).""" | |
1873 |
|
1873 | |||
1874 | dic = {} |
|
1874 | dic = {} | |
1875 | for elem in lst: |
|
1875 | for elem in lst: | |
1876 | if type(elem) in (types.ListType,types.TupleType): |
|
1876 | if type(elem) in (types.ListType,types.TupleType): | |
1877 | size = len(elem) |
|
1877 | size = len(elem) | |
1878 | if size == 0: |
|
1878 | if size == 0: | |
1879 | pass |
|
1879 | pass | |
1880 | elif size == 1: |
|
1880 | elif size == 1: | |
1881 | dic[elem] = default |
|
1881 | dic[elem] = default | |
1882 | else: |
|
1882 | else: | |
1883 | k,v = elem[0], elem[1:] |
|
1883 | k,v = elem[0], elem[1:] | |
1884 | if len(v) == 1: v = v[0] |
|
1884 | if len(v) == 1: v = v[0] | |
1885 | dic[k] = v |
|
1885 | dic[k] = v | |
1886 | else: |
|
1886 | else: | |
1887 | dic[elem] = default |
|
1887 | dic[elem] = default | |
1888 | return dic |
|
1888 | return dic | |
1889 |
|
1889 | |||
1890 | #---------------------------------------------------------------------------- |
|
1890 | #---------------------------------------------------------------------------- | |
1891 | def flatten(seq): |
|
1891 | def flatten(seq): | |
1892 | """Flatten a list of lists (NOT recursive, only works for 2d lists).""" |
|
1892 | """Flatten a list of lists (NOT recursive, only works for 2d lists).""" | |
1893 |
|
1893 | |||
1894 | return [x for subseq in seq for x in subseq] |
|
1894 | return [x for subseq in seq for x in subseq] | |
1895 |
|
1895 | |||
1896 | #---------------------------------------------------------------------------- |
|
1896 | #---------------------------------------------------------------------------- | |
1897 | def get_slice(seq,start=0,stop=None,step=1): |
|
1897 | def get_slice(seq,start=0,stop=None,step=1): | |
1898 | """Get a slice of a sequence with variable step. Specify start,stop,step.""" |
|
1898 | """Get a slice of a sequence with variable step. Specify start,stop,step.""" | |
1899 | if stop == None: |
|
1899 | if stop == None: | |
1900 | stop = len(seq) |
|
1900 | stop = len(seq) | |
1901 | item = lambda i: seq[i] |
|
1901 | item = lambda i: seq[i] | |
1902 | return map(item,xrange(start,stop,step)) |
|
1902 | return map(item,xrange(start,stop,step)) | |
1903 |
|
1903 | |||
1904 | #---------------------------------------------------------------------------- |
|
1904 | #---------------------------------------------------------------------------- | |
1905 | def chop(seq,size): |
|
1905 | def chop(seq,size): | |
1906 | """Chop a sequence into chunks of the given size.""" |
|
1906 | """Chop a sequence into chunks of the given size.""" | |
1907 | chunk = lambda i: seq[i:i+size] |
|
1907 | chunk = lambda i: seq[i:i+size] | |
1908 | return map(chunk,xrange(0,len(seq),size)) |
|
1908 | return map(chunk,xrange(0,len(seq),size)) | |
1909 |
|
1909 | |||
1910 | #---------------------------------------------------------------------------- |
|
1910 | #---------------------------------------------------------------------------- | |
1911 | # with is a keyword as of python 2.5, so this function is renamed to withobj |
|
1911 | # with is a keyword as of python 2.5, so this function is renamed to withobj | |
1912 | # from its old 'with' name. |
|
1912 | # from its old 'with' name. | |
1913 | def with_obj(object, **args): |
|
1913 | def with_obj(object, **args): | |
1914 | """Set multiple attributes for an object, similar to Pascal's with. |
|
1914 | """Set multiple attributes for an object, similar to Pascal's with. | |
1915 |
|
1915 | |||
1916 | Example: |
|
1916 | Example: | |
1917 | with_obj(jim, |
|
1917 | with_obj(jim, | |
1918 | born = 1960, |
|
1918 | born = 1960, | |
1919 | haircolour = 'Brown', |
|
1919 | haircolour = 'Brown', | |
1920 | eyecolour = 'Green') |
|
1920 | eyecolour = 'Green') | |
1921 |
|
1921 | |||
1922 | Credit: Greg Ewing, in |
|
1922 | Credit: Greg Ewing, in | |
1923 | http://mail.python.org/pipermail/python-list/2001-May/040703.html. |
|
1923 | http://mail.python.org/pipermail/python-list/2001-May/040703.html. | |
1924 |
|
1924 | |||
1925 | NOTE: up until IPython 0.7.2, this was called simply 'with', but 'with' |
|
1925 | NOTE: up until IPython 0.7.2, this was called simply 'with', but 'with' | |
1926 | has become a keyword for Python 2.5, so we had to rename it.""" |
|
1926 | has become a keyword for Python 2.5, so we had to rename it.""" | |
1927 |
|
1927 | |||
1928 | object.__dict__.update(args) |
|
1928 | object.__dict__.update(args) | |
1929 |
|
1929 | |||
1930 | #---------------------------------------------------------------------------- |
|
1930 | #---------------------------------------------------------------------------- | |
1931 | def setattr_list(obj,alist,nspace = None): |
|
1931 | def setattr_list(obj,alist,nspace = None): | |
1932 | """Set a list of attributes for an object taken from a namespace. |
|
1932 | """Set a list of attributes for an object taken from a namespace. | |
1933 |
|
1933 | |||
1934 | setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in |
|
1934 | setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in | |
1935 | alist with their values taken from nspace, which must be a dict (something |
|
1935 | alist with their values taken from nspace, which must be a dict (something | |
1936 | like locals() will often do) If nspace isn't given, locals() of the |
|
1936 | like locals() will often do) If nspace isn't given, locals() of the | |
1937 | *caller* is used, so in most cases you can omit it. |
|
1937 | *caller* is used, so in most cases you can omit it. | |
1938 |
|
1938 | |||
1939 | Note that alist can be given as a string, which will be automatically |
|
1939 | Note that alist can be given as a string, which will be automatically | |
1940 | split into a list on whitespace. If given as a list, it must be a list of |
|
1940 | split into a list on whitespace. If given as a list, it must be a list of | |
1941 | *strings* (the variable names themselves), not of variables.""" |
|
1941 | *strings* (the variable names themselves), not of variables.""" | |
1942 |
|
1942 | |||
1943 | # this grabs the local variables from the *previous* call frame -- that is |
|
1943 | # this grabs the local variables from the *previous* call frame -- that is | |
1944 | # the locals from the function that called setattr_list(). |
|
1944 | # the locals from the function that called setattr_list(). | |
1945 | # - snipped from weave.inline() |
|
1945 | # - snipped from weave.inline() | |
1946 | if nspace is None: |
|
1946 | if nspace is None: | |
1947 | call_frame = sys._getframe().f_back |
|
1947 | call_frame = sys._getframe().f_back | |
1948 | nspace = call_frame.f_locals |
|
1948 | nspace = call_frame.f_locals | |
1949 |
|
1949 | |||
1950 | if type(alist) in StringTypes: |
|
1950 | if type(alist) in StringTypes: | |
1951 | alist = alist.split() |
|
1951 | alist = alist.split() | |
1952 | for attr in alist: |
|
1952 | for attr in alist: | |
1953 | val = eval(attr,nspace) |
|
1953 | val = eval(attr,nspace) | |
1954 | setattr(obj,attr,val) |
|
1954 | setattr(obj,attr,val) | |
1955 |
|
1955 | |||
1956 | #---------------------------------------------------------------------------- |
|
1956 | #---------------------------------------------------------------------------- | |
1957 | def getattr_list(obj,alist,*args): |
|
1957 | def getattr_list(obj,alist,*args): | |
1958 | """getattr_list(obj,alist[, default]) -> attribute list. |
|
1958 | """getattr_list(obj,alist[, default]) -> attribute list. | |
1959 |
|
1959 | |||
1960 | Get a list of named attributes for an object. When a default argument is |
|
1960 | Get a list of named attributes for an object. When a default argument is | |
1961 | given, it is returned when the attribute doesn't exist; without it, an |
|
1961 | given, it is returned when the attribute doesn't exist; without it, an | |
1962 | exception is raised in that case. |
|
1962 | exception is raised in that case. | |
1963 |
|
1963 | |||
1964 | Note that alist can be given as a string, which will be automatically |
|
1964 | Note that alist can be given as a string, which will be automatically | |
1965 | split into a list on whitespace. If given as a list, it must be a list of |
|
1965 | split into a list on whitespace. If given as a list, it must be a list of | |
1966 | *strings* (the variable names themselves), not of variables.""" |
|
1966 | *strings* (the variable names themselves), not of variables.""" | |
1967 |
|
1967 | |||
1968 | if type(alist) in StringTypes: |
|
1968 | if type(alist) in StringTypes: | |
1969 | alist = alist.split() |
|
1969 | alist = alist.split() | |
1970 | if args: |
|
1970 | if args: | |
1971 | if len(args)==1: |
|
1971 | if len(args)==1: | |
1972 | default = args[0] |
|
1972 | default = args[0] | |
1973 | return map(lambda attr: getattr(obj,attr,default),alist) |
|
1973 | return map(lambda attr: getattr(obj,attr,default),alist) | |
1974 | else: |
|
1974 | else: | |
1975 | raise ValueError,'getattr_list() takes only one optional argument' |
|
1975 | raise ValueError,'getattr_list() takes only one optional argument' | |
1976 | else: |
|
1976 | else: | |
1977 | return map(lambda attr: getattr(obj,attr),alist) |
|
1977 | return map(lambda attr: getattr(obj,attr),alist) | |
1978 |
|
1978 | |||
1979 | #---------------------------------------------------------------------------- |
|
1979 | #---------------------------------------------------------------------------- | |
1980 | def map_method(method,object_list,*argseq,**kw): |
|
1980 | def map_method(method,object_list,*argseq,**kw): | |
1981 | """map_method(method,object_list,*args,**kw) -> list |
|
1981 | """map_method(method,object_list,*args,**kw) -> list | |
1982 |
|
1982 | |||
1983 | Return a list of the results of applying the methods to the items of the |
|
1983 | Return a list of the results of applying the methods to the items of the | |
1984 | argument sequence(s). If more than one sequence is given, the method is |
|
1984 | argument sequence(s). If more than one sequence is given, the method is | |
1985 | called with an argument list consisting of the corresponding item of each |
|
1985 | called with an argument list consisting of the corresponding item of each | |
1986 | sequence. All sequences must be of the same length. |
|
1986 | sequence. All sequences must be of the same length. | |
1987 |
|
1987 | |||
1988 | Keyword arguments are passed verbatim to all objects called. |
|
1988 | Keyword arguments are passed verbatim to all objects called. | |
1989 |
|
1989 | |||
1990 | This is Python code, so it's not nearly as fast as the builtin map().""" |
|
1990 | This is Python code, so it's not nearly as fast as the builtin map().""" | |
1991 |
|
1991 | |||
1992 | out_list = [] |
|
1992 | out_list = [] | |
1993 | idx = 0 |
|
1993 | idx = 0 | |
1994 | for object in object_list: |
|
1994 | for object in object_list: | |
1995 | try: |
|
1995 | try: | |
1996 | handler = getattr(object, method) |
|
1996 | handler = getattr(object, method) | |
1997 | except AttributeError: |
|
1997 | except AttributeError: | |
1998 | out_list.append(None) |
|
1998 | out_list.append(None) | |
1999 | else: |
|
1999 | else: | |
2000 | if argseq: |
|
2000 | if argseq: | |
2001 | args = map(lambda lst:lst[idx],argseq) |
|
2001 | args = map(lambda lst:lst[idx],argseq) | |
2002 | #print 'ob',object,'hand',handler,'ar',args # dbg |
|
2002 | #print 'ob',object,'hand',handler,'ar',args # dbg | |
2003 | out_list.append(handler(args,**kw)) |
|
2003 | out_list.append(handler(args,**kw)) | |
2004 | else: |
|
2004 | else: | |
2005 | out_list.append(handler(**kw)) |
|
2005 | out_list.append(handler(**kw)) | |
2006 | idx += 1 |
|
2006 | idx += 1 | |
2007 | return out_list |
|
2007 | return out_list | |
2008 |
|
2008 | |||
2009 | #---------------------------------------------------------------------------- |
|
2009 | #---------------------------------------------------------------------------- | |
2010 | def get_class_members(cls): |
|
2010 | def get_class_members(cls): | |
2011 | ret = dir(cls) |
|
2011 | ret = dir(cls) | |
2012 | if hasattr(cls,'__bases__'): |
|
2012 | if hasattr(cls,'__bases__'): | |
2013 | for base in cls.__bases__: |
|
2013 | for base in cls.__bases__: | |
2014 | ret.extend(get_class_members(base)) |
|
2014 | ret.extend(get_class_members(base)) | |
2015 | return ret |
|
2015 | return ret | |
2016 |
|
2016 | |||
2017 | #---------------------------------------------------------------------------- |
|
2017 | #---------------------------------------------------------------------------- | |
2018 | def dir2(obj): |
|
2018 | def dir2(obj): | |
2019 | """dir2(obj) -> list of strings |
|
2019 | """dir2(obj) -> list of strings | |
2020 |
|
2020 | |||
2021 | Extended version of the Python builtin dir(), which does a few extra |
|
2021 | Extended version of the Python builtin dir(), which does a few extra | |
2022 | checks, and supports common objects with unusual internals that confuse |
|
2022 | checks, and supports common objects with unusual internals that confuse | |
2023 | dir(), such as Traits and PyCrust. |
|
2023 | dir(), such as Traits and PyCrust. | |
2024 |
|
2024 | |||
2025 | This version is guaranteed to return only a list of true strings, whereas |
|
2025 | This version is guaranteed to return only a list of true strings, whereas | |
2026 | dir() returns anything that objects inject into themselves, even if they |
|
2026 | dir() returns anything that objects inject into themselves, even if they | |
2027 | are later not really valid for attribute access (many extension libraries |
|
2027 | are later not really valid for attribute access (many extension libraries | |
2028 | have such bugs). |
|
2028 | have such bugs). | |
2029 | """ |
|
2029 | """ | |
2030 |
|
2030 | |||
2031 | # Start building the attribute list via dir(), and then complete it |
|
2031 | # Start building the attribute list via dir(), and then complete it | |
2032 | # with a few extra special-purpose calls. |
|
2032 | # with a few extra special-purpose calls. | |
2033 | words = dir(obj) |
|
2033 | words = dir(obj) | |
2034 |
|
2034 | |||
2035 | if hasattr(obj,'__class__'): |
|
2035 | if hasattr(obj,'__class__'): | |
2036 | words.append('__class__') |
|
2036 | words.append('__class__') | |
2037 | words.extend(get_class_members(obj.__class__)) |
|
2037 | words.extend(get_class_members(obj.__class__)) | |
2038 | #if '__base__' in words: 1/0 |
|
2038 | #if '__base__' in words: 1/0 | |
2039 |
|
2039 | |||
2040 | # Some libraries (such as traits) may introduce duplicates, we want to |
|
2040 | # Some libraries (such as traits) may introduce duplicates, we want to | |
2041 | # track and clean this up if it happens |
|
2041 | # track and clean this up if it happens | |
2042 | may_have_dupes = False |
|
2042 | may_have_dupes = False | |
2043 |
|
2043 | |||
2044 | # this is the 'dir' function for objects with Enthought's traits |
|
2044 | # this is the 'dir' function for objects with Enthought's traits | |
2045 | if hasattr(obj, 'trait_names'): |
|
2045 | if hasattr(obj, 'trait_names'): | |
2046 | try: |
|
2046 | try: | |
2047 | words.extend(obj.trait_names()) |
|
2047 | words.extend(obj.trait_names()) | |
2048 | may_have_dupes = True |
|
2048 | may_have_dupes = True | |
2049 | except TypeError: |
|
2049 | except TypeError: | |
2050 | # This will happen if `obj` is a class and not an instance. |
|
2050 | # This will happen if `obj` is a class and not an instance. | |
2051 | pass |
|
2051 | pass | |
2052 |
|
2052 | |||
2053 | # Support for PyCrust-style _getAttributeNames magic method. |
|
2053 | # Support for PyCrust-style _getAttributeNames magic method. | |
2054 | if hasattr(obj, '_getAttributeNames'): |
|
2054 | if hasattr(obj, '_getAttributeNames'): | |
2055 | try: |
|
2055 | try: | |
2056 | words.extend(obj._getAttributeNames()) |
|
2056 | words.extend(obj._getAttributeNames()) | |
2057 | may_have_dupes = True |
|
2057 | may_have_dupes = True | |
2058 | except TypeError: |
|
2058 | except TypeError: | |
2059 | # `obj` is a class and not an instance. Ignore |
|
2059 | # `obj` is a class and not an instance. Ignore | |
2060 | # this error. |
|
2060 | # this error. | |
2061 | pass |
|
2061 | pass | |
2062 |
|
2062 | |||
2063 | if may_have_dupes: |
|
2063 | if may_have_dupes: | |
2064 | # eliminate possible duplicates, as some traits may also |
|
2064 | # eliminate possible duplicates, as some traits may also | |
2065 | # appear as normal attributes in the dir() call. |
|
2065 | # appear as normal attributes in the dir() call. | |
2066 | words = list(set(words)) |
|
2066 | words = list(set(words)) | |
2067 | words.sort() |
|
2067 | words.sort() | |
2068 |
|
2068 | |||
2069 | # filter out non-string attributes which may be stuffed by dir() calls |
|
2069 | # filter out non-string attributes which may be stuffed by dir() calls | |
2070 | # and poor coding in third-party modules |
|
2070 | # and poor coding in third-party modules | |
2071 | return [w for w in words if isinstance(w, basestring)] |
|
2071 | return [w for w in words if isinstance(w, basestring)] | |
2072 |
|
2072 | |||
2073 | #---------------------------------------------------------------------------- |
|
2073 | #---------------------------------------------------------------------------- | |
2074 | def import_fail_info(mod_name,fns=None): |
|
2074 | def import_fail_info(mod_name,fns=None): | |
2075 | """Inform load failure for a module.""" |
|
2075 | """Inform load failure for a module.""" | |
2076 |
|
2076 | |||
2077 | if fns == None: |
|
2077 | if fns == None: | |
2078 | warn("Loading of %s failed.\n" % (mod_name,)) |
|
2078 | warn("Loading of %s failed.\n" % (mod_name,)) | |
2079 | else: |
|
2079 | else: | |
2080 | warn("Loading of %s from %s failed.\n" % (fns,mod_name)) |
|
2080 | warn("Loading of %s from %s failed.\n" % (fns,mod_name)) | |
2081 |
|
2081 | |||
2082 | #---------------------------------------------------------------------------- |
|
2082 | #---------------------------------------------------------------------------- | |
2083 | # Proposed popitem() extension, written as a method |
|
2083 | # Proposed popitem() extension, written as a method | |
2084 |
|
2084 | |||
2085 |
|
2085 | |||
2086 | class NotGiven: pass |
|
2086 | class NotGiven: pass | |
2087 |
|
2087 | |||
2088 | def popkey(dct,key,default=NotGiven): |
|
2088 | def popkey(dct,key,default=NotGiven): | |
2089 | """Return dct[key] and delete dct[key]. |
|
2089 | """Return dct[key] and delete dct[key]. | |
2090 |
|
2090 | |||
2091 | If default is given, return it if dct[key] doesn't exist, otherwise raise |
|
2091 | If default is given, return it if dct[key] doesn't exist, otherwise raise | |
2092 | KeyError. """ |
|
2092 | KeyError. """ | |
2093 |
|
2093 | |||
2094 | try: |
|
2094 | try: | |
2095 | val = dct[key] |
|
2095 | val = dct[key] | |
2096 | except KeyError: |
|
2096 | except KeyError: | |
2097 | if default is NotGiven: |
|
2097 | if default is NotGiven: | |
2098 | raise |
|
2098 | raise | |
2099 | else: |
|
2099 | else: | |
2100 | return default |
|
2100 | return default | |
2101 | else: |
|
2101 | else: | |
2102 | del dct[key] |
|
2102 | del dct[key] | |
2103 | return val |
|
2103 | return val | |
2104 |
|
2104 | |||
2105 | def wrap_deprecated(func, suggest = '<nothing>'): |
|
2105 | def wrap_deprecated(func, suggest = '<nothing>'): | |
2106 | def newFunc(*args, **kwargs): |
|
2106 | def newFunc(*args, **kwargs): | |
2107 | warnings.warn("Call to deprecated function %s, use %s instead" % |
|
2107 | warnings.warn("Call to deprecated function %s, use %s instead" % | |
2108 | ( func.__name__, suggest), |
|
2108 | ( func.__name__, suggest), | |
2109 | category=DeprecationWarning, |
|
2109 | category=DeprecationWarning, | |
2110 | stacklevel = 2) |
|
2110 | stacklevel = 2) | |
2111 | return func(*args, **kwargs) |
|
2111 | return func(*args, **kwargs) | |
2112 | return newFunc |
|
2112 | return newFunc | |
2113 |
|
2113 | |||
2114 |
|
2114 | |||
2115 | def _num_cpus_unix(): |
|
2115 | def _num_cpus_unix(): | |
2116 | """Return the number of active CPUs on a Unix system.""" |
|
2116 | """Return the number of active CPUs on a Unix system.""" | |
2117 | return os.sysconf("SC_NPROCESSORS_ONLN") |
|
2117 | return os.sysconf("SC_NPROCESSORS_ONLN") | |
2118 |
|
2118 | |||
2119 |
|
2119 | |||
2120 | def _num_cpus_darwin(): |
|
2120 | def _num_cpus_darwin(): | |
2121 | """Return the number of active CPUs on a Darwin system.""" |
|
2121 | """Return the number of active CPUs on a Darwin system.""" | |
2122 | p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE) |
|
2122 | p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE) | |
2123 | return p.stdout.read() |
|
2123 | return p.stdout.read() | |
2124 |
|
2124 | |||
2125 |
|
2125 | |||
2126 | def _num_cpus_windows(): |
|
2126 | def _num_cpus_windows(): | |
2127 | """Return the number of active CPUs on a Windows system.""" |
|
2127 | """Return the number of active CPUs on a Windows system.""" | |
2128 | return os.environ.get("NUMBER_OF_PROCESSORS") |
|
2128 | return os.environ.get("NUMBER_OF_PROCESSORS") | |
2129 |
|
2129 | |||
2130 |
|
2130 | |||
2131 | def num_cpus(): |
|
2131 | def num_cpus(): | |
2132 | """Return the effective number of CPUs in the system as an integer. |
|
2132 | """Return the effective number of CPUs in the system as an integer. | |
2133 |
|
2133 | |||
2134 | This cross-platform function makes an attempt at finding the total number of |
|
2134 | This cross-platform function makes an attempt at finding the total number of | |
2135 | available CPUs in the system, as returned by various underlying system and |
|
2135 | available CPUs in the system, as returned by various underlying system and | |
2136 | python calls. |
|
2136 | python calls. | |
2137 |
|
2137 | |||
2138 | If it can't find a sensible answer, it returns 1 (though an error *may* make |
|
2138 | If it can't find a sensible answer, it returns 1 (though an error *may* make | |
2139 | it return a large positive number that's actually incorrect). |
|
2139 | it return a large positive number that's actually incorrect). | |
2140 | """ |
|
2140 | """ | |
2141 |
|
2141 | |||
2142 | # Many thanks to the Parallel Python project (http://www.parallelpython.com) |
|
2142 | # Many thanks to the Parallel Python project (http://www.parallelpython.com) | |
2143 | # for the names of the keys we needed to look up for this function. This |
|
2143 | # for the names of the keys we needed to look up for this function. This | |
2144 | # code was inspired by their equivalent function. |
|
2144 | # code was inspired by their equivalent function. | |
2145 |
|
2145 | |||
2146 | ncpufuncs = {'Linux':_num_cpus_unix, |
|
2146 | ncpufuncs = {'Linux':_num_cpus_unix, | |
2147 | 'Darwin':_num_cpus_darwin, |
|
2147 | 'Darwin':_num_cpus_darwin, | |
2148 | 'Windows':_num_cpus_windows, |
|
2148 | 'Windows':_num_cpus_windows, | |
2149 | # On Vista, python < 2.5.2 has a bug and returns 'Microsoft' |
|
2149 | # On Vista, python < 2.5.2 has a bug and returns 'Microsoft' | |
2150 | # See http://bugs.python.org/issue1082 for details. |
|
2150 | # See http://bugs.python.org/issue1082 for details. | |
2151 | 'Microsoft':_num_cpus_windows, |
|
2151 | 'Microsoft':_num_cpus_windows, | |
2152 | } |
|
2152 | } | |
2153 |
|
2153 | |||
2154 | ncpufunc = ncpufuncs.get(platform.system(), |
|
2154 | ncpufunc = ncpufuncs.get(platform.system(), | |
2155 | # default to unix version (Solaris, AIX, etc) |
|
2155 | # default to unix version (Solaris, AIX, etc) | |
2156 | _num_cpus_unix) |
|
2156 | _num_cpus_unix) | |
2157 |
|
2157 | |||
2158 | try: |
|
2158 | try: | |
2159 | ncpus = max(1,int(ncpufunc())) |
|
2159 | ncpus = max(1,int(ncpufunc())) | |
2160 | except: |
|
2160 | except: | |
2161 | ncpus = 1 |
|
2161 | ncpus = 1 | |
2162 | return ncpus |
|
2162 | return ncpus | |
2163 |
|
2163 | |||
2164 | def extract_vars(*names,**kw): |
|
2164 | def extract_vars(*names,**kw): | |
2165 | """Extract a set of variables by name from another frame. |
|
2165 | """Extract a set of variables by name from another frame. | |
2166 |
|
2166 | |||
2167 | :Parameters: |
|
2167 | :Parameters: | |
2168 | - `*names`: strings |
|
2168 | - `*names`: strings | |
2169 | One or more variable names which will be extracted from the caller's |
|
2169 | One or more variable names which will be extracted from the caller's | |
2170 | frame. |
|
2170 | frame. | |
2171 |
|
2171 | |||
2172 | :Keywords: |
|
2172 | :Keywords: | |
2173 | - `depth`: integer (0) |
|
2173 | - `depth`: integer (0) | |
2174 | How many frames in the stack to walk when looking for your variables. |
|
2174 | How many frames in the stack to walk when looking for your variables. | |
2175 |
|
2175 | |||
2176 |
|
2176 | |||
2177 | Examples: |
|
2177 | Examples: | |
2178 |
|
2178 | |||
2179 | In [2]: def func(x): |
|
2179 | In [2]: def func(x): | |
2180 | ...: y = 1 |
|
2180 | ...: y = 1 | |
2181 | ...: print extract_vars('x','y') |
|
2181 | ...: print extract_vars('x','y') | |
2182 | ...: |
|
2182 | ...: | |
2183 |
|
2183 | |||
2184 | In [3]: func('hello') |
|
2184 | In [3]: func('hello') | |
2185 | {'y': 1, 'x': 'hello'} |
|
2185 | {'y': 1, 'x': 'hello'} | |
2186 | """ |
|
2186 | """ | |
2187 |
|
2187 | |||
2188 | depth = kw.get('depth',0) |
|
2188 | depth = kw.get('depth',0) | |
2189 |
|
2189 | |||
2190 | callerNS = sys._getframe(depth+1).f_locals |
|
2190 | callerNS = sys._getframe(depth+1).f_locals | |
2191 | return dict((k,callerNS[k]) for k in names) |
|
2191 | return dict((k,callerNS[k]) for k in names) | |
2192 |
|
2192 | |||
2193 |
|
2193 | |||
2194 | def extract_vars_above(*names): |
|
2194 | def extract_vars_above(*names): | |
2195 | """Extract a set of variables by name from another frame. |
|
2195 | """Extract a set of variables by name from another frame. | |
2196 |
|
2196 | |||
2197 | Similar to extractVars(), but with a specified depth of 1, so that names |
|
2197 | Similar to extractVars(), but with a specified depth of 1, so that names | |
2198 | are exctracted exactly from above the caller. |
|
2198 | are exctracted exactly from above the caller. | |
2199 |
|
2199 | |||
2200 | This is simply a convenience function so that the very common case (for us) |
|
2200 | This is simply a convenience function so that the very common case (for us) | |
2201 | of skipping exactly 1 frame doesn't have to construct a special dict for |
|
2201 | of skipping exactly 1 frame doesn't have to construct a special dict for | |
2202 | keyword passing.""" |
|
2202 | keyword passing.""" | |
2203 |
|
2203 | |||
2204 | callerNS = sys._getframe(2).f_locals |
|
2204 | callerNS = sys._getframe(2).f_locals | |
2205 | return dict((k,callerNS[k]) for k in names) |
|
2205 | return dict((k,callerNS[k]) for k in names) | |
2206 |
|
2206 | |||
2207 | def shexp(s): |
|
2207 | def shexp(s): | |
2208 | """Expand $VARS and ~names in a string, like a shell |
|
2208 | """Expand $VARS and ~names in a string, like a shell | |
2209 |
|
2209 | |||
2210 | :Examples: |
|
2210 | :Examples: | |
2211 |
|
2211 | |||
2212 | In [2]: os.environ['FOO']='test' |
|
2212 | In [2]: os.environ['FOO']='test' | |
2213 |
|
2213 | |||
2214 | In [3]: shexp('variable FOO is $FOO') |
|
2214 | In [3]: shexp('variable FOO is $FOO') | |
2215 | Out[3]: 'variable FOO is test' |
|
2215 | Out[3]: 'variable FOO is test' | |
2216 | """ |
|
2216 | """ | |
2217 | return os.path.expandvars(os.path.expanduser(s)) |
|
2217 | return os.path.expandvars(os.path.expanduser(s)) | |
2218 |
|
2218 | |||
2219 |
|
2219 | |||
2220 | def list_strings(arg): |
|
2220 | def list_strings(arg): | |
2221 | """Always return a list of strings, given a string or list of strings |
|
2221 | """Always return a list of strings, given a string or list of strings | |
2222 | as input. |
|
2222 | as input. | |
2223 |
|
2223 | |||
2224 | :Examples: |
|
2224 | :Examples: | |
2225 |
|
2225 | |||
2226 | In [7]: list_strings('A single string') |
|
2226 | In [7]: list_strings('A single string') | |
2227 | Out[7]: ['A single string'] |
|
2227 | Out[7]: ['A single string'] | |
2228 |
|
2228 | |||
2229 | In [8]: list_strings(['A single string in a list']) |
|
2229 | In [8]: list_strings(['A single string in a list']) | |
2230 | Out[8]: ['A single string in a list'] |
|
2230 | Out[8]: ['A single string in a list'] | |
2231 |
|
2231 | |||
2232 | In [9]: list_strings(['A','list','of','strings']) |
|
2232 | In [9]: list_strings(['A','list','of','strings']) | |
2233 | Out[9]: ['A', 'list', 'of', 'strings'] |
|
2233 | Out[9]: ['A', 'list', 'of', 'strings'] | |
2234 | """ |
|
2234 | """ | |
2235 |
|
2235 | |||
2236 | if isinstance(arg,basestring): return [arg] |
|
2236 | if isinstance(arg,basestring): return [arg] | |
2237 | else: return arg |
|
2237 | else: return arg | |
2238 |
|
2238 | |||
2239 | def marquee(txt='',width=78,mark='*'): |
|
2239 | def marquee(txt='',width=78,mark='*'): | |
2240 | """Return the input string centered in a 'marquee'. |
|
2240 | """Return the input string centered in a 'marquee'. | |
2241 |
|
2241 | |||
2242 | :Examples: |
|
2242 | :Examples: | |
2243 |
|
2243 | |||
2244 | In [16]: marquee('A test',40) |
|
2244 | In [16]: marquee('A test',40) | |
2245 | Out[16]: '**************** A test ****************' |
|
2245 | Out[16]: '**************** A test ****************' | |
2246 |
|
2246 | |||
2247 | In [17]: marquee('A test',40,'-') |
|
2247 | In [17]: marquee('A test',40,'-') | |
2248 | Out[17]: '---------------- A test ----------------' |
|
2248 | Out[17]: '---------------- A test ----------------' | |
2249 |
|
2249 | |||
2250 | In [18]: marquee('A test',40,' ') |
|
2250 | In [18]: marquee('A test',40,' ') | |
2251 | Out[18]: ' A test ' |
|
2251 | Out[18]: ' A test ' | |
2252 |
|
2252 | |||
2253 | """ |
|
2253 | """ | |
2254 | if not txt: |
|
2254 | if not txt: | |
2255 | return (mark*width)[:width] |
|
2255 | return (mark*width)[:width] | |
2256 | nmark = (width-len(txt)-2)/len(mark)/2 |
|
2256 | nmark = (width-len(txt)-2)/len(mark)/2 | |
2257 | if nmark < 0: nmark =0 |
|
2257 | if nmark < 0: nmark =0 | |
2258 | marks = mark*nmark |
|
2258 | marks = mark*nmark | |
2259 | return '%s %s %s' % (marks,txt,marks) |
|
2259 | return '%s %s %s' % (marks,txt,marks) | |
2260 |
|
2260 | |||
2261 | #*************************** end of file <genutils.py> ********************** |
|
2261 | #*************************** end of file <genutils.py> ********************** |
@@ -1,44 +1,50 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 |
|
3 | |||
4 | from IPython.kernel import client |
|
4 | from IPython.kernel import client | |
5 | import time |
|
5 | import time | |
|
6 | import sys | |||
|
7 | flush = sys.stdout.flush | |||
6 |
|
8 | |||
7 | tc = client.TaskClient() |
|
9 | tc = client.TaskClient() | |
8 | mec = client.MultiEngineClient() |
|
10 | mec = client.MultiEngineClient() | |
9 |
|
11 | |||
10 | mec.execute('import time') |
|
12 | mec.execute('import time') | |
11 |
|
13 | |||
12 | for i in range(24): |
|
14 | for i in range(24): | |
13 | tc.run(client.StringTask('time.sleep(1)')) |
|
15 | tc.run(client.StringTask('time.sleep(1)')) | |
14 |
|
16 | |||
15 | for i in range(6): |
|
17 | for i in range(6): | |
16 | time.sleep(1.0) |
|
18 | time.sleep(1.0) | |
17 | print "Queue status (vebose=False)" |
|
19 | print "Queue status (vebose=False)" | |
18 | print tc.queue_status() |
|
20 | print tc.queue_status() | |
|
21 | flush() | |||
19 |
|
22 | |||
20 | for i in range(24): |
|
23 | for i in range(24): | |
21 | tc.run(client.StringTask('time.sleep(1)')) |
|
24 | tc.run(client.StringTask('time.sleep(1)')) | |
22 |
|
25 | |||
23 | for i in range(6): |
|
26 | for i in range(6): | |
24 | time.sleep(1.0) |
|
27 | time.sleep(1.0) | |
25 | print "Queue status (vebose=True)" |
|
28 | print "Queue status (vebose=True)" | |
26 | print tc.queue_status(True) |
|
29 | print tc.queue_status(True) | |
|
30 | flush() | |||
27 |
|
31 | |||
28 | for i in range(12): |
|
32 | for i in range(12): | |
29 | tc.run(client.StringTask('time.sleep(2)')) |
|
33 | tc.run(client.StringTask('time.sleep(2)')) | |
30 |
|
34 | |||
31 | print "Queue status (vebose=True)" |
|
35 | print "Queue status (vebose=True)" | |
32 | print tc.queue_status(True) |
|
36 | print tc.queue_status(True) | |
|
37 | flush() | |||
33 |
|
38 | |||
34 | qs = tc.queue_status(True) |
|
39 | qs = tc.queue_status(True) | |
35 | sched = qs['scheduled'] |
|
40 | sched = qs['scheduled'] | |
36 |
|
41 | |||
37 | for tid in sched[-4:]: |
|
42 | for tid in sched[-4:]: | |
38 | tc.abort(tid) |
|
43 | tc.abort(tid) | |
39 |
|
44 | |||
40 | for i in range(6): |
|
45 | for i in range(6): | |
41 | time.sleep(1.0) |
|
46 | time.sleep(1.0) | |
42 | print "Queue status (vebose=True)" |
|
47 | print "Queue status (vebose=True)" | |
43 | print tc.queue_status(True) |
|
48 | print tc.queue_status(True) | |
|
49 | flush() | |||
44 |
|
50 |
@@ -1,1631 +1,1632 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 |
|
14 | If invoked with no options, it executes all the files listed in sequence | |
15 | and drops you into the interpreter while still acknowledging any options |
|
15 | and drops you into the interpreter while still acknowledging any options | |
16 | you may have set in your ipythonrc file. This behavior is different from |
|
16 | you may have set in your ipythonrc file. This behavior is different from | |
17 | standard Python, which when called as python -i will only execute one |
|
17 | standard Python, which when called as python -i will only execute one | |
18 | file and ignore your configuration setup. |
|
18 | file and ignore your configuration setup. | |
19 |
|
19 | |||
20 | Please note that some of the configuration options are not available at |
|
20 | Please note that some of the configuration options are not available at | |
21 | the command line, simply because they are not practical here. Look into |
|
21 | the command line, simply because they are not practical here. Look into | |
22 | your ipythonrc configuration file for details on those. This file |
|
22 | your ipythonrc configuration file for details on those. This file | |
23 | typically installed in the $HOME/.ipython directory. For Windows users, |
|
23 | typically installed in the $HOME/.ipython directory. For Windows users, | |
24 | $HOME resolves to C:\\Documents and Settings\\YourUserName in most |
|
24 | $HOME resolves to C:\\Documents and Settings\\YourUserName in most | |
25 | instances. In the rest of this text, we will refer to this directory as |
|
25 | instances. In the rest of this text, we will refer to this directory as | |
26 | IPYTHONDIR. |
|
26 | IPYTHONDIR. | |
27 |
|
27 | |||
28 | .. _Threading options: |
|
28 | .. _Threading options: | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | Special Threading Options |
|
31 | Special Threading Options | |
32 | ------------------------- |
|
32 | ------------------------- | |
33 |
|
33 | |||
34 | The following special options are ONLY valid at the beginning of the |
|
34 | The following special options are ONLY valid at the beginning of the | |
35 | command line, and not later. This is because they control the initial- |
|
35 | command line, and not later. This is because they control the initial- | |
36 | ization of ipython itself, before the normal option-handling mechanism |
|
36 | ization of ipython itself, before the normal option-handling mechanism | |
37 | is active. |
|
37 | is active. | |
38 |
|
38 | |||
39 | -gthread, -qthread, -q4thread, -wthread, -pylab: |
|
39 | -gthread, -qthread, -q4thread, -wthread, -pylab: | |
40 | Only one of these can be given, and it can only be given as |
|
40 | Only one of these can be given, and it can only be given as | |
41 | the first option passed to IPython (it will have no effect in |
|
41 | the first option passed to IPython (it will have no effect in | |
42 | any other position). They provide threading support for the |
|
42 | any other position). They provide threading support for the | |
43 | GTK, Qt (versions 3 and 4) and WXPython toolkits, and for the |
|
43 | GTK, Qt (versions 3 and 4) and WXPython toolkits, and for the | |
44 | matplotlib library. |
|
44 | matplotlib library. | |
45 |
|
45 | |||
46 | With any of the first four options, IPython starts running a |
|
46 | With any of the first four options, IPython starts running a | |
47 | separate thread for the graphical toolkit's operation, so that |
|
47 | separate thread for the graphical toolkit's operation, so that | |
48 | you can open and control graphical elements from within an |
|
48 | you can open and control graphical elements from within an | |
49 | IPython command line, without blocking. All four provide |
|
49 | IPython command line, without blocking. All four provide | |
50 | essentially the same functionality, respectively for GTK, Qt3, |
|
50 | essentially the same functionality, respectively for GTK, Qt3, | |
51 | Qt4 and WXWidgets (via their Python interfaces). |
|
51 | Qt4 and WXWidgets (via their Python interfaces). | |
52 |
|
52 | |||
53 | Note that with -wthread, you can additionally use the |
|
53 | Note that with -wthread, you can additionally use the | |
54 | -wxversion option to request a specific version of wx to be |
|
54 | -wxversion option to request a specific version of wx to be | |
55 | used. This requires that you have the wxversion Python module |
|
55 | used. This requires that you have the wxversion Python module | |
56 | installed, which is part of recent wxPython distributions. |
|
56 | installed, which is part of recent wxPython distributions. | |
57 |
|
57 | |||
58 | If -pylab is given, IPython loads special support for the mat |
|
58 | If -pylab is given, IPython loads special support for the mat | |
59 | plotlib library (http://matplotlib.sourceforge.net), allowing |
|
59 | plotlib library (http://matplotlib.sourceforge.net), allowing | |
60 | interactive usage of any of its backends as defined in the |
|
60 | interactive usage of any of its backends as defined in the | |
61 | user's ~/.matplotlib/matplotlibrc file. It automatically |
|
61 | user's ~/.matplotlib/matplotlibrc file. It automatically | |
62 | activates GTK, Qt or WX threading for IPyhton if the choice of |
|
62 | activates GTK, Qt or WX threading for IPyhton if the choice of | |
63 | matplotlib backend requires it. It also modifies the %run |
|
63 | matplotlib backend requires it. It also modifies the %run | |
64 | command to correctly execute (without blocking) any |
|
64 | command to correctly execute (without blocking) any | |
65 | matplotlib-based script which calls show() at the end. |
|
65 | matplotlib-based script which calls show() at the end. | |
66 |
|
66 | |||
67 | -tk |
|
67 | -tk | |
68 | The -g/q/q4/wthread options, and -pylab (if matplotlib is |
|
68 | The -g/q/q4/wthread options, and -pylab (if matplotlib is | |
69 | configured to use GTK, Qt3, Qt4 or WX), will normally block Tk |
|
69 | configured to use GTK, Qt3, Qt4 or WX), will normally block Tk | |
70 | graphical interfaces. This means that when either GTK, Qt or WX |
|
70 | graphical interfaces. This means that when either GTK, Qt or WX | |
71 | threading is active, any attempt to open a Tk GUI will result in a |
|
71 | threading is active, any attempt to open a Tk GUI will result in a | |
72 | dead window, and possibly cause the Python interpreter to crash. |
|
72 | dead window, and possibly cause the Python interpreter to crash. | |
73 | An extra option, -tk, is available to address this issue. It can |
|
73 | An extra option, -tk, is available to address this issue. It can | |
74 | only be given as a second option after any of the above (-gthread, |
|
74 | only be given as a second option after any of the above (-gthread, | |
75 | -wthread or -pylab). |
|
75 | -wthread or -pylab). | |
76 |
|
76 | |||
77 | If -tk is given, IPython will try to coordinate Tk threading |
|
77 | If -tk is given, IPython will try to coordinate Tk threading | |
78 | with GTK, Qt or WX. This is however potentially unreliable, and |
|
78 | with GTK, Qt or WX. This is however potentially unreliable, and | |
79 | you will have to test on your platform and Python configuration to |
|
79 | you will have to test on your platform and Python configuration to | |
80 | determine whether it works for you. Debian users have reported |
|
80 | determine whether it works for you. Debian users have reported | |
81 | success, apparently due to the fact that Debian builds all of Tcl, |
|
81 | success, apparently due to the fact that Debian builds all of Tcl, | |
82 | Tk, Tkinter and Python with pthreads support. Under other Linux |
|
82 | Tk, Tkinter and Python with pthreads support. Under other Linux | |
83 | environments (such as Fedora Core 2/3), this option has caused |
|
83 | environments (such as Fedora Core 2/3), this option has caused | |
84 | random crashes and lockups of the Python interpreter. Under other |
|
84 | random crashes and lockups of the Python interpreter. Under other | |
85 | operating systems (Mac OSX and Windows), you'll need to try it to |
|
85 | operating systems (Mac OSX and Windows), you'll need to try it to | |
86 | find out, since currently no user reports are available. |
|
86 | find out, since currently no user reports are available. | |
87 |
|
87 | |||
88 | There is unfortunately no way for IPython to determine at run time |
|
88 | There is unfortunately no way for IPython to determine at run time | |
89 | whether -tk will work reliably or not, so you will need to do some |
|
89 | whether -tk will work reliably or not, so you will need to do some | |
90 | experiments before relying on it for regular work. |
|
90 | experiments before relying on it for regular work. | |
91 |
|
91 | |||
92 |
|
92 | |||
93 |
|
93 | |||
94 | Regular Options |
|
94 | Regular Options | |
95 | --------------- |
|
95 | --------------- | |
96 |
|
96 | |||
97 | After the above threading options have been given, regular options can |
|
97 | After the above threading options have been given, regular options can | |
98 | follow in any order. All options can be abbreviated to their shortest |
|
98 | follow in any order. All options can be abbreviated to their shortest | |
99 | non-ambiguous form and are case-sensitive. One or two dashes can be |
|
99 | non-ambiguous form and are case-sensitive. One or two dashes can be | |
100 | used. Some options have an alternate short form, indicated after a ``|``. |
|
100 | used. Some options have an alternate short form, indicated after a ``|``. | |
101 |
|
101 | |||
102 | Most options can also be set from your ipythonrc configuration file. See |
|
102 | Most options can also be set from your ipythonrc configuration file. See | |
103 | the provided example for more details on what the options do. Options |
|
103 | the provided example for more details on what the options do. Options | |
104 | given at the command line override the values set in the ipythonrc file. |
|
104 | given at the command line override the values set in the ipythonrc file. | |
105 |
|
105 | |||
106 | All options with a [no] prepended can be specified in negated form |
|
106 | All options with a [no] prepended can be specified in negated form | |
107 | (-nooption instead of -option) to turn the feature off. |
|
107 | (-nooption instead of -option) to turn the feature off. | |
108 |
|
108 | |||
109 | -help print a help message and exit. |
|
109 | -help print a help message and exit. | |
110 |
|
110 | |||
111 | -pylab |
|
111 | -pylab | |
112 | this can only be given as the first option passed to IPython |
|
112 | this can only be given as the first option passed to IPython | |
113 | (it will have no effect in any other position). It adds |
|
113 | (it will have no effect in any other position). It adds | |
114 | special support for the matplotlib library |
|
114 | special support for the matplotlib library | |
115 | (http://matplotlib.sourceforge.ne), allowing interactive usage |
|
115 | (http://matplotlib.sourceforge.ne), allowing interactive usage | |
116 | of any of its backends as defined in the user's .matplotlibrc |
|
116 | of any of its backends as defined in the user's .matplotlibrc | |
117 | file. It automatically activates GTK or WX threading for |
|
117 | file. It automatically activates GTK or WX threading for | |
118 | IPyhton if the choice of matplotlib backend requires it. It |
|
118 | IPyhton if the choice of matplotlib backend requires it. It | |
119 | also modifies the %run command to correctly execute (without |
|
119 | also modifies the %run command to correctly execute (without | |
120 | blocking) any matplotlib-based script which calls show() at |
|
120 | blocking) any matplotlib-based script which calls show() at | |
121 | the end. See `Matplotlib support`_ for more details. |
|
121 | the end. See `Matplotlib support`_ for more details. | |
122 |
|
122 | |||
123 | -autocall <val> |
|
123 | -autocall <val> | |
124 | Make IPython automatically call any callable object even if you |
|
124 | Make IPython automatically call any callable object even if you | |
125 | didn't type explicit parentheses. For example, 'str 43' becomes |
|
125 | didn't type explicit parentheses. For example, 'str 43' becomes | |
126 | 'str(43)' automatically. The value can be '0' to disable the feature, |
|
126 | 'str(43)' automatically. The value can be '0' to disable the feature, | |
127 | '1' for smart autocall, where it is not applied if there are no more |
|
127 | '1' for smart autocall, where it is not applied if there are no more | |
128 | arguments on the line, and '2' for full autocall, where all callable |
|
128 | arguments on the line, and '2' for full autocall, where all callable | |
129 | objects are automatically called (even if no arguments are |
|
129 | objects are automatically called (even if no arguments are | |
130 | present). The default is '1'. |
|
130 | present). The default is '1'. | |
131 |
|
131 | |||
132 | -[no]autoindent |
|
132 | -[no]autoindent | |
133 | Turn automatic indentation on/off. |
|
133 | Turn automatic indentation on/off. | |
134 |
|
134 | |||
135 | -[no]automagic |
|
135 | -[no]automagic | |
136 | make magic commands automatic (without needing their first character |
|
136 | make magic commands automatic (without needing their first character | |
137 | to be %). Type %magic at the IPython prompt for more information. |
|
137 | to be %). Type %magic at the IPython prompt for more information. | |
138 |
|
138 | |||
139 | -[no]autoedit_syntax |
|
139 | -[no]autoedit_syntax | |
140 | When a syntax error occurs after editing a file, automatically |
|
140 | When a syntax error occurs after editing a file, automatically | |
141 | open the file to the trouble causing line for convenient |
|
141 | open the file to the trouble causing line for convenient | |
142 | fixing. |
|
142 | fixing. | |
143 |
|
143 | |||
144 | -[no]banner Print the initial information banner (default on). |
|
144 | -[no]banner Print the initial information banner (default on). | |
145 |
|
145 | |||
146 | -c <command> |
|
146 | -c <command> | |
147 | execute the given command string. This is similar to the -c |
|
147 | execute the given command string. This is similar to the -c | |
148 | option in the normal Python interpreter. |
|
148 | option in the normal Python interpreter. | |
149 |
|
149 | |||
150 | -cache_size, cs <n> |
|
150 | -cache_size, cs <n> | |
151 | size of the output cache (maximum number of entries to hold in |
|
151 | size of the output cache (maximum number of entries to hold in | |
152 | memory). The default is 1000, you can change it permanently in your |
|
152 | memory). The default is 1000, you can change it permanently in your | |
153 | config file. Setting it to 0 completely disables the caching system, |
|
153 | config file. Setting it to 0 completely disables the caching system, | |
154 | and the minimum value accepted is 20 (if you provide a value less than |
|
154 | and the minimum value accepted is 20 (if you provide a value less than | |
155 | 20, it is reset to 0 and a warning is issued) This limit is defined |
|
155 | 20, it is reset to 0 and a warning is issued) This limit is defined | |
156 | because otherwise you'll spend more time re-flushing a too small cache |
|
156 | because otherwise you'll spend more time re-flushing a too small cache | |
157 | than working. |
|
157 | than working. | |
158 |
|
158 | |||
159 | -classic, cl |
|
159 | -classic, cl | |
160 | Gives IPython a similar feel to the classic Python |
|
160 | Gives IPython a similar feel to the classic Python | |
161 | prompt. |
|
161 | prompt. | |
162 |
|
162 | |||
163 | -colors <scheme> |
|
163 | -colors <scheme> | |
164 | Color scheme for prompts and exception reporting. Currently |
|
164 | Color scheme for prompts and exception reporting. Currently | |
165 | implemented: NoColor, Linux and LightBG. |
|
165 | implemented: NoColor, Linux and LightBG. | |
166 |
|
166 | |||
167 | -[no]color_info |
|
167 | -[no]color_info | |
168 | IPython can display information about objects via a set of functions, |
|
168 | IPython can display information about objects via a set of functions, | |
169 | and optionally can use colors for this, syntax highlighting source |
|
169 | and optionally can use colors for this, syntax highlighting source | |
170 | code and various other elements. However, because this information is |
|
170 | code and various other elements. However, because this information is | |
171 | passed through a pager (like 'less') and many pagers get confused with |
|
171 | passed through a pager (like 'less') and many pagers get confused with | |
172 | color codes, this option is off by default. You can test it and turn |
|
172 | color codes, this option is off by default. You can test it and turn | |
173 | it on permanently in your ipythonrc file if it works for you. As a |
|
173 | it on permanently in your ipythonrc file if it works for you. As a | |
174 | reference, the 'less' pager supplied with Mandrake 8.2 works ok, but |
|
174 | reference, the 'less' pager supplied with Mandrake 8.2 works ok, but | |
175 | that in RedHat 7.2 doesn't. |
|
175 | that in RedHat 7.2 doesn't. | |
176 |
|
176 | |||
177 | Test it and turn it on permanently if it works with your |
|
177 | Test it and turn it on permanently if it works with your | |
178 | system. The magic function %color_info allows you to toggle this |
|
178 | system. The magic function %color_info allows you to toggle this | |
179 | interactively for testing. |
|
179 | interactively for testing. | |
180 |
|
180 | |||
181 | -[no]debug |
|
181 | -[no]debug | |
182 | Show information about the loading process. Very useful to pin down |
|
182 | Show information about the loading process. Very useful to pin down | |
183 | problems with your configuration files or to get details about |
|
183 | problems with your configuration files or to get details about | |
184 | session restores. |
|
184 | session restores. | |
185 |
|
185 | |||
186 | -[no]deep_reload: |
|
186 | -[no]deep_reload: | |
187 | IPython can use the deep_reload module which reloads changes in |
|
187 | IPython can use the deep_reload module which reloads changes in | |
188 | modules recursively (it replaces the reload() function, so you don't |
|
188 | modules recursively (it replaces the reload() function, so you don't | |
189 | need to change anything to use it). deep_reload() forces a full |
|
189 | need to change anything to use it). deep_reload() forces a full | |
190 | reload of modules whose code may have changed, which the default |
|
190 | reload of modules whose code may have changed, which the default | |
191 | reload() function does not. |
|
191 | reload() function does not. | |
192 |
|
192 | |||
193 | When deep_reload is off, IPython will use the normal reload(), |
|
193 | When deep_reload is off, IPython will use the normal reload(), | |
194 | but deep_reload will still be available as dreload(). This |
|
194 | but deep_reload will still be available as dreload(). This | |
195 | feature is off by default [which means that you have both |
|
195 | feature is off by default [which means that you have both | |
196 | normal reload() and dreload()]. |
|
196 | normal reload() and dreload()]. | |
197 |
|
197 | |||
198 | -editor <name> |
|
198 | -editor <name> | |
199 | Which editor to use with the %edit command. By default, |
|
199 | Which editor to use with the %edit command. By default, | |
200 | IPython will honor your EDITOR environment variable (if not |
|
200 | IPython will honor your EDITOR environment variable (if not | |
201 | set, vi is the Unix default and notepad the Windows one). |
|
201 | set, vi is the Unix default and notepad the Windows one). | |
202 | Since this editor is invoked on the fly by IPython and is |
|
202 | Since this editor is invoked on the fly by IPython and is | |
203 | meant for editing small code snippets, you may want to use a |
|
203 | meant for editing small code snippets, you may want to use a | |
204 | small, lightweight editor here (in case your default EDITOR is |
|
204 | small, lightweight editor here (in case your default EDITOR is | |
205 | something like Emacs). |
|
205 | something like Emacs). | |
206 |
|
206 | |||
207 | -ipythondir <name> |
|
207 | -ipythondir <name> | |
208 | name of your IPython configuration directory IPYTHONDIR. This |
|
208 | name of your IPython configuration directory IPYTHONDIR. This | |
209 | can also be specified through the environment variable |
|
209 | can also be specified through the environment variable | |
210 | IPYTHONDIR. |
|
210 | IPYTHONDIR. | |
211 |
|
211 | |||
212 | -log, l |
|
212 | -log, l | |
213 | generate a log file of all input. The file is named |
|
213 | generate a log file of all input. The file is named | |
214 | ipython_log.py in your current directory (which prevents logs |
|
214 | ipython_log.py in your current directory (which prevents logs | |
215 | from multiple IPython sessions from trampling each other). You |
|
215 | from multiple IPython sessions from trampling each other). You | |
216 | can use this to later restore a session by loading your |
|
216 | can use this to later restore a session by loading your | |
217 | logfile as a file to be executed with option -logplay (see |
|
217 | logfile as a file to be executed with option -logplay (see | |
218 | below). |
|
218 | below). | |
219 |
|
219 | |||
220 | -logfile, lf <name> specify the name of your logfile. |
|
220 | -logfile, lf <name> specify the name of your logfile. | |
221 |
|
221 | |||
222 | -logplay, lp <name> |
|
222 | -logplay, lp <name> | |
223 |
|
223 | |||
224 | you can replay a previous log. For restoring a session as close as |
|
224 | you can replay a previous log. For restoring a session as close as | |
225 | possible to the state you left it in, use this option (don't just run |
|
225 | possible to the state you left it in, use this option (don't just run | |
226 | the logfile). With -logplay, IPython will try to reconstruct the |
|
226 | the logfile). With -logplay, IPython will try to reconstruct the | |
227 | previous working environment in full, not just execute the commands in |
|
227 | previous working environment in full, not just execute the commands in | |
228 | the logfile. |
|
228 | the logfile. | |
229 |
|
229 | |||
230 | When a session is restored, logging is automatically turned on |
|
230 | When a session is restored, logging is automatically turned on | |
231 | again with the name of the logfile it was invoked with (it is |
|
231 | again with the name of the logfile it was invoked with (it is | |
232 | read from the log header). So once you've turned logging on for |
|
232 | read from the log header). So once you've turned logging on for | |
233 | a session, you can quit IPython and reload it as many times as |
|
233 | a session, you can quit IPython and reload it as many times as | |
234 | you want and it will continue to log its history and restore |
|
234 | you want and it will continue to log its history and restore | |
235 | from the beginning every time. |
|
235 | from the beginning every time. | |
236 |
|
236 | |||
237 | Caveats: there are limitations in this option. The history |
|
237 | Caveats: there are limitations in this option. The history | |
238 | variables _i*,_* and _dh don't get restored properly. In the |
|
238 | variables _i*,_* and _dh don't get restored properly. In the | |
239 | future we will try to implement full session saving by writing |
|
239 | future we will try to implement full session saving by writing | |
240 | and retrieving a 'snapshot' of the memory state of IPython. But |
|
240 | and retrieving a 'snapshot' of the memory state of IPython. But | |
241 | our first attempts failed because of inherent limitations of |
|
241 | our first attempts failed because of inherent limitations of | |
242 | Python's Pickle module, so this may have to wait. |
|
242 | Python's Pickle module, so this may have to wait. | |
243 |
|
243 | |||
244 | -[no]messages |
|
244 | -[no]messages | |
245 | Print messages which IPython collects about its startup |
|
245 | Print messages which IPython collects about its startup | |
246 | process (default on). |
|
246 | process (default on). | |
247 |
|
247 | |||
248 | -[no]pdb |
|
248 | -[no]pdb | |
249 | Automatically call the pdb debugger after every uncaught |
|
249 | Automatically call the pdb debugger after every uncaught | |
250 | exception. If you are used to debugging using pdb, this puts |
|
250 | exception. If you are used to debugging using pdb, this puts | |
251 | you automatically inside of it after any call (either in |
|
251 | you automatically inside of it after any call (either in | |
252 | IPython or in code called by it) which triggers an exception |
|
252 | IPython or in code called by it) which triggers an exception | |
253 | which goes uncaught. |
|
253 | which goes uncaught. | |
254 |
|
254 | |||
255 | -pydb |
|
255 | -pydb | |
256 | Makes IPython use the third party "pydb" package as debugger, |
|
256 | Makes IPython use the third party "pydb" package as debugger, | |
257 | instead of pdb. Requires that pydb is installed. |
|
257 | instead of pdb. Requires that pydb is installed. | |
258 |
|
258 | |||
259 | -[no]pprint |
|
259 | -[no]pprint | |
260 | ipython can optionally use the pprint (pretty printer) module |
|
260 | ipython can optionally use the pprint (pretty printer) module | |
261 | for displaying results. pprint tends to give a nicer display |
|
261 | for displaying results. pprint tends to give a nicer display | |
262 | of nested data structures. If you like it, you can turn it on |
|
262 | of nested data structures. If you like it, you can turn it on | |
263 | permanently in your config file (default off). |
|
263 | permanently in your config file (default off). | |
264 |
|
264 | |||
265 | -profile, p <name> |
|
265 | -profile, p <name> | |
266 |
|
266 | |||
267 | assume that your config file is ipythonrc-<name> or |
|
267 | assume that your config file is ipythonrc-<name> or | |
268 | ipy_profile_<name>.py (looks in current dir first, then in |
|
268 | ipy_profile_<name>.py (looks in current dir first, then in | |
269 | IPYTHONDIR). This is a quick way to keep and load multiple |
|
269 | IPYTHONDIR). This is a quick way to keep and load multiple | |
270 | config files for different tasks, especially if you use the |
|
270 | config files for different tasks, especially if you use the | |
271 | include option of config files. You can keep a basic |
|
271 | include option of config files. You can keep a basic | |
272 | IPYTHONDIR/ipythonrc file and then have other 'profiles' which |
|
272 | IPYTHONDIR/ipythonrc file and then have other 'profiles' which | |
273 | include this one and load extra things for particular |
|
273 | include this one and load extra things for particular | |
274 | tasks. For example: |
|
274 | tasks. For example: | |
275 |
|
275 | |||
276 | 1. $HOME/.ipython/ipythonrc : load basic things you always want. |
|
276 | 1. $HOME/.ipython/ipythonrc : load basic things you always want. | |
277 | 2. $HOME/.ipython/ipythonrc-math : load (1) and basic math-related modules. |
|
277 | 2. $HOME/.ipython/ipythonrc-math : load (1) and basic math-related modules. | |
278 | 3. $HOME/.ipython/ipythonrc-numeric : load (1) and Numeric and plotting modules. |
|
278 | 3. $HOME/.ipython/ipythonrc-numeric : load (1) and Numeric and plotting modules. | |
279 |
|
279 | |||
280 | Since it is possible to create an endless loop by having |
|
280 | Since it is possible to create an endless loop by having | |
281 | circular file inclusions, IPython will stop if it reaches 15 |
|
281 | circular file inclusions, IPython will stop if it reaches 15 | |
282 | recursive inclusions. |
|
282 | recursive inclusions. | |
283 |
|
283 | |||
284 | -prompt_in1, pi1 <string> |
|
284 | -prompt_in1, pi1 <string> | |
285 |
|
285 | |||
286 | Specify the string used for input prompts. Note that if you are using |
|
286 | Specify the string used for input prompts. Note that if you are using | |
287 | numbered prompts, the number is represented with a '\#' in the |
|
287 | numbered prompts, the number is represented with a '\#' in the | |
288 | string. Don't forget to quote strings with spaces embedded in |
|
288 | string. Don't forget to quote strings with spaces embedded in | |
289 | them. Default: 'In [\#]:'. The :ref:`prompts section <prompts>` |
|
289 | them. Default: 'In [\#]:'. The :ref:`prompts section <prompts>` | |
290 | discusses in detail all the available escapes to customize your |
|
290 | discusses in detail all the available escapes to customize your | |
291 | prompts. |
|
291 | prompts. | |
292 |
|
292 | |||
293 | -prompt_in2, pi2 <string> |
|
293 | -prompt_in2, pi2 <string> | |
294 | Similar to the previous option, but used for the continuation |
|
294 | Similar to the previous option, but used for the continuation | |
295 | prompts. The special sequence '\D' is similar to '\#', but |
|
295 | prompts. The special sequence '\D' is similar to '\#', but | |
296 | with all digits replaced dots (so you can have your |
|
296 | with all digits replaced dots (so you can have your | |
297 | continuation prompt aligned with your input prompt). Default: |
|
297 | continuation prompt aligned with your input prompt). Default: | |
298 | ' .\D.:' (note three spaces at the start for alignment with |
|
298 | ' .\D.:' (note three spaces at the start for alignment with | |
299 | 'In [\#]'). |
|
299 | 'In [\#]'). | |
300 |
|
300 | |||
301 | -prompt_out,po <string> |
|
301 | -prompt_out,po <string> | |
302 | String used for output prompts, also uses numbers like |
|
302 | String used for output prompts, also uses numbers like | |
303 | prompt_in1. Default: 'Out[\#]:' |
|
303 | prompt_in1. Default: 'Out[\#]:' | |
304 |
|
304 | |||
305 | -quick start in bare bones mode (no config file loaded). |
|
305 | -quick start in bare bones mode (no config file loaded). | |
306 |
|
306 | |||
307 | -rcfile <name> |
|
307 | -rcfile <name> | |
308 | name of your IPython resource configuration file. Normally |
|
308 | name of your IPython resource configuration file. Normally | |
309 | IPython loads ipythonrc (from current directory) or |
|
309 | IPython loads ipythonrc (from current directory) or | |
310 | IPYTHONDIR/ipythonrc. |
|
310 | IPYTHONDIR/ipythonrc. | |
311 |
|
311 | |||
312 | If the loading of your config file fails, IPython starts with |
|
312 | If the loading of your config file fails, IPython starts with | |
313 | a bare bones configuration (no modules loaded at all). |
|
313 | a bare bones configuration (no modules loaded at all). | |
314 |
|
314 | |||
315 | -[no]readline |
|
315 | -[no]readline | |
316 | use the readline library, which is needed to support name |
|
316 | use the readline library, which is needed to support name | |
317 | completion and command history, among other things. It is |
|
317 | completion and command history, among other things. It is | |
318 | enabled by default, but may cause problems for users of |
|
318 | enabled by default, but may cause problems for users of | |
319 | X/Emacs in Python comint or shell buffers. |
|
319 | X/Emacs in Python comint or shell buffers. | |
320 |
|
320 | |||
321 | Note that X/Emacs 'eterm' buffers (opened with M-x term) support |
|
321 | Note that X/Emacs 'eterm' buffers (opened with M-x term) support | |
322 | IPython's readline and syntax coloring fine, only 'emacs' (M-x |
|
322 | IPython's readline and syntax coloring fine, only 'emacs' (M-x | |
323 | shell and C-c !) buffers do not. |
|
323 | shell and C-c !) buffers do not. | |
324 |
|
324 | |||
325 | -screen_length, sl <n> |
|
325 | -screen_length, sl <n> | |
326 | number of lines of your screen. This is used to control |
|
326 | number of lines of your screen. This is used to control | |
327 | printing of very long strings. Strings longer than this number |
|
327 | printing of very long strings. Strings longer than this number | |
328 | of lines will be sent through a pager instead of directly |
|
328 | of lines will be sent through a pager instead of directly | |
329 | printed. |
|
329 | printed. | |
330 |
|
330 | |||
331 | The default value for this is 0, which means IPython will |
|
331 | The default value for this is 0, which means IPython will | |
332 | auto-detect your screen size every time it needs to print certain |
|
332 | auto-detect your screen size every time it needs to print certain | |
333 | potentially long strings (this doesn't change the behavior of the |
|
333 | potentially long strings (this doesn't change the behavior of the | |
334 | 'print' keyword, it's only triggered internally). If for some |
|
334 | 'print' keyword, it's only triggered internally). If for some | |
335 | reason this isn't working well (it needs curses support), specify |
|
335 | reason this isn't working well (it needs curses support), specify | |
336 | it yourself. Otherwise don't change the default. |
|
336 | it yourself. Otherwise don't change the default. | |
337 |
|
337 | |||
338 | -separate_in, si <string> |
|
338 | -separate_in, si <string> | |
339 |
|
339 | |||
340 | separator before input prompts. |
|
340 | separator before input prompts. | |
341 | Default: '\n' |
|
341 | Default: '\n' | |
342 |
|
342 | |||
343 | -separate_out, so <string> |
|
343 | -separate_out, so <string> | |
344 | separator before output prompts. |
|
344 | separator before output prompts. | |
345 | Default: nothing. |
|
345 | Default: nothing. | |
346 |
|
346 | |||
347 | -separate_out2, so2 |
|
347 | -separate_out2, so2 | |
348 | separator after output prompts. |
|
348 | separator after output prompts. | |
349 | Default: nothing. |
|
349 | Default: nothing. | |
350 | For these three options, use the value 0 to specify no separator. |
|
350 | For these three options, use the value 0 to specify no separator. | |
351 |
|
351 | |||
352 | -nosep |
|
352 | -nosep | |
353 | shorthand for '-SeparateIn 0 -SeparateOut 0 -SeparateOut2 |
|
353 | shorthand for '-SeparateIn 0 -SeparateOut 0 -SeparateOut2 | |
354 | 0'. Simply removes all input/output separators. |
|
354 | 0'. Simply removes all input/output separators. | |
355 |
|
355 | |||
356 | -upgrade |
|
356 | -upgrade | |
357 | allows you to upgrade your IPYTHONDIR configuration when you |
|
357 | allows you to upgrade your IPYTHONDIR configuration when you | |
358 | install a new version of IPython. Since new versions may |
|
358 | install a new version of IPython. Since new versions may | |
359 | include new command line options or example files, this copies |
|
359 | include new command line options or example files, this copies | |
360 | updated ipythonrc-type files. However, it backs up (with a |
|
360 | updated ipythonrc-type files. However, it backs up (with a | |
361 | .old extension) all files which it overwrites so that you can |
|
361 | .old extension) all files which it overwrites so that you can | |
362 | merge back any customizations you might have in your personal |
|
362 | merge back any customizations you might have in your personal | |
363 | files. Note that you should probably use %upgrade instead, |
|
363 | files. Note that you should probably use %upgrade instead, | |
364 | it's a safer alternative. |
|
364 | it's a safer alternative. | |
365 |
|
365 | |||
366 |
|
366 | |||
367 | -Version print version information and exit. |
|
367 | -Version print version information and exit. | |
368 |
|
368 | |||
369 | -wxversion <string> |
|
369 | -wxversion <string> | |
370 | Select a specific version of wxPython (used in conjunction |
|
370 | Select a specific version of wxPython (used in conjunction | |
371 | with -wthread). Requires the wxversion module, part of recent |
|
371 | with -wthread). Requires the wxversion module, part of recent | |
372 | wxPython distributions |
|
372 | wxPython distributions | |
373 |
|
373 | |||
374 | -xmode <modename> |
|
374 | -xmode <modename> | |
375 |
|
375 | |||
376 | Mode for exception reporting. |
|
376 | Mode for exception reporting. | |
377 |
|
377 | |||
378 | Valid modes: Plain, Context and Verbose. |
|
378 | Valid modes: Plain, Context and Verbose. | |
379 |
|
379 | |||
380 | * Plain: similar to python's normal traceback printing. |
|
380 | * Plain: similar to python's normal traceback printing. | |
381 | * Context: prints 5 lines of context source code around each |
|
381 | * Context: prints 5 lines of context source code around each | |
382 | line in the traceback. |
|
382 | line in the traceback. | |
383 | * Verbose: similar to Context, but additionally prints the |
|
383 | * Verbose: similar to Context, but additionally prints the | |
384 | variables currently visible where the exception happened |
|
384 | variables currently visible where the exception happened | |
385 | (shortening their strings if too long). This can potentially be |
|
385 | (shortening their strings if too long). This can potentially be | |
386 | very slow, if you happen to have a huge data structure whose |
|
386 | very slow, if you happen to have a huge data structure whose | |
387 | string representation is complex to compute. Your computer may |
|
387 | string representation is complex to compute. Your computer may | |
388 | appear to freeze for a while with cpu usage at 100%. If this |
|
388 | appear to freeze for a while with cpu usage at 100%. If this | |
389 | occurs, you can cancel the traceback with Ctrl-C (maybe hitting it |
|
389 | occurs, you can cancel the traceback with Ctrl-C (maybe hitting it | |
390 | more than once). |
|
390 | more than once). | |
391 |
|
391 | |||
392 | Interactive use |
|
392 | Interactive use | |
393 | =============== |
|
393 | =============== | |
394 |
|
394 | |||
395 | Warning: IPython relies on the existence of a global variable called |
|
395 | Warning: IPython relies on the existence of a global variable called | |
396 | _ip which controls the shell itself. If you redefine _ip to anything, |
|
396 | _ip which controls the shell itself. If you redefine _ip to anything, | |
397 | bizarre behavior will quickly occur. |
|
397 | bizarre behavior will quickly occur. | |
398 |
|
398 | |||
399 | Other than the above warning, IPython is meant to work as a drop-in |
|
399 | Other than the above warning, IPython is meant to work as a drop-in | |
400 | replacement for the standard interactive interpreter. As such, any code |
|
400 | replacement for the standard interactive interpreter. As such, any code | |
401 | which is valid python should execute normally under IPython (cases where |
|
401 | which is valid python should execute normally under IPython (cases where | |
402 | this is not true should be reported as bugs). It does, however, offer |
|
402 | this is not true should be reported as bugs). It does, however, offer | |
403 | many features which are not available at a standard python prompt. What |
|
403 | many features which are not available at a standard python prompt. What | |
404 | follows is a list of these. |
|
404 | follows is a list of these. | |
405 |
|
405 | |||
406 |
|
406 | |||
407 | Caution for Windows users |
|
407 | Caution for Windows users | |
408 | ------------------------- |
|
408 | ------------------------- | |
409 |
|
409 | |||
410 | Windows, unfortunately, uses the '\' character as a path |
|
410 | Windows, unfortunately, uses the '\' character as a path | |
411 | separator. This is a terrible choice, because '\' also represents the |
|
411 | separator. This is a terrible choice, because '\' also represents the | |
412 | escape character in most modern programming languages, including |
|
412 | escape character in most modern programming languages, including | |
413 | Python. For this reason, using '/' character is recommended if you |
|
413 | Python. For this reason, using '/' character is recommended if you | |
414 | have problems with ``\``. However, in Windows commands '/' flags |
|
414 | have problems with ``\``. However, in Windows commands '/' flags | |
415 | options, so you can not use it for the root directory. This means that |
|
415 | options, so you can not use it for the root directory. This means that | |
416 | paths beginning at the root must be typed in a contrived manner like: |
|
416 | paths beginning at the root must be typed in a contrived manner like: | |
417 | ``%copy \opt/foo/bar.txt \tmp`` |
|
417 | ``%copy \opt/foo/bar.txt \tmp`` | |
418 |
|
418 | |||
419 | .. _magic: |
|
419 | .. _magic: | |
420 |
|
420 | |||
421 | Magic command system |
|
421 | Magic command system | |
422 | -------------------- |
|
422 | -------------------- | |
423 |
|
423 | |||
424 | IPython will treat any line whose first character is a % as a special |
|
424 | IPython will treat any line whose first character is a % as a special | |
425 | call to a 'magic' function. These allow you to control the behavior of |
|
425 | call to a 'magic' function. These allow you to control the behavior of | |
426 | IPython itself, plus a lot of system-type features. They are all |
|
426 | IPython itself, plus a lot of system-type features. They are all | |
427 | prefixed with a % character, but parameters are given without |
|
427 | prefixed with a % character, but parameters are given without | |
428 | parentheses or quotes. |
|
428 | parentheses or quotes. | |
429 |
|
429 | |||
430 | Example: typing '%cd mydir' (without the quotes) changes you working |
|
430 | Example: typing '%cd mydir' (without the quotes) changes you working | |
431 | directory to 'mydir', if it exists. |
|
431 | directory to 'mydir', if it exists. | |
432 |
|
432 | |||
433 | If you have 'automagic' enabled (in your ipythonrc file, via the command |
|
433 | If you have 'automagic' enabled (in your ipythonrc file, via the command | |
434 | line option -automagic or with the %automagic function), you don't need |
|
434 | line option -automagic or with the %automagic function), you don't need | |
435 | to type in the % explicitly. IPython will scan its internal list of |
|
435 | to type in the % explicitly. IPython will scan its internal list of | |
436 | magic functions and call one if it exists. With automagic on you can |
|
436 | magic functions and call one if it exists. With automagic on you can | |
437 | then just type 'cd mydir' to go to directory 'mydir'. The automagic |
|
437 | then just type 'cd mydir' to go to directory 'mydir'. The automagic | |
438 | system has the lowest possible precedence in name searches, so defining |
|
438 | system has the lowest possible precedence in name searches, so defining | |
439 | an identifier with the same name as an existing magic function will |
|
439 | an identifier with the same name as an existing magic function will | |
440 | shadow it for automagic use. You can still access the shadowed magic |
|
440 | shadow it for automagic use. You can still access the shadowed magic | |
441 | function by explicitly using the % character at the beginning of the line. |
|
441 | function by explicitly using the % character at the beginning of the line. | |
442 |
|
442 | |||
443 | An example (with automagic on) should clarify all this:: |
|
443 | An example (with automagic on) should clarify all this:: | |
444 |
|
444 | |||
445 | In [1]: cd ipython # %cd is called by automagic |
|
445 | In [1]: cd ipython # %cd is called by automagic | |
446 |
|
446 | |||
447 | /home/fperez/ipython |
|
447 | /home/fperez/ipython | |
448 |
|
448 | |||
449 | In [2]: cd=1 # now cd is just a variable |
|
449 | In [2]: cd=1 # now cd is just a variable | |
450 |
|
450 | |||
451 | In [3]: cd .. # and doesn't work as a function anymore |
|
451 | In [3]: cd .. # and doesn't work as a function anymore | |
452 |
|
452 | |||
453 | ------------------------------ |
|
453 | ------------------------------ | |
454 |
|
454 | |||
455 | File "<console>", line 1 |
|
455 | File "<console>", line 1 | |
456 |
|
456 | |||
457 | cd .. |
|
457 | cd .. | |
458 |
|
458 | |||
459 | ^ |
|
459 | ^ | |
460 |
|
460 | |||
461 | SyntaxError: invalid syntax |
|
461 | SyntaxError: invalid syntax | |
462 |
|
462 | |||
463 | In [4]: %cd .. # but %cd always works |
|
463 | In [4]: %cd .. # but %cd always works | |
464 |
|
464 | |||
465 | /home/fperez |
|
465 | /home/fperez | |
466 |
|
466 | |||
467 | In [5]: del cd # if you remove the cd variable |
|
467 | In [5]: del cd # if you remove the cd variable | |
468 |
|
468 | |||
469 | In [6]: cd ipython # automagic can work again |
|
469 | In [6]: cd ipython # automagic can work again | |
470 |
|
470 | |||
471 | /home/fperez/ipython |
|
471 | /home/fperez/ipython | |
472 |
|
472 | |||
473 | You can define your own magic functions to extend the system. The |
|
473 | You can define your own magic functions to extend the system. The | |
474 | following example defines a new magic command, %impall:: |
|
474 | following example defines a new magic command, %impall:: | |
475 |
|
475 | |||
476 | import IPython.ipapi |
|
476 | import IPython.ipapi | |
477 |
|
477 | |||
478 | ip = IPython.ipapi.get() |
|
478 | ip = IPython.ipapi.get() | |
479 |
|
479 | |||
480 | def doimp(self, arg): |
|
480 | def doimp(self, arg): | |
481 |
|
481 | |||
482 | ip = self.api |
|
482 | ip = self.api | |
483 |
|
483 | |||
484 | ip.ex("import %s; reload(%s); from %s import *" % ( |
|
484 | ip.ex("import %s; reload(%s); from %s import *" % ( | |
485 |
|
485 | |||
486 | arg,arg,arg) |
|
486 | arg,arg,arg) | |
487 |
|
487 | |||
488 | ) |
|
488 | ) | |
489 |
|
489 | |||
490 | ip.expose_magic('impall', doimp) |
|
490 | ip.expose_magic('impall', doimp) | |
491 |
|
491 | |||
492 | You can also define your own aliased names for magic functions. In your |
|
492 | You can also define your own aliased names for magic functions. In your | |
493 | ipythonrc file, placing a line like:: |
|
493 | ipythonrc file, placing a line like:: | |
494 |
|
494 | |||
495 | execute __IP.magic_cl = __IP.magic_clear |
|
495 | execute __IP.magic_cl = __IP.magic_clear | |
496 |
|
496 | |||
497 | will define %cl as a new name for %clear. |
|
497 | will define %cl as a new name for %clear. | |
498 |
|
498 | |||
499 | Type %magic for more information, including a list of all available |
|
499 | Type %magic for more information, including a list of all available | |
500 | magic functions at any time and their docstrings. You can also type |
|
500 | magic functions at any time and their docstrings. You can also type | |
501 | %magic_function_name? (see sec. 6.4 <#sec:dyn-object-info> for |
|
501 | %magic_function_name? (see sec. 6.4 <#sec:dyn-object-info> for | |
502 | information on the '?' system) to get information about any particular |
|
502 | information on the '?' system) to get information about any particular | |
503 | magic function you are interested in. |
|
503 | magic function you are interested in. | |
504 |
|
504 | |||
505 | The API documentation for the :mod:`IPython.Magic` module contains the full |
|
505 | The API documentation for the :mod:`IPython.Magic` module contains the full | |
506 | docstrings of all currently available magic commands. |
|
506 | docstrings of all currently available magic commands. | |
507 |
|
507 | |||
508 |
|
508 | |||
509 | Access to the standard Python help |
|
509 | Access to the standard Python help | |
510 | ---------------------------------- |
|
510 | ---------------------------------- | |
511 |
|
511 | |||
512 | As of Python 2.1, a help system is available with access to object docstrings |
|
512 | As of Python 2.1, a help system is available with access to object docstrings | |
513 | and the Python manuals. Simply type 'help' (no quotes) to access it. You can |
|
513 | and the Python manuals. Simply type 'help' (no quotes) to access it. You can | |
514 | also type help(object) to obtain information about a given object, and |
|
514 | also type help(object) to obtain information about a given object, and | |
515 | help('keyword') for information on a keyword. As noted :ref:`here |
|
515 | help('keyword') for information on a keyword. As noted :ref:`here | |
516 | <accessing_help>`, you need to properly configure your environment variable |
|
516 | <accessing_help>`, you need to properly configure your environment variable | |
517 | PYTHONDOCS for this feature to work correctly. |
|
517 | PYTHONDOCS for this feature to work correctly. | |
518 |
|
518 | |||
519 | .. _dynamic_object_info: |
|
519 | .. _dynamic_object_info: | |
520 |
|
520 | |||
521 | Dynamic object information |
|
521 | Dynamic object information | |
522 | -------------------------- |
|
522 | -------------------------- | |
523 |
|
523 | |||
524 | Typing ?word or word? prints detailed information about an object. If |
|
524 | Typing ?word or word? prints detailed information about an object. If | |
525 | certain strings in the object are too long (docstrings, code, etc.) they |
|
525 | certain strings in the object are too long (docstrings, code, etc.) they | |
526 | get snipped in the center for brevity. This system gives access variable |
|
526 | get snipped in the center for brevity. This system gives access variable | |
527 | types and values, full source code for any object (if available), |
|
527 | types and values, full source code for any object (if available), | |
528 | function prototypes and other useful information. |
|
528 | function prototypes and other useful information. | |
529 |
|
529 | |||
530 | Typing ??word or word?? gives access to the full information without |
|
530 | Typing ??word or word?? gives access to the full information without | |
531 | snipping long strings. Long strings are sent to the screen through the |
|
531 | snipping long strings. Long strings are sent to the screen through the | |
532 | less pager if longer than the screen and printed otherwise. On systems |
|
532 | less pager if longer than the screen and printed otherwise. On systems | |
533 | lacking the less command, IPython uses a very basic internal pager. |
|
533 | lacking the less command, IPython uses a very basic internal pager. | |
534 |
|
534 | |||
535 | The following magic functions are particularly useful for gathering |
|
535 | The following magic functions are particularly useful for gathering | |
536 | information about your working environment. You can get more details by |
|
536 | information about your working environment. You can get more details by | |
537 | typing %magic or querying them individually (use %function_name? with or |
|
537 | typing %magic or querying them individually (use %function_name? with or | |
538 | without the %), this is just a summary: |
|
538 | without the %), this is just a summary: | |
539 |
|
539 | |||
540 | * **%pdoc <object>**: Print (or run through a pager if too long) the |
|
540 | * **%pdoc <object>**: Print (or run through a pager if too long) the | |
541 | docstring for an object. If the given object is a class, it will |
|
541 | docstring for an object. If the given object is a class, it will | |
542 | print both the class and the constructor docstrings. |
|
542 | print both the class and the constructor docstrings. | |
543 | * **%pdef <object>**: Print the definition header for any callable |
|
543 | * **%pdef <object>**: Print the definition header for any callable | |
544 | object. If the object is a class, print the constructor information. |
|
544 | object. If the object is a class, print the constructor information. | |
545 | * **%psource <object>**: Print (or run through a pager if too long) |
|
545 | * **%psource <object>**: Print (or run through a pager if too long) | |
546 | the source code for an object. |
|
546 | the source code for an object. | |
547 | * **%pfile <object>**: Show the entire source file where an object was |
|
547 | * **%pfile <object>**: Show the entire source file where an object was | |
548 | defined via a pager, opening it at the line where the object |
|
548 | defined via a pager, opening it at the line where the object | |
549 | definition begins. |
|
549 | definition begins. | |
550 | * **%who/%whos**: These functions give information about identifiers |
|
550 | * **%who/%whos**: These functions give information about identifiers | |
551 | you have defined interactively (not things you loaded or defined |
|
551 | you have defined interactively (not things you loaded or defined | |
552 | in your configuration files). %who just prints a list of |
|
552 | in your configuration files). %who just prints a list of | |
553 | identifiers and %whos prints a table with some basic details about |
|
553 | identifiers and %whos prints a table with some basic details about | |
554 | each identifier. |
|
554 | each identifier. | |
555 |
|
555 | |||
556 | Note that the dynamic object information functions (?/??, %pdoc, %pfile, |
|
556 | Note that the dynamic object information functions (?/??, %pdoc, %pfile, | |
557 | %pdef, %psource) give you access to documentation even on things which |
|
557 | %pdef, %psource) give you access to documentation even on things which | |
558 | are not really defined as separate identifiers. Try for example typing |
|
558 | are not really defined as separate identifiers. Try for example typing | |
559 | {}.get? or after doing import os, type os.path.abspath??. |
|
559 | {}.get? or after doing import os, type os.path.abspath??. | |
560 |
|
560 | |||
561 |
|
561 | |||
562 | .. _readline: |
|
562 | .. _readline: | |
563 |
|
563 | |||
564 | Readline-based features |
|
564 | Readline-based features | |
565 | ----------------------- |
|
565 | ----------------------- | |
566 |
|
566 | |||
567 | These features require the GNU readline library, so they won't work if |
|
567 | These features require the GNU readline library, so they won't work if | |
568 | your Python installation lacks readline support. We will first describe |
|
568 | your Python installation lacks readline support. We will first describe | |
569 | the default behavior IPython uses, and then how to change it to suit |
|
569 | the default behavior IPython uses, and then how to change it to suit | |
570 | your preferences. |
|
570 | your preferences. | |
571 |
|
571 | |||
572 |
|
572 | |||
573 | Command line completion |
|
573 | Command line completion | |
574 | +++++++++++++++++++++++ |
|
574 | +++++++++++++++++++++++ | |
575 |
|
575 | |||
576 | At any time, hitting TAB will complete any available python commands or |
|
576 | At any time, hitting TAB will complete any available python commands or | |
577 | variable names, and show you a list of the possible completions if |
|
577 | variable names, and show you a list of the possible completions if | |
578 | there's no unambiguous one. It will also complete filenames in the |
|
578 | there's no unambiguous one. It will also complete filenames in the | |
579 | current directory if no python names match what you've typed so far. |
|
579 | current directory if no python names match what you've typed so far. | |
580 |
|
580 | |||
581 |
|
581 | |||
582 | Search command history |
|
582 | Search command history | |
583 | ++++++++++++++++++++++ |
|
583 | ++++++++++++++++++++++ | |
584 |
|
584 | |||
585 | IPython provides two ways for searching through previous input and thus |
|
585 | IPython provides two ways for searching through previous input and thus | |
586 | reduce the need for repetitive typing: |
|
586 | reduce the need for repetitive typing: | |
587 |
|
587 | |||
588 | 1. Start typing, and then use Ctrl-p (previous,up) and Ctrl-n |
|
588 | 1. Start typing, and then use Ctrl-p (previous,up) and Ctrl-n | |
589 | (next,down) to search through only the history items that match |
|
589 | (next,down) to search through only the history items that match | |
590 | what you've typed so far. If you use Ctrl-p/Ctrl-n at a blank |
|
590 | what you've typed so far. If you use Ctrl-p/Ctrl-n at a blank | |
591 | prompt, they just behave like normal arrow keys. |
|
591 | prompt, they just behave like normal arrow keys. | |
592 | 2. Hit Ctrl-r: opens a search prompt. Begin typing and the system |
|
592 | 2. Hit Ctrl-r: opens a search prompt. Begin typing and the system | |
593 | searches your history for lines that contain what you've typed so |
|
593 | searches your history for lines that contain what you've typed so | |
594 | far, completing as much as it can. |
|
594 | far, completing as much as it can. | |
595 |
|
595 | |||
596 |
|
596 | |||
597 | Persistent command history across sessions |
|
597 | Persistent command history across sessions | |
598 | ++++++++++++++++++++++++++++++++++++++++++ |
|
598 | ++++++++++++++++++++++++++++++++++++++++++ | |
599 |
|
599 | |||
600 | IPython will save your input history when it leaves and reload it next |
|
600 | IPython will save your input history when it leaves and reload it next | |
601 | time you restart it. By default, the history file is named |
|
601 | time you restart it. By default, the history file is named | |
602 | $IPYTHONDIR/history, but if you've loaded a named profile, |
|
602 | $IPYTHONDIR/history, but if you've loaded a named profile, | |
603 | '-PROFILE_NAME' is appended to the name. This allows you to keep |
|
603 | '-PROFILE_NAME' is appended to the name. This allows you to keep | |
604 | separate histories related to various tasks: commands related to |
|
604 | separate histories related to various tasks: commands related to | |
605 | numerical work will not be clobbered by a system shell history, for |
|
605 | numerical work will not be clobbered by a system shell history, for | |
606 | example. |
|
606 | example. | |
607 |
|
607 | |||
608 |
|
608 | |||
609 | Autoindent |
|
609 | Autoindent | |
610 | ++++++++++ |
|
610 | ++++++++++ | |
611 |
|
611 | |||
612 | IPython can recognize lines ending in ':' and indent the next line, |
|
612 | IPython can recognize lines ending in ':' and indent the next line, | |
613 | while also un-indenting automatically after 'raise' or 'return'. |
|
613 | while also un-indenting automatically after 'raise' or 'return'. | |
614 |
|
614 | |||
615 | This feature uses the readline library, so it will honor your ~/.inputrc |
|
615 | This feature uses the readline library, so it will honor your ~/.inputrc | |
616 | configuration (or whatever file your INPUTRC variable points to). Adding |
|
616 | configuration (or whatever file your INPUTRC variable points to). Adding | |
617 | the following lines to your .inputrc file can make indenting/unindenting |
|
617 | the following lines to your .inputrc file can make indenting/unindenting | |
618 | more convenient (M-i indents, M-u unindents):: |
|
618 | more convenient (M-i indents, M-u unindents):: | |
619 |
|
619 | |||
620 | $if Python |
|
620 | $if Python | |
621 | "\M-i": " " |
|
621 | "\M-i": " " | |
622 | "\M-u": "\d\d\d\d" |
|
622 | "\M-u": "\d\d\d\d" | |
623 | $endif |
|
623 | $endif | |
624 |
|
624 | |||
625 | Note that there are 4 spaces between the quote marks after "M-i" above. |
|
625 | Note that there are 4 spaces between the quote marks after "M-i" above. | |
626 |
|
626 | |||
627 | Warning: this feature is ON by default, but it can cause problems with |
|
627 | Warning: this feature is ON by default, but it can cause problems with | |
628 | the pasting of multi-line indented code (the pasted code gets |
|
628 | the pasting of multi-line indented code (the pasted code gets | |
629 | re-indented on each line). A magic function %autoindent allows you to |
|
629 | re-indented on each line). A magic function %autoindent allows you to | |
630 | toggle it on/off at runtime. You can also disable it permanently on in |
|
630 | toggle it on/off at runtime. You can also disable it permanently on in | |
631 | your ipythonrc file (set autoindent 0). |
|
631 | your ipythonrc file (set autoindent 0). | |
632 |
|
632 | |||
633 |
|
633 | |||
634 | Customizing readline behavior |
|
634 | Customizing readline behavior | |
635 | +++++++++++++++++++++++++++++ |
|
635 | +++++++++++++++++++++++++++++ | |
636 |
|
636 | |||
637 | All these features are based on the GNU readline library, which has an |
|
637 | All these features are based on the GNU readline library, which has an | |
638 | extremely customizable interface. Normally, readline is configured via a |
|
638 | extremely customizable interface. Normally, readline is configured via a | |
639 | file which defines the behavior of the library; the details of the |
|
639 | file which defines the behavior of the library; the details of the | |
640 | syntax for this can be found in the readline documentation available |
|
640 | syntax for this can be found in the readline documentation available | |
641 | with your system or on the Internet. IPython doesn't read this file (if |
|
641 | with your system or on the Internet. IPython doesn't read this file (if | |
642 | it exists) directly, but it does support passing to readline valid |
|
642 | it exists) directly, but it does support passing to readline valid | |
643 | options via a simple interface. In brief, you can customize readline by |
|
643 | options via a simple interface. In brief, you can customize readline by | |
644 | setting the following options in your ipythonrc configuration file (note |
|
644 | setting the following options in your ipythonrc configuration file (note | |
645 | that these options can not be specified at the command line): |
|
645 | that these options can not be specified at the command line): | |
646 |
|
646 | |||
647 | * **readline_parse_and_bind**: this option can appear as many times as |
|
647 | * **readline_parse_and_bind**: this option can appear as many times as | |
648 | you want, each time defining a string to be executed via a |
|
648 | you want, each time defining a string to be executed via a | |
649 | readline.parse_and_bind() command. The syntax for valid commands |
|
649 | readline.parse_and_bind() command. The syntax for valid commands | |
650 | of this kind can be found by reading the documentation for the GNU |
|
650 | of this kind can be found by reading the documentation for the GNU | |
651 | readline library, as these commands are of the kind which readline |
|
651 | readline library, as these commands are of the kind which readline | |
652 | accepts in its configuration file. |
|
652 | accepts in its configuration file. | |
653 | * **readline_remove_delims**: a string of characters to be removed |
|
653 | * **readline_remove_delims**: a string of characters to be removed | |
654 | from the default word-delimiters list used by readline, so that |
|
654 | from the default word-delimiters list used by readline, so that | |
655 | completions may be performed on strings which contain them. Do not |
|
655 | completions may be performed on strings which contain them. Do not | |
656 | change the default value unless you know what you're doing. |
|
656 | change the default value unless you know what you're doing. | |
657 | * **readline_omit__names**: when tab-completion is enabled, hitting |
|
657 | * **readline_omit__names**: when tab-completion is enabled, hitting | |
658 | <tab> after a '.' in a name will complete all attributes of an |
|
658 | <tab> after a '.' in a name will complete all attributes of an | |
659 | object, including all the special methods whose names include |
|
659 | object, including all the special methods whose names include | |
660 | double underscores (like __getitem__ or __class__). If you'd |
|
660 | double underscores (like __getitem__ or __class__). If you'd | |
661 | rather not see these names by default, you can set this option to |
|
661 | rather not see these names by default, you can set this option to | |
662 | 1. Note that even when this option is set, you can still see those |
|
662 | 1. Note that even when this option is set, you can still see those | |
663 | names by explicitly typing a _ after the period and hitting <tab>: |
|
663 | names by explicitly typing a _ after the period and hitting <tab>: | |
664 | 'name._<tab>' will always complete attribute names starting with '_'. |
|
664 | 'name._<tab>' will always complete attribute names starting with '_'. | |
665 |
|
665 | |||
666 | This option is off by default so that new users see all |
|
666 | This option is off by default so that new users see all | |
667 | attributes of any objects they are dealing with. |
|
667 | attributes of any objects they are dealing with. | |
668 |
|
668 | |||
669 | You will find the default values along with a corresponding detailed |
|
669 | You will find the default values along with a corresponding detailed | |
670 | explanation in your ipythonrc file. |
|
670 | explanation in your ipythonrc file. | |
671 |
|
671 | |||
672 |
|
672 | |||
673 | Session logging and restoring |
|
673 | Session logging and restoring | |
674 | ----------------------------- |
|
674 | ----------------------------- | |
675 |
|
675 | |||
676 | You can log all input from a session either by starting IPython with the |
|
676 | You can log all input from a session either by starting IPython with the | |
677 | command line switches -log or -logfile (see :ref:`here <command_line_options>`) |
|
677 | command line switches -log or -logfile (see :ref:`here <command_line_options>`) | |
678 | or by activating the logging at any moment with the magic function %logstart. |
|
678 | or by activating the logging at any moment with the magic function %logstart. | |
679 |
|
679 | |||
680 | Log files can later be reloaded with the -logplay option and IPython |
|
680 | Log files can later be reloaded with the -logplay option and IPython | |
681 | will attempt to 'replay' the log by executing all the lines in it, thus |
|
681 | will attempt to 'replay' the log by executing all the lines in it, thus | |
682 | restoring the state of a previous session. This feature is not quite |
|
682 | restoring the state of a previous session. This feature is not quite | |
683 | perfect, but can still be useful in many cases. |
|
683 | perfect, but can still be useful in many cases. | |
684 |
|
684 | |||
685 | The log files can also be used as a way to have a permanent record of |
|
685 | The log files can also be used as a way to have a permanent record of | |
686 | any code you wrote while experimenting. Log files are regular text files |
|
686 | any code you wrote while experimenting. Log files are regular text files | |
687 | which you can later open in your favorite text editor to extract code or |
|
687 | which you can later open in your favorite text editor to extract code or | |
688 | to 'clean them up' before using them to replay a session. |
|
688 | to 'clean them up' before using them to replay a session. | |
689 |
|
689 | |||
690 | The %logstart function for activating logging in mid-session is used as |
|
690 | The %logstart function for activating logging in mid-session is used as | |
691 | follows: |
|
691 | follows: | |
692 |
|
692 | |||
693 | %logstart [log_name [log_mode]] |
|
693 | %logstart [log_name [log_mode]] | |
694 |
|
694 | |||
695 | If no name is given, it defaults to a file named 'log' in your |
|
695 | If no name is given, it defaults to a file named 'log' in your | |
696 | IPYTHONDIR directory, in 'rotate' mode (see below). |
|
696 | IPYTHONDIR directory, in 'rotate' mode (see below). | |
697 |
|
697 | |||
698 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your |
|
698 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your | |
699 | history up to that point and then continues logging. |
|
699 | history up to that point and then continues logging. | |
700 |
|
700 | |||
701 | %logstart takes a second optional parameter: logging mode. This can be |
|
701 | %logstart takes a second optional parameter: logging mode. This can be | |
702 | one of (note that the modes are given unquoted): |
|
702 | one of (note that the modes are given unquoted): | |
703 |
|
703 | |||
704 | * [over:] overwrite existing log_name. |
|
704 | * [over:] overwrite existing log_name. | |
705 | * [backup:] rename (if exists) to log_name~ and start log_name. |
|
705 | * [backup:] rename (if exists) to log_name~ and start log_name. | |
706 | * [append:] well, that says it. |
|
706 | * [append:] well, that says it. | |
707 | * [rotate:] create rotating logs log_name.1~, log_name.2~, etc. |
|
707 | * [rotate:] create rotating logs log_name.1~, log_name.2~, etc. | |
708 |
|
708 | |||
709 | The %logoff and %logon functions allow you to temporarily stop and |
|
709 | The %logoff and %logon functions allow you to temporarily stop and | |
710 | resume logging to a file which had previously been started with |
|
710 | resume logging to a file which had previously been started with | |
711 | %logstart. They will fail (with an explanation) if you try to use them |
|
711 | %logstart. They will fail (with an explanation) if you try to use them | |
712 | before logging has been started. |
|
712 | before logging has been started. | |
713 |
|
713 | |||
714 | .. _system_shell_access: |
|
714 | .. _system_shell_access: | |
715 |
|
715 | |||
716 | System shell access |
|
716 | System shell access | |
717 | ------------------- |
|
717 | ------------------- | |
718 |
|
718 | |||
719 | Any input line beginning with a ! character is passed verbatim (minus |
|
719 | Any input line beginning with a ! character is passed verbatim (minus | |
720 | the !, of course) to the underlying operating system. For example, |
|
720 | the !, of course) to the underlying operating system. For example, | |
721 | typing !ls will run 'ls' in the current directory. |
|
721 | typing !ls will run 'ls' in the current directory. | |
722 |
|
722 | |||
723 | Manual capture of command output |
|
723 | Manual capture of command output | |
724 | -------------------------------- |
|
724 | -------------------------------- | |
725 |
|
725 | |||
726 | If the input line begins with two exclamation marks, !!, the command is |
|
726 | If the input line begins with two exclamation marks, !!, the command is | |
727 | executed but its output is captured and returned as a python list, split |
|
727 | executed but its output is captured and returned as a python list, split | |
728 | on newlines. Any output sent by the subprocess to standard error is |
|
728 | on newlines. Any output sent by the subprocess to standard error is | |
729 | printed separately, so that the resulting list only captures standard |
|
729 | printed separately, so that the resulting list only captures standard | |
730 | output. The !! syntax is a shorthand for the %sx magic command. |
|
730 | output. The !! syntax is a shorthand for the %sx magic command. | |
731 |
|
731 | |||
732 | Finally, the %sc magic (short for 'shell capture') is similar to %sx, |
|
732 | Finally, the %sc magic (short for 'shell capture') is similar to %sx, | |
733 | but allowing more fine-grained control of the capture details, and |
|
733 | but allowing more fine-grained control of the capture details, and | |
734 | storing the result directly into a named variable. The direct use of |
|
734 | storing the result directly into a named variable. The direct use of | |
735 | %sc is now deprecated, and you should ise the ``var = !cmd`` syntax |
|
735 | %sc is now deprecated, and you should ise the ``var = !cmd`` syntax | |
736 | instead. |
|
736 | instead. | |
737 |
|
737 | |||
738 | IPython also allows you to expand the value of python variables when |
|
738 | IPython also allows you to expand the value of python variables when | |
739 | making system calls. Any python variable or expression which you prepend |
|
739 | making system calls. Any python variable or expression which you prepend | |
740 | with $ will get expanded before the system call is made:: |
|
740 | with $ will get expanded before the system call is made:: | |
741 |
|
741 | |||
742 | In [1]: pyvar='Hello world' |
|
742 | In [1]: pyvar='Hello world' | |
743 | In [2]: !echo "A python variable: $pyvar" |
|
743 | In [2]: !echo "A python variable: $pyvar" | |
744 | A python variable: Hello world |
|
744 | A python variable: Hello world | |
745 |
|
745 | |||
746 | If you want the shell to actually see a literal $, you need to type it |
|
746 | If you want the shell to actually see a literal $, you need to type it | |
747 | twice:: |
|
747 | twice:: | |
748 |
|
748 | |||
749 | In [3]: !echo "A system variable: $$HOME" |
|
749 | In [3]: !echo "A system variable: $$HOME" | |
750 | A system variable: /home/fperez |
|
750 | A system variable: /home/fperez | |
751 |
|
751 | |||
752 | You can pass arbitrary expressions, though you'll need to delimit them |
|
752 | You can pass arbitrary expressions, though you'll need to delimit them | |
753 | with {} if there is ambiguity as to the extent of the expression:: |
|
753 | with {} if there is ambiguity as to the extent of the expression:: | |
754 |
|
754 | |||
755 | In [5]: x=10 |
|
755 | In [5]: x=10 | |
756 | In [6]: y=20 |
|
756 | In [6]: y=20 | |
757 | In [13]: !echo $x+y |
|
757 | In [13]: !echo $x+y | |
758 | 10+y |
|
758 | 10+y | |
759 | In [7]: !echo ${x+y} |
|
759 | In [7]: !echo ${x+y} | |
760 | 30 |
|
760 | 30 | |
761 |
|
761 | |||
762 | Even object attributes can be expanded:: |
|
762 | Even object attributes can be expanded:: | |
763 |
|
763 | |||
764 | In [12]: !echo $sys.argv |
|
764 | In [12]: !echo $sys.argv | |
765 | [/home/fperez/usr/bin/ipython] |
|
765 | [/home/fperez/usr/bin/ipython] | |
766 |
|
766 | |||
767 |
|
767 | |||
768 | System command aliases |
|
768 | System command aliases | |
769 | ---------------------- |
|
769 | ---------------------- | |
770 |
|
770 | |||
771 | The %alias magic function and the alias option in the ipythonrc |
|
771 | The %alias magic function and the alias option in the ipythonrc | |
772 | configuration file allow you to define magic functions which are in fact |
|
772 | configuration file allow you to define magic functions which are in fact | |
773 | system shell commands. These aliases can have parameters. |
|
773 | system shell commands. These aliases can have parameters. | |
774 |
|
774 | |||
775 | '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd' |
|
775 | '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd' | |
776 |
|
776 | |||
777 | Then, typing '%alias_name params' will execute the system command 'cmd |
|
777 | Then, typing '%alias_name params' will execute the system command 'cmd | |
778 | params' (from your underlying operating system). |
|
778 | params' (from your underlying operating system). | |
779 |
|
779 | |||
780 | You can also define aliases with parameters using %s specifiers (one per |
|
780 | You can also define aliases with parameters using %s specifiers (one per | |
781 | parameter). The following example defines the %parts function as an |
|
781 | parameter). The following example defines the %parts function as an | |
782 | alias to the command 'echo first %s second %s' where each %s will be |
|
782 | alias to the command 'echo first %s second %s' where each %s will be | |
783 | replaced by a positional parameter to the call to %parts:: |
|
783 | replaced by a positional parameter to the call to %parts:: | |
784 |
|
784 | |||
785 | In [1]: alias parts echo first %s second %s |
|
785 | In [1]: alias parts echo first %s second %s | |
786 | In [2]: %parts A B |
|
786 | In [2]: %parts A B | |
787 | first A second B |
|
787 | first A second B | |
788 | In [3]: %parts A |
|
788 | In [3]: %parts A | |
789 | Incorrect number of arguments: 2 expected. |
|
789 | Incorrect number of arguments: 2 expected. | |
790 | parts is an alias to: 'echo first %s second %s' |
|
790 | parts is an alias to: 'echo first %s second %s' | |
791 |
|
791 | |||
792 | If called with no parameters, %alias prints the table of currently |
|
792 | If called with no parameters, %alias prints the table of currently | |
793 | defined aliases. |
|
793 | defined aliases. | |
794 |
|
794 | |||
795 | The %rehash/rehashx magics allow you to load your entire $PATH as |
|
795 | The %rehash/rehashx magics allow you to load your entire $PATH as | |
796 | ipython aliases. See their respective docstrings (or sec. 6.2 |
|
796 | ipython aliases. See their respective docstrings (or sec. 6.2 | |
797 | <#sec:magic> for further details). |
|
797 | <#sec:magic> for further details). | |
798 |
|
798 | |||
799 |
|
799 | |||
800 | .. _dreload: |
|
800 | .. _dreload: | |
801 |
|
801 | |||
802 | Recursive reload |
|
802 | Recursive reload | |
803 | ---------------- |
|
803 | ---------------- | |
804 |
|
804 | |||
805 | The dreload function does a recursive reload of a module: changes made |
|
805 | The dreload function does a recursive reload of a module: changes made | |
806 | to the module since you imported will actually be available without |
|
806 | to the module since you imported will actually be available without | |
807 | having to exit. |
|
807 | having to exit. | |
808 |
|
808 | |||
809 |
|
809 | |||
810 | Verbose and colored exception traceback printouts |
|
810 | Verbose and colored exception traceback printouts | |
811 | ------------------------------------------------- |
|
811 | ------------------------------------------------- | |
812 |
|
812 | |||
813 | IPython provides the option to see very detailed exception tracebacks, |
|
813 | IPython provides the option to see very detailed exception tracebacks, | |
814 | which can be especially useful when debugging large programs. You can |
|
814 | which can be especially useful when debugging large programs. You can | |
815 | run any Python file with the %run function to benefit from these |
|
815 | run any Python file with the %run function to benefit from these | |
816 | detailed tracebacks. Furthermore, both normal and verbose tracebacks can |
|
816 | detailed tracebacks. Furthermore, both normal and verbose tracebacks can | |
817 | be colored (if your terminal supports it) which makes them much easier |
|
817 | be colored (if your terminal supports it) which makes them much easier | |
818 | to parse visually. |
|
818 | to parse visually. | |
819 |
|
819 | |||
820 | See the magic xmode and colors functions for details (just type %magic). |
|
820 | See the magic xmode and colors functions for details (just type %magic). | |
821 |
|
821 | |||
822 | These features are basically a terminal version of Ka-Ping Yee's cgitb |
|
822 | These features are basically a terminal version of Ka-Ping Yee's cgitb | |
823 | module, now part of the standard Python library. |
|
823 | module, now part of the standard Python library. | |
824 |
|
824 | |||
825 |
|
825 | |||
826 | .. _input_caching: |
|
826 | .. _input_caching: | |
827 |
|
827 | |||
828 | Input caching system |
|
828 | Input caching system | |
829 | -------------------- |
|
829 | -------------------- | |
830 |
|
830 | |||
831 | IPython offers numbered prompts (In/Out) with input and output caching |
|
831 | IPython offers numbered prompts (In/Out) with input and output caching | |
832 | (also referred to as 'input history'). All input is saved and can be |
|
832 | (also referred to as 'input history'). All input is saved and can be | |
833 | retrieved as variables (besides the usual arrow key recall), in |
|
833 | retrieved as variables (besides the usual arrow key recall), in | |
834 | addition to the %rep magic command that brings a history entry |
|
834 | addition to the %rep magic command that brings a history entry | |
835 | up for editing on the next command line. |
|
835 | up for editing on the next command line. | |
836 |
|
836 | |||
837 | The following GLOBAL variables always exist (so don't overwrite them!): |
|
837 | The following GLOBAL variables always exist (so don't overwrite them!): | |
838 | _i: stores previous input. _ii: next previous. _iii: next-next previous. |
|
838 | _i: stores previous input. _ii: next previous. _iii: next-next previous. | |
839 | _ih : a list of all input _ih[n] is the input from line n and this list |
|
839 | _ih : a list of all input _ih[n] is the input from line n and this list | |
840 | is aliased to the global variable In. If you overwrite In with a |
|
840 | is aliased to the global variable In. If you overwrite In with a | |
841 | variable of your own, you can remake the assignment to the internal list |
|
841 | variable of your own, you can remake the assignment to the internal list | |
842 | with a simple 'In=_ih'. |
|
842 | with a simple 'In=_ih'. | |
843 |
|
843 | |||
844 | Additionally, global variables named _i<n> are dynamically created (<n> |
|
844 | Additionally, global variables named _i<n> are dynamically created (<n> | |
845 | being the prompt counter), such that |
|
845 | being the prompt counter), such that | |
846 | _i<n> == _ih[<n>] == In[<n>]. |
|
846 | _i<n> == _ih[<n>] == In[<n>]. | |
847 |
|
847 | |||
848 | For example, what you typed at prompt 14 is available as _i14, _ih[14] |
|
848 | For example, what you typed at prompt 14 is available as _i14, _ih[14] | |
849 | and In[14]. |
|
849 | and In[14]. | |
850 |
|
850 | |||
851 | This allows you to easily cut and paste multi line interactive prompts |
|
851 | This allows you to easily cut and paste multi line interactive prompts | |
852 | by printing them out: they print like a clean string, without prompt |
|
852 | by printing them out: they print like a clean string, without prompt | |
853 | characters. You can also manipulate them like regular variables (they |
|
853 | characters. You can also manipulate them like regular variables (they | |
854 | are strings), modify or exec them (typing 'exec _i9' will re-execute the |
|
854 | are strings), modify or exec them (typing 'exec _i9' will re-execute the | |
855 | contents of input prompt 9, 'exec In[9:14]+In[18]' will re-execute lines |
|
855 | contents of input prompt 9, 'exec In[9:14]+In[18]' will re-execute lines | |
856 | 9 through 13 and line 18). |
|
856 | 9 through 13 and line 18). | |
857 |
|
857 | |||
858 | You can also re-execute multiple lines of input easily by using the |
|
858 | You can also re-execute multiple lines of input easily by using the | |
859 | magic %macro function (which automates the process and allows |
|
859 | magic %macro function (which automates the process and allows | |
860 | re-execution without having to type 'exec' every time). The macro system |
|
860 | re-execution without having to type 'exec' every time). The macro system | |
861 | also allows you to re-execute previous lines which include magic |
|
861 | also allows you to re-execute previous lines which include magic | |
862 | function calls (which require special processing). Type %macro? or see |
|
862 | function calls (which require special processing). Type %macro? or see | |
863 | sec. 6.2 <#sec:magic> for more details on the macro system. |
|
863 | sec. 6.2 <#sec:magic> for more details on the macro system. | |
864 |
|
864 | |||
865 | A history function %hist allows you to see any part of your input |
|
865 | A history function %hist allows you to see any part of your input | |
866 | history by printing a range of the _i variables. |
|
866 | history by printing a range of the _i variables. | |
867 |
|
867 | |||
868 | You can also search ('grep') through your history by typing |
|
868 | You can also search ('grep') through your history by typing | |
869 | '%hist -g somestring'. This also searches through the so called *shadow history*, |
|
869 | '%hist -g somestring'. This also searches through the so called *shadow history*, | |
870 | which remembers all the commands (apart from multiline code blocks) |
|
870 | which remembers all the commands (apart from multiline code blocks) | |
871 | you have ever entered. Handy for searching for svn/bzr URL's, IP adrresses |
|
871 | you have ever entered. Handy for searching for svn/bzr URL's, IP adrresses | |
872 | etc. You can bring shadow history entries listed by '%hist -g' up for editing |
|
872 | etc. You can bring shadow history entries listed by '%hist -g' up for editing | |
873 | (or re-execution by just pressing ENTER) with %rep command. Shadow history |
|
873 | (or re-execution by just pressing ENTER) with %rep command. Shadow history | |
874 | entries are not available as _iNUMBER variables, and they are identified by |
|
874 | entries are not available as _iNUMBER variables, and they are identified by | |
875 | the '0' prefix in %hist -g output. That is, history entry 12 is a normal |
|
875 | the '0' prefix in %hist -g output. That is, history entry 12 is a normal | |
876 | history entry, but 0231 is a shadow history entry. |
|
876 | history entry, but 0231 is a shadow history entry. | |
877 |
|
877 | |||
878 | Shadow history was added because the readline history is inherently very |
|
878 | Shadow history was added because the readline history is inherently very | |
879 | unsafe - if you have multiple IPython sessions open, the last session |
|
879 | unsafe - if you have multiple IPython sessions open, the last session | |
880 | to close will overwrite the history of previountly closed session. Likewise, |
|
880 | to close will overwrite the history of previountly closed session. Likewise, | |
881 | if a crash occurs, history is never saved, whereas shadow history entries |
|
881 | if a crash occurs, history is never saved, whereas shadow history entries | |
882 | are added after entering every command (so a command executed |
|
882 | are added after entering every command (so a command executed | |
883 | in another IPython session is immediately available in other IPython |
|
883 | in another IPython session is immediately available in other IPython | |
884 | sessions that are open). |
|
884 | sessions that are open). | |
885 |
|
885 | |||
886 | To conserve space, a command can exist in shadow history only once - it doesn't |
|
886 | To conserve space, a command can exist in shadow history only once - it doesn't | |
887 | make sense to store a common line like "cd .." a thousand times. The idea is |
|
887 | make sense to store a common line like "cd .." a thousand times. The idea is | |
888 | mainly to provide a reliable place where valuable, hard-to-remember commands can |
|
888 | mainly to provide a reliable place where valuable, hard-to-remember commands can | |
889 | always be retrieved, as opposed to providing an exact sequence of commands |
|
889 | always be retrieved, as opposed to providing an exact sequence of commands | |
890 | you have entered in actual order. |
|
890 | you have entered in actual order. | |
891 |
|
891 | |||
892 | Because shadow history has all the commands you have ever executed, |
|
892 | Because shadow history has all the commands you have ever executed, | |
893 | time taken by %hist -g will increase oven time. If it ever starts to take |
|
893 | time taken by %hist -g will increase oven time. If it ever starts to take | |
894 | too long (or it ends up containing sensitive information like passwords), |
|
894 | too long (or it ends up containing sensitive information like passwords), | |
895 | clear the shadow history by `%clear shadow_nuke`. |
|
895 | clear the shadow history by `%clear shadow_nuke`. | |
896 |
|
896 | |||
897 | Time taken to add entries to shadow history should be negligible, but |
|
897 | Time taken to add entries to shadow history should be negligible, but | |
898 | in any case, if you start noticing performance degradation after using |
|
898 | in any case, if you start noticing performance degradation after using | |
899 | IPython for a long time (or running a script that floods the shadow history!), |
|
899 | IPython for a long time (or running a script that floods the shadow history!), | |
900 | you can 'compress' the shadow history by executing |
|
900 | you can 'compress' the shadow history by executing | |
901 | `%clear shadow_compress`. In practice, this should never be necessary |
|
901 | `%clear shadow_compress`. In practice, this should never be necessary | |
902 | in normal use. |
|
902 | in normal use. | |
903 |
|
903 | |||
904 | .. _output_caching: |
|
904 | .. _output_caching: | |
905 |
|
905 | |||
906 | Output caching system |
|
906 | Output caching system | |
907 | --------------------- |
|
907 | --------------------- | |
908 |
|
908 | |||
909 | For output that is returned from actions, a system similar to the input |
|
909 | For output that is returned from actions, a system similar to the input | |
910 | cache exists but using _ instead of _i. Only actions that produce a |
|
910 | cache exists but using _ instead of _i. Only actions that produce a | |
911 | result (NOT assignments, for example) are cached. If you are familiar |
|
911 | result (NOT assignments, for example) are cached. If you are familiar | |
912 | with Mathematica, IPython's _ variables behave exactly like |
|
912 | with Mathematica, IPython's _ variables behave exactly like | |
913 | Mathematica's % variables. |
|
913 | Mathematica's % variables. | |
914 |
|
914 | |||
915 | The following GLOBAL variables always exist (so don't overwrite them!): |
|
915 | The following GLOBAL variables always exist (so don't overwrite them!): | |
916 |
|
916 | |||
917 | * [_] (a single underscore) : stores previous output, like Python's |
|
917 | * [_] (a single underscore) : stores previous output, like Python's | |
918 | default interpreter. |
|
918 | default interpreter. | |
919 | * [__] (two underscores): next previous. |
|
919 | * [__] (two underscores): next previous. | |
920 | * [___] (three underscores): next-next previous. |
|
920 | * [___] (three underscores): next-next previous. | |
921 |
|
921 | |||
922 | Additionally, global variables named _<n> are dynamically created (<n> |
|
922 | Additionally, global variables named _<n> are dynamically created (<n> | |
923 | being the prompt counter), such that the result of output <n> is always |
|
923 | being the prompt counter), such that the result of output <n> is always | |
924 | available as _<n> (don't use the angle brackets, just the number, e.g. |
|
924 | available as _<n> (don't use the angle brackets, just the number, e.g. | |
925 | _21). |
|
925 | _21). | |
926 |
|
926 | |||
927 | These global variables are all stored in a global dictionary (not a |
|
927 | These global variables are all stored in a global dictionary (not a | |
928 | list, since it only has entries for lines which returned a result) |
|
928 | list, since it only has entries for lines which returned a result) | |
929 | available under the names _oh and Out (similar to _ih and In). So the |
|
929 | available under the names _oh and Out (similar to _ih and In). So the | |
930 | output from line 12 can be obtained as _12, Out[12] or _oh[12]. If you |
|
930 | output from line 12 can be obtained as _12, Out[12] or _oh[12]. If you | |
931 | accidentally overwrite the Out variable you can recover it by typing |
|
931 | accidentally overwrite the Out variable you can recover it by typing | |
932 | 'Out=_oh' at the prompt. |
|
932 | 'Out=_oh' at the prompt. | |
933 |
|
933 | |||
934 | This system obviously can potentially put heavy memory demands on your |
|
934 | This system obviously can potentially put heavy memory demands on your | |
935 | system, since it prevents Python's garbage collector from removing any |
|
935 | system, since it prevents Python's garbage collector from removing any | |
936 | previously computed results. You can control how many results are kept |
|
936 | previously computed results. You can control how many results are kept | |
937 | in memory with the option (at the command line or in your ipythonrc |
|
937 | in memory with the option (at the command line or in your ipythonrc | |
938 | file) cache_size. If you set it to 0, the whole system is completely |
|
938 | file) cache_size. If you set it to 0, the whole system is completely | |
939 | disabled and the prompts revert to the classic '>>>' of normal Python. |
|
939 | disabled and the prompts revert to the classic '>>>' of normal Python. | |
940 |
|
940 | |||
941 |
|
941 | |||
942 | Directory history |
|
942 | Directory history | |
943 | ----------------- |
|
943 | ----------------- | |
944 |
|
944 | |||
945 | Your history of visited directories is kept in the global list _dh, and |
|
945 | Your history of visited directories is kept in the global list _dh, and | |
946 | the magic %cd command can be used to go to any entry in that list. The |
|
946 | the magic %cd command can be used to go to any entry in that list. The | |
947 | %dhist command allows you to view this history. Do ``cd -<TAB`` to |
|
947 | %dhist command allows you to view this history. Do ``cd -<TAB`` to | |
948 | conventiently view the directory history. |
|
948 | conventiently view the directory history. | |
949 |
|
949 | |||
950 |
|
950 | |||
951 | Automatic parentheses and quotes |
|
951 | Automatic parentheses and quotes | |
952 | -------------------------------- |
|
952 | -------------------------------- | |
953 |
|
953 | |||
954 | These features were adapted from Nathan Gray's LazyPython. They are |
|
954 | These features were adapted from Nathan Gray's LazyPython. They are | |
955 | meant to allow less typing for common situations. |
|
955 | meant to allow less typing for common situations. | |
956 |
|
956 | |||
957 |
|
957 | |||
958 | Automatic parentheses |
|
958 | Automatic parentheses | |
959 | --------------------- |
|
959 | --------------------- | |
960 |
|
960 | |||
961 | Callable objects (i.e. functions, methods, etc) can be invoked like this |
|
961 | Callable objects (i.e. functions, methods, etc) can be invoked like this | |
962 | (notice the commas between the arguments):: |
|
962 | (notice the commas between the arguments):: | |
963 |
|
963 | |||
964 | >>> callable_ob arg1, arg2, arg3 |
|
964 | >>> callable_ob arg1, arg2, arg3 | |
965 |
|
965 | |||
966 | and the input will be translated to this:: |
|
966 | and the input will be translated to this:: | |
967 |
|
967 | |||
968 | -> callable_ob(arg1, arg2, arg3) |
|
968 | -> callable_ob(arg1, arg2, arg3) | |
969 |
|
969 | |||
970 | You can force automatic parentheses by using '/' as the first character |
|
970 | You can force automatic parentheses by using '/' as the first character | |
971 | of a line. For example:: |
|
971 | of a line. For example:: | |
972 |
|
972 | |||
973 | >>> /globals # becomes 'globals()' |
|
973 | >>> /globals # becomes 'globals()' | |
974 |
|
974 | |||
975 | Note that the '/' MUST be the first character on the line! This won't work:: |
|
975 | Note that the '/' MUST be the first character on the line! This won't work:: | |
976 |
|
976 | |||
977 | >>> print /globals # syntax error |
|
977 | >>> print /globals # syntax error | |
978 |
|
978 | |||
979 | In most cases the automatic algorithm should work, so you should rarely |
|
979 | In most cases the automatic algorithm should work, so you should rarely | |
980 | need to explicitly invoke /. One notable exception is if you are trying |
|
980 | need to explicitly invoke /. One notable exception is if you are trying | |
981 | to call a function with a list of tuples as arguments (the parenthesis |
|
981 | to call a function with a list of tuples as arguments (the parenthesis | |
982 | will confuse IPython):: |
|
982 | will confuse IPython):: | |
983 |
|
983 | |||
984 | In [1]: zip (1,2,3),(4,5,6) # won't work |
|
984 | In [1]: zip (1,2,3),(4,5,6) # won't work | |
985 |
|
985 | |||
986 | but this will work:: |
|
986 | but this will work:: | |
987 |
|
987 | |||
988 | In [2]: /zip (1,2,3),(4,5,6) |
|
988 | In [2]: /zip (1,2,3),(4,5,6) | |
989 | ---> zip ((1,2,3),(4,5,6)) |
|
989 | ---> zip ((1,2,3),(4,5,6)) | |
990 | Out[2]= [(1, 4), (2, 5), (3, 6)] |
|
990 | Out[2]= [(1, 4), (2, 5), (3, 6)] | |
991 |
|
991 | |||
992 | IPython tells you that it has altered your command line by displaying |
|
992 | IPython tells you that it has altered your command line by displaying | |
993 | the new command line preceded by ->. e.g.:: |
|
993 | the new command line preceded by ->. e.g.:: | |
994 |
|
994 | |||
995 | In [18]: callable list |
|
995 | In [18]: callable list | |
996 | ----> callable (list) |
|
996 | ----> callable (list) | |
997 |
|
997 | |||
998 |
|
998 | |||
999 | Automatic quoting |
|
999 | Automatic quoting | |
1000 | ----------------- |
|
1000 | ----------------- | |
1001 |
|
1001 | |||
1002 | You can force automatic quoting of a function's arguments by using ',' |
|
1002 | You can force automatic quoting of a function's arguments by using ',' | |
1003 | or ';' as the first character of a line. For example:: |
|
1003 | or ';' as the first character of a line. For example:: | |
1004 |
|
1004 | |||
1005 | >>> ,my_function /home/me # becomes my_function("/home/me") |
|
1005 | >>> ,my_function /home/me # becomes my_function("/home/me") | |
1006 |
|
1006 | |||
1007 | If you use ';' instead, the whole argument is quoted as a single string |
|
1007 | If you use ';' instead, the whole argument is quoted as a single string | |
1008 | (while ',' splits on whitespace):: |
|
1008 | (while ',' splits on whitespace):: | |
1009 |
|
1009 | |||
1010 | >>> ,my_function a b c # becomes my_function("a","b","c") |
|
1010 | >>> ,my_function a b c # becomes my_function("a","b","c") | |
1011 |
|
1011 | |||
1012 | >>> ;my_function a b c # becomes my_function("a b c") |
|
1012 | >>> ;my_function a b c # becomes my_function("a b c") | |
1013 |
|
1013 | |||
1014 | Note that the ',' or ';' MUST be the first character on the line! This |
|
1014 | Note that the ',' or ';' MUST be the first character on the line! This | |
1015 | won't work:: |
|
1015 | won't work:: | |
1016 |
|
1016 | |||
1017 | >>> x = ,my_function /home/me # syntax error |
|
1017 | >>> x = ,my_function /home/me # syntax error | |
1018 |
|
1018 | |||
1019 | IPython as your default Python environment |
|
1019 | IPython as your default Python environment | |
1020 | ========================================== |
|
1020 | ========================================== | |
1021 |
|
1021 | |||
1022 | Python honors the environment variable PYTHONSTARTUP and will execute at |
|
1022 | Python honors the environment variable PYTHONSTARTUP and will execute at | |
1023 | startup the file referenced by this variable. If you put at the end of |
|
1023 | startup the file referenced by this variable. If you put at the end of | |
1024 | this file the following two lines of code:: |
|
1024 | this file the following two lines of code:: | |
1025 |
|
1025 | |||
1026 | import IPython |
|
1026 | import IPython | |
1027 | IPython.Shell.IPShell().mainloop(sys_exit=1) |
|
1027 | IPython.Shell.IPShell().mainloop(sys_exit=1) | |
1028 |
|
1028 | |||
1029 | then IPython will be your working environment anytime you start Python. |
|
1029 | then IPython will be your working environment anytime you start Python. | |
1030 | The sys_exit=1 is needed to have IPython issue a call to sys.exit() when |
|
1030 | The sys_exit=1 is needed to have IPython issue a call to sys.exit() when | |
1031 | it finishes, otherwise you'll be back at the normal Python '>>>' |
|
1031 | it finishes, otherwise you'll be back at the normal Python '>>>' | |
1032 | prompt. |
|
1032 | prompt. | |
1033 |
|
1033 | |||
1034 | This is probably useful to developers who manage multiple Python |
|
1034 | This is probably useful to developers who manage multiple Python | |
1035 | versions and don't want to have correspondingly multiple IPython |
|
1035 | versions and don't want to have correspondingly multiple IPython | |
1036 | versions. Note that in this mode, there is no way to pass IPython any |
|
1036 | versions. Note that in this mode, there is no way to pass IPython any | |
1037 | command-line options, as those are trapped first by Python itself. |
|
1037 | command-line options, as those are trapped first by Python itself. | |
1038 |
|
1038 | |||
1039 | .. _Embedding: |
|
1039 | .. _Embedding: | |
1040 |
|
1040 | |||
1041 | Embedding IPython |
|
1041 | Embedding IPython | |
1042 | ================= |
|
1042 | ================= | |
1043 |
|
1043 | |||
1044 | It is possible to start an IPython instance inside your own Python |
|
1044 | It is possible to start an IPython instance inside your own Python | |
1045 | programs. This allows you to evaluate dynamically the state of your |
|
1045 | programs. This allows you to evaluate dynamically the state of your | |
1046 | code, operate with your variables, analyze them, etc. Note however that |
|
1046 | code, operate with your variables, analyze them, etc. Note however that | |
1047 | any changes you make to values while in the shell do not propagate back |
|
1047 | any changes you make to values while in the shell do not propagate back | |
1048 | to the running code, so it is safe to modify your values because you |
|
1048 | to the running code, so it is safe to modify your values because you | |
1049 | won't break your code in bizarre ways by doing so. |
|
1049 | won't break your code in bizarre ways by doing so. | |
1050 |
|
1050 | |||
1051 | This feature allows you to easily have a fully functional python |
|
1051 | This feature allows you to easily have a fully functional python | |
1052 | environment for doing object introspection anywhere in your code with a |
|
1052 | environment for doing object introspection anywhere in your code with a | |
1053 | simple function call. In some cases a simple print statement is enough, |
|
1053 | simple function call. In some cases a simple print statement is enough, | |
1054 | but if you need to do more detailed analysis of a code fragment this |
|
1054 | but if you need to do more detailed analysis of a code fragment this | |
1055 | feature can be very valuable. |
|
1055 | feature can be very valuable. | |
1056 |
|
1056 | |||
1057 | It can also be useful in scientific computing situations where it is |
|
1057 | It can also be useful in scientific computing situations where it is | |
1058 | common to need to do some automatic, computationally intensive part and |
|
1058 | common to need to do some automatic, computationally intensive part and | |
1059 | then stop to look at data, plots, etc. |
|
1059 | then stop to look at data, plots, etc. | |
1060 | Opening an IPython instance will give you full access to your data and |
|
1060 | Opening an IPython instance will give you full access to your data and | |
1061 | functions, and you can resume program execution once you are done with |
|
1061 | functions, and you can resume program execution once you are done with | |
1062 | the interactive part (perhaps to stop again later, as many times as |
|
1062 | the interactive part (perhaps to stop again later, as many times as | |
1063 | needed). |
|
1063 | needed). | |
1064 |
|
1064 | |||
1065 | The following code snippet is the bare minimum you need to include in |
|
1065 | The following code snippet is the bare minimum you need to include in | |
1066 | your Python programs for this to work (detailed examples follow later):: |
|
1066 | your Python programs for this to work (detailed examples follow later):: | |
1067 |
|
1067 | |||
1068 | from IPython.Shell import IPShellEmbed |
|
1068 | from IPython.Shell import IPShellEmbed | |
1069 |
|
1069 | |||
1070 | ipshell = IPShellEmbed() |
|
1070 | ipshell = IPShellEmbed() | |
1071 |
|
1071 | |||
1072 | ipshell() # this call anywhere in your program will start IPython |
|
1072 | ipshell() # this call anywhere in your program will start IPython | |
1073 |
|
1073 | |||
1074 | You can run embedded instances even in code which is itself being run at |
|
1074 | You can run embedded instances even in code which is itself being run at | |
1075 | the IPython interactive prompt with '%run <filename>'. Since it's easy |
|
1075 | the IPython interactive prompt with '%run <filename>'. Since it's easy | |
1076 | to get lost as to where you are (in your top-level IPython or in your |
|
1076 | to get lost as to where you are (in your top-level IPython or in your | |
1077 | embedded one), it's a good idea in such cases to set the in/out prompts |
|
1077 | embedded one), it's a good idea in such cases to set the in/out prompts | |
1078 | to something different for the embedded instances. The code examples |
|
1078 | to something different for the embedded instances. The code examples | |
1079 | below illustrate this. |
|
1079 | below illustrate this. | |
1080 |
|
1080 | |||
1081 | You can also have multiple IPython instances in your program and open |
|
1081 | You can also have multiple IPython instances in your program and open | |
1082 | them separately, for example with different options for data |
|
1082 | them separately, for example with different options for data | |
1083 | presentation. If you close and open the same instance multiple times, |
|
1083 | presentation. If you close and open the same instance multiple times, | |
1084 | its prompt counters simply continue from each execution to the next. |
|
1084 | its prompt counters simply continue from each execution to the next. | |
1085 |
|
1085 | |||
1086 | Please look at the docstrings in the Shell.py module for more details on |
|
1086 | Please look at the docstrings in the Shell.py module for more details on | |
1087 | the use of this system. |
|
1087 | the use of this system. | |
1088 |
|
1088 | |||
1089 | The following sample file illustrating how to use the embedding |
|
1089 | The following sample file illustrating how to use the embedding | |
1090 | functionality is provided in the examples directory as example-embed.py. |
|
1090 | functionality is provided in the examples directory as example-embed.py. | |
1091 | It should be fairly self-explanatory:: |
|
1091 | It should be fairly self-explanatory:: | |
1092 |
|
1092 | |||
1093 |
|
1093 | |||
1094 | #!/usr/bin/env python |
|
1094 | #!/usr/bin/env python | |
1095 |
|
1095 | |||
1096 | """An example of how to embed an IPython shell into a running program. |
|
1096 | """An example of how to embed an IPython shell into a running program. | |
1097 |
|
1097 | |||
1098 | Please see the documentation in the IPython.Shell module for more details. |
|
1098 | Please see the documentation in the IPython.Shell module for more details. | |
1099 |
|
1099 | |||
1100 | The accompanying file example-embed-short.py has quick code fragments for |
|
1100 | The accompanying file example-embed-short.py has quick code fragments for | |
1101 | embedding which you can cut and paste in your code once you understand how |
|
1101 | embedding which you can cut and paste in your code once you understand how | |
1102 | things work. |
|
1102 | things work. | |
1103 |
|
1103 | |||
1104 | The code in this file is deliberately extra-verbose, meant for learning.""" |
|
1104 | The code in this file is deliberately extra-verbose, meant for learning.""" | |
1105 |
|
1105 | |||
1106 | # The basics to get you going: |
|
1106 | # The basics to get you going: | |
1107 |
|
1107 | |||
1108 | # IPython sets the __IPYTHON__ variable so you can know if you have nested |
|
1108 | # IPython sets the __IPYTHON__ variable so you can know if you have nested | |
1109 | # copies running. |
|
1109 | # copies running. | |
1110 |
|
1110 | |||
1111 | # Try running this code both at the command line and from inside IPython (with |
|
1111 | # Try running this code both at the command line and from inside IPython (with | |
1112 | # %run example-embed.py) |
|
1112 | # %run example-embed.py) | |
1113 | try: |
|
1113 | try: | |
1114 | __IPYTHON__ |
|
1114 | __IPYTHON__ | |
1115 | except NameError: |
|
1115 | except NameError: | |
1116 | nested = 0 |
|
1116 | nested = 0 | |
1117 | args = [''] |
|
1117 | args = [''] | |
1118 | else: |
|
1118 | else: | |
1119 | print "Running nested copies of IPython." |
|
1119 | print "Running nested copies of IPython." | |
1120 | print "The prompts for the nested copy have been modified" |
|
1120 | print "The prompts for the nested copy have been modified" | |
1121 | nested = 1 |
|
1121 | nested = 1 | |
1122 | # what the embedded instance will see as sys.argv: |
|
1122 | # what the embedded instance will see as sys.argv: | |
1123 | args = ['-pi1','In <\\#>: ','-pi2',' .\\D.: ', |
|
1123 | args = ['-pi1','In <\\#>: ','-pi2',' .\\D.: ', | |
1124 | '-po','Out<\\#>: ','-nosep'] |
|
1124 | '-po','Out<\\#>: ','-nosep'] | |
1125 |
|
1125 | |||
1126 | # First import the embeddable shell class |
|
1126 | # First import the embeddable shell class | |
1127 | from IPython.Shell import IPShellEmbed |
|
1127 | from IPython.Shell import IPShellEmbed | |
1128 |
|
1128 | |||
1129 | # Now create an instance of the embeddable shell. The first argument is a |
|
1129 | # Now create an instance of the embeddable shell. The first argument is a | |
1130 | # string with options exactly as you would type them if you were starting |
|
1130 | # string with options exactly as you would type them if you were starting | |
1131 | # IPython at the system command line. Any parameters you want to define for |
|
1131 | # IPython at the system command line. Any parameters you want to define for | |
1132 | # configuration can thus be specified here. |
|
1132 | # configuration can thus be specified here. | |
1133 | ipshell = IPShellEmbed(args, |
|
1133 | ipshell = IPShellEmbed(args, | |
1134 | banner = 'Dropping into IPython', |
|
1134 | banner = 'Dropping into IPython', | |
1135 | exit_msg = 'Leaving Interpreter, back to program.') |
|
1135 | exit_msg = 'Leaving Interpreter, back to program.') | |
1136 |
|
1136 | |||
1137 | # Make a second instance, you can have as many as you want. |
|
1137 | # Make a second instance, you can have as many as you want. | |
1138 | if nested: |
|
1138 | if nested: | |
1139 | args[1] = 'In2<\\#>' |
|
1139 | args[1] = 'In2<\\#>' | |
1140 | else: |
|
1140 | else: | |
1141 | args = ['-pi1','In2<\\#>: ','-pi2',' .\\D.: ', |
|
1141 | args = ['-pi1','In2<\\#>: ','-pi2',' .\\D.: ', | |
1142 | '-po','Out<\\#>: ','-nosep'] |
|
1142 | '-po','Out<\\#>: ','-nosep'] | |
1143 | ipshell2 = IPShellEmbed(args,banner = 'Second IPython instance.') |
|
1143 | ipshell2 = IPShellEmbed(args,banner = 'Second IPython instance.') | |
1144 |
|
1144 | |||
1145 | print '\nHello. This is printed from the main controller program.\n' |
|
1145 | print '\nHello. This is printed from the main controller program.\n' | |
1146 |
|
1146 | |||
1147 | # You can then call ipshell() anywhere you need it (with an optional |
|
1147 | # You can then call ipshell() anywhere you need it (with an optional | |
1148 | # message): |
|
1148 | # message): | |
1149 | ipshell('***Called from top level. ' |
|
1149 | ipshell('***Called from top level. ' | |
1150 | 'Hit Ctrl-D to exit interpreter and continue program.\n' |
|
1150 | 'Hit Ctrl-D to exit interpreter and continue program.\n' | |
1151 | 'Note that if you use %kill_embedded, you can fully deactivate\n' |
|
1151 | 'Note that if you use %kill_embedded, you can fully deactivate\n' | |
1152 | 'This embedded instance so it will never turn on again') |
|
1152 | 'This embedded instance so it will never turn on again') | |
1153 |
|
1153 | |||
1154 | print '\nBack in caller program, moving along...\n' |
|
1154 | print '\nBack in caller program, moving along...\n' | |
1155 |
|
1155 | |||
1156 | #--------------------------------------------------------------------------- |
|
1156 | #--------------------------------------------------------------------------- | |
1157 | # More details: |
|
1157 | # More details: | |
1158 |
|
1158 | |||
1159 | # IPShellEmbed instances don't print the standard system banner and |
|
1159 | # IPShellEmbed instances don't print the standard system banner and | |
1160 | # messages. The IPython banner (which actually may contain initialization |
|
1160 | # messages. The IPython banner (which actually may contain initialization | |
1161 | # messages) is available as <instance>.IP.BANNER in case you want it. |
|
1161 | # messages) is available as <instance>.IP.BANNER in case you want it. | |
1162 |
|
1162 | |||
1163 | # IPShellEmbed instances print the following information everytime they |
|
1163 | # IPShellEmbed instances print the following information everytime they | |
1164 | # start: |
|
1164 | # start: | |
1165 |
|
1165 | |||
1166 | # - A global startup banner. |
|
1166 | # - A global startup banner. | |
1167 |
|
1167 | |||
1168 | # - A call-specific header string, which you can use to indicate where in the |
|
1168 | # - A call-specific header string, which you can use to indicate where in the | |
1169 | # execution flow the shell is starting. |
|
1169 | # execution flow the shell is starting. | |
1170 |
|
1170 | |||
1171 | # They also print an exit message every time they exit. |
|
1171 | # They also print an exit message every time they exit. | |
1172 |
|
1172 | |||
1173 | # Both the startup banner and the exit message default to None, and can be set |
|
1173 | # Both the startup banner and the exit message default to None, and can be set | |
1174 | # either at the instance constructor or at any other time with the |
|
1174 | # either at the instance constructor or at any other time with the | |
1175 | # set_banner() and set_exit_msg() methods. |
|
1175 | # set_banner() and set_exit_msg() methods. | |
1176 |
|
1176 | |||
1177 | # The shell instance can be also put in 'dummy' mode globally or on a per-call |
|
1177 | # The shell instance can be also put in 'dummy' mode globally or on a per-call | |
1178 | # basis. This gives you fine control for debugging without having to change |
|
1178 | # basis. This gives you fine control for debugging without having to change | |
1179 | # code all over the place. |
|
1179 | # code all over the place. | |
1180 |
|
1180 | |||
1181 | # The code below illustrates all this. |
|
1181 | # The code below illustrates all this. | |
1182 |
|
1182 | |||
1183 |
|
1183 | |||
1184 | # This is how the global banner and exit_msg can be reset at any point |
|
1184 | # This is how the global banner and exit_msg can be reset at any point | |
1185 | ipshell.set_banner('Entering interpreter - New Banner') |
|
1185 | ipshell.set_banner('Entering interpreter - New Banner') | |
1186 | ipshell.set_exit_msg('Leaving interpreter - New exit_msg') |
|
1186 | ipshell.set_exit_msg('Leaving interpreter - New exit_msg') | |
1187 |
|
1187 | |||
1188 | def foo(m): |
|
1188 | def foo(m): | |
1189 | s = 'spam' |
|
1189 | s = 'spam' | |
1190 | ipshell('***In foo(). Try @whos, or print s or m:') |
|
1190 | ipshell('***In foo(). Try @whos, or print s or m:') | |
1191 | print 'foo says m = ',m |
|
1191 | print 'foo says m = ',m | |
1192 |
|
1192 | |||
1193 | def bar(n): |
|
1193 | def bar(n): | |
1194 | s = 'eggs' |
|
1194 | s = 'eggs' | |
1195 | ipshell('***In bar(). Try @whos, or print s or n:') |
|
1195 | ipshell('***In bar(). Try @whos, or print s or n:') | |
1196 | print 'bar says n = ',n |
|
1196 | print 'bar says n = ',n | |
1197 |
|
1197 | |||
1198 | # Some calls to the above functions which will trigger IPython: |
|
1198 | # Some calls to the above functions which will trigger IPython: | |
1199 | print 'Main program calling foo("eggs")\n' |
|
1199 | print 'Main program calling foo("eggs")\n' | |
1200 | foo('eggs') |
|
1200 | foo('eggs') | |
1201 |
|
1201 | |||
1202 | # The shell can be put in 'dummy' mode where calls to it silently return. This |
|
1202 | # The shell can be put in 'dummy' mode where calls to it silently return. This | |
1203 | # allows you, for example, to globally turn off debugging for a program with a |
|
1203 | # allows you, for example, to globally turn off debugging for a program with a | |
1204 | # single call. |
|
1204 | # single call. | |
1205 | ipshell.set_dummy_mode(1) |
|
1205 | ipshell.set_dummy_mode(1) | |
1206 | print '\nTrying to call IPython which is now "dummy":' |
|
1206 | print '\nTrying to call IPython which is now "dummy":' | |
1207 | ipshell() |
|
1207 | ipshell() | |
1208 | print 'Nothing happened...' |
|
1208 | print 'Nothing happened...' | |
1209 | # The global 'dummy' mode can still be overridden for a single call |
|
1209 | # The global 'dummy' mode can still be overridden for a single call | |
1210 | print '\nOverriding dummy mode manually:' |
|
1210 | print '\nOverriding dummy mode manually:' | |
1211 | ipshell(dummy=0) |
|
1211 | ipshell(dummy=0) | |
1212 |
|
1212 | |||
1213 | # Reactivate the IPython shell |
|
1213 | # Reactivate the IPython shell | |
1214 | ipshell.set_dummy_mode(0) |
|
1214 | ipshell.set_dummy_mode(0) | |
1215 |
|
1215 | |||
1216 | print 'You can even have multiple embedded instances:' |
|
1216 | print 'You can even have multiple embedded instances:' | |
1217 | ipshell2() |
|
1217 | ipshell2() | |
1218 |
|
1218 | |||
1219 | print '\nMain program calling bar("spam")\n' |
|
1219 | print '\nMain program calling bar("spam")\n' | |
1220 | bar('spam') |
|
1220 | bar('spam') | |
1221 |
|
1221 | |||
1222 | print 'Main program finished. Bye!' |
|
1222 | print 'Main program finished. Bye!' | |
1223 |
|
1223 | |||
1224 | #********************** End of file <example-embed.py> *********************** |
|
1224 | #********************** End of file <example-embed.py> *********************** | |
1225 |
|
1225 | |||
1226 | Once you understand how the system functions, you can use the following |
|
1226 | Once you understand how the system functions, you can use the following | |
1227 | code fragments in your programs which are ready for cut and paste:: |
|
1227 | code fragments in your programs which are ready for cut and paste:: | |
1228 |
|
1228 | |||
1229 |
|
1229 | |||
1230 | """Quick code snippets for embedding IPython into other programs. |
|
1230 | """Quick code snippets for embedding IPython into other programs. | |
1231 |
|
1231 | |||
1232 | See example-embed.py for full details, this file has the bare minimum code for |
|
1232 | See example-embed.py for full details, this file has the bare minimum code for | |
1233 | cut and paste use once you understand how to use the system.""" |
|
1233 | cut and paste use once you understand how to use the system.""" | |
1234 |
|
1234 | |||
1235 | #--------------------------------------------------------------------------- |
|
1235 | #--------------------------------------------------------------------------- | |
1236 | # This code loads IPython but modifies a few things if it detects it's running |
|
1236 | # This code loads IPython but modifies a few things if it detects it's running | |
1237 | # embedded in another IPython session (helps avoid confusion) |
|
1237 | # embedded in another IPython session (helps avoid confusion) | |
1238 |
|
1238 | |||
1239 | try: |
|
1239 | try: | |
1240 | __IPYTHON__ |
|
1240 | __IPYTHON__ | |
1241 | except NameError: |
|
1241 | except NameError: | |
1242 | argv = [''] |
|
1242 | argv = [''] | |
1243 | banner = exit_msg = '' |
|
1243 | banner = exit_msg = '' | |
1244 | else: |
|
1244 | else: | |
1245 | # Command-line options for IPython (a list like sys.argv) |
|
1245 | # Command-line options for IPython (a list like sys.argv) | |
1246 | argv = ['-pi1','In <\\#>:','-pi2',' .\\D.:','-po','Out<\\#>:'] |
|
1246 | argv = ['-pi1','In <\\#>:','-pi2',' .\\D.:','-po','Out<\\#>:'] | |
1247 | banner = '*** Nested interpreter ***' |
|
1247 | banner = '*** Nested interpreter ***' | |
1248 | exit_msg = '*** Back in main IPython ***' |
|
1248 | exit_msg = '*** Back in main IPython ***' | |
1249 |
|
1249 | |||
1250 | # First import the embeddable shell class |
|
1250 | # First import the embeddable shell class | |
1251 | from IPython.Shell import IPShellEmbed |
|
1251 | from IPython.Shell import IPShellEmbed | |
1252 | # Now create the IPython shell instance. Put ipshell() anywhere in your code |
|
1252 | # Now create the IPython shell instance. Put ipshell() anywhere in your code | |
1253 | # where you want it to open. |
|
1253 | # where you want it to open. | |
1254 | ipshell = IPShellEmbed(argv,banner=banner,exit_msg=exit_msg) |
|
1254 | ipshell = IPShellEmbed(argv,banner=banner,exit_msg=exit_msg) | |
1255 |
|
1255 | |||
1256 | #--------------------------------------------------------------------------- |
|
1256 | #--------------------------------------------------------------------------- | |
1257 | # This code will load an embeddable IPython shell always with no changes for |
|
1257 | # This code will load an embeddable IPython shell always with no changes for | |
1258 | # nested embededings. |
|
1258 | # nested embededings. | |
1259 |
|
1259 | |||
1260 | from IPython.Shell import IPShellEmbed |
|
1260 | from IPython.Shell import IPShellEmbed | |
1261 | ipshell = IPShellEmbed() |
|
1261 | ipshell = IPShellEmbed() | |
1262 | # Now ipshell() will open IPython anywhere in the code. |
|
1262 | # Now ipshell() will open IPython anywhere in the code. | |
1263 |
|
1263 | |||
1264 | #--------------------------------------------------------------------------- |
|
1264 | #--------------------------------------------------------------------------- | |
1265 | # This code loads an embeddable shell only if NOT running inside |
|
1265 | # This code loads an embeddable shell only if NOT running inside | |
1266 | # IPython. Inside IPython, the embeddable shell variable ipshell is just a |
|
1266 | # IPython. Inside IPython, the embeddable shell variable ipshell is just a | |
1267 | # dummy function. |
|
1267 | # dummy function. | |
1268 |
|
1268 | |||
1269 | try: |
|
1269 | try: | |
1270 | __IPYTHON__ |
|
1270 | __IPYTHON__ | |
1271 | except NameError: |
|
1271 | except NameError: | |
1272 | from IPython.Shell import IPShellEmbed |
|
1272 | from IPython.Shell import IPShellEmbed | |
1273 | ipshell = IPShellEmbed() |
|
1273 | ipshell = IPShellEmbed() | |
1274 | # Now ipshell() will open IPython anywhere in the code |
|
1274 | # Now ipshell() will open IPython anywhere in the code | |
1275 | else: |
|
1275 | else: | |
1276 | # Define a dummy ipshell() so the same code doesn't crash inside an |
|
1276 | # Define a dummy ipshell() so the same code doesn't crash inside an | |
1277 | # interactive IPython |
|
1277 | # interactive IPython | |
1278 | def ipshell(): pass |
|
1278 | def ipshell(): pass | |
1279 |
|
1279 | |||
1280 | #******************* End of file <example-embed-short.py> ******************** |
|
1280 | #******************* End of file <example-embed-short.py> ******************** | |
1281 |
|
1281 | |||
1282 | Using the Python debugger (pdb) |
|
1282 | Using the Python debugger (pdb) | |
1283 | =============================== |
|
1283 | =============================== | |
1284 |
|
1284 | |||
1285 | Running entire programs via pdb |
|
1285 | Running entire programs via pdb | |
1286 | ------------------------------- |
|
1286 | ------------------------------- | |
1287 |
|
1287 | |||
1288 | pdb, the Python debugger, is a powerful interactive debugger which |
|
1288 | pdb, the Python debugger, is a powerful interactive debugger which | |
1289 | allows you to step through code, set breakpoints, watch variables, |
|
1289 | allows you to step through code, set breakpoints, watch variables, | |
1290 | etc. IPython makes it very easy to start any script under the control |
|
1290 | etc. IPython makes it very easy to start any script under the control | |
1291 | of pdb, regardless of whether you have wrapped it into a 'main()' |
|
1291 | of pdb, regardless of whether you have wrapped it into a 'main()' | |
1292 | function or not. For this, simply type '%run -d myscript' at an |
|
1292 | function or not. For this, simply type '%run -d myscript' at an | |
1293 | IPython prompt. See the %run command's documentation (via '%run?' or |
|
1293 | IPython prompt. See the %run command's documentation (via '%run?' or | |
1294 | in Sec. magic_ for more details, including how to control where pdb |
|
1294 | in Sec. magic_ for more details, including how to control where pdb | |
1295 | will stop execution first. |
|
1295 | will stop execution first. | |
1296 |
|
1296 | |||
1297 | For more information on the use of the pdb debugger, read the included |
|
1297 | For more information on the use of the pdb debugger, read the included | |
1298 | pdb.doc file (part of the standard Python distribution). On a stock |
|
1298 | pdb.doc file (part of the standard Python distribution). On a stock | |
1299 | Linux system it is located at /usr/lib/python2.3/pdb.doc, but the |
|
1299 | Linux system it is located at /usr/lib/python2.3/pdb.doc, but the | |
1300 | easiest way to read it is by using the help() function of the pdb module |
|
1300 | easiest way to read it is by using the help() function of the pdb module | |
1301 | as follows (in an IPython prompt): |
|
1301 | as follows (in an IPython prompt): | |
1302 |
|
1302 | |||
1303 | In [1]: import pdb |
|
1303 | In [1]: import pdb | |
1304 | In [2]: pdb.help() |
|
1304 | In [2]: pdb.help() | |
1305 |
|
1305 | |||
1306 | This will load the pdb.doc document in a file viewer for you automatically. |
|
1306 | This will load the pdb.doc document in a file viewer for you automatically. | |
1307 |
|
1307 | |||
1308 |
|
1308 | |||
1309 | Automatic invocation of pdb on exceptions |
|
1309 | Automatic invocation of pdb on exceptions | |
1310 | ----------------------------------------- |
|
1310 | ----------------------------------------- | |
1311 |
|
1311 | |||
1312 | IPython, if started with the -pdb option (or if the option is set in |
|
1312 | IPython, if started with the -pdb option (or if the option is set in | |
1313 | your rc file) can call the Python pdb debugger every time your code |
|
1313 | your rc file) can call the Python pdb debugger every time your code | |
1314 | triggers an uncaught exception. This feature |
|
1314 | triggers an uncaught exception. This feature | |
1315 | can also be toggled at any time with the %pdb magic command. This can be |
|
1315 | can also be toggled at any time with the %pdb magic command. This can be | |
1316 | extremely useful in order to find the origin of subtle bugs, because pdb |
|
1316 | extremely useful in order to find the origin of subtle bugs, because pdb | |
1317 | opens up at the point in your code which triggered the exception, and |
|
1317 | opens up at the point in your code which triggered the exception, and | |
1318 | while your program is at this point 'dead', all the data is still |
|
1318 | while your program is at this point 'dead', all the data is still | |
1319 | available and you can walk up and down the stack frame and understand |
|
1319 | available and you can walk up and down the stack frame and understand | |
1320 | the origin of the problem. |
|
1320 | the origin of the problem. | |
1321 |
|
1321 | |||
1322 | Furthermore, you can use these debugging facilities both with the |
|
1322 | Furthermore, you can use these debugging facilities both with the | |
1323 | embedded IPython mode and without IPython at all. For an embedded shell |
|
1323 | embedded IPython mode and without IPython at all. For an embedded shell | |
1324 | (see sec. Embedding_), simply call the constructor with |
|
1324 | (see sec. Embedding_), simply call the constructor with | |
1325 | '-pdb' in the argument string and automatically pdb will be called if an |
|
1325 | '-pdb' in the argument string and automatically pdb will be called if an | |
1326 | uncaught exception is triggered by your code. |
|
1326 | uncaught exception is triggered by your code. | |
1327 |
|
1327 | |||
1328 | For stand-alone use of the feature in your programs which do not use |
|
1328 | For stand-alone use of the feature in your programs which do not use | |
1329 | IPython at all, put the following lines toward the top of your 'main' |
|
1329 | IPython at all, put the following lines toward the top of your 'main' | |
1330 | routine:: |
|
1330 | routine:: | |
1331 |
|
1331 | |||
1332 |
import sys |
|
1332 | import sys | |
1333 | sys.excepthook = IPython.ultraTB.FormattedTB(mode='Verbose', |
|
1333 | from IPython.core import ultratb | |
|
1334 | sys.excepthook = ultratb.FormattedTB(mode='Verbose', | |||
1334 | color_scheme='Linux', call_pdb=1) |
|
1335 | color_scheme='Linux', call_pdb=1) | |
1335 |
|
1336 | |||
1336 | The mode keyword can be either 'Verbose' or 'Plain', giving either very |
|
1337 | The mode keyword can be either 'Verbose' or 'Plain', giving either very | |
1337 | detailed or normal tracebacks respectively. The color_scheme keyword can |
|
1338 | detailed or normal tracebacks respectively. The color_scheme keyword can | |
1338 | be one of 'NoColor', 'Linux' (default) or 'LightBG'. These are the same |
|
1339 | be one of 'NoColor', 'Linux' (default) or 'LightBG'. These are the same | |
1339 | options which can be set in IPython with -colors and -xmode. |
|
1340 | options which can be set in IPython with -colors and -xmode. | |
1340 |
|
1341 | |||
1341 | This will give any of your programs detailed, colored tracebacks with |
|
1342 | This will give any of your programs detailed, colored tracebacks with | |
1342 | automatic invocation of pdb. |
|
1343 | automatic invocation of pdb. | |
1343 |
|
1344 | |||
1344 |
|
1345 | |||
1345 | Extensions for syntax processing |
|
1346 | Extensions for syntax processing | |
1346 | ================================ |
|
1347 | ================================ | |
1347 |
|
1348 | |||
1348 | This isn't for the faint of heart, because the potential for breaking |
|
1349 | This isn't for the faint of heart, because the potential for breaking | |
1349 | things is quite high. But it can be a very powerful and useful feature. |
|
1350 | things is quite high. But it can be a very powerful and useful feature. | |
1350 | In a nutshell, you can redefine the way IPython processes the user input |
|
1351 | In a nutshell, you can redefine the way IPython processes the user input | |
1351 | line to accept new, special extensions to the syntax without needing to |
|
1352 | line to accept new, special extensions to the syntax without needing to | |
1352 | change any of IPython's own code. |
|
1353 | change any of IPython's own code. | |
1353 |
|
1354 | |||
1354 | In the IPython/extensions directory you will find some examples |
|
1355 | In the IPython/extensions directory you will find some examples | |
1355 | supplied, which we will briefly describe now. These can be used 'as is' |
|
1356 | supplied, which we will briefly describe now. These can be used 'as is' | |
1356 | (and both provide very useful functionality), or you can use them as a |
|
1357 | (and both provide very useful functionality), or you can use them as a | |
1357 | starting point for writing your own extensions. |
|
1358 | starting point for writing your own extensions. | |
1358 |
|
1359 | |||
1359 |
|
1360 | |||
1360 | Pasting of code starting with '>>> ' or '... ' |
|
1361 | Pasting of code starting with '>>> ' or '... ' | |
1361 | ---------------------------------------------- |
|
1362 | ---------------------------------------------- | |
1362 |
|
1363 | |||
1363 | In the python tutorial it is common to find code examples which have |
|
1364 | In the python tutorial it is common to find code examples which have | |
1364 | been taken from real python sessions. The problem with those is that all |
|
1365 | been taken from real python sessions. The problem with those is that all | |
1365 | the lines begin with either '>>> ' or '... ', which makes it impossible |
|
1366 | the lines begin with either '>>> ' or '... ', which makes it impossible | |
1366 | to paste them all at once. One must instead do a line by line manual |
|
1367 | to paste them all at once. One must instead do a line by line manual | |
1367 | copying, carefully removing the leading extraneous characters. |
|
1368 | copying, carefully removing the leading extraneous characters. | |
1368 |
|
1369 | |||
1369 | This extension identifies those starting characters and removes them |
|
1370 | This extension identifies those starting characters and removes them | |
1370 | from the input automatically, so that one can paste multi-line examples |
|
1371 | from the input automatically, so that one can paste multi-line examples | |
1371 | directly into IPython, saving a lot of time. Please look at the file |
|
1372 | directly into IPython, saving a lot of time. Please look at the file | |
1372 | InterpreterPasteInput.py in the IPython/extensions directory for details |
|
1373 | InterpreterPasteInput.py in the IPython/extensions directory for details | |
1373 | on how this is done. |
|
1374 | on how this is done. | |
1374 |
|
1375 | |||
1375 | IPython comes with a special profile enabling this feature, called |
|
1376 | IPython comes with a special profile enabling this feature, called | |
1376 | tutorial. Simply start IPython via 'ipython -p tutorial' and the feature |
|
1377 | tutorial. Simply start IPython via 'ipython -p tutorial' and the feature | |
1377 | will be available. In a normal IPython session you can activate the |
|
1378 | will be available. In a normal IPython session you can activate the | |
1378 | feature by importing the corresponding module with: |
|
1379 | feature by importing the corresponding module with: | |
1379 | In [1]: import IPython.extensions.InterpreterPasteInput |
|
1380 | In [1]: import IPython.extensions.InterpreterPasteInput | |
1380 |
|
1381 | |||
1381 | The following is a 'screenshot' of how things work when this extension |
|
1382 | The following is a 'screenshot' of how things work when this extension | |
1382 | is on, copying an example from the standard tutorial:: |
|
1383 | is on, copying an example from the standard tutorial:: | |
1383 |
|
1384 | |||
1384 | IPython profile: tutorial |
|
1385 | IPython profile: tutorial | |
1385 |
|
1386 | |||
1386 | *** Pasting of code with ">>>" or "..." has been enabled. |
|
1387 | *** Pasting of code with ">>>" or "..." has been enabled. | |
1387 |
|
1388 | |||
1388 | In [1]: >>> def fib2(n): # return Fibonacci series up to n |
|
1389 | In [1]: >>> def fib2(n): # return Fibonacci series up to n | |
1389 | ...: ... """Return a list containing the Fibonacci series up to |
|
1390 | ...: ... """Return a list containing the Fibonacci series up to | |
1390 | n.""" |
|
1391 | n.""" | |
1391 | ...: ... result = [] |
|
1392 | ...: ... result = [] | |
1392 | ...: ... a, b = 0, 1 |
|
1393 | ...: ... a, b = 0, 1 | |
1393 | ...: ... while b < n: |
|
1394 | ...: ... while b < n: | |
1394 | ...: ... result.append(b) # see below |
|
1395 | ...: ... result.append(b) # see below | |
1395 | ...: ... a, b = b, a+b |
|
1396 | ...: ... a, b = b, a+b | |
1396 | ...: ... return result |
|
1397 | ...: ... return result | |
1397 | ...: |
|
1398 | ...: | |
1398 |
|
1399 | |||
1399 | In [2]: fib2(10) |
|
1400 | In [2]: fib2(10) | |
1400 | Out[2]: [1, 1, 2, 3, 5, 8] |
|
1401 | Out[2]: [1, 1, 2, 3, 5, 8] | |
1401 |
|
1402 | |||
1402 | Note that as currently written, this extension does not recognize |
|
1403 | Note that as currently written, this extension does not recognize | |
1403 | IPython's prompts for pasting. Those are more complicated, since the |
|
1404 | IPython's prompts for pasting. Those are more complicated, since the | |
1404 | user can change them very easily, they involve numbers and can vary in |
|
1405 | user can change them very easily, they involve numbers and can vary in | |
1405 | length. One could however extract all the relevant information from the |
|
1406 | length. One could however extract all the relevant information from the | |
1406 | IPython instance and build an appropriate regular expression. This is |
|
1407 | IPython instance and build an appropriate regular expression. This is | |
1407 | left as an exercise for the reader. |
|
1408 | left as an exercise for the reader. | |
1408 |
|
1409 | |||
1409 |
|
1410 | |||
1410 | Input of physical quantities with units |
|
1411 | Input of physical quantities with units | |
1411 | --------------------------------------- |
|
1412 | --------------------------------------- | |
1412 |
|
1413 | |||
1413 | The module PhysicalQInput allows a simplified form of input for physical |
|
1414 | The module PhysicalQInput allows a simplified form of input for physical | |
1414 | quantities with units. This file is meant to be used in conjunction with |
|
1415 | quantities with units. This file is meant to be used in conjunction with | |
1415 | the PhysicalQInteractive module (in the same directory) and |
|
1416 | the PhysicalQInteractive module (in the same directory) and | |
1416 | Physics.PhysicalQuantities from Konrad Hinsen's ScientificPython |
|
1417 | Physics.PhysicalQuantities from Konrad Hinsen's ScientificPython | |
1417 | (http://dirac.cnrs-orleans.fr/ScientificPython/). |
|
1418 | (http://dirac.cnrs-orleans.fr/ScientificPython/). | |
1418 |
|
1419 | |||
1419 | The Physics.PhysicalQuantities module defines PhysicalQuantity objects, |
|
1420 | The Physics.PhysicalQuantities module defines PhysicalQuantity objects, | |
1420 | but these must be declared as instances of a class. For example, to |
|
1421 | but these must be declared as instances of a class. For example, to | |
1421 | define v as a velocity of 3 m/s, normally you would write:: |
|
1422 | define v as a velocity of 3 m/s, normally you would write:: | |
1422 |
|
1423 | |||
1423 | In [1]: v = PhysicalQuantity(3,'m/s') |
|
1424 | In [1]: v = PhysicalQuantity(3,'m/s') | |
1424 |
|
1425 | |||
1425 | Using the PhysicalQ_Input extension this can be input instead as: |
|
1426 | Using the PhysicalQ_Input extension this can be input instead as: | |
1426 | In [1]: v = 3 m/s |
|
1427 | In [1]: v = 3 m/s | |
1427 | which is much more convenient for interactive use (even though it is |
|
1428 | which is much more convenient for interactive use (even though it is | |
1428 | blatantly invalid Python syntax). |
|
1429 | blatantly invalid Python syntax). | |
1429 |
|
1430 | |||
1430 | The physics profile supplied with IPython (enabled via 'ipython -p |
|
1431 | The physics profile supplied with IPython (enabled via 'ipython -p | |
1431 | physics') uses these extensions, which you can also activate with: |
|
1432 | physics') uses these extensions, which you can also activate with: | |
1432 |
|
1433 | |||
1433 | from math import * # math MUST be imported BEFORE PhysicalQInteractive |
|
1434 | from math import * # math MUST be imported BEFORE PhysicalQInteractive | |
1434 | from IPython.extensions.PhysicalQInteractive import * |
|
1435 | from IPython.extensions.PhysicalQInteractive import * | |
1435 | import IPython.extensions.PhysicalQInput |
|
1436 | import IPython.extensions.PhysicalQInput | |
1436 |
|
1437 | |||
1437 |
|
1438 | |||
1438 | Threading support |
|
1439 | Threading support | |
1439 | ================= |
|
1440 | ================= | |
1440 |
|
1441 | |||
1441 | WARNING: The threading support is still somewhat experimental, and it |
|
1442 | WARNING: The threading support is still somewhat experimental, and it | |
1442 | has only seen reasonable testing under Linux. Threaded code is |
|
1443 | has only seen reasonable testing under Linux. Threaded code is | |
1443 | particularly tricky to debug, and it tends to show extremely |
|
1444 | particularly tricky to debug, and it tends to show extremely | |
1444 | platform-dependent behavior. Since I only have access to Linux machines, |
|
1445 | platform-dependent behavior. Since I only have access to Linux machines, | |
1445 | I will have to rely on user's experiences and assistance for this area |
|
1446 | I will have to rely on user's experiences and assistance for this area | |
1446 | of IPython to improve under other platforms. |
|
1447 | of IPython to improve under other platforms. | |
1447 |
|
1448 | |||
1448 | IPython, via the -gthread , -qthread, -q4thread and -wthread options |
|
1449 | IPython, via the -gthread , -qthread, -q4thread and -wthread options | |
1449 | (described in Sec. `Threading options`_), can run in |
|
1450 | (described in Sec. `Threading options`_), can run in | |
1450 | multithreaded mode to support pyGTK, Qt3, Qt4 and WXPython applications |
|
1451 | multithreaded mode to support pyGTK, Qt3, Qt4 and WXPython applications | |
1451 | respectively. These GUI toolkits need to control the python main loop of |
|
1452 | respectively. These GUI toolkits need to control the python main loop of | |
1452 | execution, so under a normal Python interpreter, starting a pyGTK, Qt3, |
|
1453 | execution, so under a normal Python interpreter, starting a pyGTK, Qt3, | |
1453 | Qt4 or WXPython application will immediately freeze the shell. |
|
1454 | Qt4 or WXPython application will immediately freeze the shell. | |
1454 |
|
1455 | |||
1455 | IPython, with one of these options (you can only use one at a time), |
|
1456 | IPython, with one of these options (you can only use one at a time), | |
1456 | separates the graphical loop and IPython's code execution run into |
|
1457 | separates the graphical loop and IPython's code execution run into | |
1457 | different threads. This allows you to test interactively (with %run, for |
|
1458 | different threads. This allows you to test interactively (with %run, for | |
1458 | example) your GUI code without blocking. |
|
1459 | example) your GUI code without blocking. | |
1459 |
|
1460 | |||
1460 | A nice mini-tutorial on using IPython along with the Qt Designer |
|
1461 | A nice mini-tutorial on using IPython along with the Qt Designer | |
1461 | application is available at the SciPy wiki: |
|
1462 | application is available at the SciPy wiki: | |
1462 | http://www.scipy.org/Cookbook/Matplotlib/Qt_with_IPython_and_Designer. |
|
1463 | http://www.scipy.org/Cookbook/Matplotlib/Qt_with_IPython_and_Designer. | |
1463 |
|
1464 | |||
1464 |
|
1465 | |||
1465 | Tk issues |
|
1466 | Tk issues | |
1466 | --------- |
|
1467 | --------- | |
1467 |
|
1468 | |||
1468 | As indicated in Sec. `Threading options`_, a special -tk option is |
|
1469 | As indicated in Sec. `Threading options`_, a special -tk option is | |
1469 | provided to try and allow Tk graphical applications to coexist |
|
1470 | provided to try and allow Tk graphical applications to coexist | |
1470 | interactively with WX, Qt or GTK ones. Whether this works at all, |
|
1471 | interactively with WX, Qt or GTK ones. Whether this works at all, | |
1471 | however, is very platform and configuration dependent. Please |
|
1472 | however, is very platform and configuration dependent. Please | |
1472 | experiment with simple test cases before committing to using this |
|
1473 | experiment with simple test cases before committing to using this | |
1473 | combination of Tk and GTK/Qt/WX threading in a production environment. |
|
1474 | combination of Tk and GTK/Qt/WX threading in a production environment. | |
1474 |
|
1475 | |||
1475 |
|
1476 | |||
1476 | I/O pitfalls |
|
1477 | I/O pitfalls | |
1477 | ------------ |
|
1478 | ------------ | |
1478 |
|
1479 | |||
1479 | Be mindful that the Python interpreter switches between threads every |
|
1480 | Be mindful that the Python interpreter switches between threads every | |
1480 | $N$ bytecodes, where the default value as of Python 2.3 is $N=100.$ This |
|
1481 | $N$ bytecodes, where the default value as of Python 2.3 is $N=100.$ This | |
1481 | value can be read by using the sys.getcheckinterval() function, and it |
|
1482 | value can be read by using the sys.getcheckinterval() function, and it | |
1482 | can be reset via sys.setcheckinterval(N). This switching of threads can |
|
1483 | can be reset via sys.setcheckinterval(N). This switching of threads can | |
1483 | cause subtly confusing effects if one of your threads is doing file I/O. |
|
1484 | cause subtly confusing effects if one of your threads is doing file I/O. | |
1484 | In text mode, most systems only flush file buffers when they encounter a |
|
1485 | In text mode, most systems only flush file buffers when they encounter a | |
1485 | '\n'. An instruction as simple as:: |
|
1486 | '\n'. An instruction as simple as:: | |
1486 |
|
1487 | |||
1487 | print >> filehandle, ''hello world'' |
|
1488 | print >> filehandle, ''hello world'' | |
1488 |
|
1489 | |||
1489 | actually consists of several bytecodes, so it is possible that the |
|
1490 | actually consists of several bytecodes, so it is possible that the | |
1490 | newline does not reach your file before the next thread switch. |
|
1491 | newline does not reach your file before the next thread switch. | |
1491 | Similarly, if you are writing to a file in binary mode, the file won't |
|
1492 | Similarly, if you are writing to a file in binary mode, the file won't | |
1492 | be flushed until the buffer fills, and your other thread may see |
|
1493 | be flushed until the buffer fills, and your other thread may see | |
1493 | apparently truncated files. |
|
1494 | apparently truncated files. | |
1494 |
|
1495 | |||
1495 | For this reason, if you are using IPython's thread support and have (for |
|
1496 | For this reason, if you are using IPython's thread support and have (for | |
1496 | example) a GUI application which will read data generated by files |
|
1497 | example) a GUI application which will read data generated by files | |
1497 | written to from the IPython thread, the safest approach is to open all |
|
1498 | written to from the IPython thread, the safest approach is to open all | |
1498 | of your files in unbuffered mode (the third argument to the file/open |
|
1499 | of your files in unbuffered mode (the third argument to the file/open | |
1499 | function is the buffering value):: |
|
1500 | function is the buffering value):: | |
1500 |
|
1501 | |||
1501 | filehandle = open(filename,mode,0) |
|
1502 | filehandle = open(filename,mode,0) | |
1502 |
|
1503 | |||
1503 | This is obviously a brute force way of avoiding race conditions with the |
|
1504 | This is obviously a brute force way of avoiding race conditions with the | |
1504 | file buffering. If you want to do it cleanly, and you have a resource |
|
1505 | file buffering. If you want to do it cleanly, and you have a resource | |
1505 | which is being shared by the interactive IPython loop and your GUI |
|
1506 | which is being shared by the interactive IPython loop and your GUI | |
1506 | thread, you should really handle it with thread locking and |
|
1507 | thread, you should really handle it with thread locking and | |
1507 | syncrhonization properties. The Python documentation discusses these. |
|
1508 | syncrhonization properties. The Python documentation discusses these. | |
1508 |
|
1509 | |||
1509 | .. _interactive_demos: |
|
1510 | .. _interactive_demos: | |
1510 |
|
1511 | |||
1511 | Interactive demos with IPython |
|
1512 | Interactive demos with IPython | |
1512 | ============================== |
|
1513 | ============================== | |
1513 |
|
1514 | |||
1514 | IPython ships with a basic system for running scripts interactively in |
|
1515 | IPython ships with a basic system for running scripts interactively in | |
1515 | sections, useful when presenting code to audiences. A few tags embedded |
|
1516 | sections, useful when presenting code to audiences. A few tags embedded | |
1516 | in comments (so that the script remains valid Python code) divide a file |
|
1517 | in comments (so that the script remains valid Python code) divide a file | |
1517 | into separate blocks, and the demo can be run one block at a time, with |
|
1518 | into separate blocks, and the demo can be run one block at a time, with | |
1518 | IPython printing (with syntax highlighting) the block before executing |
|
1519 | IPython printing (with syntax highlighting) the block before executing | |
1519 | it, and returning to the interactive prompt after each block. The |
|
1520 | it, and returning to the interactive prompt after each block. The | |
1520 | interactive namespace is updated after each block is run with the |
|
1521 | interactive namespace is updated after each block is run with the | |
1521 | contents of the demo's namespace. |
|
1522 | contents of the demo's namespace. | |
1522 |
|
1523 | |||
1523 | This allows you to show a piece of code, run it and then execute |
|
1524 | This allows you to show a piece of code, run it and then execute | |
1524 | interactively commands based on the variables just created. Once you |
|
1525 | interactively commands based on the variables just created. Once you | |
1525 | want to continue, you simply execute the next block of the demo. The |
|
1526 | want to continue, you simply execute the next block of the demo. The | |
1526 | following listing shows the markup necessary for dividing a script into |
|
1527 | following listing shows the markup necessary for dividing a script into | |
1527 | sections for execution as a demo:: |
|
1528 | sections for execution as a demo:: | |
1528 |
|
1529 | |||
1529 |
|
1530 | |||
1530 | """A simple interactive demo to illustrate the use of IPython's Demo class. |
|
1531 | """A simple interactive demo to illustrate the use of IPython's Demo class. | |
1531 |
|
1532 | |||
1532 | Any python script can be run as a demo, but that does little more than showing |
|
1533 | Any python script can be run as a demo, but that does little more than showing | |
1533 | it on-screen, syntax-highlighted in one shot. If you add a little simple |
|
1534 | it on-screen, syntax-highlighted in one shot. If you add a little simple | |
1534 | markup, you can stop at specified intervals and return to the ipython prompt, |
|
1535 | markup, you can stop at specified intervals and return to the ipython prompt, | |
1535 | resuming execution later. |
|
1536 | resuming execution later. | |
1536 | """ |
|
1537 | """ | |
1537 |
|
1538 | |||
1538 | print 'Hello, welcome to an interactive IPython demo.' |
|
1539 | print 'Hello, welcome to an interactive IPython demo.' | |
1539 | print 'Executing this block should require confirmation before proceeding,' |
|
1540 | print 'Executing this block should require confirmation before proceeding,' | |
1540 | print 'unless auto_all has been set to true in the demo object' |
|
1541 | print 'unless auto_all has been set to true in the demo object' | |
1541 |
|
1542 | |||
1542 | # The mark below defines a block boundary, which is a point where IPython will |
|
1543 | # The mark below defines a block boundary, which is a point where IPython will | |
1543 | # stop execution and return to the interactive prompt. |
|
1544 | # stop execution and return to the interactive prompt. | |
1544 | # Note that in actual interactive execution, |
|
1545 | # Note that in actual interactive execution, | |
1545 | # <demo> --- stop --- |
|
1546 | # <demo> --- stop --- | |
1546 |
|
1547 | |||
1547 | x = 1 |
|
1548 | x = 1 | |
1548 | y = 2 |
|
1549 | y = 2 | |
1549 |
|
1550 | |||
1550 | # <demo> --- stop --- |
|
1551 | # <demo> --- stop --- | |
1551 |
|
1552 | |||
1552 | # the mark below makes this block as silent |
|
1553 | # the mark below makes this block as silent | |
1553 | # <demo> silent |
|
1554 | # <demo> silent | |
1554 |
|
1555 | |||
1555 | print 'This is a silent block, which gets executed but not printed.' |
|
1556 | print 'This is a silent block, which gets executed but not printed.' | |
1556 |
|
1557 | |||
1557 | # <demo> --- stop --- |
|
1558 | # <demo> --- stop --- | |
1558 | # <demo> auto |
|
1559 | # <demo> auto | |
1559 | print 'This is an automatic block.' |
|
1560 | print 'This is an automatic block.' | |
1560 | print 'It is executed without asking for confirmation, but printed.' |
|
1561 | print 'It is executed without asking for confirmation, but printed.' | |
1561 | z = x+y |
|
1562 | z = x+y | |
1562 |
|
1563 | |||
1563 | print 'z=',x |
|
1564 | print 'z=',x | |
1564 |
|
1565 | |||
1565 | # <demo> --- stop --- |
|
1566 | # <demo> --- stop --- | |
1566 | # This is just another normal block. |
|
1567 | # This is just another normal block. | |
1567 | print 'z is now:', z |
|
1568 | print 'z is now:', z | |
1568 |
|
1569 | |||
1569 | print 'bye!' |
|
1570 | print 'bye!' | |
1570 |
|
1571 | |||
1571 | In order to run a file as a demo, you must first make a Demo object out |
|
1572 | In order to run a file as a demo, you must first make a Demo object out | |
1572 | of it. If the file is named myscript.py, the following code will make a |
|
1573 | of it. If the file is named myscript.py, the following code will make a | |
1573 | demo:: |
|
1574 | demo:: | |
1574 |
|
1575 | |||
1575 | from IPython.demo import Demo |
|
1576 | from IPython.demo import Demo | |
1576 |
|
1577 | |||
1577 | mydemo = Demo('myscript.py') |
|
1578 | mydemo = Demo('myscript.py') | |
1578 |
|
1579 | |||
1579 | This creates the mydemo object, whose blocks you run one at a time by |
|
1580 | This creates the mydemo object, whose blocks you run one at a time by | |
1580 | simply calling the object with no arguments. If you have autocall active |
|
1581 | simply calling the object with no arguments. If you have autocall active | |
1581 | in IPython (the default), all you need to do is type:: |
|
1582 | in IPython (the default), all you need to do is type:: | |
1582 |
|
1583 | |||
1583 | mydemo |
|
1584 | mydemo | |
1584 |
|
1585 | |||
1585 | and IPython will call it, executing each block. Demo objects can be |
|
1586 | and IPython will call it, executing each block. Demo objects can be | |
1586 | restarted, you can move forward or back skipping blocks, re-execute the |
|
1587 | restarted, you can move forward or back skipping blocks, re-execute the | |
1587 | last block, etc. Simply use the Tab key on a demo object to see its |
|
1588 | last block, etc. Simply use the Tab key on a demo object to see its | |
1588 | methods, and call '?' on them to see their docstrings for more usage |
|
1589 | methods, and call '?' on them to see their docstrings for more usage | |
1589 | details. In addition, the demo module itself contains a comprehensive |
|
1590 | details. In addition, the demo module itself contains a comprehensive | |
1590 | docstring, which you can access via:: |
|
1591 | docstring, which you can access via:: | |
1591 |
|
1592 | |||
1592 | from IPython import demo |
|
1593 | from IPython import demo | |
1593 |
|
1594 | |||
1594 | demo? |
|
1595 | demo? | |
1595 |
|
1596 | |||
1596 | Limitations: It is important to note that these demos are limited to |
|
1597 | Limitations: It is important to note that these demos are limited to | |
1597 | fairly simple uses. In particular, you can not put division marks in |
|
1598 | fairly simple uses. In particular, you can not put division marks in | |
1598 | indented code (loops, if statements, function definitions, etc.) |
|
1599 | indented code (loops, if statements, function definitions, etc.) | |
1599 | Supporting something like this would basically require tracking the |
|
1600 | Supporting something like this would basically require tracking the | |
1600 | internal execution state of the Python interpreter, so only top-level |
|
1601 | internal execution state of the Python interpreter, so only top-level | |
1601 | divisions are allowed. If you want to be able to open an IPython |
|
1602 | divisions are allowed. If you want to be able to open an IPython | |
1602 | instance at an arbitrary point in a program, you can use IPython's |
|
1603 | instance at an arbitrary point in a program, you can use IPython's | |
1603 | embedding facilities, described in detail in Sec. 9 |
|
1604 | embedding facilities, described in detail in Sec. 9 | |
1604 |
|
1605 | |||
1605 |
|
1606 | |||
1606 | .. _Matplotlib support: |
|
1607 | .. _Matplotlib support: | |
1607 |
|
1608 | |||
1608 | Plotting with matplotlib |
|
1609 | Plotting with matplotlib | |
1609 | ======================== |
|
1610 | ======================== | |
1610 |
|
1611 | |||
1611 | The matplotlib library (http://matplotlib.sourceforge.net |
|
1612 | The matplotlib library (http://matplotlib.sourceforge.net | |
1612 | http://matplotlib.sourceforge.net) provides high quality 2D plotting for |
|
1613 | http://matplotlib.sourceforge.net) provides high quality 2D plotting for | |
1613 | Python. Matplotlib can produce plots on screen using a variety of GUI |
|
1614 | Python. Matplotlib can produce plots on screen using a variety of GUI | |
1614 | toolkits, including Tk, GTK and WXPython. It also provides a number of |
|
1615 | toolkits, including Tk, GTK and WXPython. It also provides a number of | |
1615 | commands useful for scientific computing, all with a syntax compatible |
|
1616 | commands useful for scientific computing, all with a syntax compatible | |
1616 | with that of the popular Matlab program. |
|
1617 | with that of the popular Matlab program. | |
1617 |
|
1618 | |||
1618 | IPython accepts the special option -pylab (see :ref:`here |
|
1619 | IPython accepts the special option -pylab (see :ref:`here | |
1619 | <command_line_options>`). This configures it to support matplotlib, honoring |
|
1620 | <command_line_options>`). This configures it to support matplotlib, honoring | |
1620 | the settings in the .matplotlibrc file. IPython will detect the user's choice |
|
1621 | the settings in the .matplotlibrc file. IPython will detect the user's choice | |
1621 | of matplotlib GUI backend, and automatically select the proper threading model |
|
1622 | of matplotlib GUI backend, and automatically select the proper threading model | |
1622 | to prevent blocking. It also sets matplotlib in interactive mode and modifies |
|
1623 | to prevent blocking. It also sets matplotlib in interactive mode and modifies | |
1623 | %run slightly, so that any matplotlib-based script can be executed using %run |
|
1624 | %run slightly, so that any matplotlib-based script can be executed using %run | |
1624 | and the final show() command does not block the interactive shell. |
|
1625 | and the final show() command does not block the interactive shell. | |
1625 |
|
1626 | |||
1626 | The -pylab option must be given first in order for IPython to configure its |
|
1627 | The -pylab option must be given first in order for IPython to configure its | |
1627 | threading mode. However, you can still issue other options afterwards. This |
|
1628 | threading mode. However, you can still issue other options afterwards. This | |
1628 | allows you to have a matplotlib-based environment customized with additional |
|
1629 | allows you to have a matplotlib-based environment customized with additional | |
1629 | modules using the standard IPython profile mechanism (see :ref:`here |
|
1630 | modules using the standard IPython profile mechanism (see :ref:`here | |
1630 | <profiles>`): ``ipython -pylab -p myprofile`` will load the profile defined in |
|
1631 | <profiles>`): ``ipython -pylab -p myprofile`` will load the profile defined in | |
1631 | ipythonrc-myprofile after configuring matplotlib. |
|
1632 | ipythonrc-myprofile after configuring matplotlib. |
General Comments 0
You need to be logged in to leave comments.
Login now