##// END OF EJS Templates
Merge pull request #12301 from datadesk/run-magic-raise-errors
Matthias Bussonnier -
r25792:c24e8af4 merge
parent child Browse files
Show More
@@ -672,17 +672,16 b' class ExecutionMagics(Magics):'
672 modulename = opts["m"][0]
672 modulename = opts["m"][0]
673 modpath = find_mod(modulename)
673 modpath = find_mod(modulename)
674 if modpath is None:
674 if modpath is None:
675 warn('%r is not a valid modulename on sys.path'%modulename)
675 msg = '%r is not a valid modulename on sys.path'%modulename
676 return
676 raise Exception(msg)
677 arg_lst = [modpath] + arg_lst
677 arg_lst = [modpath] + arg_lst
678 try:
678 try:
679 fpath = None # initialize to make sure fpath is in scope later
679 fpath = None # initialize to make sure fpath is in scope later
680 fpath = arg_lst[0]
680 fpath = arg_lst[0]
681 filename = file_finder(fpath)
681 filename = file_finder(fpath)
682 except IndexError:
682 except IndexError:
683 warn('you must provide at least a filename.')
683 msg = 'you must provide at least a filename.'
684 print('\n%run:\n', oinspect.getdoc(self.run))
684 raise Exception(msg)
685 return
686 except IOError as e:
685 except IOError as e:
687 try:
686 try:
688 msg = str(e)
687 msg = str(e)
@@ -690,8 +689,7 b' class ExecutionMagics(Magics):'
690 msg = e.message
689 msg = e.message
691 if os.name == 'nt' and re.match(r"^'.*'$",fpath):
690 if os.name == 'nt' and re.match(r"^'.*'$",fpath):
692 warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"')
691 warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"')
693 error(msg)
692 raise Exception(msg)
694 return
695 except TypeError:
693 except TypeError:
696 if fpath in sys.meta_path:
694 if fpath in sys.meta_path:
697 filename = ""
695 filename = ""
@@ -701,7 +699,7 b' class ExecutionMagics(Magics):'
701 if filename.lower().endswith(('.ipy', '.ipynb')):
699 if filename.lower().endswith(('.ipy', '.ipynb')):
702 with preserve_keys(self.shell.user_ns, '__file__'):
700 with preserve_keys(self.shell.user_ns, '__file__'):
703 self.shell.user_ns['__file__'] = filename
701 self.shell.user_ns['__file__'] = filename
704 self.shell.safe_execfile_ipy(filename)
702 self.shell.safe_execfile_ipy(filename, raise_exceptions=True)
705 return
703 return
706
704
707 # Control the response to exit() calls made by the script being run
705 # Control the response to exit() calls made by the script being run
@@ -396,6 +396,25 b' tclass.py: deleting object: C-third'
396
396
397 nt.assert_equal(_ip.user_ns['answer'], 42)
397 nt.assert_equal(_ip.user_ns['answer'], 42)
398
398
399 def test_run_nb_error(self):
400 """Test %run notebook.ipynb error"""
401 from nbformat import v4, writes
402 # %run when a file name isn't provided
403 nt.assert_raises(Exception, _ip.magic, "run")
404
405 # %run when a file doesn't exist
406 nt.assert_raises(Exception, _ip.magic, "run foobar.ipynb")
407
408 # %run on a notebook with an error
409 nb = v4.new_notebook(
410 cells=[
411 v4.new_code_cell("0/0")
412 ]
413 )
414 src = writes(nb, version=4)
415 self.mktmp(src, ext='.ipynb')
416 nt.assert_raises(Exception, _ip.magic, "run %s" % self.fname)
417
399 def test_file_options(self):
418 def test_file_options(self):
400 src = ('import sys\n'
419 src = ('import sys\n'
401 'a = " ".join(sys.argv[1:])\n')
420 'a = " ".join(sys.argv[1:])\n')
General Comments 0
You need to be logged in to leave comments. Login now