##// END OF EJS Templates
Backport PR #12301: Exceptions raised when executing notebooks via the %run magic command
Matthias Bussonnier -
Show More
@@ -688,17 +688,16 b' python-profiler package from non-free.""")'
688 688 modulename = opts["m"][0]
689 689 modpath = find_mod(modulename)
690 690 if modpath is None:
691 warn('%r is not a valid modulename on sys.path'%modulename)
692 return
691 msg = '%r is not a valid modulename on sys.path'%modulename
692 raise Exception(msg)
693 693 arg_lst = [modpath] + arg_lst
694 694 try:
695 695 fpath = None # initialize to make sure fpath is in scope later
696 696 fpath = arg_lst[0]
697 697 filename = file_finder(fpath)
698 698 except IndexError:
699 warn('you must provide at least a filename.')
700 print('\n%run:\n', oinspect.getdoc(self.run))
701 return
699 msg = 'you must provide at least a filename.'
700 raise Exception(msg)
702 701 except IOError as e:
703 702 try:
704 703 msg = str(e)
@@ -706,13 +705,17 b' python-profiler package from non-free.""")'
706 705 msg = e.message
707 706 if os.name == 'nt' and re.match(r"^'.*'$",fpath):
708 707 warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"')
709 error(msg)
710 return
708 raise Exception(msg)
709 except TypeError:
710 if fpath in sys.meta_path:
711 filename = ""
712 else:
713 raise
711 714
712 715 if filename.lower().endswith(('.ipy', '.ipynb')):
713 716 with preserve_keys(self.shell.user_ns, '__file__'):
714 717 self.shell.user_ns['__file__'] = filename
715 self.shell.safe_execfile_ipy(filename)
718 self.shell.safe_execfile_ipy(filename, raise_exceptions=True)
716 719 return
717 720
718 721 # Control the response to exit() calls made by the script being run
@@ -402,6 +402,25 b' tclass.py: deleting object: C-third'
402 402
403 403 nt.assert_equal(_ip.user_ns['answer'], 42)
404 404
405 def test_run_nb_error(self):
406 """Test %run notebook.ipynb error"""
407 from nbformat import v4, writes
408 # %run when a file name isn't provided
409 nt.assert_raises(Exception, _ip.magic, "run")
410
411 # %run when a file doesn't exist
412 nt.assert_raises(Exception, _ip.magic, "run foobar.ipynb")
413
414 # %run on a notebook with an error
415 nb = v4.new_notebook(
416 cells=[
417 v4.new_code_cell("0/0")
418 ]
419 )
420 src = writes(nb, version=4)
421 self.mktmp(src, ext='.ipynb')
422 nt.assert_raises(Exception, _ip.magic, "run %s" % self.fname)
423
405 424 def test_file_options(self):
406 425 src = ('import sys\n'
407 426 'a = " ".join(sys.argv[1:])\n')
General Comments 0
You need to be logged in to leave comments. Login now