##// END OF EJS Templates
Update test_magic.py
hongshaoyang -
Show More
@@ -1,1094 +1,1094 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Tests for various magic functions.
3 3
4 4 Needs to be run by nose (to make ipython session available).
5 5 """
6 6
7 7 import io
8 8 import os
9 9 import re
10 10 import sys
11 11 import warnings
12 12 from unittest import TestCase
13 13 from importlib import invalidate_caches
14 14 from io import StringIO
15 15
16 16 import nose.tools as nt
17 17
18 18 import shlex
19 19
20 20 from IPython import get_ipython
21 21 from IPython.core import magic
22 22 from IPython.core.error import UsageError
23 23 from IPython.core.magic import (Magics, magics_class, line_magic,
24 24 cell_magic,
25 25 register_line_magic, register_cell_magic)
26 26 from IPython.core.magics import execution, script, code, logging
27 27 from IPython.testing import decorators as dec
28 28 from IPython.testing import tools as tt
29 29 from IPython.utils.io import capture_output
30 30 from IPython.utils.tempdir import (TemporaryDirectory,
31 31 TemporaryWorkingDirectory)
32 32 from IPython.utils.process import find_cmd
33 33
34 34
35 35
36 36 _ip = get_ipython()
37 37
38 38 @magic.magics_class
39 39 class DummyMagics(magic.Magics): pass
40 40
41 41 def test_extract_code_ranges():
42 42 instr = "1 3 5-6 7-9 10:15 17: :10 10- -13 :"
43 43 expected = [(0, 1),
44 44 (2, 3),
45 45 (4, 6),
46 46 (6, 9),
47 47 (9, 14),
48 48 (16, None),
49 49 (None, 9),
50 50 (9, None),
51 51 (None, 13),
52 52 (None, None)]
53 53 actual = list(code.extract_code_ranges(instr))
54 54 nt.assert_equal(actual, expected)
55 55
56 56 def test_extract_symbols():
57 57 source = """import foo\na = 10\ndef b():\n return 42\n\n\nclass A: pass\n\n\n"""
58 58 symbols_args = ["a", "b", "A", "A,b", "A,a", "z"]
59 59 expected = [([], ['a']),
60 60 (["def b():\n return 42\n"], []),
61 61 (["class A: pass\n"], []),
62 62 (["class A: pass\n", "def b():\n return 42\n"], []),
63 63 (["class A: pass\n"], ['a']),
64 64 ([], ['z'])]
65 65 for symbols, exp in zip(symbols_args, expected):
66 66 nt.assert_equal(code.extract_symbols(source, symbols), exp)
67 67
68 68
69 69 def test_extract_symbols_raises_exception_with_non_python_code():
70 70 source = ("=begin A Ruby program :)=end\n"
71 71 "def hello\n"
72 72 "puts 'Hello world'\n"
73 73 "end")
74 74 with nt.assert_raises(SyntaxError):
75 75 code.extract_symbols(source, "hello")
76 76
77 77
78 78 def test_magic_not_found():
79 79 # magic not found raises UsageError
80 80 with nt.assert_raises(UsageError):
81 81 _ip.magic('doesntexist')
82 82
83 83 # ensure result isn't success when a magic isn't found
84 84 result = _ip.run_cell('%doesntexist')
85 85 assert isinstance(result.error_in_exec, UsageError)
86 86
87 87
88 88 def test_cell_magic_not_found():
89 89 # magic not found raises UsageError
90 90 with nt.assert_raises(UsageError):
91 91 _ip.run_cell_magic('doesntexist', 'line', 'cell')
92 92
93 93 # ensure result isn't success when a magic isn't found
94 94 result = _ip.run_cell('%%doesntexist')
95 95 assert isinstance(result.error_in_exec, UsageError)
96 96
97 97
98 98 def test_magic_error_status():
99 99 def fail(shell):
100 100 1/0
101 101 _ip.register_magic_function(fail)
102 102 result = _ip.run_cell('%fail')
103 103 assert isinstance(result.error_in_exec, ZeroDivisionError)
104 104
105 105
106 106 def test_config():
107 107 """ test that config magic does not raise
108 108 can happen if Configurable init is moved too early into
109 109 Magics.__init__ as then a Config object will be registered as a
110 110 magic.
111 111 """
112 112 ## should not raise.
113 113 _ip.magic('config')
114 114
115 115 def test_config_available_configs():
116 116 """ test that config magic prints available configs in unique and
117 117 sorted order. """
118 118 with capture_output() as captured:
119 119 _ip.magic('config')
120 120
121 121 stdout = captured.stdout
122 122 config_classes = stdout.strip().split('\n')[1:]
123 123 nt.assert_list_equal(config_classes, sorted(set(config_classes)))
124 124
125 125 def test_config_print_class():
126 126 """ test that config with a classname prints the class's options. """
127 127 with capture_output() as captured:
128 128 _ip.magic('config TerminalInteractiveShell')
129 129
130 130 stdout = captured.stdout
131 131 if not re.match("TerminalInteractiveShell.* options", stdout.splitlines()[0]):
132 132 print(stdout)
133 133 raise AssertionError("1st line of stdout not like "
134 134 "'TerminalInteractiveShell.* options'")
135 135
136 136 def test_rehashx():
137 137 # clear up everything
138 138 _ip.alias_manager.clear_aliases()
139 139 del _ip.db['syscmdlist']
140 140
141 141 _ip.magic('rehashx')
142 142 # Practically ALL ipython development systems will have more than 10 aliases
143 143
144 144 nt.assert_true(len(_ip.alias_manager.aliases) > 10)
145 145 for name, cmd in _ip.alias_manager.aliases:
146 146 # we must strip dots from alias names
147 147 nt.assert_not_in('.', name)
148 148
149 149 # rehashx must fill up syscmdlist
150 150 scoms = _ip.db['syscmdlist']
151 151 nt.assert_true(len(scoms) > 10)
152 152
153 153
154 154 def test_magic_parse_options():
155 155 """Test that we don't mangle paths when parsing magic options."""
156 156 ip = get_ipython()
157 157 path = 'c:\\x'
158 158 m = DummyMagics(ip)
159 159 opts = m.parse_options('-f %s' % path,'f:')[0]
160 160 # argv splitting is os-dependent
161 161 if os.name == 'posix':
162 162 expected = 'c:x'
163 163 else:
164 164 expected = path
165 165 nt.assert_equal(opts['f'], expected)
166 166
167 167 def test_magic_parse_long_options():
168 168 """Magic.parse_options can handle --foo=bar long options"""
169 169 ip = get_ipython()
170 170 m = DummyMagics(ip)
171 171 opts, _ = m.parse_options('--foo --bar=bubble', 'a', 'foo', 'bar=')
172 172 nt.assert_in('foo', opts)
173 173 nt.assert_in('bar', opts)
174 174 nt.assert_equal(opts['bar'], "bubble")
175 175
176 176
177 177 @dec.skip_without('sqlite3')
178 178 def doctest_hist_f():
179 179 """Test %hist -f with temporary filename.
180 180
181 181 In [9]: import tempfile
182 182
183 183 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
184 184
185 185 In [11]: %hist -nl -f $tfile 3
186 186
187 187 In [13]: import os; os.unlink(tfile)
188 188 """
189 189
190 190
191 191 @dec.skip_without('sqlite3')
192 192 def doctest_hist_r():
193 193 """Test %hist -r
194 194
195 195 XXX - This test is not recording the output correctly. For some reason, in
196 196 testing mode the raw history isn't getting populated. No idea why.
197 197 Disabling the output checking for now, though at least we do run it.
198 198
199 199 In [1]: 'hist' in _ip.lsmagic()
200 200 Out[1]: True
201 201
202 202 In [2]: x=1
203 203
204 204 In [3]: %hist -rl 2
205 205 x=1 # random
206 206 %hist -r 2
207 207 """
208 208
209 209
210 210 @dec.skip_without('sqlite3')
211 211 def doctest_hist_op():
212 212 """Test %hist -op
213 213
214 214 In [1]: class b(float):
215 215 ...: pass
216 216 ...:
217 217
218 218 In [2]: class s(object):
219 219 ...: def __str__(self):
220 220 ...: return 's'
221 221 ...:
222 222
223 223 In [3]:
224 224
225 225 In [4]: class r(b):
226 226 ...: def __repr__(self):
227 227 ...: return 'r'
228 228 ...:
229 229
230 230 In [5]: class sr(s,r): pass
231 231 ...:
232 232
233 233 In [6]:
234 234
235 235 In [7]: bb=b()
236 236
237 237 In [8]: ss=s()
238 238
239 239 In [9]: rr=r()
240 240
241 241 In [10]: ssrr=sr()
242 242
243 243 In [11]: 4.5
244 244 Out[11]: 4.5
245 245
246 246 In [12]: str(ss)
247 247 Out[12]: 's'
248 248
249 249 In [13]:
250 250
251 251 In [14]: %hist -op
252 252 >>> class b:
253 253 ... pass
254 254 ...
255 255 >>> class s(b):
256 256 ... def __str__(self):
257 257 ... return 's'
258 258 ...
259 259 >>>
260 260 >>> class r(b):
261 261 ... def __repr__(self):
262 262 ... return 'r'
263 263 ...
264 264 >>> class sr(s,r): pass
265 265 >>>
266 266 >>> bb=b()
267 267 >>> ss=s()
268 268 >>> rr=r()
269 269 >>> ssrr=sr()
270 270 >>> 4.5
271 271 4.5
272 272 >>> str(ss)
273 273 's'
274 274 >>>
275 275 """
276 276
277 277 def test_hist_pof():
278 278 ip = get_ipython()
279 279 ip.run_cell(u"1+2", store_history=True)
280 280 #raise Exception(ip.history_manager.session_number)
281 281 #raise Exception(list(ip.history_manager._get_range_session()))
282 282 with TemporaryDirectory() as td:
283 283 tf = os.path.join(td, 'hist.py')
284 284 ip.run_line_magic('history', '-pof %s' % tf)
285 285 assert os.path.isfile(tf)
286 286
287 287
288 288 @dec.skip_without('sqlite3')
289 289 def test_macro():
290 290 ip = get_ipython()
291 291 ip.history_manager.reset() # Clear any existing history.
292 292 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
293 293 for i, cmd in enumerate(cmds, start=1):
294 294 ip.history_manager.store_inputs(i, cmd)
295 295 ip.magic("macro test 1-3")
296 296 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
297 297
298 298 # List macros
299 299 nt.assert_in("test", ip.magic("macro"))
300 300
301 301
302 302 @dec.skip_without('sqlite3')
303 303 def test_macro_run():
304 304 """Test that we can run a multi-line macro successfully."""
305 305 ip = get_ipython()
306 306 ip.history_manager.reset()
307 307 cmds = ["a=10", "a+=1", "print(a)", "%macro test 2-3"]
308 308 for cmd in cmds:
309 309 ip.run_cell(cmd, store_history=True)
310 310 nt.assert_equal(ip.user_ns["test"].value, "a+=1\nprint(a)\n")
311 311 with tt.AssertPrints("12"):
312 312 ip.run_cell("test")
313 313 with tt.AssertPrints("13"):
314 314 ip.run_cell("test")
315 315
316 316
317 317 def test_magic_magic():
318 318 """Test %magic"""
319 319 ip = get_ipython()
320 320 with capture_output() as captured:
321 321 ip.magic("magic")
322 322
323 323 stdout = captured.stdout
324 324 nt.assert_in('%magic', stdout)
325 325 nt.assert_in('IPython', stdout)
326 326 nt.assert_in('Available', stdout)
327 327
328 328
329 329 @dec.skipif_not_numpy
330 330 def test_numpy_reset_array_undec():
331 331 "Test '%reset array' functionality"
332 332 _ip.ex('import numpy as np')
333 333 _ip.ex('a = np.empty(2)')
334 334 nt.assert_in('a', _ip.user_ns)
335 335 _ip.magic('reset -f array')
336 336 nt.assert_not_in('a', _ip.user_ns)
337 337
338 338 def test_reset_out():
339 339 "Test '%reset out' magic"
340 340 _ip.run_cell("parrot = 'dead'", store_history=True)
341 341 # test '%reset -f out', make an Out prompt
342 342 _ip.run_cell("parrot", store_history=True)
343 343 nt.assert_true('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
344 344 _ip.magic('reset -f out')
345 345 nt.assert_false('dead' in [_ip.user_ns[x] for x in ('_','__','___')])
346 346 nt.assert_equal(len(_ip.user_ns['Out']), 0)
347 347
348 348 def test_reset_in():
349 349 "Test '%reset in' magic"
350 350 # test '%reset -f in'
351 351 _ip.run_cell("parrot", store_history=True)
352 352 nt.assert_true('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
353 353 _ip.magic('%reset -f in')
354 354 nt.assert_false('parrot' in [_ip.user_ns[x] for x in ('_i','_ii','_iii')])
355 355 nt.assert_equal(len(set(_ip.user_ns['In'])), 1)
356 356
357 357 def test_reset_dhist():
358 358 "Test '%reset dhist' magic"
359 359 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
360 360 _ip.magic('cd ' + os.path.dirname(nt.__file__))
361 361 _ip.magic('cd -')
362 362 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
363 363 _ip.magic('reset -f dhist')
364 364 nt.assert_equal(len(_ip.user_ns['_dh']), 0)
365 365 _ip.run_cell("_dh = [d for d in tmp]") #restore
366 366
367 367 def test_reset_in_length():
368 368 "Test that '%reset in' preserves In[] length"
369 369 _ip.run_cell("print 'foo'")
370 370 _ip.run_cell("reset -f in")
371 371 nt.assert_equal(len(_ip.user_ns['In']), _ip.displayhook.prompt_count+1)
372 372
373 373 def test_tb_syntaxerror():
374 374 """test %tb after a SyntaxError"""
375 375 ip = get_ipython()
376 376 ip.run_cell("for")
377 377
378 378 # trap and validate stdout
379 379 save_stdout = sys.stdout
380 380 try:
381 381 sys.stdout = StringIO()
382 382 ip.run_cell("%tb")
383 383 out = sys.stdout.getvalue()
384 384 finally:
385 385 sys.stdout = save_stdout
386 386 # trim output, and only check the last line
387 387 last_line = out.rstrip().splitlines()[-1].strip()
388 388 nt.assert_equal(last_line, "SyntaxError: invalid syntax")
389 389
390 390
391 391 def test_time():
392 392 ip = get_ipython()
393 393
394 394 with tt.AssertPrints("Wall time: "):
395 395 ip.run_cell("%time None")
396 396
397 397 ip.run_cell("def f(kmjy):\n"
398 398 " %time print (2*kmjy)")
399 399
400 400 with tt.AssertPrints("Wall time: "):
401 401 with tt.AssertPrints("hihi", suppress=False):
402 402 ip.run_cell("f('hi')")
403 403
404 404
405 405 @dec.skip_win32
406 406 def test_time2():
407 407 ip = get_ipython()
408 408
409 409 with tt.AssertPrints("CPU times: user "):
410 410 ip.run_cell("%time None")
411 411
412 412 def test_time3():
413 413 """Erroneous magic function calls, issue gh-3334"""
414 414 ip = get_ipython()
415 415 ip.user_ns.pop('run', None)
416 416
417 417 with tt.AssertNotPrints("not found", channel='stderr'):
418 418 ip.run_cell("%%time\n"
419 419 "run = 0\n"
420 420 "run += 1")
421 421
422 422 def test_doctest_mode():
423 423 "Toggle doctest_mode twice, it should be a no-op and run without error"
424 424 _ip.magic('doctest_mode')
425 425 _ip.magic('doctest_mode')
426 426
427 427
428 428 def test_parse_options():
429 429 """Tests for basic options parsing in magics."""
430 430 # These are only the most minimal of tests, more should be added later. At
431 431 # the very least we check that basic text/unicode calls work OK.
432 432 m = DummyMagics(_ip)
433 433 nt.assert_equal(m.parse_options('foo', '')[1], 'foo')
434 434 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
435 435
436 436
437 437 def test_dirops():
438 438 """Test various directory handling operations."""
439 439 # curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/')
440 440 curpath = os.getcwd
441 441 startdir = os.getcwd()
442 442 ipdir = os.path.realpath(_ip.ipython_dir)
443 443 try:
444 444 _ip.magic('cd "%s"' % ipdir)
445 445 nt.assert_equal(curpath(), ipdir)
446 446 _ip.magic('cd -')
447 447 nt.assert_equal(curpath(), startdir)
448 448 _ip.magic('pushd "%s"' % ipdir)
449 449 nt.assert_equal(curpath(), ipdir)
450 450 _ip.magic('popd')
451 451 nt.assert_equal(curpath(), startdir)
452 452 finally:
453 453 os.chdir(startdir)
454 454
455 455
456 456 def test_xmode():
457 457 # Calling xmode three times should be a no-op
458 458 xmode = _ip.InteractiveTB.mode
459 459 for i in range(3):
460 460 _ip.magic("xmode")
461 461 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
462 462
463 463 def test_reset_hard():
464 464 monitor = []
465 465 class A(object):
466 466 def __del__(self):
467 467 monitor.append(1)
468 468 def __repr__(self):
469 469 return "<A instance>"
470 470
471 471 _ip.user_ns["a"] = A()
472 472 _ip.run_cell("a")
473 473
474 474 nt.assert_equal(monitor, [])
475 475 _ip.magic("reset -f")
476 476 nt.assert_equal(monitor, [1])
477 477
478 478 class TestXdel(tt.TempFileMixin):
479 479 def test_xdel(self):
480 480 """Test that references from %run are cleared by xdel."""
481 481 src = ("class A(object):\n"
482 482 " monitor = []\n"
483 483 " def __del__(self):\n"
484 484 " self.monitor.append(1)\n"
485 485 "a = A()\n")
486 486 self.mktmp(src)
487 487 # %run creates some hidden references...
488 488 _ip.magic("run %s" % self.fname)
489 489 # ... as does the displayhook.
490 490 _ip.run_cell("a")
491 491
492 492 monitor = _ip.user_ns["A"].monitor
493 493 nt.assert_equal(monitor, [])
494 494
495 495 _ip.magic("xdel a")
496 496
497 497 # Check that a's __del__ method has been called.
498 498 nt.assert_equal(monitor, [1])
499 499
500 500 def doctest_who():
501 501 """doctest for %who
502 502
503 503 In [1]: %reset -f
504 504
505 505 In [2]: alpha = 123
506 506
507 507 In [3]: beta = 'beta'
508 508
509 509 In [4]: %who int
510 510 alpha
511 511
512 512 In [5]: %who str
513 513 beta
514 514
515 515 In [6]: %whos
516 516 Variable Type Data/Info
517 517 ----------------------------
518 518 alpha int 123
519 519 beta str beta
520 520
521 521 In [7]: %who_ls
522 522 Out[7]: ['alpha', 'beta']
523 523 """
524 524
525 525 def test_whos():
526 526 """Check that whos is protected against objects where repr() fails."""
527 527 class A(object):
528 528 def __repr__(self):
529 529 raise Exception()
530 530 _ip.user_ns['a'] = A()
531 531 _ip.magic("whos")
532 532
533 533 def doctest_precision():
534 534 """doctest for %precision
535 535
536 536 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
537 537
538 538 In [2]: %precision 5
539 539 Out[2]: '%.5f'
540 540
541 541 In [3]: f.float_format
542 542 Out[3]: '%.5f'
543 543
544 544 In [4]: %precision %e
545 545 Out[4]: '%e'
546 546
547 547 In [5]: f(3.1415927)
548 548 Out[5]: '3.141593e+00'
549 549 """
550 550
551 551 def test_psearch():
552 552 with tt.AssertPrints("dict.fromkeys"):
553 553 _ip.run_cell("dict.fr*?")
554 554
555 555 def test_timeit_shlex():
556 556 """test shlex issues with timeit (#1109)"""
557 557 _ip.ex("def f(*a,**kw): pass")
558 558 _ip.magic('timeit -n1 "this is a bug".count(" ")')
559 559 _ip.magic('timeit -r1 -n1 f(" ", 1)')
560 560 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
561 561 _ip.magic('timeit -r1 -n1 ("a " + "b")')
562 562 _ip.magic('timeit -r1 -n1 f("a " + "b")')
563 563 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
564 564
565 565
566 566 def test_timeit_special_syntax():
567 567 "Test %%timeit with IPython special syntax"
568 568 @register_line_magic
569 569 def lmagic(line):
570 570 ip = get_ipython()
571 571 ip.user_ns['lmagic_out'] = line
572 572
573 573 # line mode test
574 574 _ip.run_line_magic('timeit', '-n1 -r1 %lmagic my line')
575 575 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
576 576 # cell mode test
577 577 _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2')
578 578 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
579 579
580 580 def test_timeit_return():
581 581 """
582 582 test whether timeit -o return object
583 583 """
584 584
585 585 res = _ip.run_line_magic('timeit','-n10 -r10 -o 1')
586 586 assert(res is not None)
587 587
588 588 def test_timeit_quiet():
589 589 """
590 590 test quiet option of timeit magic
591 591 """
592 592 with tt.AssertNotPrints("loops"):
593 593 _ip.run_cell("%timeit -n1 -r1 -q 1")
594 594
595 595 def test_timeit_return_quiet():
596 596 with tt.AssertNotPrints("loops"):
597 597 res = _ip.run_line_magic('timeit', '-n1 -r1 -q -o 1')
598 598 assert (res is not None)
599 599
600 600 def test_timeit_invalid_return():
601 601 with nt.assert_raises_regex(SyntaxError, "outside function"):
602 602 _ip.run_line_magic('timeit', 'return')
603 603
604 604 @dec.skipif(execution.profile is None)
605 605 def test_prun_special_syntax():
606 606 "Test %%prun with IPython special syntax"
607 607 @register_line_magic
608 608 def lmagic(line):
609 609 ip = get_ipython()
610 610 ip.user_ns['lmagic_out'] = line
611 611
612 612 # line mode test
613 613 _ip.run_line_magic('prun', '-q %lmagic my line')
614 614 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
615 615 # cell mode test
616 616 _ip.run_cell_magic('prun', '-q', '%lmagic my line2')
617 617 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
618 618
619 619 @dec.skipif(execution.profile is None)
620 620 def test_prun_quotes():
621 621 "Test that prun does not clobber string escapes (GH #1302)"
622 622 _ip.magic(r"prun -q x = '\t'")
623 623 nt.assert_equal(_ip.user_ns['x'], '\t')
624 624
625 625 def test_extension():
626 626 # Debugging information for failures of this test
627 627 print('sys.path:')
628 628 for p in sys.path:
629 629 print(' ', p)
630 630 print('CWD', os.getcwd())
631 631
632 632 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
633 633 daft_path = os.path.join(os.path.dirname(__file__), "daft_extension")
634 634 sys.path.insert(0, daft_path)
635 635 try:
636 636 _ip.user_ns.pop('arq', None)
637 637 invalidate_caches() # Clear import caches
638 638 _ip.magic("load_ext daft_extension")
639 639 nt.assert_equal(_ip.user_ns['arq'], 185)
640 640 _ip.magic("unload_ext daft_extension")
641 641 assert 'arq' not in _ip.user_ns
642 642 finally:
643 643 sys.path.remove(daft_path)
644 644
645 645
646 646 def test_notebook_export_json():
647 647 _ip = get_ipython()
648 648 _ip.history_manager.reset() # Clear any existing history.
649 649 cmds = [u"a=1", u"def b():\n return a**2", u"print('noΓ«l, Γ©tΓ©', b())"]
650 650 for i, cmd in enumerate(cmds, start=1):
651 651 _ip.history_manager.store_inputs(i, cmd)
652 652 with TemporaryDirectory() as td:
653 653 outfile = os.path.join(td, "nb.ipynb")
654 654 _ip.magic("notebook -e %s" % outfile)
655 655
656 656
657 657 class TestEnv(TestCase):
658 658
659 659 def test_env(self):
660 660 env = _ip.magic("env")
661 661 self.assertTrue(isinstance(env, dict))
662 662
663 663 def test_env_get_set_simple(self):
664 664 env = _ip.magic("env var val1")
665 665 self.assertEqual(env, None)
666 666 self.assertEqual(os.environ['var'], 'val1')
667 667 self.assertEqual(_ip.magic("env var"), 'val1')
668 668 env = _ip.magic("env var=val2")
669 669 self.assertEqual(env, None)
670 670 self.assertEqual(os.environ['var'], 'val2')
671 671
672 672 def test_env_get_set_complex(self):
673 673 env = _ip.magic("env var 'val1 '' 'val2")
674 674 self.assertEqual(env, None)
675 675 self.assertEqual(os.environ['var'], "'val1 '' 'val2")
676 676 self.assertEqual(_ip.magic("env var"), "'val1 '' 'val2")
677 677 env = _ip.magic('env var=val2 val3="val4')
678 678 self.assertEqual(env, None)
679 679 self.assertEqual(os.environ['var'], 'val2 val3="val4')
680 680
681 681 def test_env_set_bad_input(self):
682 682 self.assertRaises(UsageError, lambda: _ip.magic("set_env var"))
683 683
684 684 def test_env_set_whitespace(self):
685 685 self.assertRaises(UsageError, lambda: _ip.magic("env var A=B"))
686 686
687 687
688 688 class CellMagicTestCase(TestCase):
689 689
690 690 def check_ident(self, magic):
691 691 # Manually called, we get the result
692 692 out = _ip.run_cell_magic(magic, 'a', 'b')
693 693 nt.assert_equal(out, ('a','b'))
694 694 # Via run_cell, it goes into the user's namespace via displayhook
695 695 _ip.run_cell('%%' + magic +' c\nd\n')
696 696 nt.assert_equal(_ip.user_ns['_'], ('c','d\n'))
697 697
698 698 def test_cell_magic_func_deco(self):
699 699 "Cell magic using simple decorator"
700 700 @register_cell_magic
701 701 def cellm(line, cell):
702 702 return line, cell
703 703
704 704 self.check_ident('cellm')
705 705
706 706 def test_cell_magic_reg(self):
707 707 "Cell magic manually registered"
708 708 def cellm(line, cell):
709 709 return line, cell
710 710
711 711 _ip.register_magic_function(cellm, 'cell', 'cellm2')
712 712 self.check_ident('cellm2')
713 713
714 714 def test_cell_magic_class(self):
715 715 "Cell magics declared via a class"
716 716 @magics_class
717 717 class MyMagics(Magics):
718 718
719 719 @cell_magic
720 720 def cellm3(self, line, cell):
721 721 return line, cell
722 722
723 723 _ip.register_magics(MyMagics)
724 724 self.check_ident('cellm3')
725 725
726 726 def test_cell_magic_class2(self):
727 727 "Cell magics declared via a class, #2"
728 728 @magics_class
729 729 class MyMagics2(Magics):
730 730
731 731 @cell_magic('cellm4')
732 732 def cellm33(self, line, cell):
733 733 return line, cell
734 734
735 735 _ip.register_magics(MyMagics2)
736 736 self.check_ident('cellm4')
737 737 # Check that nothing is registered as 'cellm33'
738 738 c33 = _ip.find_cell_magic('cellm33')
739 739 nt.assert_equal(c33, None)
740 740
741 741 def test_file():
742 742 """Basic %%file"""
743 743 ip = get_ipython()
744 744 with TemporaryDirectory() as td:
745 745 fname = os.path.join(td, 'file1')
746 746 ip.run_cell_magic("file", fname, u'\n'.join([
747 747 'line1',
748 748 'line2',
749 749 ]))
750 750 with open(fname) as f:
751 751 s = f.read()
752 752 nt.assert_in('line1\n', s)
753 753 nt.assert_in('line2', s)
754 754
755 755 def test_file_var_expand():
756 756 """%%file $filename"""
757 757 ip = get_ipython()
758 758 with TemporaryDirectory() as td:
759 759 fname = os.path.join(td, 'file1')
760 760 ip.user_ns['filename'] = fname
761 761 ip.run_cell_magic("file", '$filename', u'\n'.join([
762 762 'line1',
763 763 'line2',
764 764 ]))
765 765 with open(fname) as f:
766 766 s = f.read()
767 767 nt.assert_in('line1\n', s)
768 768 nt.assert_in('line2', s)
769 769
770 770 def test_file_unicode():
771 771 """%%file with unicode cell"""
772 772 ip = get_ipython()
773 773 with TemporaryDirectory() as td:
774 774 fname = os.path.join(td, 'file1')
775 775 ip.run_cell_magic("file", fname, u'\n'.join([
776 776 u'linΓ©1',
777 777 u'linΓ©2',
778 778 ]))
779 779 with io.open(fname, encoding='utf-8') as f:
780 780 s = f.read()
781 781 nt.assert_in(u'linΓ©1\n', s)
782 782 nt.assert_in(u'linΓ©2', s)
783 783
784 784 def test_file_amend():
785 785 """%%file -a amends files"""
786 786 ip = get_ipython()
787 787 with TemporaryDirectory() as td:
788 788 fname = os.path.join(td, 'file2')
789 789 ip.run_cell_magic("file", fname, u'\n'.join([
790 790 'line1',
791 791 'line2',
792 792 ]))
793 793 ip.run_cell_magic("file", "-a %s" % fname, u'\n'.join([
794 794 'line3',
795 795 'line4',
796 796 ]))
797 797 with open(fname) as f:
798 798 s = f.read()
799 799 nt.assert_in('line1\n', s)
800 800 nt.assert_in('line3\n', s)
801 801
802 802 def test_file_spaces():
803 803 """%%file with spaces in filename"""
804 804 ip = get_ipython()
805 805 with TemporaryWorkingDirectory() as td:
806 fname = 'file name'
806 fname = "file name"
807 807 ip.run_cell_magic("file", "'%s'"%fname, u'\n'.join([
808 808 'line1',
809 809 'line2',
810 810 ]))
811 811 with open(fname) as f:
812 812 s = f.read()
813 813 nt.assert_in('line1\n', s)
814 814 nt.assert_in('line2', s)
815 815
816 816 def test_script_config():
817 817 ip = get_ipython()
818 818 ip.config.ScriptMagics.script_magics = ['whoda']
819 819 sm = script.ScriptMagics(shell=ip)
820 820 nt.assert_in('whoda', sm.magics['cell'])
821 821
822 822 @dec.skip_win32
823 823 def test_script_out():
824 824 ip = get_ipython()
825 825 ip.run_cell_magic("script", "--out output sh", "echo 'hi'")
826 826 nt.assert_equal(ip.user_ns['output'], 'hi\n')
827 827
828 828 @dec.skip_win32
829 829 def test_script_err():
830 830 ip = get_ipython()
831 831 ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2")
832 832 nt.assert_equal(ip.user_ns['error'], 'hello\n')
833 833
834 834 @dec.skip_win32
835 835 def test_script_out_err():
836 836 ip = get_ipython()
837 837 ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2")
838 838 nt.assert_equal(ip.user_ns['output'], 'hi\n')
839 839 nt.assert_equal(ip.user_ns['error'], 'hello\n')
840 840
841 841 @dec.skip_win32
842 842 def test_script_bg_out():
843 843 ip = get_ipython()
844 844 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
845 845
846 846 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
847 847 ip.user_ns['output'].close()
848 848
849 849 @dec.skip_win32
850 850 def test_script_bg_err():
851 851 ip = get_ipython()
852 852 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
853 853 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
854 854 ip.user_ns['error'].close()
855 855
856 856 @dec.skip_win32
857 857 def test_script_bg_out_err():
858 858 ip = get_ipython()
859 859 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
860 860 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
861 861 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
862 862 ip.user_ns['output'].close()
863 863 ip.user_ns['error'].close()
864 864
865 865 def test_script_defaults():
866 866 ip = get_ipython()
867 867 for cmd in ['sh', 'bash', 'perl', 'ruby']:
868 868 try:
869 869 find_cmd(cmd)
870 870 except Exception:
871 871 pass
872 872 else:
873 873 nt.assert_in(cmd, ip.magics_manager.magics['cell'])
874 874
875 875
876 876 @magics_class
877 877 class FooFoo(Magics):
878 878 """class with both %foo and %%foo magics"""
879 879 @line_magic('foo')
880 880 def line_foo(self, line):
881 881 "I am line foo"
882 882 pass
883 883
884 884 @cell_magic("foo")
885 885 def cell_foo(self, line, cell):
886 886 "I am cell foo, not line foo"
887 887 pass
888 888
889 889 def test_line_cell_info():
890 890 """%%foo and %foo magics are distinguishable to inspect"""
891 891 ip = get_ipython()
892 892 ip.magics_manager.register(FooFoo)
893 893 oinfo = ip.object_inspect('foo')
894 894 nt.assert_true(oinfo['found'])
895 895 nt.assert_true(oinfo['ismagic'])
896 896
897 897 oinfo = ip.object_inspect('%%foo')
898 898 nt.assert_true(oinfo['found'])
899 899 nt.assert_true(oinfo['ismagic'])
900 900 nt.assert_equal(oinfo['docstring'], FooFoo.cell_foo.__doc__)
901 901
902 902 oinfo = ip.object_inspect('%foo')
903 903 nt.assert_true(oinfo['found'])
904 904 nt.assert_true(oinfo['ismagic'])
905 905 nt.assert_equal(oinfo['docstring'], FooFoo.line_foo.__doc__)
906 906
907 907 def test_multiple_magics():
908 908 ip = get_ipython()
909 909 foo1 = FooFoo(ip)
910 910 foo2 = FooFoo(ip)
911 911 mm = ip.magics_manager
912 912 mm.register(foo1)
913 913 nt.assert_true(mm.magics['line']['foo'].__self__ is foo1)
914 914 mm.register(foo2)
915 915 nt.assert_true(mm.magics['line']['foo'].__self__ is foo2)
916 916
917 917 def test_alias_magic():
918 918 """Test %alias_magic."""
919 919 ip = get_ipython()
920 920 mm = ip.magics_manager
921 921
922 922 # Basic operation: both cell and line magics are created, if possible.
923 923 ip.run_line_magic('alias_magic', 'timeit_alias timeit')
924 924 nt.assert_in('timeit_alias', mm.magics['line'])
925 925 nt.assert_in('timeit_alias', mm.magics['cell'])
926 926
927 927 # --cell is specified, line magic not created.
928 928 ip.run_line_magic('alias_magic', '--cell timeit_cell_alias timeit')
929 929 nt.assert_not_in('timeit_cell_alias', mm.magics['line'])
930 930 nt.assert_in('timeit_cell_alias', mm.magics['cell'])
931 931
932 932 # Test that line alias is created successfully.
933 933 ip.run_line_magic('alias_magic', '--line env_alias env')
934 934 nt.assert_equal(ip.run_line_magic('env', ''),
935 935 ip.run_line_magic('env_alias', ''))
936 936
937 937 # Test that line alias with parameters passed in is created successfully.
938 938 ip.run_line_magic('alias_magic', '--line history_alias history --params ' + shlex.quote('3'))
939 939 nt.assert_in('history_alias', mm.magics['line'])
940 940
941 941
942 942 def test_save():
943 943 """Test %save."""
944 944 ip = get_ipython()
945 945 ip.history_manager.reset() # Clear any existing history.
946 946 cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"]
947 947 for i, cmd in enumerate(cmds, start=1):
948 948 ip.history_manager.store_inputs(i, cmd)
949 949 with TemporaryDirectory() as tmpdir:
950 950 file = os.path.join(tmpdir, "testsave.py")
951 951 ip.run_line_magic("save", "%s 1-10" % file)
952 952 with open(file) as f:
953 953 content = f.read()
954 954 nt.assert_equal(content.count(cmds[0]), 1)
955 955 nt.assert_in('coding: utf-8', content)
956 956 ip.run_line_magic("save", "-a %s 1-10" % file)
957 957 with open(file) as f:
958 958 content = f.read()
959 959 nt.assert_equal(content.count(cmds[0]), 2)
960 960 nt.assert_in('coding: utf-8', content)
961 961
962 962
963 963 def test_store():
964 964 """Test %store."""
965 965 ip = get_ipython()
966 966 ip.run_line_magic('load_ext', 'storemagic')
967 967
968 968 # make sure the storage is empty
969 969 ip.run_line_magic('store', '-z')
970 970 ip.user_ns['var'] = 42
971 971 ip.run_line_magic('store', 'var')
972 972 ip.user_ns['var'] = 39
973 973 ip.run_line_magic('store', '-r')
974 974 nt.assert_equal(ip.user_ns['var'], 42)
975 975
976 976 ip.run_line_magic('store', '-d var')
977 977 ip.user_ns['var'] = 39
978 978 ip.run_line_magic('store' , '-r')
979 979 nt.assert_equal(ip.user_ns['var'], 39)
980 980
981 981
982 982 def _run_edit_test(arg_s, exp_filename=None,
983 983 exp_lineno=-1,
984 984 exp_contents=None,
985 985 exp_is_temp=None):
986 986 ip = get_ipython()
987 987 M = code.CodeMagics(ip)
988 988 last_call = ['','']
989 989 opts,args = M.parse_options(arg_s,'prxn:')
990 990 filename, lineno, is_temp = M._find_edit_target(ip, args, opts, last_call)
991 991
992 992 if exp_filename is not None:
993 993 nt.assert_equal(exp_filename, filename)
994 994 if exp_contents is not None:
995 995 with io.open(filename, 'r', encoding='utf-8') as f:
996 996 contents = f.read()
997 997 nt.assert_equal(exp_contents, contents)
998 998 if exp_lineno != -1:
999 999 nt.assert_equal(exp_lineno, lineno)
1000 1000 if exp_is_temp is not None:
1001 1001 nt.assert_equal(exp_is_temp, is_temp)
1002 1002
1003 1003
1004 1004 def test_edit_interactive():
1005 1005 """%edit on interactively defined objects"""
1006 1006 ip = get_ipython()
1007 1007 n = ip.execution_count
1008 1008 ip.run_cell(u"def foo(): return 1", store_history=True)
1009 1009
1010 1010 try:
1011 1011 _run_edit_test("foo")
1012 1012 except code.InteractivelyDefined as e:
1013 1013 nt.assert_equal(e.index, n)
1014 1014 else:
1015 1015 raise AssertionError("Should have raised InteractivelyDefined")
1016 1016
1017 1017
1018 1018 def test_edit_cell():
1019 1019 """%edit [cell id]"""
1020 1020 ip = get_ipython()
1021 1021
1022 1022 ip.run_cell(u"def foo(): return 1", store_history=True)
1023 1023
1024 1024 # test
1025 1025 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
1026 1026
1027 1027 def test_bookmark():
1028 1028 ip = get_ipython()
1029 1029 ip.run_line_magic('bookmark', 'bmname')
1030 1030 with tt.AssertPrints('bmname'):
1031 1031 ip.run_line_magic('bookmark', '-l')
1032 1032 ip.run_line_magic('bookmark', '-d bmname')
1033 1033
1034 1034 def test_ls_magic():
1035 1035 ip = get_ipython()
1036 1036 json_formatter = ip.display_formatter.formatters['application/json']
1037 1037 json_formatter.enabled = True
1038 1038 lsmagic = ip.magic('lsmagic')
1039 1039 with warnings.catch_warnings(record=True) as w:
1040 1040 j = json_formatter(lsmagic)
1041 1041 nt.assert_equal(sorted(j), ['cell', 'line'])
1042 1042 nt.assert_equal(w, []) # no warnings
1043 1043
1044 1044 def test_strip_initial_indent():
1045 1045 def sii(s):
1046 1046 lines = s.splitlines()
1047 1047 return '\n'.join(code.strip_initial_indent(lines))
1048 1048
1049 1049 nt.assert_equal(sii(" a = 1\nb = 2"), "a = 1\nb = 2")
1050 1050 nt.assert_equal(sii(" a\n b\nc"), "a\n b\nc")
1051 1051 nt.assert_equal(sii("a\n b"), "a\n b")
1052 1052
1053 1053 def test_logging_magic_quiet_from_arg():
1054 1054 _ip.config.LoggingMagics.quiet = False
1055 1055 lm = logging.LoggingMagics(shell=_ip)
1056 1056 with TemporaryDirectory() as td:
1057 1057 try:
1058 1058 with tt.AssertNotPrints(re.compile("Activating.*")):
1059 1059 lm.logstart('-q {}'.format(
1060 1060 os.path.join(td, "quiet_from_arg.log")))
1061 1061 finally:
1062 1062 _ip.logger.logstop()
1063 1063
1064 1064 def test_logging_magic_quiet_from_config():
1065 1065 _ip.config.LoggingMagics.quiet = True
1066 1066 lm = logging.LoggingMagics(shell=_ip)
1067 1067 with TemporaryDirectory() as td:
1068 1068 try:
1069 1069 with tt.AssertNotPrints(re.compile("Activating.*")):
1070 1070 lm.logstart(os.path.join(td, "quiet_from_config.log"))
1071 1071 finally:
1072 1072 _ip.logger.logstop()
1073 1073
1074 1074 def test_logging_magic_not_quiet():
1075 1075 _ip.config.LoggingMagics.quiet = False
1076 1076 lm = logging.LoggingMagics(shell=_ip)
1077 1077 with TemporaryDirectory() as td:
1078 1078 try:
1079 1079 with tt.AssertPrints(re.compile("Activating.*")):
1080 1080 lm.logstart(os.path.join(td, "not_quiet.log"))
1081 1081 finally:
1082 1082 _ip.logger.logstop()
1083 1083
1084 1084 ##
1085 1085 # this is slow, put at the end for local testing.
1086 1086 ##
1087 1087 def test_timeit_arguments():
1088 1088 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
1089 1089 if sys.version_info < (3,7):
1090 1090 _ip.magic("timeit ('#')")
1091 1091 else:
1092 1092 # 3.7 optimize no-op statement like above out, and complain there is
1093 1093 # nothing in the for loop.
1094 1094 _ip.magic("timeit a=('#')")
General Comments 0
You need to be logged in to leave comments. Login now