##// END OF EJS Templates
Again
nfgf -
Show More
@@ -1,1471 +1,1471 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Tests for various magic functions."""
3 3
4 4 import gc
5 5 import io
6 6 import os
7 7 import re
8 8 import shlex
9 9 import sys
10 10 import warnings
11 11 from importlib import invalidate_caches
12 12 from io import StringIO
13 13 from pathlib import Path
14 14 from textwrap import dedent
15 15 from unittest import TestCase, mock
16 16
17 17 import pytest
18 18
19 19 from IPython import get_ipython
20 20 from IPython.core import magic
21 21 from IPython.core.error import UsageError
22 22 from IPython.core.magic import (
23 23 Magics,
24 24 cell_magic,
25 25 line_magic,
26 26 magics_class,
27 27 register_cell_magic,
28 28 register_line_magic,
29 29 )
30 30 from IPython.core.magics import code, execution, logging, osm, script
31 31 from IPython.testing import decorators as dec
32 32 from IPython.testing import tools as tt
33 33 from IPython.utils.io import capture_output
34 34 from IPython.utils.process import find_cmd
35 35 from IPython.utils.tempdir import TemporaryDirectory, TemporaryWorkingDirectory
36 36 from IPython.utils.syspathcontext import prepended_to_syspath
37 37
38 38 from .test_debugger import PdbTestInput
39 39
40 40 from tempfile import NamedTemporaryFile
41 41
42 42 @magic.magics_class
43 43 class DummyMagics(magic.Magics): pass
44 44
45 45 def test_extract_code_ranges():
46 46 instr = "1 3 5-6 7-9 10:15 17: :10 10- -13 :"
47 47 expected = [
48 48 (0, 1),
49 49 (2, 3),
50 50 (4, 6),
51 51 (6, 9),
52 52 (9, 14),
53 53 (16, None),
54 54 (None, 9),
55 55 (9, None),
56 56 (None, 13),
57 57 (None, None),
58 58 ]
59 59 actual = list(code.extract_code_ranges(instr))
60 60 assert actual == expected
61 61
62 62 def test_extract_symbols():
63 63 source = """import foo\na = 10\ndef b():\n return 42\n\n\nclass A: pass\n\n\n"""
64 64 symbols_args = ["a", "b", "A", "A,b", "A,a", "z"]
65 65 expected = [([], ['a']),
66 66 (["def b():\n return 42\n"], []),
67 67 (["class A: pass\n"], []),
68 68 (["class A: pass\n", "def b():\n return 42\n"], []),
69 69 (["class A: pass\n"], ['a']),
70 70 ([], ['z'])]
71 71 for symbols, exp in zip(symbols_args, expected):
72 72 assert code.extract_symbols(source, symbols) == exp
73 73
74 74
75 75 def test_extract_symbols_raises_exception_with_non_python_code():
76 76 source = ("=begin A Ruby program :)=end\n"
77 77 "def hello\n"
78 78 "puts 'Hello world'\n"
79 79 "end")
80 80 with pytest.raises(SyntaxError):
81 81 code.extract_symbols(source, "hello")
82 82
83 83
84 84 def test_magic_not_found():
85 85 # magic not found raises UsageError
86 86 with pytest.raises(UsageError):
87 87 _ip.run_line_magic("doesntexist", "")
88 88
89 89 # ensure result isn't success when a magic isn't found
90 90 result = _ip.run_cell('%doesntexist')
91 91 assert isinstance(result.error_in_exec, UsageError)
92 92
93 93
94 94 def test_cell_magic_not_found():
95 95 # magic not found raises UsageError
96 96 with pytest.raises(UsageError):
97 97 _ip.run_cell_magic('doesntexist', 'line', 'cell')
98 98
99 99 # ensure result isn't success when a magic isn't found
100 100 result = _ip.run_cell('%%doesntexist')
101 101 assert isinstance(result.error_in_exec, UsageError)
102 102
103 103
104 104 def test_magic_error_status():
105 105 def fail(shell):
106 106 1/0
107 107 _ip.register_magic_function(fail)
108 108 result = _ip.run_cell('%fail')
109 109 assert isinstance(result.error_in_exec, ZeroDivisionError)
110 110
111 111
112 112 def test_config():
113 113 """ test that config magic does not raise
114 114 can happen if Configurable init is moved too early into
115 115 Magics.__init__ as then a Config object will be registered as a
116 116 magic.
117 117 """
118 118 ## should not raise.
119 119 _ip.run_line_magic("config", "")
120 120
121 121
122 122 def test_config_available_configs():
123 123 """ test that config magic prints available configs in unique and
124 124 sorted order. """
125 125 with capture_output() as captured:
126 126 _ip.run_line_magic("config", "")
127 127
128 128 stdout = captured.stdout
129 129 config_classes = stdout.strip().split('\n')[1:]
130 130 assert config_classes == sorted(set(config_classes))
131 131
132 132 def test_config_print_class():
133 133 """ test that config with a classname prints the class's options. """
134 134 with capture_output() as captured:
135 135 _ip.run_line_magic("config", "TerminalInteractiveShell")
136 136
137 137 stdout = captured.stdout
138 138 assert re.match(
139 139 "TerminalInteractiveShell.* options", stdout.splitlines()[0]
140 140 ), f"{stdout}\n\n1st line of stdout not like 'TerminalInteractiveShell.* options'"
141 141
142 142
143 143 def test_rehashx():
144 144 # clear up everything
145 145 _ip.alias_manager.clear_aliases()
146 146 del _ip.db['syscmdlist']
147 147
148 148 _ip.run_line_magic("rehashx", "")
149 149 # Practically ALL ipython development systems will have more than 10 aliases
150 150
151 151 assert len(_ip.alias_manager.aliases) > 10
152 152 for name, cmd in _ip.alias_manager.aliases:
153 153 # we must strip dots from alias names
154 154 assert "." not in name
155 155
156 156 # rehashx must fill up syscmdlist
157 157 scoms = _ip.db['syscmdlist']
158 158 assert len(scoms) > 10
159 159
160 160
161 161 def test_magic_parse_options():
162 162 """Test that we don't mangle paths when parsing magic options."""
163 163 ip = get_ipython()
164 164 path = 'c:\\x'
165 165 m = DummyMagics(ip)
166 166 opts = m.parse_options('-f %s' % path,'f:')[0]
167 167 # argv splitting is os-dependent
168 168 if os.name == 'posix':
169 169 expected = 'c:x'
170 170 else:
171 171 expected = path
172 172 assert opts["f"] == expected
173 173
174 174
175 175 def test_magic_parse_long_options():
176 176 """Magic.parse_options can handle --foo=bar long options"""
177 177 ip = get_ipython()
178 178 m = DummyMagics(ip)
179 179 opts, _ = m.parse_options("--foo --bar=bubble", "a", "foo", "bar=")
180 180 assert "foo" in opts
181 181 assert "bar" in opts
182 182 assert opts["bar"] == "bubble"
183 183
184 184
185 185 def doctest_hist_f():
186 186 """Test %hist -f with temporary filename.
187 187
188 188 In [9]: import tempfile
189 189
190 190 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
191 191
192 192 In [11]: %hist -nl -f $tfile 3
193 193
194 194 In [13]: import os; os.unlink(tfile)
195 195 """
196 196
197 197
198 198 def doctest_hist_op():
199 199 """Test %hist -op
200 200
201 201 In [1]: class b(float):
202 202 ...: pass
203 203 ...:
204 204
205 205 In [2]: class s(object):
206 206 ...: def __str__(self):
207 207 ...: return 's'
208 208 ...:
209 209
210 210 In [3]:
211 211
212 212 In [4]: class r(b):
213 213 ...: def __repr__(self):
214 214 ...: return 'r'
215 215 ...:
216 216
217 217 In [5]: class sr(s,r): pass
218 218 ...:
219 219
220 220 In [6]:
221 221
222 222 In [7]: bb=b()
223 223
224 224 In [8]: ss=s()
225 225
226 226 In [9]: rr=r()
227 227
228 228 In [10]: ssrr=sr()
229 229
230 230 In [11]: 4.5
231 231 Out[11]: 4.5
232 232
233 233 In [12]: str(ss)
234 234 Out[12]: 's'
235 235
236 236 In [13]:
237 237
238 238 In [14]: %hist -op
239 239 >>> class b:
240 240 ... pass
241 241 ...
242 242 >>> class s(b):
243 243 ... def __str__(self):
244 244 ... return 's'
245 245 ...
246 246 >>>
247 247 >>> class r(b):
248 248 ... def __repr__(self):
249 249 ... return 'r'
250 250 ...
251 251 >>> class sr(s,r): pass
252 252 >>>
253 253 >>> bb=b()
254 254 >>> ss=s()
255 255 >>> rr=r()
256 256 >>> ssrr=sr()
257 257 >>> 4.5
258 258 4.5
259 259 >>> str(ss)
260 260 's'
261 261 >>>
262 262 """
263 263
264 264 def test_hist_pof():
265 265 ip = get_ipython()
266 266 ip.run_cell("1+2", store_history=True)
267 267 #raise Exception(ip.history_manager.session_number)
268 268 #raise Exception(list(ip.history_manager._get_range_session()))
269 269 with TemporaryDirectory() as td:
270 270 tf = os.path.join(td, 'hist.py')
271 271 ip.run_line_magic('history', '-pof %s' % tf)
272 272 assert os.path.isfile(tf)
273 273
274 274
275 275 def test_macro():
276 276 ip = get_ipython()
277 277 ip.history_manager.reset() # Clear any existing history.
278 278 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
279 279 for i, cmd in enumerate(cmds, start=1):
280 280 ip.history_manager.store_inputs(i, cmd)
281 281 ip.run_line_magic("macro", "test 1-3")
282 282 assert ip.user_ns["test"].value == "\n".join(cmds) + "\n"
283 283
284 284 # List macros
285 285 assert "test" in ip.run_line_magic("macro", "")
286 286
287 287
288 288 def test_macro_run():
289 289 """Test that we can run a multi-line macro successfully."""
290 290 ip = get_ipython()
291 291 ip.history_manager.reset()
292 292 cmds = ["a=10", "a+=1", "print(a)", "%macro test 2-3"]
293 293 for cmd in cmds:
294 294 ip.run_cell(cmd, store_history=True)
295 295 assert ip.user_ns["test"].value == "a+=1\nprint(a)\n"
296 296 with tt.AssertPrints("12"):
297 297 ip.run_cell("test")
298 298 with tt.AssertPrints("13"):
299 299 ip.run_cell("test")
300 300
301 301
302 302 def test_magic_magic():
303 303 """Test %magic"""
304 304 ip = get_ipython()
305 305 with capture_output() as captured:
306 306 ip.run_line_magic("magic", "")
307 307
308 308 stdout = captured.stdout
309 309 assert "%magic" in stdout
310 310 assert "IPython" in stdout
311 311 assert "Available" in stdout
312 312
313 313
314 314 @dec.skipif_not_numpy
315 315 def test_numpy_reset_array_undec():
316 316 "Test '%reset array' functionality"
317 317 _ip.ex("import numpy as np")
318 318 _ip.ex("a = np.empty(2)")
319 319 assert "a" in _ip.user_ns
320 320 _ip.run_line_magic("reset", "-f array")
321 321 assert "a" not in _ip.user_ns
322 322
323 323
324 324 def test_reset_out():
325 325 "Test '%reset out' magic"
326 326 _ip.run_cell("parrot = 'dead'", store_history=True)
327 327 # test '%reset -f out', make an Out prompt
328 328 _ip.run_cell("parrot", store_history=True)
329 329 assert "dead" in [_ip.user_ns[x] for x in ("_", "__", "___")]
330 330 _ip.run_line_magic("reset", "-f out")
331 331 assert "dead" not in [_ip.user_ns[x] for x in ("_", "__", "___")]
332 332 assert len(_ip.user_ns["Out"]) == 0
333 333
334 334
335 335 def test_reset_in():
336 336 "Test '%reset in' magic"
337 337 # test '%reset -f in'
338 338 _ip.run_cell("parrot", store_history=True)
339 339 assert "parrot" in [_ip.user_ns[x] for x in ("_i", "_ii", "_iii")]
340 340 _ip.run_line_magic("reset", "-f in")
341 341 assert "parrot" not in [_ip.user_ns[x] for x in ("_i", "_ii", "_iii")]
342 342 assert len(set(_ip.user_ns["In"])) == 1
343 343
344 344
345 345 def test_reset_dhist():
346 346 "Test '%reset dhist' magic"
347 347 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
348 348 _ip.run_line_magic("cd", os.path.dirname(pytest.__file__))
349 349 _ip.run_line_magic("cd", "-")
350 350 assert len(_ip.user_ns["_dh"]) > 0
351 351 _ip.run_line_magic("reset", "-f dhist")
352 352 assert len(_ip.user_ns["_dh"]) == 0
353 353 _ip.run_cell("_dh = [d for d in tmp]") # restore
354 354
355 355
356 356 def test_reset_in_length():
357 357 "Test that '%reset in' preserves In[] length"
358 358 _ip.run_cell("print 'foo'")
359 359 _ip.run_cell("reset -f in")
360 360 assert len(_ip.user_ns["In"]) == _ip.displayhook.prompt_count + 1
361 361
362 362
363 363 class TestResetErrors(TestCase):
364 364
365 365 def test_reset_redefine(self):
366 366
367 367 @magics_class
368 368 class KernelMagics(Magics):
369 369 @line_magic
370 370 def less(self, shell): pass
371 371
372 372 _ip.register_magics(KernelMagics)
373 373
374 374 with self.assertLogs() as cm:
375 375 # hack, we want to just capture logs, but assertLogs fails if not
376 376 # logs get produce.
377 377 # so log one things we ignore.
378 378 import logging as log_mod
379 379 log = log_mod.getLogger()
380 380 log.info('Nothing')
381 381 # end hack.
382 382 _ip.run_cell("reset -f")
383 383
384 384 assert len(cm.output) == 1
385 385 for out in cm.output:
386 386 assert "Invalid alias" not in out
387 387
388 388 def test_tb_syntaxerror():
389 389 """test %tb after a SyntaxError"""
390 390 ip = get_ipython()
391 391 ip.run_cell("for")
392 392
393 393 # trap and validate stdout
394 394 save_stdout = sys.stdout
395 395 try:
396 396 sys.stdout = StringIO()
397 397 ip.run_cell("%tb")
398 398 out = sys.stdout.getvalue()
399 399 finally:
400 400 sys.stdout = save_stdout
401 401 # trim output, and only check the last line
402 402 last_line = out.rstrip().splitlines()[-1].strip()
403 403 assert last_line == "SyntaxError: invalid syntax"
404 404
405 405
406 406 def test_time():
407 407 ip = get_ipython()
408 408
409 409 with tt.AssertPrints("Wall time: "):
410 410 ip.run_cell("%time None")
411 411
412 412 ip.run_cell("def f(kmjy):\n"
413 413 " %time print (2*kmjy)")
414 414
415 415 with tt.AssertPrints("Wall time: "):
416 416 with tt.AssertPrints("hihi", suppress=False):
417 417 ip.run_cell("f('hi')")
418 418
419 419
420 420 # ';' at the end of %time prevents instruction value to be printed.
421 # This tests fix for #13837
421 # This tests fix for #13837.
422 422 def test_time_no_outputwith_semicolon():
423 423 ip = get_ipython()
424 424
425 425 with tt.AssertPrints(" 123456"):
426 426 with tt.AssertPrints("Wall time: ", suppress=False):
427 427 with tt.AssertPrints("CPU times: ", suppress=False):
428 428 ip.run_cell("%time 123000+456")
429 429
430 430 with tt.AssertNotPrints(" 123456"):
431 431 with tt.AssertPrints("Wall time: ", suppress=False):
432 432 with tt.AssertPrints("CPU times: ", suppress=False):
433 433 ip.run_cell("%time 123000+456;")
434 434
435 435
436 436 def test_time_last_not_expression():
437 437 ip.run_cell("%%time\n"
438 438 "var_1 = 1\n"
439 439 "var_2 = 2\n")
440 440 assert ip.user_ns['var_1'] == 1
441 441 del ip.user_ns['var_1']
442 442 assert ip.user_ns['var_2'] == 2
443 443 del ip.user_ns['var_2']
444 444
445 445
446 446 @dec.skip_win32
447 447 def test_time2():
448 448 ip = get_ipython()
449 449
450 450 with tt.AssertPrints("CPU times: user "):
451 451 ip.run_cell("%time None")
452 452
453 453 def test_time3():
454 454 """Erroneous magic function calls, issue gh-3334"""
455 455 ip = get_ipython()
456 456 ip.user_ns.pop('run', None)
457 457
458 458 with tt.AssertNotPrints("not found", channel='stderr'):
459 459 ip.run_cell("%%time\n"
460 460 "run = 0\n"
461 461 "run += 1")
462 462
463 463 def test_multiline_time():
464 464 """Make sure last statement from time return a value."""
465 465 ip = get_ipython()
466 466 ip.user_ns.pop('run', None)
467 467
468 468 ip.run_cell(
469 469 dedent(
470 470 """\
471 471 %%time
472 472 a = "ho"
473 473 b = "hey"
474 474 a+b
475 475 """
476 476 )
477 477 )
478 478 assert ip.user_ns_hidden["_"] == "hohey"
479 479
480 480
481 481 def test_time_local_ns():
482 482 """
483 483 Test that local_ns is actually global_ns when running a cell magic
484 484 """
485 485 ip = get_ipython()
486 486 ip.run_cell("%%time\n" "myvar = 1")
487 487 assert ip.user_ns["myvar"] == 1
488 488 del ip.user_ns["myvar"]
489 489
490 490
491 491 def test_doctest_mode():
492 492 "Toggle doctest_mode twice, it should be a no-op and run without error"
493 493 _ip.run_line_magic("doctest_mode", "")
494 494 _ip.run_line_magic("doctest_mode", "")
495 495
496 496
497 497 def test_parse_options():
498 498 """Tests for basic options parsing in magics."""
499 499 # These are only the most minimal of tests, more should be added later. At
500 500 # the very least we check that basic text/unicode calls work OK.
501 501 m = DummyMagics(_ip)
502 502 assert m.parse_options("foo", "")[1] == "foo"
503 503 assert m.parse_options("foo", "")[1] == "foo"
504 504
505 505
506 506 def test_parse_options_preserve_non_option_string():
507 507 """Test to assert preservation of non-option part of magic-block, while parsing magic options."""
508 508 m = DummyMagics(_ip)
509 509 opts, stmt = m.parse_options(
510 510 " -n1 -r 13 _ = 314 + foo", "n:r:", preserve_non_opts=True
511 511 )
512 512 assert opts == {"n": "1", "r": "13"}
513 513 assert stmt == "_ = 314 + foo"
514 514
515 515
516 516 def test_run_magic_preserve_code_block():
517 517 """Test to assert preservation of non-option part of magic-block, while running magic."""
518 518 _ip.user_ns["spaces"] = []
519 519 _ip.run_line_magic(
520 520 "timeit", "-n1 -r1 spaces.append([s.count(' ') for s in ['document']])"
521 521 )
522 522 assert _ip.user_ns["spaces"] == [[0]]
523 523
524 524
525 525 def test_dirops():
526 526 """Test various directory handling operations."""
527 527 # curpath = lambda :os.path.splitdrive(os.getcwd())[1].replace('\\','/')
528 528 curpath = os.getcwd
529 529 startdir = os.getcwd()
530 530 ipdir = os.path.realpath(_ip.ipython_dir)
531 531 try:
532 532 _ip.run_line_magic("cd", '"%s"' % ipdir)
533 533 assert curpath() == ipdir
534 534 _ip.run_line_magic("cd", "-")
535 535 assert curpath() == startdir
536 536 _ip.run_line_magic("pushd", '"%s"' % ipdir)
537 537 assert curpath() == ipdir
538 538 _ip.run_line_magic("popd", "")
539 539 assert curpath() == startdir
540 540 finally:
541 541 os.chdir(startdir)
542 542
543 543
544 544 def test_cd_force_quiet():
545 545 """Test OSMagics.cd_force_quiet option"""
546 546 _ip.config.OSMagics.cd_force_quiet = True
547 547 osmagics = osm.OSMagics(shell=_ip)
548 548
549 549 startdir = os.getcwd()
550 550 ipdir = os.path.realpath(_ip.ipython_dir)
551 551
552 552 try:
553 553 with tt.AssertNotPrints(ipdir):
554 554 osmagics.cd('"%s"' % ipdir)
555 555 with tt.AssertNotPrints(startdir):
556 556 osmagics.cd('-')
557 557 finally:
558 558 os.chdir(startdir)
559 559
560 560
561 561 def test_xmode():
562 562 # Calling xmode three times should be a no-op
563 563 xmode = _ip.InteractiveTB.mode
564 564 for i in range(4):
565 565 _ip.run_line_magic("xmode", "")
566 566 assert _ip.InteractiveTB.mode == xmode
567 567
568 568 def test_reset_hard():
569 569 monitor = []
570 570 class A(object):
571 571 def __del__(self):
572 572 monitor.append(1)
573 573 def __repr__(self):
574 574 return "<A instance>"
575 575
576 576 _ip.user_ns["a"] = A()
577 577 _ip.run_cell("a")
578 578
579 579 assert monitor == []
580 580 _ip.run_line_magic("reset", "-f")
581 581 assert monitor == [1]
582 582
583 583 class TestXdel(tt.TempFileMixin):
584 584 def test_xdel(self):
585 585 """Test that references from %run are cleared by xdel."""
586 586 src = ("class A(object):\n"
587 587 " monitor = []\n"
588 588 " def __del__(self):\n"
589 589 " self.monitor.append(1)\n"
590 590 "a = A()\n")
591 591 self.mktmp(src)
592 592 # %run creates some hidden references...
593 593 _ip.run_line_magic("run", "%s" % self.fname)
594 594 # ... as does the displayhook.
595 595 _ip.run_cell("a")
596 596
597 597 monitor = _ip.user_ns["A"].monitor
598 598 assert monitor == []
599 599
600 600 _ip.run_line_magic("xdel", "a")
601 601
602 602 # Check that a's __del__ method has been called.
603 603 gc.collect(0)
604 604 assert monitor == [1]
605 605
606 606 def doctest_who():
607 607 """doctest for %who
608 608
609 609 In [1]: %reset -sf
610 610
611 611 In [2]: alpha = 123
612 612
613 613 In [3]: beta = 'beta'
614 614
615 615 In [4]: %who int
616 616 alpha
617 617
618 618 In [5]: %who str
619 619 beta
620 620
621 621 In [6]: %whos
622 622 Variable Type Data/Info
623 623 ----------------------------
624 624 alpha int 123
625 625 beta str beta
626 626
627 627 In [7]: %who_ls
628 628 Out[7]: ['alpha', 'beta']
629 629 """
630 630
631 631 def test_whos():
632 632 """Check that whos is protected against objects where repr() fails."""
633 633 class A(object):
634 634 def __repr__(self):
635 635 raise Exception()
636 636 _ip.user_ns['a'] = A()
637 637 _ip.run_line_magic("whos", "")
638 638
639 639 def doctest_precision():
640 640 """doctest for %precision
641 641
642 642 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
643 643
644 644 In [2]: %precision 5
645 645 Out[2]: '%.5f'
646 646
647 647 In [3]: f.float_format
648 648 Out[3]: '%.5f'
649 649
650 650 In [4]: %precision %e
651 651 Out[4]: '%e'
652 652
653 653 In [5]: f(3.1415927)
654 654 Out[5]: '3.141593e+00'
655 655 """
656 656
657 657 def test_debug_magic():
658 658 """Test debugging a small code with %debug
659 659
660 660 In [1]: with PdbTestInput(['c']):
661 661 ...: %debug print("a b") #doctest: +ELLIPSIS
662 662 ...:
663 663 ...
664 664 ipdb> c
665 665 a b
666 666 In [2]:
667 667 """
668 668
669 669 def test_psearch():
670 670 with tt.AssertPrints("dict.fromkeys"):
671 671 _ip.run_cell("dict.fr*?")
672 672 with tt.AssertPrints("Ο€.is_integer"):
673 673 _ip.run_cell("Ο€ = 3.14;\nΟ€.is_integ*?")
674 674
675 675 def test_timeit_shlex():
676 676 """test shlex issues with timeit (#1109)"""
677 677 _ip.ex("def f(*a,**kw): pass")
678 678 _ip.run_line_magic("timeit", '-n1 "this is a bug".count(" ")')
679 679 _ip.run_line_magic("timeit", '-r1 -n1 f(" ", 1)')
680 680 _ip.run_line_magic("timeit", '-r1 -n1 f(" ", 1, " ", 2, " ")')
681 681 _ip.run_line_magic("timeit", '-r1 -n1 ("a " + "b")')
682 682 _ip.run_line_magic("timeit", '-r1 -n1 f("a " + "b")')
683 683 _ip.run_line_magic("timeit", '-r1 -n1 f("a " + "b ")')
684 684
685 685
686 686 def test_timeit_special_syntax():
687 687 "Test %%timeit with IPython special syntax"
688 688 @register_line_magic
689 689 def lmagic(line):
690 690 ip = get_ipython()
691 691 ip.user_ns['lmagic_out'] = line
692 692
693 693 # line mode test
694 694 _ip.run_line_magic("timeit", "-n1 -r1 %lmagic my line")
695 695 assert _ip.user_ns["lmagic_out"] == "my line"
696 696 # cell mode test
697 697 _ip.run_cell_magic("timeit", "-n1 -r1", "%lmagic my line2")
698 698 assert _ip.user_ns["lmagic_out"] == "my line2"
699 699
700 700
701 701 def test_timeit_return():
702 702 """
703 703 test whether timeit -o return object
704 704 """
705 705
706 706 res = _ip.run_line_magic('timeit','-n10 -r10 -o 1')
707 707 assert(res is not None)
708 708
709 709 def test_timeit_quiet():
710 710 """
711 711 test quiet option of timeit magic
712 712 """
713 713 with tt.AssertNotPrints("loops"):
714 714 _ip.run_cell("%timeit -n1 -r1 -q 1")
715 715
716 716 def test_timeit_return_quiet():
717 717 with tt.AssertNotPrints("loops"):
718 718 res = _ip.run_line_magic('timeit', '-n1 -r1 -q -o 1')
719 719 assert (res is not None)
720 720
721 721 def test_timeit_invalid_return():
722 722 with pytest.raises(SyntaxError):
723 723 _ip.run_line_magic('timeit', 'return')
724 724
725 725 @dec.skipif(execution.profile is None)
726 726 def test_prun_special_syntax():
727 727 "Test %%prun with IPython special syntax"
728 728 @register_line_magic
729 729 def lmagic(line):
730 730 ip = get_ipython()
731 731 ip.user_ns['lmagic_out'] = line
732 732
733 733 # line mode test
734 734 _ip.run_line_magic("prun", "-q %lmagic my line")
735 735 assert _ip.user_ns["lmagic_out"] == "my line"
736 736 # cell mode test
737 737 _ip.run_cell_magic("prun", "-q", "%lmagic my line2")
738 738 assert _ip.user_ns["lmagic_out"] == "my line2"
739 739
740 740
741 741 @dec.skipif(execution.profile is None)
742 742 def test_prun_quotes():
743 743 "Test that prun does not clobber string escapes (GH #1302)"
744 744 _ip.magic(r"prun -q x = '\t'")
745 745 assert _ip.user_ns["x"] == "\t"
746 746
747 747
748 748 def test_extension():
749 749 # Debugging information for failures of this test
750 750 print('sys.path:')
751 751 for p in sys.path:
752 752 print(' ', p)
753 753 print('CWD', os.getcwd())
754 754
755 755 pytest.raises(ImportError, _ip.magic, "load_ext daft_extension")
756 756 daft_path = os.path.join(os.path.dirname(__file__), "daft_extension")
757 757 sys.path.insert(0, daft_path)
758 758 try:
759 759 _ip.user_ns.pop('arq', None)
760 760 invalidate_caches() # Clear import caches
761 761 _ip.run_line_magic("load_ext", "daft_extension")
762 762 assert _ip.user_ns["arq"] == 185
763 763 _ip.run_line_magic("unload_ext", "daft_extension")
764 764 assert 'arq' not in _ip.user_ns
765 765 finally:
766 766 sys.path.remove(daft_path)
767 767
768 768
769 769 def test_notebook_export_json():
770 770 pytest.importorskip("nbformat")
771 771 _ip = get_ipython()
772 772 _ip.history_manager.reset() # Clear any existing history.
773 773 cmds = ["a=1", "def b():\n return a**2", "print('noΓ«l, Γ©tΓ©', b())"]
774 774 for i, cmd in enumerate(cmds, start=1):
775 775 _ip.history_manager.store_inputs(i, cmd)
776 776 with TemporaryDirectory() as td:
777 777 outfile = os.path.join(td, "nb.ipynb")
778 778 _ip.run_line_magic("notebook", "%s" % outfile)
779 779
780 780
781 781 class TestEnv(TestCase):
782 782
783 783 def test_env(self):
784 784 env = _ip.run_line_magic("env", "")
785 785 self.assertTrue(isinstance(env, dict))
786 786
787 787 def test_env_secret(self):
788 788 env = _ip.run_line_magic("env", "")
789 789 hidden = "<hidden>"
790 790 with mock.patch.dict(
791 791 os.environ,
792 792 {
793 793 "API_KEY": "abc123",
794 794 "SECRET_THING": "ssshhh",
795 795 "JUPYTER_TOKEN": "",
796 796 "VAR": "abc"
797 797 }
798 798 ):
799 799 env = _ip.run_line_magic("env", "")
800 800 assert env["API_KEY"] == hidden
801 801 assert env["SECRET_THING"] == hidden
802 802 assert env["JUPYTER_TOKEN"] == hidden
803 803 assert env["VAR"] == "abc"
804 804
805 805 def test_env_get_set_simple(self):
806 806 env = _ip.run_line_magic("env", "var val1")
807 807 self.assertEqual(env, None)
808 808 self.assertEqual(os.environ["var"], "val1")
809 809 self.assertEqual(_ip.run_line_magic("env", "var"), "val1")
810 810 env = _ip.run_line_magic("env", "var=val2")
811 811 self.assertEqual(env, None)
812 812 self.assertEqual(os.environ['var'], 'val2')
813 813
814 814 def test_env_get_set_complex(self):
815 815 env = _ip.run_line_magic("env", "var 'val1 '' 'val2")
816 816 self.assertEqual(env, None)
817 817 self.assertEqual(os.environ['var'], "'val1 '' 'val2")
818 818 self.assertEqual(_ip.run_line_magic("env", "var"), "'val1 '' 'val2")
819 819 env = _ip.run_line_magic("env", 'var=val2 val3="val4')
820 820 self.assertEqual(env, None)
821 821 self.assertEqual(os.environ['var'], 'val2 val3="val4')
822 822
823 823 def test_env_set_bad_input(self):
824 824 self.assertRaises(UsageError, lambda: _ip.run_line_magic("set_env", "var"))
825 825
826 826 def test_env_set_whitespace(self):
827 827 self.assertRaises(UsageError, lambda: _ip.run_line_magic("env", "var A=B"))
828 828
829 829
830 830 class CellMagicTestCase(TestCase):
831 831
832 832 def check_ident(self, magic):
833 833 # Manually called, we get the result
834 834 out = _ip.run_cell_magic(magic, "a", "b")
835 835 assert out == ("a", "b")
836 836 # Via run_cell, it goes into the user's namespace via displayhook
837 837 _ip.run_cell("%%" + magic + " c\nd\n")
838 838 assert _ip.user_ns["_"] == ("c", "d\n")
839 839
840 840 def test_cell_magic_func_deco(self):
841 841 "Cell magic using simple decorator"
842 842 @register_cell_magic
843 843 def cellm(line, cell):
844 844 return line, cell
845 845
846 846 self.check_ident('cellm')
847 847
848 848 def test_cell_magic_reg(self):
849 849 "Cell magic manually registered"
850 850 def cellm(line, cell):
851 851 return line, cell
852 852
853 853 _ip.register_magic_function(cellm, 'cell', 'cellm2')
854 854 self.check_ident('cellm2')
855 855
856 856 def test_cell_magic_class(self):
857 857 "Cell magics declared via a class"
858 858 @magics_class
859 859 class MyMagics(Magics):
860 860
861 861 @cell_magic
862 862 def cellm3(self, line, cell):
863 863 return line, cell
864 864
865 865 _ip.register_magics(MyMagics)
866 866 self.check_ident('cellm3')
867 867
868 868 def test_cell_magic_class2(self):
869 869 "Cell magics declared via a class, #2"
870 870 @magics_class
871 871 class MyMagics2(Magics):
872 872
873 873 @cell_magic('cellm4')
874 874 def cellm33(self, line, cell):
875 875 return line, cell
876 876
877 877 _ip.register_magics(MyMagics2)
878 878 self.check_ident('cellm4')
879 879 # Check that nothing is registered as 'cellm33'
880 880 c33 = _ip.find_cell_magic('cellm33')
881 881 assert c33 == None
882 882
883 883 def test_file():
884 884 """Basic %%writefile"""
885 885 ip = get_ipython()
886 886 with TemporaryDirectory() as td:
887 887 fname = os.path.join(td, "file1")
888 888 ip.run_cell_magic(
889 889 "writefile",
890 890 fname,
891 891 "\n".join(
892 892 [
893 893 "line1",
894 894 "line2",
895 895 ]
896 896 ),
897 897 )
898 898 s = Path(fname).read_text(encoding="utf-8")
899 899 assert "line1\n" in s
900 900 assert "line2" in s
901 901
902 902
903 903 @dec.skip_win32
904 904 def test_file_single_quote():
905 905 """Basic %%writefile with embedded single quotes"""
906 906 ip = get_ipython()
907 907 with TemporaryDirectory() as td:
908 908 fname = os.path.join(td, "'file1'")
909 909 ip.run_cell_magic(
910 910 "writefile",
911 911 fname,
912 912 "\n".join(
913 913 [
914 914 "line1",
915 915 "line2",
916 916 ]
917 917 ),
918 918 )
919 919 s = Path(fname).read_text(encoding="utf-8")
920 920 assert "line1\n" in s
921 921 assert "line2" in s
922 922
923 923
924 924 @dec.skip_win32
925 925 def test_file_double_quote():
926 926 """Basic %%writefile with embedded double quotes"""
927 927 ip = get_ipython()
928 928 with TemporaryDirectory() as td:
929 929 fname = os.path.join(td, '"file1"')
930 930 ip.run_cell_magic(
931 931 "writefile",
932 932 fname,
933 933 "\n".join(
934 934 [
935 935 "line1",
936 936 "line2",
937 937 ]
938 938 ),
939 939 )
940 940 s = Path(fname).read_text(encoding="utf-8")
941 941 assert "line1\n" in s
942 942 assert "line2" in s
943 943
944 944
945 945 def test_file_var_expand():
946 946 """%%writefile $filename"""
947 947 ip = get_ipython()
948 948 with TemporaryDirectory() as td:
949 949 fname = os.path.join(td, "file1")
950 950 ip.user_ns["filename"] = fname
951 951 ip.run_cell_magic(
952 952 "writefile",
953 953 "$filename",
954 954 "\n".join(
955 955 [
956 956 "line1",
957 957 "line2",
958 958 ]
959 959 ),
960 960 )
961 961 s = Path(fname).read_text(encoding="utf-8")
962 962 assert "line1\n" in s
963 963 assert "line2" in s
964 964
965 965
966 966 def test_file_unicode():
967 967 """%%writefile with unicode cell"""
968 968 ip = get_ipython()
969 969 with TemporaryDirectory() as td:
970 970 fname = os.path.join(td, 'file1')
971 971 ip.run_cell_magic("writefile", fname, u'\n'.join([
972 972 u'linΓ©1',
973 973 u'linΓ©2',
974 974 ]))
975 975 with io.open(fname, encoding='utf-8') as f:
976 976 s = f.read()
977 977 assert "linΓ©1\n" in s
978 978 assert "linΓ©2" in s
979 979
980 980
981 981 def test_file_amend():
982 982 """%%writefile -a amends files"""
983 983 ip = get_ipython()
984 984 with TemporaryDirectory() as td:
985 985 fname = os.path.join(td, "file2")
986 986 ip.run_cell_magic(
987 987 "writefile",
988 988 fname,
989 989 "\n".join(
990 990 [
991 991 "line1",
992 992 "line2",
993 993 ]
994 994 ),
995 995 )
996 996 ip.run_cell_magic(
997 997 "writefile",
998 998 "-a %s" % fname,
999 999 "\n".join(
1000 1000 [
1001 1001 "line3",
1002 1002 "line4",
1003 1003 ]
1004 1004 ),
1005 1005 )
1006 1006 s = Path(fname).read_text(encoding="utf-8")
1007 1007 assert "line1\n" in s
1008 1008 assert "line3\n" in s
1009 1009
1010 1010
1011 1011 def test_file_spaces():
1012 1012 """%%file with spaces in filename"""
1013 1013 ip = get_ipython()
1014 1014 with TemporaryWorkingDirectory() as td:
1015 1015 fname = "file name"
1016 1016 ip.run_cell_magic(
1017 1017 "file",
1018 1018 '"%s"' % fname,
1019 1019 "\n".join(
1020 1020 [
1021 1021 "line1",
1022 1022 "line2",
1023 1023 ]
1024 1024 ),
1025 1025 )
1026 1026 s = Path(fname).read_text(encoding="utf-8")
1027 1027 assert "line1\n" in s
1028 1028 assert "line2" in s
1029 1029
1030 1030
1031 1031 def test_script_config():
1032 1032 ip = get_ipython()
1033 1033 ip.config.ScriptMagics.script_magics = ['whoda']
1034 1034 sm = script.ScriptMagics(shell=ip)
1035 1035 assert "whoda" in sm.magics["cell"]
1036 1036
1037 1037
1038 1038 def test_script_out():
1039 1039 ip = get_ipython()
1040 1040 ip.run_cell_magic("script", f"--out output {sys.executable}", "print('hi')")
1041 1041 assert ip.user_ns["output"].strip() == "hi"
1042 1042
1043 1043
1044 1044 def test_script_err():
1045 1045 ip = get_ipython()
1046 1046 ip.run_cell_magic(
1047 1047 "script",
1048 1048 f"--err error {sys.executable}",
1049 1049 "import sys; print('hello', file=sys.stderr)",
1050 1050 )
1051 1051 assert ip.user_ns["error"].strip() == "hello"
1052 1052
1053 1053
1054 1054 def test_script_out_err():
1055 1055
1056 1056 ip = get_ipython()
1057 1057 ip.run_cell_magic(
1058 1058 "script",
1059 1059 f"--out output --err error {sys.executable}",
1060 1060 "\n".join(
1061 1061 [
1062 1062 "import sys",
1063 1063 "print('hi')",
1064 1064 "print('hello', file=sys.stderr)",
1065 1065 ]
1066 1066 ),
1067 1067 )
1068 1068 assert ip.user_ns["output"].strip() == "hi"
1069 1069 assert ip.user_ns["error"].strip() == "hello"
1070 1070
1071 1071
1072 1072 async def test_script_bg_out():
1073 1073 ip = get_ipython()
1074 1074 ip.run_cell_magic("script", f"--bg --out output {sys.executable}", "print('hi')")
1075 1075 assert (await ip.user_ns["output"].read()).strip() == b"hi"
1076 1076 assert ip.user_ns["output"].at_eof()
1077 1077
1078 1078
1079 1079 async def test_script_bg_err():
1080 1080 ip = get_ipython()
1081 1081 ip.run_cell_magic(
1082 1082 "script",
1083 1083 f"--bg --err error {sys.executable}",
1084 1084 "import sys; print('hello', file=sys.stderr)",
1085 1085 )
1086 1086 assert (await ip.user_ns["error"].read()).strip() == b"hello"
1087 1087 assert ip.user_ns["error"].at_eof()
1088 1088
1089 1089
1090 1090 async def test_script_bg_out_err():
1091 1091 ip = get_ipython()
1092 1092 ip.run_cell_magic(
1093 1093 "script",
1094 1094 f"--bg --out output --err error {sys.executable}",
1095 1095 "\n".join(
1096 1096 [
1097 1097 "import sys",
1098 1098 "print('hi')",
1099 1099 "print('hello', file=sys.stderr)",
1100 1100 ]
1101 1101 ),
1102 1102 )
1103 1103 assert (await ip.user_ns["output"].read()).strip() == b"hi"
1104 1104 assert (await ip.user_ns["error"].read()).strip() == b"hello"
1105 1105 assert ip.user_ns["output"].at_eof()
1106 1106 assert ip.user_ns["error"].at_eof()
1107 1107
1108 1108
1109 1109 async def test_script_bg_proc():
1110 1110 ip = get_ipython()
1111 1111 ip.run_cell_magic(
1112 1112 "script",
1113 1113 f"--bg --out output --proc p {sys.executable}",
1114 1114 "\n".join(
1115 1115 [
1116 1116 "import sys",
1117 1117 "print('hi')",
1118 1118 "print('hello', file=sys.stderr)",
1119 1119 ]
1120 1120 ),
1121 1121 )
1122 1122 p = ip.user_ns["p"]
1123 1123 await p.wait()
1124 1124 assert p.returncode == 0
1125 1125 assert (await p.stdout.read()).strip() == b"hi"
1126 1126 # not captured, so empty
1127 1127 assert (await p.stderr.read()) == b""
1128 1128 assert p.stdout.at_eof()
1129 1129 assert p.stderr.at_eof()
1130 1130
1131 1131
1132 1132 def test_script_defaults():
1133 1133 ip = get_ipython()
1134 1134 for cmd in ['sh', 'bash', 'perl', 'ruby']:
1135 1135 try:
1136 1136 find_cmd(cmd)
1137 1137 except Exception:
1138 1138 pass
1139 1139 else:
1140 1140 assert cmd in ip.magics_manager.magics["cell"]
1141 1141
1142 1142
1143 1143 @magics_class
1144 1144 class FooFoo(Magics):
1145 1145 """class with both %foo and %%foo magics"""
1146 1146 @line_magic('foo')
1147 1147 def line_foo(self, line):
1148 1148 "I am line foo"
1149 1149 pass
1150 1150
1151 1151 @cell_magic("foo")
1152 1152 def cell_foo(self, line, cell):
1153 1153 "I am cell foo, not line foo"
1154 1154 pass
1155 1155
1156 1156 def test_line_cell_info():
1157 1157 """%%foo and %foo magics are distinguishable to inspect"""
1158 1158 ip = get_ipython()
1159 1159 ip.magics_manager.register(FooFoo)
1160 1160 oinfo = ip.object_inspect("foo")
1161 1161 assert oinfo["found"] is True
1162 1162 assert oinfo["ismagic"] is True
1163 1163
1164 1164 oinfo = ip.object_inspect("%%foo")
1165 1165 assert oinfo["found"] is True
1166 1166 assert oinfo["ismagic"] is True
1167 1167 assert oinfo["docstring"] == FooFoo.cell_foo.__doc__
1168 1168
1169 1169 oinfo = ip.object_inspect("%foo")
1170 1170 assert oinfo["found"] is True
1171 1171 assert oinfo["ismagic"] is True
1172 1172 assert oinfo["docstring"] == FooFoo.line_foo.__doc__
1173 1173
1174 1174
1175 1175 def test_multiple_magics():
1176 1176 ip = get_ipython()
1177 1177 foo1 = FooFoo(ip)
1178 1178 foo2 = FooFoo(ip)
1179 1179 mm = ip.magics_manager
1180 1180 mm.register(foo1)
1181 1181 assert mm.magics["line"]["foo"].__self__ is foo1
1182 1182 mm.register(foo2)
1183 1183 assert mm.magics["line"]["foo"].__self__ is foo2
1184 1184
1185 1185
1186 1186 def test_alias_magic():
1187 1187 """Test %alias_magic."""
1188 1188 ip = get_ipython()
1189 1189 mm = ip.magics_manager
1190 1190
1191 1191 # Basic operation: both cell and line magics are created, if possible.
1192 1192 ip.run_line_magic("alias_magic", "timeit_alias timeit")
1193 1193 assert "timeit_alias" in mm.magics["line"]
1194 1194 assert "timeit_alias" in mm.magics["cell"]
1195 1195
1196 1196 # --cell is specified, line magic not created.
1197 1197 ip.run_line_magic("alias_magic", "--cell timeit_cell_alias timeit")
1198 1198 assert "timeit_cell_alias" not in mm.magics["line"]
1199 1199 assert "timeit_cell_alias" in mm.magics["cell"]
1200 1200
1201 1201 # Test that line alias is created successfully.
1202 1202 ip.run_line_magic("alias_magic", "--line env_alias env")
1203 1203 assert ip.run_line_magic("env", "") == ip.run_line_magic("env_alias", "")
1204 1204
1205 1205 # Test that line alias with parameters passed in is created successfully.
1206 1206 ip.run_line_magic(
1207 1207 "alias_magic", "--line history_alias history --params " + shlex.quote("3")
1208 1208 )
1209 1209 assert "history_alias" in mm.magics["line"]
1210 1210
1211 1211
1212 1212 def test_save():
1213 1213 """Test %save."""
1214 1214 ip = get_ipython()
1215 1215 ip.history_manager.reset() # Clear any existing history.
1216 1216 cmds = ["a=1", "def b():\n return a**2", "print(a, b())"]
1217 1217 for i, cmd in enumerate(cmds, start=1):
1218 1218 ip.history_manager.store_inputs(i, cmd)
1219 1219 with TemporaryDirectory() as tmpdir:
1220 1220 file = os.path.join(tmpdir, "testsave.py")
1221 1221 ip.run_line_magic("save", "%s 1-10" % file)
1222 1222 content = Path(file).read_text(encoding="utf-8")
1223 1223 assert content.count(cmds[0]) == 1
1224 1224 assert "coding: utf-8" in content
1225 1225 ip.run_line_magic("save", "-a %s 1-10" % file)
1226 1226 content = Path(file).read_text(encoding="utf-8")
1227 1227 assert content.count(cmds[0]) == 2
1228 1228 assert "coding: utf-8" in content
1229 1229
1230 1230
1231 1231 def test_save_with_no_args():
1232 1232 ip = get_ipython()
1233 1233 ip.history_manager.reset() # Clear any existing history.
1234 1234 cmds = ["a=1", "def b():\n return a**2", "print(a, b())", "%save"]
1235 1235 for i, cmd in enumerate(cmds, start=1):
1236 1236 ip.history_manager.store_inputs(i, cmd)
1237 1237
1238 1238 with TemporaryDirectory() as tmpdir:
1239 1239 path = os.path.join(tmpdir, "testsave.py")
1240 1240 ip.run_line_magic("save", path)
1241 1241 content = Path(path).read_text(encoding="utf-8")
1242 1242 expected_content = dedent(
1243 1243 """\
1244 1244 # coding: utf-8
1245 1245 a=1
1246 1246 def b():
1247 1247 return a**2
1248 1248 print(a, b())
1249 1249 """
1250 1250 )
1251 1251 assert content == expected_content
1252 1252
1253 1253
1254 1254 def test_store():
1255 1255 """Test %store."""
1256 1256 ip = get_ipython()
1257 1257 ip.run_line_magic('load_ext', 'storemagic')
1258 1258
1259 1259 # make sure the storage is empty
1260 1260 ip.run_line_magic("store", "-z")
1261 1261 ip.user_ns["var"] = 42
1262 1262 ip.run_line_magic("store", "var")
1263 1263 ip.user_ns["var"] = 39
1264 1264 ip.run_line_magic("store", "-r")
1265 1265 assert ip.user_ns["var"] == 42
1266 1266
1267 1267 ip.run_line_magic("store", "-d var")
1268 1268 ip.user_ns["var"] = 39
1269 1269 ip.run_line_magic("store", "-r")
1270 1270 assert ip.user_ns["var"] == 39
1271 1271
1272 1272
1273 1273 def _run_edit_test(arg_s, exp_filename=None,
1274 1274 exp_lineno=-1,
1275 1275 exp_contents=None,
1276 1276 exp_is_temp=None):
1277 1277 ip = get_ipython()
1278 1278 M = code.CodeMagics(ip)
1279 1279 last_call = ['','']
1280 1280 opts,args = M.parse_options(arg_s,'prxn:')
1281 1281 filename, lineno, is_temp = M._find_edit_target(ip, args, opts, last_call)
1282 1282
1283 1283 if exp_filename is not None:
1284 1284 assert exp_filename == filename
1285 1285 if exp_contents is not None:
1286 1286 with io.open(filename, 'r', encoding='utf-8') as f:
1287 1287 contents = f.read()
1288 1288 assert exp_contents == contents
1289 1289 if exp_lineno != -1:
1290 1290 assert exp_lineno == lineno
1291 1291 if exp_is_temp is not None:
1292 1292 assert exp_is_temp == is_temp
1293 1293
1294 1294
1295 1295 def test_edit_interactive():
1296 1296 """%edit on interactively defined objects"""
1297 1297 ip = get_ipython()
1298 1298 n = ip.execution_count
1299 1299 ip.run_cell("def foo(): return 1", store_history=True)
1300 1300
1301 1301 with pytest.raises(code.InteractivelyDefined) as e:
1302 1302 _run_edit_test("foo")
1303 1303 assert e.value.index == n
1304 1304
1305 1305
1306 1306 def test_edit_cell():
1307 1307 """%edit [cell id]"""
1308 1308 ip = get_ipython()
1309 1309
1310 1310 ip.run_cell("def foo(): return 1", store_history=True)
1311 1311
1312 1312 # test
1313 1313 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
1314 1314
1315 1315 def test_edit_fname():
1316 1316 """%edit file"""
1317 1317 # test
1318 1318 _run_edit_test("test file.py", exp_filename="test file.py")
1319 1319
1320 1320 def test_bookmark():
1321 1321 ip = get_ipython()
1322 1322 ip.run_line_magic('bookmark', 'bmname')
1323 1323 with tt.AssertPrints('bmname'):
1324 1324 ip.run_line_magic('bookmark', '-l')
1325 1325 ip.run_line_magic('bookmark', '-d bmname')
1326 1326
1327 1327 def test_ls_magic():
1328 1328 ip = get_ipython()
1329 1329 json_formatter = ip.display_formatter.formatters['application/json']
1330 1330 json_formatter.enabled = True
1331 1331 lsmagic = ip.run_line_magic("lsmagic", "")
1332 1332 with warnings.catch_warnings(record=True) as w:
1333 1333 j = json_formatter(lsmagic)
1334 1334 assert sorted(j) == ["cell", "line"]
1335 1335 assert w == [] # no warnings
1336 1336
1337 1337
1338 1338 def test_strip_initial_indent():
1339 1339 def sii(s):
1340 1340 lines = s.splitlines()
1341 1341 return '\n'.join(code.strip_initial_indent(lines))
1342 1342
1343 1343 assert sii(" a = 1\nb = 2") == "a = 1\nb = 2"
1344 1344 assert sii(" a\n b\nc") == "a\n b\nc"
1345 1345 assert sii("a\n b") == "a\n b"
1346 1346
1347 1347 def test_logging_magic_quiet_from_arg():
1348 1348 _ip.config.LoggingMagics.quiet = False
1349 1349 lm = logging.LoggingMagics(shell=_ip)
1350 1350 with TemporaryDirectory() as td:
1351 1351 try:
1352 1352 with tt.AssertNotPrints(re.compile("Activating.*")):
1353 1353 lm.logstart('-q {}'.format(
1354 1354 os.path.join(td, "quiet_from_arg.log")))
1355 1355 finally:
1356 1356 _ip.logger.logstop()
1357 1357
1358 1358 def test_logging_magic_quiet_from_config():
1359 1359 _ip.config.LoggingMagics.quiet = True
1360 1360 lm = logging.LoggingMagics(shell=_ip)
1361 1361 with TemporaryDirectory() as td:
1362 1362 try:
1363 1363 with tt.AssertNotPrints(re.compile("Activating.*")):
1364 1364 lm.logstart(os.path.join(td, "quiet_from_config.log"))
1365 1365 finally:
1366 1366 _ip.logger.logstop()
1367 1367
1368 1368
1369 1369 def test_logging_magic_not_quiet():
1370 1370 _ip.config.LoggingMagics.quiet = False
1371 1371 lm = logging.LoggingMagics(shell=_ip)
1372 1372 with TemporaryDirectory() as td:
1373 1373 try:
1374 1374 with tt.AssertPrints(re.compile("Activating.*")):
1375 1375 lm.logstart(os.path.join(td, "not_quiet.log"))
1376 1376 finally:
1377 1377 _ip.logger.logstop()
1378 1378
1379 1379
1380 1380 def test_time_no_var_expand():
1381 1381 _ip.user_ns["a"] = 5
1382 1382 _ip.user_ns["b"] = []
1383 1383 _ip.run_line_magic("time", 'b.append("{a}")')
1384 1384 assert _ip.user_ns["b"] == ["{a}"]
1385 1385
1386 1386
1387 1387 # this is slow, put at the end for local testing.
1388 1388 def test_timeit_arguments():
1389 1389 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
1390 1390 _ip.run_line_magic("timeit", "-n1 -r1 a=('#')")
1391 1391
1392 1392
1393 1393 MINIMAL_LAZY_MAGIC = """
1394 1394 from IPython.core.magic import (
1395 1395 Magics,
1396 1396 magics_class,
1397 1397 line_magic,
1398 1398 cell_magic,
1399 1399 )
1400 1400
1401 1401
1402 1402 @magics_class
1403 1403 class LazyMagics(Magics):
1404 1404 @line_magic
1405 1405 def lazy_line(self, line):
1406 1406 print("Lazy Line")
1407 1407
1408 1408 @cell_magic
1409 1409 def lazy_cell(self, line, cell):
1410 1410 print("Lazy Cell")
1411 1411
1412 1412
1413 1413 def load_ipython_extension(ipython):
1414 1414 ipython.register_magics(LazyMagics)
1415 1415 """
1416 1416
1417 1417
1418 1418 def test_lazy_magics():
1419 1419 with pytest.raises(UsageError):
1420 1420 ip.run_line_magic("lazy_line", "")
1421 1421
1422 1422 startdir = os.getcwd()
1423 1423
1424 1424 with TemporaryDirectory() as tmpdir:
1425 1425 with prepended_to_syspath(tmpdir):
1426 1426 ptempdir = Path(tmpdir)
1427 1427 tf = ptempdir / "lazy_magic_module.py"
1428 1428 tf.write_text(MINIMAL_LAZY_MAGIC)
1429 1429 ip.magics_manager.register_lazy("lazy_line", Path(tf.name).name[:-3])
1430 1430 with tt.AssertPrints("Lazy Line"):
1431 1431 ip.run_line_magic("lazy_line", "")
1432 1432
1433 1433
1434 1434 TEST_MODULE = """
1435 1435 print('Loaded my_tmp')
1436 1436 if __name__ == "__main__":
1437 1437 print('I just ran a script')
1438 1438 """
1439 1439
1440 1440 def test_run_module_from_import_hook():
1441 1441 "Test that a module can be loaded via an import hook"
1442 1442 with TemporaryDirectory() as tmpdir:
1443 1443 fullpath = os.path.join(tmpdir, "my_tmp.py")
1444 1444 Path(fullpath).write_text(TEST_MODULE, encoding="utf-8")
1445 1445
1446 1446 import importlib.abc
1447 1447 import importlib.util
1448 1448
1449 1449 class MyTempImporter(importlib.abc.MetaPathFinder, importlib.abc.SourceLoader):
1450 1450 def find_spec(self, fullname, path, target=None):
1451 1451 if fullname == "my_tmp":
1452 1452 return importlib.util.spec_from_loader(fullname, self)
1453 1453
1454 1454 def get_filename(self, fullname):
1455 1455 assert fullname == "my_tmp"
1456 1456 return fullpath
1457 1457
1458 1458 def get_data(self, path):
1459 1459 assert Path(path).samefile(fullpath)
1460 1460 return Path(fullpath).read_text(encoding="utf-8")
1461 1461
1462 1462 sys.meta_path.insert(0, MyTempImporter())
1463 1463
1464 1464 with capture_output() as captured:
1465 1465 _ip.run_line_magic("run", "-m my_tmp")
1466 1466 _ip.run_cell("import my_tmp")
1467 1467
1468 1468 output = "Loaded my_tmp\nI just ran a script\nLoaded my_tmp\n"
1469 1469 assert output == captured.stdout
1470 1470
1471 1471 sys.meta_path.pop(0)
General Comments 0
You need to be logged in to leave comments. Login now