Show More
@@ -26,8 +26,7 b' import textwrap' | |||
|
26 | 26 | import unittest |
|
27 | 27 | from unittest.mock import patch |
|
28 | 28 | |
|
29 | import nose.tools as nt | |
|
30 | from nose import SkipTest | |
|
29 | import pytest | |
|
31 | 30 | |
|
32 | 31 | from IPython.testing import decorators as dec |
|
33 | 32 | from IPython.testing import tools as tt |
@@ -188,7 +187,7 b' class TestMagicRunPass(tt.TempFileMixin):' | |||
|
188 | 187 | bid1 = id(_ip.user_ns['__builtins__']) |
|
189 | 188 | self.run_tmpfile() |
|
190 | 189 | bid2 = id(_ip.user_ns['__builtins__']) |
|
191 |
|
|
|
190 | assert bid1 == bid2 | |
|
192 | 191 | |
|
193 | 192 | def test_builtins_type(self): |
|
194 | 193 | """Check that the type of __builtins__ doesn't change with %run. |
@@ -199,7 +198,7 b' class TestMagicRunPass(tt.TempFileMixin):' | |||
|
199 | 198 | """ |
|
200 | 199 | _ip = get_ipython() |
|
201 | 200 | self.run_tmpfile() |
|
202 |
|
|
|
201 | assert type(_ip.user_ns["__builtins__"]) == type(sys) | |
|
203 | 202 | |
|
204 | 203 |
def test_run_profile( |
|
205 | 204 | """Test that the option -p, which invokes the profiler, do not |
@@ -232,9 +231,9 b' class TestMagicRunSimple(tt.TempFileMixin):' | |||
|
232 | 231 | src = ("class foo: pass\n" |
|
233 | 232 | "def f(): return foo()") |
|
234 | 233 | self.mktmp(src) |
|
235 |
_ip.magic( |
|
|
236 |
_ip.run_cell( |
|
|
237 |
|
|
|
234 | _ip.magic("run %s" % self.fname) | |
|
235 | _ip.run_cell("t = isinstance(f(), foo)") | |
|
236 | assert _ip.user_ns["t"] is True | |
|
238 | 237 | |
|
239 | 238 | def test_obj_del(self): |
|
240 | 239 | """Test that object's __del__ methods are called on exit.""" |
@@ -242,7 +241,7 b' class TestMagicRunSimple(tt.TempFileMixin):' | |||
|
242 | 241 | try: |
|
243 | 242 | import win32api |
|
244 | 243 | except ImportError as e: |
|
245 | raise SkipTest("Test requires pywin32") from e | |
|
244 | raise unittest.SkipTest("Test requires pywin32") from e | |
|
246 | 245 | src = ("class A(object):\n" |
|
247 | 246 | " def __del__(self):\n" |
|
248 | 247 | " print('object A deleted')\n" |
@@ -258,34 +257,33 b' class TestMagicRunSimple(tt.TempFileMixin):' | |||
|
258 | 257 | # see ticket https://github.com/ipython/ipython/issues/238 |
|
259 | 258 | |
|
260 | 259 | with tt.TempFileMixin() as empty: |
|
261 |
empty.mktmp( |
|
|
260 | empty.mktmp("") | |
|
262 | 261 | # On Windows, the filename will have \users in it, so we need to use the |
|
263 | 262 | # repr so that the \u becomes \\u. |
|
264 |
src = ( |
|
|
263 | src = ( | |
|
264 | "ip = get_ipython()\n" | |
|
265 | 265 |
|
|
266 | 266 |
|
|
267 | 267 |
|
|
268 | 268 |
|
|
269 | 269 |
|
|
270 |
|
|
|
270 | " break\n" % ("run " + empty.fname) | |
|
271 | ) | |
|
271 | 272 | self.mktmp(src) |
|
272 |
_ip.magic( |
|
|
273 |
_ip.run_cell( |
|
|
274 |
|
|
|
273 | _ip.magic("run %s" % self.fname) | |
|
274 | _ip.run_cell("ip == get_ipython()") | |
|
275 | assert _ip.user_ns["i"] == 4 | |
|
275 | 276 | |
|
276 | 277 | def test_run_second(self): |
|
277 | """Test that running a second file doesn't clobber the first, gh-3547 | |
|
278 | """ | |
|
279 | self.mktmp("avar = 1\n" | |
|
280 | "def afunc():\n" | |
|
281 | " return avar\n") | |
|
278 | """Test that running a second file doesn't clobber the first, gh-3547""" | |
|
279 | self.mktmp("avar = 1\n" "def afunc():\n" " return avar\n") | |
|
282 | 280 | |
|
283 | 281 | with tt.TempFileMixin() as empty: |
|
284 | 282 | empty.mktmp("") |
|
285 | 283 | |
|
286 |
_ip.magic( |
|
|
287 |
_ip.magic( |
|
|
288 |
|
|
|
284 | _ip.magic("run %s" % self.fname) | |
|
285 | _ip.magic("run %s" % empty.fname) | |
|
286 | assert _ip.user_ns["afunc"]() == 1 | |
|
289 | 287 | |
|
290 | 288 | @dec.skip_win32 |
|
291 | 289 | def test_tclass(self): |
@@ -312,15 +310,15 b' tclass.py: deleting object: C-third' | |||
|
312 | 310 | self.mktmp(src) |
|
313 | 311 | _ip.run_cell("zz = 23") |
|
314 | 312 | try: |
|
315 |
_ip.magic( |
|
|
316 |
|
|
|
313 | _ip.magic("run -i %s" % self.fname) | |
|
314 | assert _ip.user_ns["yy"] == 23 | |
|
317 | 315 | finally: |
|
318 | 316 | _ip.magic('reset -f') |
|
319 | 317 | |
|
320 | 318 | _ip.run_cell("zz = 23") |
|
321 | 319 | try: |
|
322 |
_ip.magic( |
|
|
323 |
|
|
|
320 | _ip.magic("run -i %s" % self.fname) | |
|
321 | assert _ip.user_ns["yy"] == 23 | |
|
324 | 322 | finally: |
|
325 | 323 | _ip.magic('reset -f') |
|
326 | 324 | |
@@ -329,7 +327,7 b' tclass.py: deleting object: C-third' | |||
|
329 | 327 | mydir = os.path.dirname(__file__) |
|
330 | 328 | na = os.path.join(mydir, 'nonascii.py') |
|
331 | 329 | _ip.magic('run "%s"' % na) |
|
332 |
|
|
|
330 | assert _ip.user_ns["u"] == "ΠΡβΠ€" | |
|
333 | 331 | |
|
334 | 332 | def test_run_py_file_attribute(self): |
|
335 | 333 | """Test handling of `__file__` attribute in `%run <file>.py`.""" |
@@ -342,10 +340,10 b' tclass.py: deleting object: C-third' | |||
|
342 | 340 | |
|
343 | 341 | # Check that __file__ was equal to the filename in the script's |
|
344 | 342 | # namespace. |
|
345 |
|
|
|
343 | assert _ip.user_ns["t"] == self.fname | |
|
346 | 344 | |
|
347 | 345 | # Check that __file__ was not leaked back into user_ns. |
|
348 |
|
|
|
346 | assert file1 == file2 | |
|
349 | 347 | |
|
350 | 348 | def test_run_ipy_file_attribute(self): |
|
351 | 349 | """Test handling of `__file__` attribute in `%run <file.ipy>`.""" |
@@ -358,10 +356,10 b' tclass.py: deleting object: C-third' | |||
|
358 | 356 | |
|
359 | 357 | # Check that __file__ was equal to the filename in the script's |
|
360 | 358 | # namespace. |
|
361 |
|
|
|
359 | assert _ip.user_ns["t"] == self.fname | |
|
362 | 360 | |
|
363 | 361 | # Check that __file__ was not leaked back into user_ns. |
|
364 |
|
|
|
362 | assert file1 == file2 | |
|
365 | 363 | |
|
366 | 364 | def test_run_formatting(self): |
|
367 | 365 | """ Test that %run -t -N<N> does not raise a TypeError for N > 1.""" |
@@ -394,16 +392,16 b' tclass.py: deleting object: C-third' | |||
|
394 | 392 | |
|
395 | 393 | _ip.magic("run %s" % self.fname) |
|
396 | 394 | |
|
397 |
|
|
|
395 | assert _ip.user_ns["answer"] == 42 | |
|
398 | 396 | |
|
399 | 397 | def test_run_nb_error(self): |
|
400 | 398 | """Test %run notebook.ipynb error""" |
|
401 | 399 | from nbformat import v4, writes |
|
402 | 400 | # %run when a file name isn't provided |
|
403 |
|
|
|
401 | pytest.raises(Exception, _ip.magic, "run") | |
|
404 | 402 | |
|
405 | 403 | # %run when a file doesn't exist |
|
406 |
|
|
|
404 | pytest.raises(Exception, _ip.magic, "run foobar.ipynb") | |
|
407 | 405 | |
|
408 | 406 | # %run on a notebook with an error |
|
409 | 407 | nb = v4.new_notebook( |
@@ -413,15 +411,15 b' tclass.py: deleting object: C-third' | |||
|
413 | 411 | ) |
|
414 | 412 | src = writes(nb, version=4) |
|
415 | 413 | self.mktmp(src, ext='.ipynb') |
|
416 |
|
|
|
414 | pytest.raises(Exception, _ip.magic, "run %s" % self.fname) | |
|
417 | 415 | |
|
418 | 416 | def test_file_options(self): |
|
419 | 417 | src = ('import sys\n' |
|
420 | 418 | 'a = " ".join(sys.argv[1:])\n') |
|
421 | 419 | self.mktmp(src) |
|
422 |
test_opts = |
|
|
423 |
_ip.run_line_magic("run", |
|
|
424 |
|
|
|
420 | test_opts = "-x 3 --verbose" | |
|
421 | _ip.run_line_magic("run", "{0} {1}".format(self.fname, test_opts)) | |
|
422 | assert _ip.user_ns["a"] == test_opts | |
|
425 | 423 | |
|
426 | 424 | |
|
427 | 425 | class TestMagicRunWithPackage(unittest.TestCase): |
@@ -500,16 +498,17 b' class TestMagicRunWithPackage(unittest.TestCase):' | |||
|
500 | 498 | self.check_run_submodule('relative', '-d') |
|
501 | 499 | |
|
502 | 500 | def test_module_options(self): |
|
503 |
_ip.user_ns.pop( |
|
|
504 |
test_opts = |
|
|
505 |
_ip.run_line_magic( |
|
|
506 |
|
|
|
501 | _ip.user_ns.pop("a", None) | |
|
502 | test_opts = "-x abc -m test" | |
|
503 | _ip.run_line_magic("run", "-m {0}.args {1}".format(self.package, test_opts)) | |
|
504 | assert _ip.user_ns["a"] == test_opts | |
|
507 | 505 | |
|
508 | 506 | def test_module_options_with_separator(self): |
|
509 |
_ip.user_ns.pop( |
|
|
510 |
test_opts = |
|
|
511 |
_ip.run_line_magic( |
|
|
512 |
|
|
|
507 | _ip.user_ns.pop("a", None) | |
|
508 | test_opts = "-x abc -m test" | |
|
509 | _ip.run_line_magic("run", "-m {0}.args -- {1}".format(self.package, test_opts)) | |
|
510 | assert _ip.user_ns["a"] == test_opts | |
|
511 | ||
|
513 | 512 | |
|
514 | 513 | def test_run__name__(): |
|
515 | 514 | with TemporaryDirectory() as td: |
@@ -517,16 +516,16 b' def test_run__name__():' | |||
|
517 | 516 | with open(path, 'w') as f: |
|
518 | 517 | f.write("q = __name__") |
|
519 | 518 | |
|
520 |
_ip.user_ns.pop( |
|
|
521 |
_ip.magic( |
|
|
522 |
|
|
|
519 | _ip.user_ns.pop("q", None) | |
|
520 | _ip.magic("run {}".format(path)) | |
|
521 | assert _ip.user_ns.pop("q") == "__main__" | |
|
523 | 522 | |
|
524 |
_ip.magic( |
|
|
525 |
|
|
|
523 | _ip.magic("run -n {}".format(path)) | |
|
524 | assert _ip.user_ns.pop("q") == "foo" | |
|
526 | 525 | |
|
527 | 526 | try: |
|
528 |
_ip.magic( |
|
|
529 |
|
|
|
527 | _ip.magic("run -i -n {}".format(path)) | |
|
528 | assert _ip.user_ns.pop("q") == "foo" | |
|
530 | 529 | finally: |
|
531 | 530 | _ip.magic('reset -f') |
|
532 | 531 | |
@@ -546,9 +545,9 b' def test_run_tb():' | |||
|
546 | 545 | with capture_output() as io: |
|
547 | 546 | _ip.magic('run {}'.format(path)) |
|
548 | 547 | out = io.stdout |
|
549 |
|
|
|
550 |
|
|
|
551 |
|
|
|
548 | assert "execfile" not in out | |
|
549 | assert "RuntimeError" in out | |
|
550 | assert out.count("---->") == 3 | |
|
552 | 551 | del ip.user_ns['bar'] |
|
553 | 552 | del ip.user_ns['foo'] |
|
554 | 553 | |
@@ -572,10 +571,10 b' def test_multiprocessing_run():' | |||
|
572 | 571 | _ip.run_line_magic('run', path) |
|
573 | 572 | _ip.run_cell("i_m_undefined") |
|
574 | 573 | out = io.stdout |
|
575 |
|
|
|
576 |
|
|
|
577 |
|
|
|
578 |
|
|
|
574 | assert "hoy" in out | |
|
575 | assert "AttributeError" not in out | |
|
576 | assert "NameError" in out | |
|
577 | assert out.count("---->") == 1 | |
|
579 | 578 | except: |
|
580 | 579 | raise |
|
581 | 580 | finally: |
@@ -595,7 +594,6 b' def test_script_tb():' | |||
|
595 | 594 | "foo()", |
|
596 | 595 | ])) |
|
597 | 596 | out, err = tt.ipexec(path) |
|
598 |
|
|
|
599 |
|
|
|
600 |
|
|
|
601 | ||
|
597 | assert "execfile" not in out | |
|
598 | assert "RuntimeError" in out | |
|
599 | assert out.count("---->") == 3 |
General Comments 0
You need to be logged in to leave comments.
Login now