##// 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 modulename = opts["m"][0]
688 modulename = opts["m"][0]
689 modpath = find_mod(modulename)
689 modpath = find_mod(modulename)
690 if modpath is None:
690 if modpath is None:
691 warn('%r is not a valid modulename on sys.path'%modulename)
691 msg = '%r is not a valid modulename on sys.path'%modulename
692 return
692 raise Exception(msg)
693 arg_lst = [modpath] + arg_lst
693 arg_lst = [modpath] + arg_lst
694 try:
694 try:
695 fpath = None # initialize to make sure fpath is in scope later
695 fpath = None # initialize to make sure fpath is in scope later
696 fpath = arg_lst[0]
696 fpath = arg_lst[0]
697 filename = file_finder(fpath)
697 filename = file_finder(fpath)
698 except IndexError:
698 except IndexError:
699 warn('you must provide at least a filename.')
699 msg = 'you must provide at least a filename.'
700 print('\n%run:\n', oinspect.getdoc(self.run))
700 raise Exception(msg)
701 return
702 except IOError as e:
701 except IOError as e:
703 try:
702 try:
704 msg = str(e)
703 msg = str(e)
@@ -706,13 +705,17 b' python-profiler package from non-free.""")'
706 msg = e.message
705 msg = e.message
707 if os.name == 'nt' and re.match(r"^'.*'$",fpath):
706 if os.name == 'nt' and re.match(r"^'.*'$",fpath):
708 warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"')
707 warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"')
709 error(msg)
708 raise Exception(msg)
710 return
709 except TypeError:
710 if fpath in sys.meta_path:
711 filename = ""
712 else:
713 raise
711
714
712 if filename.lower().endswith(('.ipy', '.ipynb')):
715 if filename.lower().endswith(('.ipy', '.ipynb')):
713 with preserve_keys(self.shell.user_ns, '__file__'):
716 with preserve_keys(self.shell.user_ns, '__file__'):
714 self.shell.user_ns['__file__'] = filename
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 return
719 return
717
720
718 # Control the response to exit() calls made by the script being run
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 nt.assert_equal(_ip.user_ns['answer'], 42)
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 def test_file_options(self):
424 def test_file_options(self):
406 src = ('import sys\n'
425 src = ('import sys\n'
407 'a = " ".join(sys.argv[1:])\n')
426 'a = " ".join(sys.argv[1:])\n')
General Comments 0
You need to be logged in to leave comments. Login now