##// END OF EJS Templates
Fix invalid syntax for Python 2.4 in a test,...
Fernando Perez -
Show More
@@ -1,295 +1,302 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.platutils import find_cmd, get_long_path_name
13 from IPython.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 yield (nt.assert_true, 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 nt.assert_true('.' not in key)
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 yield (nt.assert_true, 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 -n -f $tfile 3
45 In [11]: %hist -n -f $tfile 3
46 """
46 """
47
47
48
48
49 def doctest_hist_r():
49 def doctest_hist_r():
50 """Test %hist -r
50 """Test %hist -r
51
51
52 XXX - This test is not recording the output correctly. Not sure why...
52 XXX - This test is not recording the output correctly. Not sure why...
53
53
54 In [20]: 'hist' in _ip.IP.lsmagic()
54 In [20]: 'hist' in _ip.IP.lsmagic()
55 Out[20]: True
55 Out[20]: True
56
56
57 In [6]: x=1
57 In [6]: x=1
58
58
59 In [7]: %hist -n -r 2
59 In [7]: %hist -n -r 2
60 x=1 # random
60 x=1 # random
61 hist -n -r 2 # random
61 hist -n -r 2 # random
62 """
62 """
63
63
64 # This test is known to fail on win32.
64 # This test is known to fail on win32.
65 # See ticket https://bugs.launchpad.net/bugs/366334
65 # See ticket https://bugs.launchpad.net/bugs/366334
66 def test_obj_del():
66 def test_obj_del():
67 """Test that object's __del__ methods are called on exit."""
67 """Test that object's __del__ methods are called on exit."""
68 test_dir = os.path.dirname(__file__)
68 test_dir = os.path.dirname(__file__)
69 del_file = os.path.join(test_dir,'obj_del.py')
69 del_file = os.path.join(test_dir,'obj_del.py')
70 ipython_cmd = find_cmd('ipython')
70 ipython_cmd = find_cmd('ipython')
71 out = _ip.IP.getoutput('%s %s' % (ipython_cmd, del_file))
71 out = _ip.IP.getoutput('%s %s' % (ipython_cmd, del_file))
72 nt.assert_equals(out,'obj_del.py: object A deleted')
72 nt.assert_equals(out,'obj_del.py: object A deleted')
73
73
74
74
75 def test_shist():
75 def test_shist():
76 # Simple tests of ShadowHist class - test generator.
76 # Simple tests of ShadowHist class - test generator.
77 import os, shutil, tempfile
77 import os, shutil, tempfile
78
78
79 from IPython.Extensions import pickleshare
79 from IPython.Extensions import pickleshare
80 from IPython.history import ShadowHist
80 from IPython.history import ShadowHist
81
81
82 tfile = tempfile.mktemp('','tmp-ipython-')
82 tfile = tempfile.mktemp('','tmp-ipython-')
83
83
84 db = pickleshare.PickleShareDB(tfile)
84 db = pickleshare.PickleShareDB(tfile)
85 s = ShadowHist(db)
85 s = ShadowHist(db)
86 s.add('hello')
86 s.add('hello')
87 s.add('world')
87 s.add('world')
88 s.add('hello')
88 s.add('hello')
89 s.add('hello')
89 s.add('hello')
90 s.add('karhu')
90 s.add('karhu')
91
91
92 yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')]
92 yield nt.assert_equals,s.all(),[(1, 'hello'), (2, 'world'), (3, 'karhu')]
93
93
94 yield nt.assert_equal,s.get(2),'world'
94 yield nt.assert_equal,s.get(2),'world'
95
95
96 shutil.rmtree(tfile)
96 shutil.rmtree(tfile)
97
97
98 @dec.skipif_not_numpy
98 @dec.skipif_not_numpy
99 def test_numpy_clear_array_undec():
99 def test_numpy_clear_array_undec():
100 from IPython.Extensions import clearcmd
100 from IPython.Extensions import clearcmd
101
101
102 _ip.ex('import numpy as np')
102 _ip.ex('import numpy as np')
103 _ip.ex('a = np.empty(2)')
103 _ip.ex('a = np.empty(2)')
104 yield (nt.assert_true, 'a' in _ip.user_ns)
104 yield (nt.assert_true, 'a' in _ip.user_ns)
105 _ip.magic('clear array')
105 _ip.magic('clear array')
106 yield (nt.assert_false, 'a' in _ip.user_ns)
106 yield (nt.assert_false, 'a' in _ip.user_ns)
107
107
108
108
109 @dec.skip()
109 @dec.skip()
110 def test_fail_dec(*a,**k):
110 def test_fail_dec(*a,**k):
111 yield nt.assert_true, False
111 yield nt.assert_true, False
112
112
113 @dec.skip('This one shouldn not run')
113 @dec.skip('This one shouldn not run')
114 def test_fail_dec2(*a,**k):
114 def test_fail_dec2(*a,**k):
115 yield nt.assert_true, False
115 yield nt.assert_true, False
116
116
117 @dec.skipknownfailure
117 @dec.skipknownfailure
118 def test_fail_dec3(*a,**k):
118 def test_fail_dec3(*a,**k):
119 yield nt.assert_true, False
119 yield nt.assert_true, False
120
120
121
121
122 def doctest_refbug():
122 def doctest_refbug():
123 """Very nasty problem with references held by multiple runs of a script.
123 """Very nasty problem with references held by multiple runs of a script.
124 See: https://bugs.launchpad.net/ipython/+bug/269966
124 See: https://bugs.launchpad.net/ipython/+bug/269966
125
125
126 In [1]: _ip.IP.clear_main_mod_cache()
126 In [1]: _ip.IP.clear_main_mod_cache()
127
127
128 In [2]: run refbug
128 In [2]: run refbug
129
129
130 In [3]: call_f()
130 In [3]: call_f()
131 lowercased: hello
131 lowercased: hello
132
132
133 In [4]: run refbug
133 In [4]: run refbug
134
134
135 In [5]: call_f()
135 In [5]: call_f()
136 lowercased: hello
136 lowercased: hello
137 lowercased: hello
137 lowercased: hello
138 """
138 """
139
139
140 #-----------------------------------------------------------------------------
140 #-----------------------------------------------------------------------------
141 # Tests for %run
141 # Tests for %run
142 #-----------------------------------------------------------------------------
142 #-----------------------------------------------------------------------------
143
143
144 # %run is critical enough that it's a good idea to have a solid collection of
144 # %run is critical enough that it's a good idea to have a solid collection of
145 # tests for it, some as doctests and some as normal tests.
145 # tests for it, some as doctests and some as normal tests.
146
146
147 def doctest_run_ns():
147 def doctest_run_ns():
148 """Classes declared %run scripts must be instantiable afterwards.
148 """Classes declared %run scripts must be instantiable afterwards.
149
149
150 In [11]: run tclass foo
150 In [11]: run tclass foo
151
151
152 In [12]: isinstance(f(),foo)
152 In [12]: isinstance(f(),foo)
153 Out[12]: True
153 Out[12]: True
154 """
154 """
155
155
156
156
157 def doctest_run_ns2():
157 def doctest_run_ns2():
158 """Classes declared %run scripts must be instantiable afterwards.
158 """Classes declared %run scripts must be instantiable afterwards.
159
159
160 In [4]: run tclass C-first_pass
160 In [4]: run tclass C-first_pass
161
161
162 In [5]: run tclass C-second_pass
162 In [5]: run tclass C-second_pass
163 tclass.py: deleting object: C-first_pass
163 tclass.py: deleting object: C-first_pass
164 """
164 """
165
165
166 def doctest_run_builtins():
166 def doctest_run_builtins():
167 """Check that %run doesn't damage __builtins__ via a doctest.
167 """Check that %run doesn't damage __builtins__ via a doctest.
168
168
169 This is similar to the test_run_builtins, but I want *both* forms of the
169 This is similar to the test_run_builtins, but I want *both* forms of the
170 test to catch any possible glitches in our testing machinery, since that
170 test to catch any possible glitches in our testing machinery, since that
171 modifies %run somewhat. So for this, we have both a normal test (below)
171 modifies %run somewhat. So for this, we have both a normal test (below)
172 and a doctest (this one).
172 and a doctest (this one).
173
173
174 In [1]: import tempfile
174 In [1]: import tempfile
175
175
176 In [2]: bid1 = id(__builtins__)
176 In [2]: bid1 = id(__builtins__)
177
177
178 In [3]: fname = tempfile.mkstemp()[1]
178 In [3]: fname = tempfile.mkstemp()[1]
179
179
180 In [3]: f = open(fname,'w')
180 In [3]: f = open(fname,'w')
181
181
182 In [4]: f.write('pass\\n')
182 In [4]: f.write('pass\\n')
183
183
184 In [5]: f.flush()
184 In [5]: f.flush()
185
185
186 In [6]: print type(__builtins__)
186 In [6]: print type(__builtins__)
187 <type 'module'>
187 <type 'module'>
188
188
189 In [7]: %run "$fname"
189 In [7]: %run "$fname"
190
190
191 In [7]: f.close()
191 In [7]: f.close()
192
192
193 In [8]: bid2 = id(__builtins__)
193 In [8]: bid2 = id(__builtins__)
194
194
195 In [9]: print type(__builtins__)
195 In [9]: print type(__builtins__)
196 <type 'module'>
196 <type 'module'>
197
197
198 In [10]: bid1 == bid2
198 In [10]: bid1 == bid2
199 Out[10]: True
199 Out[10]: True
200
200
201 In [12]: try:
201 In [12]: try:
202 ....: os.unlink(fname)
202 ....: os.unlink(fname)
203 ....: except:
203 ....: except:
204 ....: pass
204 ....: pass
205 ....:
205 ....:
206 """
206 """
207
207
208 # For some tests, it will be handy to organize them in a class with a common
208 # For some tests, it will be handy to organize them in a class with a common
209 # setup that makes a temp file
209 # setup that makes a temp file
210
210
211 class TestMagicRun(object):
211 class TestMagicRun(object):
212
212
213 def setup(self):
213 def setup(self):
214 """Make a valid python temp file."""
214 """Make a valid python temp file."""
215 fname = tempfile.mkstemp()[1]
215 fname = tempfile.mkstemp()[1]
216 f = open(fname,'w')
216 f = open(fname,'w')
217 f.write('pass\n')
217 f.write('pass\n')
218 f.flush()
218 f.flush()
219 self.tmpfile = f
219 self.tmpfile = f
220 self.fname = fname
220 self.fname = fname
221
221
222 def run_tmpfile(self):
222 def run_tmpfile(self):
223 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
223 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
224 # See below and ticket https://bugs.launchpad.net/bugs/366353
224 # See below and ticket https://bugs.launchpad.net/bugs/366353
225 _ip.magic('run "%s"' % self.fname)
225 _ip.magic('run "%s"' % self.fname)
226
226
227 def test_builtins_id(self):
227 def test_builtins_id(self):
228 """Check that %run doesn't damage __builtins__ """
228 """Check that %run doesn't damage __builtins__ """
229
229
230 # Test that the id of __builtins__ is not modified by %run
230 # Test that the id of __builtins__ is not modified by %run
231 bid1 = id(_ip.user_ns['__builtins__'])
231 bid1 = id(_ip.user_ns['__builtins__'])
232 self.run_tmpfile()
232 self.run_tmpfile()
233 bid2 = id(_ip.user_ns['__builtins__'])
233 bid2 = id(_ip.user_ns['__builtins__'])
234 tt.assert_equals(bid1, bid2)
234 tt.assert_equals(bid1, bid2)
235
235
236 def test_builtins_type(self):
236 def test_builtins_type(self):
237 """Check that the type of __builtins__ doesn't change with %run.
237 """Check that the type of __builtins__ doesn't change with %run.
238
238
239 However, the above could pass if __builtins__ was already modified to
239 However, the above could pass if __builtins__ was already modified to
240 be a dict (it should be a module) by a previous use of %run. So we
240 be a dict (it should be a module) by a previous use of %run. So we
241 also check explicitly that it really is a module:
241 also check explicitly that it really is a module:
242 """
242 """
243 self.run_tmpfile()
243 self.run_tmpfile()
244 tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
244 tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
245
245
246 def test_prompts(self):
246 def test_prompts(self):
247 """Test that prompts correctly generate after %run"""
247 """Test that prompts correctly generate after %run"""
248 self.run_tmpfile()
248 self.run_tmpfile()
249 p2 = str(_ip.IP.outputcache.prompt2).strip()
249 p2 = str(_ip.IP.outputcache.prompt2).strip()
250 nt.assert_equals(p2[:3], '...')
250 nt.assert_equals(p2[:3], '...')
251
251
252 def teardown(self):
252 def teardown(self):
253 self.tmpfile.close()
253 self.tmpfile.close()
254 try:
254 try:
255 os.unlink(self.fname)
255 os.unlink(self.fname)
256 except:
256 except:
257 # On Windows, even though we close the file, we still can't delete
257 # On Windows, even though we close the file, we still can't delete
258 # it. I have no clue why
258 # it. I have no clue why
259 pass
259 pass
260
260
261 # Multiple tests for clipboard pasting
261 # Multiple tests for clipboard pasting
262 def test_paste():
262 def test_paste():
263
263
264 def paste(txt):
264 def paste(txt):
265 hooks.clipboard_get = lambda : txt
265 hooks.clipboard_get = lambda : txt
266 _ip.magic('paste')
266 _ip.magic('paste')
267
267
268 # Inject fake clipboard hook but save original so we can restore it later
268 # Inject fake clipboard hook but save original so we can restore it later
269 hooks = _ip.IP.hooks
269 hooks = _ip.IP.hooks
270 user_ns = _ip.user_ns
270 user_ns = _ip.user_ns
271 original_clip = hooks.clipboard_get
271 original_clip = hooks.clipboard_get
272
272
273 try:
273 try:
274 # This try/except with an emtpy except clause is here only because
275 # try/yield/finally is invalid syntax in Python 2.4. This will be
276 # removed when we drop 2.4-compatibility, and the emtpy except below
277 # will be changed to a finally.
278
274 # Run tests with fake clipboard function
279 # Run tests with fake clipboard function
275 user_ns.pop('x', None)
280 user_ns.pop('x', None)
276 paste('x=1')
281 paste('x=1')
277 yield (nt.assert_equal, user_ns['x'], 1)
282 yield (nt.assert_equal, user_ns['x'], 1)
278
283
279 user_ns.pop('x', None)
284 user_ns.pop('x', None)
280 paste('>>> x=2')
285 paste('>>> x=2')
281 yield (nt.assert_equal, user_ns['x'], 2)
286 yield (nt.assert_equal, user_ns['x'], 2)
282
287
283 paste("""
288 paste("""
284 >>> x = [1,2,3]
289 >>> x = [1,2,3]
285 >>> y = []
290 >>> y = []
286 >>> for i in x:
291 >>> for i in x:
287 ... y.append(i**2)
292 ... y.append(i**2)
288 ...
293 ...
289 """)
294 """)
290 yield (nt.assert_equal, user_ns['x'], [1,2,3])
295 yield (nt.assert_equal, user_ns['x'], [1,2,3])
291 yield (nt.assert_equal, user_ns['y'], [1,4,9])
296 yield (nt.assert_equal, user_ns['y'], [1,4,9])
297 except:
298 pass
292
299
293 finally:
300 # This should be in a finally clause, instead of the bare except above.
294 # Restore original hook
301 # Restore original hook
295 hooks.clipboard_get = original_clip
302 hooks.clipboard_get = original_clip
General Comments 0
You need to be logged in to leave comments. Login now