Show More
@@ -1,588 +1,582 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """Tests for code execution (%run and related), which is particularly tricky. |
|
3 | 3 | |
|
4 | 4 | Because of how %run manages namespaces, and the fact that we are trying here to |
|
5 | 5 | verify subtle object deletion and reference counting issues, the %run tests |
|
6 | 6 | will be kept in this separate file. This makes it easier to aggregate in one |
|
7 | 7 | place the tricks needed to handle it; most other magics are much easier to test |
|
8 | 8 | and we do so in a common test_magic file. |
|
9 | 9 | |
|
10 | 10 | Note that any test using `run -i` should make sure to do a `reset` afterwards, |
|
11 | 11 | as otherwise it may influence later tests. |
|
12 | 12 | """ |
|
13 | 13 | |
|
14 | 14 | # Copyright (c) IPython Development Team. |
|
15 | 15 | # Distributed under the terms of the Modified BSD License. |
|
16 | 16 | |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | import functools |
|
20 | 20 | import os |
|
21 | 21 | from os.path import join as pjoin |
|
22 | 22 | import random |
|
23 | 23 | import string |
|
24 | 24 | import sys |
|
25 | 25 | import textwrap |
|
26 | 26 | import unittest |
|
27 | 27 | from unittest.mock import patch |
|
28 | 28 | |
|
29 | 29 | import nose.tools as nt |
|
30 | 30 | from nose import SkipTest |
|
31 | 31 | |
|
32 | 32 | from IPython.testing import decorators as dec |
|
33 | 33 | from IPython.testing import tools as tt |
|
34 | 34 | from IPython.utils.io import capture_output |
|
35 | 35 | from IPython.utils.tempdir import TemporaryDirectory |
|
36 | 36 | from IPython.core import debugger |
|
37 | 37 | |
|
38 | 38 | def doctest_refbug(): |
|
39 | 39 | """Very nasty problem with references held by multiple runs of a script. |
|
40 | 40 | See: https://github.com/ipython/ipython/issues/141 |
|
41 | 41 | |
|
42 | 42 | In [1]: _ip.clear_main_mod_cache() |
|
43 | 43 | # random |
|
44 | 44 | |
|
45 | 45 | In [2]: %run refbug |
|
46 | 46 | |
|
47 | 47 | In [3]: call_f() |
|
48 | 48 | lowercased: hello |
|
49 | 49 | |
|
50 | 50 | In [4]: %run refbug |
|
51 | 51 | |
|
52 | 52 | In [5]: call_f() |
|
53 | 53 | lowercased: hello |
|
54 | 54 | lowercased: hello |
|
55 | 55 | """ |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | def doctest_run_builtins(): |
|
59 | 59 | r"""Check that %run doesn't damage __builtins__. |
|
60 | 60 | |
|
61 | 61 | In [1]: import tempfile |
|
62 | 62 | |
|
63 | 63 | In [2]: bid1 = id(__builtins__) |
|
64 | 64 | |
|
65 | 65 | In [3]: fname = tempfile.mkstemp('.py')[1] |
|
66 | 66 | |
|
67 | 67 | In [3]: f = open(fname,'w') |
|
68 | 68 | |
|
69 | 69 | In [4]: dummy= f.write('pass\n') |
|
70 | 70 | |
|
71 | 71 | In [5]: f.flush() |
|
72 | 72 | |
|
73 | 73 | In [6]: t1 = type(__builtins__) |
|
74 | 74 | |
|
75 | 75 | In [7]: %run $fname |
|
76 | 76 | |
|
77 | 77 | In [7]: f.close() |
|
78 | 78 | |
|
79 | 79 | In [8]: bid2 = id(__builtins__) |
|
80 | 80 | |
|
81 | 81 | In [9]: t2 = type(__builtins__) |
|
82 | 82 | |
|
83 | 83 | In [10]: t1 == t2 |
|
84 | 84 | Out[10]: True |
|
85 | 85 | |
|
86 | 86 | In [10]: bid1 == bid2 |
|
87 | 87 | Out[10]: True |
|
88 | 88 | |
|
89 | 89 | In [12]: try: |
|
90 | 90 | ....: os.unlink(fname) |
|
91 | 91 | ....: except: |
|
92 | 92 | ....: pass |
|
93 | 93 | ....: |
|
94 | 94 | """ |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | def doctest_run_option_parser(): |
|
98 | 98 | r"""Test option parser in %run. |
|
99 | 99 | |
|
100 | 100 | In [1]: %run print_argv.py |
|
101 | 101 | [] |
|
102 | 102 | |
|
103 | 103 | In [2]: %run print_argv.py print*.py |
|
104 | 104 | ['print_argv.py'] |
|
105 | 105 | |
|
106 | 106 | In [3]: %run -G print_argv.py print*.py |
|
107 | 107 | ['print*.py'] |
|
108 | 108 | |
|
109 | 109 | """ |
|
110 | 110 | |
|
111 | 111 | |
|
112 | 112 | @dec.skip_win32 |
|
113 | 113 | def doctest_run_option_parser_for_posix(): |
|
114 | 114 | r"""Test option parser in %run (Linux/OSX specific). |
|
115 | 115 | |
|
116 | 116 | You need double quote to escape glob in POSIX systems: |
|
117 | 117 | |
|
118 | 118 | In [1]: %run print_argv.py print\\*.py |
|
119 | 119 | ['print*.py'] |
|
120 | 120 | |
|
121 | 121 | You can't use quote to escape glob in POSIX systems: |
|
122 | 122 | |
|
123 | 123 | In [2]: %run print_argv.py 'print*.py' |
|
124 | 124 | ['print_argv.py'] |
|
125 | 125 | |
|
126 | 126 | """ |
|
127 | 127 | |
|
128 | 128 | |
|
129 | 129 | @dec.skip_if_not_win32 |
|
130 | 130 | def doctest_run_option_parser_for_windows(): |
|
131 | 131 | r"""Test option parser in %run (Windows specific). |
|
132 | 132 | |
|
133 | 133 | In Windows, you can't escape ``*` `by backslash: |
|
134 | 134 | |
|
135 | 135 | In [1]: %run print_argv.py print\\*.py |
|
136 | 136 | ['print\\*.py'] |
|
137 | 137 | |
|
138 | 138 | You can use quote to escape glob: |
|
139 | 139 | |
|
140 | 140 | In [2]: %run print_argv.py 'print*.py' |
|
141 | 141 | ['print*.py'] |
|
142 | 142 | |
|
143 | 143 | """ |
|
144 | 144 | |
|
145 | 145 | |
|
146 | 146 | def doctest_reset_del(): |
|
147 | 147 | """Test that resetting doesn't cause errors in __del__ methods. |
|
148 | 148 | |
|
149 | 149 | In [2]: class A(object): |
|
150 | 150 | ...: def __del__(self): |
|
151 | 151 | ...: print(str("Hi")) |
|
152 | 152 | ...: |
|
153 | 153 | |
|
154 | 154 | In [3]: a = A() |
|
155 | 155 | |
|
156 | 156 | In [4]: get_ipython().reset() |
|
157 | 157 | Hi |
|
158 | 158 | |
|
159 | 159 | In [5]: 1+1 |
|
160 | 160 | Out[5]: 2 |
|
161 | 161 | """ |
|
162 | 162 | |
|
163 | 163 | # For some tests, it will be handy to organize them in a class with a common |
|
164 | 164 | # setup that makes a temp file |
|
165 | 165 | |
|
166 | 166 | class TestMagicRunPass(tt.TempFileMixin): |
|
167 | 167 | |
|
168 | 168 | def setUp(self): |
|
169 | 169 | content = "a = [1,2,3]\nb = 1" |
|
170 | 170 | self.mktmp(content) |
|
171 | 171 | |
|
172 | 172 | def run_tmpfile(self): |
|
173 | 173 | _ip = get_ipython() |
|
174 | 174 | # This fails on Windows if self.tmpfile.name has spaces or "~" in it. |
|
175 | 175 | # See below and ticket https://bugs.launchpad.net/bugs/366353 |
|
176 | 176 | _ip.magic('run %s' % self.fname) |
|
177 | 177 | |
|
178 | 178 | def run_tmpfile_p(self): |
|
179 | 179 | _ip = get_ipython() |
|
180 | 180 | # This fails on Windows if self.tmpfile.name has spaces or "~" in it. |
|
181 | 181 | # See below and ticket https://bugs.launchpad.net/bugs/366353 |
|
182 | 182 | _ip.magic('run -p %s' % self.fname) |
|
183 | 183 | |
|
184 | 184 | def test_builtins_id(self): |
|
185 | 185 | """Check that %run doesn't damage __builtins__ """ |
|
186 | 186 | _ip = get_ipython() |
|
187 | 187 | # Test that the id of __builtins__ is not modified by %run |
|
188 | 188 | bid1 = id(_ip.user_ns['__builtins__']) |
|
189 | 189 | self.run_tmpfile() |
|
190 | 190 | bid2 = id(_ip.user_ns['__builtins__']) |
|
191 | 191 | nt.assert_equal(bid1, bid2) |
|
192 | 192 | |
|
193 | 193 | def test_builtins_type(self): |
|
194 | 194 | """Check that the type of __builtins__ doesn't change with %run. |
|
195 | 195 | |
|
196 | 196 | However, the above could pass if __builtins__ was already modified to |
|
197 | 197 | be a dict (it should be a module) by a previous use of %run. So we |
|
198 | 198 | also check explicitly that it really is a module: |
|
199 | 199 | """ |
|
200 | 200 | _ip = get_ipython() |
|
201 | 201 | self.run_tmpfile() |
|
202 | 202 | nt.assert_equal(type(_ip.user_ns['__builtins__']),type(sys)) |
|
203 | 203 | |
|
204 | 204 | def test_run_profile( self ): |
|
205 | 205 | """Test that the option -p, which invokes the profiler, do not |
|
206 | 206 | crash by invoking execfile""" |
|
207 | 207 | self.run_tmpfile_p() |
|
208 | 208 | |
|
209 | 209 | def test_run_debug_twice(self): |
|
210 | 210 | # https://github.com/ipython/ipython/issues/10028 |
|
211 | 211 | _ip = get_ipython() |
|
212 | 212 | with tt.fake_input(['c']): |
|
213 | 213 | _ip.magic('run -d %s' % self.fname) |
|
214 | 214 | with tt.fake_input(['c']): |
|
215 | 215 | _ip.magic('run -d %s' % self.fname) |
|
216 | 216 | |
|
217 | 217 | def test_run_debug_twice_with_breakpoint(self): |
|
218 | 218 | """Make a valid python temp file.""" |
|
219 | 219 | _ip = get_ipython() |
|
220 | 220 | with tt.fake_input(['b 2', 'c', 'c']): |
|
221 | 221 | _ip.magic('run -d %s' % self.fname) |
|
222 | 222 | |
|
223 | 223 | with tt.fake_input(['c']): |
|
224 | 224 | with tt.AssertNotPrints('KeyError'): |
|
225 | 225 | _ip.magic('run -d %s' % self.fname) |
|
226 | 226 | |
|
227 | 227 | |
|
228 | 228 | class TestMagicRunSimple(tt.TempFileMixin): |
|
229 | 229 | |
|
230 | 230 | def test_simpledef(self): |
|
231 | 231 | """Test that simple class definitions work.""" |
|
232 | 232 | src = ("class foo: pass\n" |
|
233 | 233 | "def f(): return foo()") |
|
234 | 234 | self.mktmp(src) |
|
235 | 235 | _ip.magic('run %s' % self.fname) |
|
236 | 236 | _ip.run_cell('t = isinstance(f(), foo)') |
|
237 | 237 | nt.assert_true(_ip.user_ns['t']) |
|
238 | 238 | |
|
239 | 239 | def test_obj_del(self): |
|
240 | 240 | """Test that object's __del__ methods are called on exit.""" |
|
241 | 241 | if sys.platform == 'win32': |
|
242 | 242 | try: |
|
243 | 243 | import win32api |
|
244 | 244 | except ImportError: |
|
245 | 245 | raise SkipTest("Test requires pywin32") |
|
246 | 246 | src = ("class A(object):\n" |
|
247 | 247 | " def __del__(self):\n" |
|
248 | 248 | " print('object A deleted')\n" |
|
249 | 249 | "a = A()\n") |
|
250 | 250 | self.mktmp(src) |
|
251 | if dec.module_not_available('sqlite3'): | |
|
252 | err = 'WARNING: IPython History requires SQLite, your history will not be saved\n' | |
|
253 | else: | |
|
254 | err = None | |
|
251 | err = None | |
|
255 | 252 | tt.ipexec_validate(self.fname, 'object A deleted', err) |
|
256 | 253 | |
|
257 | 254 | def test_aggressive_namespace_cleanup(self): |
|
258 | 255 | """Test that namespace cleanup is not too aggressive GH-238 |
|
259 | 256 | |
|
260 | 257 | Returning from another run magic deletes the namespace""" |
|
261 | 258 | # see ticket https://github.com/ipython/ipython/issues/238 |
|
262 | 259 | |
|
263 | 260 | with tt.TempFileMixin() as empty: |
|
264 | 261 | empty.mktmp('') |
|
265 | 262 | # On Windows, the filename will have \users in it, so we need to use the |
|
266 | 263 | # repr so that the \u becomes \\u. |
|
267 | 264 | src = ("ip = get_ipython()\n" |
|
268 | 265 | "for i in range(5):\n" |
|
269 | 266 | " try:\n" |
|
270 | 267 | " ip.magic(%r)\n" |
|
271 | 268 | " except NameError as e:\n" |
|
272 | 269 | " print(i)\n" |
|
273 | 270 | " break\n" % ('run ' + empty.fname)) |
|
274 | 271 | self.mktmp(src) |
|
275 | 272 | _ip.magic('run %s' % self.fname) |
|
276 | 273 | _ip.run_cell('ip == get_ipython()') |
|
277 | 274 | nt.assert_equal(_ip.user_ns['i'], 4) |
|
278 | 275 | |
|
279 | 276 | def test_run_second(self): |
|
280 | 277 | """Test that running a second file doesn't clobber the first, gh-3547 |
|
281 | 278 | """ |
|
282 | 279 | self.mktmp("avar = 1\n" |
|
283 | 280 | "def afunc():\n" |
|
284 | 281 | " return avar\n") |
|
285 | 282 | |
|
286 | 283 | with tt.TempFileMixin() as empty: |
|
287 | 284 | empty.mktmp("") |
|
288 | 285 | |
|
289 | 286 | _ip.magic('run %s' % self.fname) |
|
290 | 287 | _ip.magic('run %s' % empty.fname) |
|
291 | 288 | nt.assert_equal(_ip.user_ns['afunc'](), 1) |
|
292 | 289 | |
|
293 | 290 | @dec.skip_win32 |
|
294 | 291 | def test_tclass(self): |
|
295 | 292 | mydir = os.path.dirname(__file__) |
|
296 | 293 | tc = os.path.join(mydir, 'tclass') |
|
297 | 294 | src = ("%%run '%s' C-first\n" |
|
298 | 295 | "%%run '%s' C-second\n" |
|
299 | 296 | "%%run '%s' C-third\n") % (tc, tc, tc) |
|
300 | 297 | self.mktmp(src, '.ipy') |
|
301 | 298 | out = """\ |
|
302 | 299 | ARGV 1-: ['C-first'] |
|
303 | 300 | ARGV 1-: ['C-second'] |
|
304 | 301 | tclass.py: deleting object: C-first |
|
305 | 302 | ARGV 1-: ['C-third'] |
|
306 | 303 | tclass.py: deleting object: C-second |
|
307 | 304 | tclass.py: deleting object: C-third |
|
308 | 305 | """ |
|
309 | if dec.module_not_available('sqlite3'): | |
|
310 | err = 'WARNING: IPython History requires SQLite, your history will not be saved\n' | |
|
311 | else: | |
|
312 | err = None | |
|
306 | err = None | |
|
313 | 307 | tt.ipexec_validate(self.fname, out, err) |
|
314 | 308 | |
|
315 | 309 | def test_run_i_after_reset(self): |
|
316 | 310 | """Check that %run -i still works after %reset (gh-693)""" |
|
317 | 311 | src = "yy = zz\n" |
|
318 | 312 | self.mktmp(src) |
|
319 | 313 | _ip.run_cell("zz = 23") |
|
320 | 314 | try: |
|
321 | 315 | _ip.magic('run -i %s' % self.fname) |
|
322 | 316 | nt.assert_equal(_ip.user_ns['yy'], 23) |
|
323 | 317 | finally: |
|
324 | 318 | _ip.magic('reset -f') |
|
325 | 319 | |
|
326 | 320 | _ip.run_cell("zz = 23") |
|
327 | 321 | try: |
|
328 | 322 | _ip.magic('run -i %s' % self.fname) |
|
329 | 323 | nt.assert_equal(_ip.user_ns['yy'], 23) |
|
330 | 324 | finally: |
|
331 | 325 | _ip.magic('reset -f') |
|
332 | 326 | |
|
333 | 327 | def test_unicode(self): |
|
334 | 328 | """Check that files in odd encodings are accepted.""" |
|
335 | 329 | mydir = os.path.dirname(__file__) |
|
336 | 330 | na = os.path.join(mydir, 'nonascii.py') |
|
337 | 331 | _ip.magic('run "%s"' % na) |
|
338 | 332 | nt.assert_equal(_ip.user_ns['u'], u'ΠΡβΠ€') |
|
339 | 333 | |
|
340 | 334 | def test_run_py_file_attribute(self): |
|
341 | 335 | """Test handling of `__file__` attribute in `%run <file>.py`.""" |
|
342 | 336 | src = "t = __file__\n" |
|
343 | 337 | self.mktmp(src) |
|
344 | 338 | _missing = object() |
|
345 | 339 | file1 = _ip.user_ns.get('__file__', _missing) |
|
346 | 340 | _ip.magic('run %s' % self.fname) |
|
347 | 341 | file2 = _ip.user_ns.get('__file__', _missing) |
|
348 | 342 | |
|
349 | 343 | # Check that __file__ was equal to the filename in the script's |
|
350 | 344 | # namespace. |
|
351 | 345 | nt.assert_equal(_ip.user_ns['t'], self.fname) |
|
352 | 346 | |
|
353 | 347 | # Check that __file__ was not leaked back into user_ns. |
|
354 | 348 | nt.assert_equal(file1, file2) |
|
355 | 349 | |
|
356 | 350 | def test_run_ipy_file_attribute(self): |
|
357 | 351 | """Test handling of `__file__` attribute in `%run <file.ipy>`.""" |
|
358 | 352 | src = "t = __file__\n" |
|
359 | 353 | self.mktmp(src, ext='.ipy') |
|
360 | 354 | _missing = object() |
|
361 | 355 | file1 = _ip.user_ns.get('__file__', _missing) |
|
362 | 356 | _ip.magic('run %s' % self.fname) |
|
363 | 357 | file2 = _ip.user_ns.get('__file__', _missing) |
|
364 | 358 | |
|
365 | 359 | # Check that __file__ was equal to the filename in the script's |
|
366 | 360 | # namespace. |
|
367 | 361 | nt.assert_equal(_ip.user_ns['t'], self.fname) |
|
368 | 362 | |
|
369 | 363 | # Check that __file__ was not leaked back into user_ns. |
|
370 | 364 | nt.assert_equal(file1, file2) |
|
371 | 365 | |
|
372 | 366 | def test_run_formatting(self): |
|
373 | 367 | """ Test that %run -t -N<N> does not raise a TypeError for N > 1.""" |
|
374 | 368 | src = "pass" |
|
375 | 369 | self.mktmp(src) |
|
376 | 370 | _ip.magic('run -t -N 1 %s' % self.fname) |
|
377 | 371 | _ip.magic('run -t -N 10 %s' % self.fname) |
|
378 | 372 | |
|
379 | 373 | def test_ignore_sys_exit(self): |
|
380 | 374 | """Test the -e option to ignore sys.exit()""" |
|
381 | 375 | src = "import sys; sys.exit(1)" |
|
382 | 376 | self.mktmp(src) |
|
383 | 377 | with tt.AssertPrints('SystemExit'): |
|
384 | 378 | _ip.magic('run %s' % self.fname) |
|
385 | 379 | |
|
386 | 380 | with tt.AssertNotPrints('SystemExit'): |
|
387 | 381 | _ip.magic('run -e %s' % self.fname) |
|
388 | 382 | |
|
389 | 383 | def test_run_nb(self): |
|
390 | 384 | """Test %run notebook.ipynb""" |
|
391 | 385 | from nbformat import v4, writes |
|
392 | 386 | nb = v4.new_notebook( |
|
393 | 387 | cells=[ |
|
394 | 388 | v4.new_markdown_cell("The Ultimate Question of Everything"), |
|
395 | 389 | v4.new_code_cell("answer=42") |
|
396 | 390 | ] |
|
397 | 391 | ) |
|
398 | 392 | src = writes(nb, version=4) |
|
399 | 393 | self.mktmp(src, ext='.ipynb') |
|
400 | 394 | |
|
401 | 395 | _ip.magic("run %s" % self.fname) |
|
402 | 396 | |
|
403 | 397 | nt.assert_equal(_ip.user_ns['answer'], 42) |
|
404 | 398 | |
|
405 | 399 | def test_file_options(self): |
|
406 | 400 | src = ('import sys\n' |
|
407 | 401 | 'a = " ".join(sys.argv[1:])\n') |
|
408 | 402 | self.mktmp(src) |
|
409 | 403 | test_opts = '-x 3 --verbose' |
|
410 | 404 | _ip.run_line_magic("run", '{0} {1}'.format(self.fname, test_opts)) |
|
411 | 405 | nt.assert_equal(_ip.user_ns['a'], test_opts) |
|
412 | 406 | |
|
413 | 407 | |
|
414 | 408 | class TestMagicRunWithPackage(unittest.TestCase): |
|
415 | 409 | |
|
416 | 410 | def writefile(self, name, content): |
|
417 | 411 | path = os.path.join(self.tempdir.name, name) |
|
418 | 412 | d = os.path.dirname(path) |
|
419 | 413 | if not os.path.isdir(d): |
|
420 | 414 | os.makedirs(d) |
|
421 | 415 | with open(path, 'w') as f: |
|
422 | 416 | f.write(textwrap.dedent(content)) |
|
423 | 417 | |
|
424 | 418 | def setUp(self): |
|
425 | 419 | self.package = package = 'tmp{0}'.format(''.join([random.choice(string.ascii_letters) for i in range(10)])) |
|
426 | 420 | """Temporary (probably) valid python package name.""" |
|
427 | 421 | |
|
428 | 422 | self.value = int(random.random() * 10000) |
|
429 | 423 | |
|
430 | 424 | self.tempdir = TemporaryDirectory() |
|
431 | 425 | self.__orig_cwd = os.getcwd() |
|
432 | 426 | sys.path.insert(0, self.tempdir.name) |
|
433 | 427 | |
|
434 | 428 | self.writefile(os.path.join(package, '__init__.py'), '') |
|
435 | 429 | self.writefile(os.path.join(package, 'sub.py'), """ |
|
436 | 430 | x = {0!r} |
|
437 | 431 | """.format(self.value)) |
|
438 | 432 | self.writefile(os.path.join(package, 'relative.py'), """ |
|
439 | 433 | from .sub import x |
|
440 | 434 | """) |
|
441 | 435 | self.writefile(os.path.join(package, 'absolute.py'), """ |
|
442 | 436 | from {0}.sub import x |
|
443 | 437 | """.format(package)) |
|
444 | 438 | self.writefile(os.path.join(package, 'args.py'), """ |
|
445 | 439 | import sys |
|
446 | 440 | a = " ".join(sys.argv[1:]) |
|
447 | 441 | """.format(package)) |
|
448 | 442 | |
|
449 | 443 | def tearDown(self): |
|
450 | 444 | os.chdir(self.__orig_cwd) |
|
451 | 445 | sys.path[:] = [p for p in sys.path if p != self.tempdir.name] |
|
452 | 446 | self.tempdir.cleanup() |
|
453 | 447 | |
|
454 | 448 | def check_run_submodule(self, submodule, opts=''): |
|
455 | 449 | _ip.user_ns.pop('x', None) |
|
456 | 450 | _ip.magic('run {2} -m {0}.{1}'.format(self.package, submodule, opts)) |
|
457 | 451 | self.assertEqual(_ip.user_ns['x'], self.value, |
|
458 | 452 | 'Variable `x` is not loaded from module `{0}`.' |
|
459 | 453 | .format(submodule)) |
|
460 | 454 | |
|
461 | 455 | def test_run_submodule_with_absolute_import(self): |
|
462 | 456 | self.check_run_submodule('absolute') |
|
463 | 457 | |
|
464 | 458 | def test_run_submodule_with_relative_import(self): |
|
465 | 459 | """Run submodule that has a relative import statement (#2727).""" |
|
466 | 460 | self.check_run_submodule('relative') |
|
467 | 461 | |
|
468 | 462 | def test_prun_submodule_with_absolute_import(self): |
|
469 | 463 | self.check_run_submodule('absolute', '-p') |
|
470 | 464 | |
|
471 | 465 | def test_prun_submodule_with_relative_import(self): |
|
472 | 466 | self.check_run_submodule('relative', '-p') |
|
473 | 467 | |
|
474 | 468 | def with_fake_debugger(func): |
|
475 | 469 | @functools.wraps(func) |
|
476 | 470 | def wrapper(*args, **kwds): |
|
477 | 471 | with patch.object(debugger.Pdb, 'run', staticmethod(eval)): |
|
478 | 472 | return func(*args, **kwds) |
|
479 | 473 | return wrapper |
|
480 | 474 | |
|
481 | 475 | @with_fake_debugger |
|
482 | 476 | def test_debug_run_submodule_with_absolute_import(self): |
|
483 | 477 | self.check_run_submodule('absolute', '-d') |
|
484 | 478 | |
|
485 | 479 | @with_fake_debugger |
|
486 | 480 | def test_debug_run_submodule_with_relative_import(self): |
|
487 | 481 | self.check_run_submodule('relative', '-d') |
|
488 | 482 | |
|
489 | 483 | def test_module_options(self): |
|
490 | 484 | _ip.user_ns.pop('a', None) |
|
491 | 485 | test_opts = '-x abc -m test' |
|
492 | 486 | _ip.run_line_magic('run', '-m {0}.args {1}'.format(self.package, test_opts)) |
|
493 | 487 | nt.assert_equal(_ip.user_ns['a'], test_opts) |
|
494 | 488 | |
|
495 | 489 | def test_module_options_with_separator(self): |
|
496 | 490 | _ip.user_ns.pop('a', None) |
|
497 | 491 | test_opts = '-x abc -m test' |
|
498 | 492 | _ip.run_line_magic('run', '-m {0}.args -- {1}'.format(self.package, test_opts)) |
|
499 | 493 | nt.assert_equal(_ip.user_ns['a'], test_opts) |
|
500 | 494 | |
|
501 | 495 | def test_run__name__(): |
|
502 | 496 | with TemporaryDirectory() as td: |
|
503 | 497 | path = pjoin(td, 'foo.py') |
|
504 | 498 | with open(path, 'w') as f: |
|
505 | 499 | f.write("q = __name__") |
|
506 | 500 | |
|
507 | 501 | _ip.user_ns.pop('q', None) |
|
508 | 502 | _ip.magic('run {}'.format(path)) |
|
509 | 503 | nt.assert_equal(_ip.user_ns.pop('q'), '__main__') |
|
510 | 504 | |
|
511 | 505 | _ip.magic('run -n {}'.format(path)) |
|
512 | 506 | nt.assert_equal(_ip.user_ns.pop('q'), 'foo') |
|
513 | 507 | |
|
514 | 508 | try: |
|
515 | 509 | _ip.magic('run -i -n {}'.format(path)) |
|
516 | 510 | nt.assert_equal(_ip.user_ns.pop('q'), 'foo') |
|
517 | 511 | finally: |
|
518 | 512 | _ip.magic('reset -f') |
|
519 | 513 | |
|
520 | 514 | |
|
521 | 515 | def test_run_tb(): |
|
522 | 516 | """Test traceback offset in %run""" |
|
523 | 517 | with TemporaryDirectory() as td: |
|
524 | 518 | path = pjoin(td, 'foo.py') |
|
525 | 519 | with open(path, 'w') as f: |
|
526 | 520 | f.write('\n'.join([ |
|
527 | 521 | "def foo():", |
|
528 | 522 | " return bar()", |
|
529 | 523 | "def bar():", |
|
530 | 524 | " raise RuntimeError('hello!')", |
|
531 | 525 | "foo()", |
|
532 | 526 | ])) |
|
533 | 527 | with capture_output() as io: |
|
534 | 528 | _ip.magic('run {}'.format(path)) |
|
535 | 529 | out = io.stdout |
|
536 | 530 | nt.assert_not_in("execfile", out) |
|
537 | 531 | nt.assert_in("RuntimeError", out) |
|
538 | 532 | nt.assert_equal(out.count("---->"), 3) |
|
539 | 533 | del ip.user_ns['bar'] |
|
540 | 534 | del ip.user_ns['foo'] |
|
541 | 535 | |
|
542 | 536 | |
|
543 | 537 | def test_multiprocessing_run(): |
|
544 | 538 | """Set we can run mutiprocesgin without messing up up main namespace |
|
545 | 539 | |
|
546 | 540 | Note that import `nose.tools as nt` mdify the value s |
|
547 | 541 | sys.module['__mp_main__'] so wee need to temporarily set it to None to test |
|
548 | 542 | the issue. |
|
549 | 543 | """ |
|
550 | 544 | with TemporaryDirectory() as td: |
|
551 | 545 | mpm = sys.modules.get('__mp_main__') |
|
552 | 546 | assert mpm is not None |
|
553 | 547 | sys.modules['__mp_main__'] = None |
|
554 | 548 | try: |
|
555 | 549 | path = pjoin(td, 'test.py') |
|
556 | 550 | with open(path, 'w') as f: |
|
557 | 551 | f.write("import multiprocessing\nprint('hoy')") |
|
558 | 552 | with capture_output() as io: |
|
559 | 553 | _ip.run_line_magic('run', path) |
|
560 | 554 | _ip.run_cell("i_m_undefined") |
|
561 | 555 | out = io.stdout |
|
562 | 556 | nt.assert_in("hoy", out) |
|
563 | 557 | nt.assert_not_in("AttributeError", out) |
|
564 | 558 | nt.assert_in("NameError", out) |
|
565 | 559 | nt.assert_equal(out.count("---->"), 1) |
|
566 | 560 | except: |
|
567 | 561 | raise |
|
568 | 562 | finally: |
|
569 | 563 | sys.modules['__mp_main__'] = mpm |
|
570 | 564 | |
|
571 | 565 | @dec.knownfailureif(sys.platform == 'win32', "writes to io.stdout aren't captured on Windows") |
|
572 | 566 | def test_script_tb(): |
|
573 | 567 | """Test traceback offset in `ipython script.py`""" |
|
574 | 568 | with TemporaryDirectory() as td: |
|
575 | 569 | path = pjoin(td, 'foo.py') |
|
576 | 570 | with open(path, 'w') as f: |
|
577 | 571 | f.write('\n'.join([ |
|
578 | 572 | "def foo():", |
|
579 | 573 | " return bar()", |
|
580 | 574 | "def bar():", |
|
581 | 575 | " raise RuntimeError('hello!')", |
|
582 | 576 | "foo()", |
|
583 | 577 | ])) |
|
584 | 578 | out, err = tt.ipexec(path) |
|
585 | 579 | nt.assert_not_in("execfile", out) |
|
586 | 580 | nt.assert_in("RuntimeError", out) |
|
587 | 581 | nt.assert_equal(out.count("---->"), 3) |
|
588 | 582 |
@@ -1,60 +1,57 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Tests for shellapp module. |
|
3 | 3 | |
|
4 | 4 | Authors |
|
5 | 5 | ------- |
|
6 | 6 | * Bradley Froehle |
|
7 | 7 | """ |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | # Copyright (C) 2012 The IPython Development Team |
|
10 | 10 | # |
|
11 | 11 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | 12 | # the file COPYING, distributed as part of this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | # Imports |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | import unittest |
|
19 | 19 | |
|
20 | 20 | from IPython.testing import decorators as dec |
|
21 | 21 | from IPython.testing import tools as tt |
|
22 | 22 | |
|
23 | sqlite_err_maybe = dec.module_not_available('sqlite3') | |
|
24 | SQLITE_NOT_AVAILABLE_ERROR = ('WARNING: IPython History requires SQLite,' | |
|
25 | ' your history will not be saved\n') | |
|
26 | 23 | |
|
27 | 24 | class TestFileToRun(tt.TempFileMixin, unittest.TestCase): |
|
28 | 25 | """Test the behavior of the file_to_run parameter.""" |
|
29 | 26 | |
|
30 | 27 | def test_py_script_file_attribute(self): |
|
31 | 28 | """Test that `__file__` is set when running `ipython file.py`""" |
|
32 | 29 | src = "print(__file__)\n" |
|
33 | 30 | self.mktmp(src) |
|
34 | 31 | |
|
35 | err = SQLITE_NOT_AVAILABLE_ERROR if sqlite_err_maybe else None | |
|
32 | err = None | |
|
36 | 33 | tt.ipexec_validate(self.fname, self.fname, err) |
|
37 | 34 | |
|
38 | 35 | def test_ipy_script_file_attribute(self): |
|
39 | 36 | """Test that `__file__` is set when running `ipython file.ipy`""" |
|
40 | 37 | src = "print(__file__)\n" |
|
41 | 38 | self.mktmp(src, ext='.ipy') |
|
42 | 39 | |
|
43 | err = SQLITE_NOT_AVAILABLE_ERROR if sqlite_err_maybe else None | |
|
40 | err = None | |
|
44 | 41 | tt.ipexec_validate(self.fname, self.fname, err) |
|
45 | 42 | |
|
46 | 43 | # The commands option to ipexec_validate doesn't work on Windows, and it |
|
47 | 44 | # doesn't seem worth fixing |
|
48 | 45 | @dec.skip_win32 |
|
49 | 46 | def test_py_script_file_attribute_interactively(self): |
|
50 | 47 | """Test that `__file__` is not set after `ipython -i file.py`""" |
|
51 | 48 | src = "True\n" |
|
52 | 49 | self.mktmp(src) |
|
53 | 50 | |
|
54 | 51 | out, err = tt.ipexec(self.fname, options=['-i'], |
|
55 | 52 | commands=['"__file__" in globals()', 'print(123)', 'exit()']) |
|
56 | 53 | if 'False' not in out: |
|
57 | 54 | print("Subprocess stderr:") |
|
58 | 55 | print(err) |
|
59 | 56 | print('-----') |
|
60 | 57 | raise AssertionError("'False' not found in %r" % out) |
General Comments 0
You need to be logged in to leave comments.
Login now