##// END OF EJS Templates
Fix exception causes in 7 modules
Ram Rachum -
Show More
@@ -643,17 +643,17 b' class PlainTextFormatter(BaseFormatter):'
643 fmt = new
643 fmt = new
644 try:
644 try:
645 fmt%3.14159
645 fmt%3.14159
646 except Exception:
646 except Exception as e:
647 raise ValueError("Precision must be int or format string, not %r"%new)
647 raise ValueError("Precision must be int or format string, not %r"%new) from e
648 elif new:
648 elif new:
649 # otherwise, should be an int
649 # otherwise, should be an int
650 try:
650 try:
651 i = int(new)
651 i = int(new)
652 assert i >= 0
652 assert i >= 0
653 except ValueError:
653 except ValueError as e:
654 raise ValueError("Precision must be int or format string, not %r"%new)
654 raise ValueError("Precision must be int or format string, not %r"%new) from e
655 except AssertionError:
655 except AssertionError as e:
656 raise ValueError("int precision must be non-negative, not %r"%i)
656 raise ValueError("int precision must be non-negative, not %r"%i) from e
657
657
658 fmt = '%%.%if'%i
658 fmt = '%%.%if'%i
659 if 'numpy' in sys.modules:
659 if 'numpy' in sys.modules:
@@ -1490,8 +1490,8 b' class InteractiveShell(SingletonConfigurable):'
1490 else: # Delete by object
1490 else: # Delete by object
1491 try:
1491 try:
1492 obj = self.user_ns[varname]
1492 obj = self.user_ns[varname]
1493 except KeyError:
1493 except KeyError as e:
1494 raise NameError("name '%s' is not defined" % varname)
1494 raise NameError("name '%s' is not defined" % varname) from e
1495 # Also check in output history
1495 # Also check in output history
1496 ns_refs.append(self.history_manager.output_hist)
1496 ns_refs.append(self.history_manager.output_hist)
1497 for ns in ns_refs:
1497 for ns in ns_refs:
@@ -1521,8 +1521,8 b' class InteractiveShell(SingletonConfigurable):'
1521 if regex is not None:
1521 if regex is not None:
1522 try:
1522 try:
1523 m = re.compile(regex)
1523 m = re.compile(regex)
1524 except TypeError:
1524 except TypeError as e:
1525 raise TypeError('regex must be a string or compiled pattern')
1525 raise TypeError('regex must be a string or compiled pattern') from e
1526 # Search for keys in each namespace that match the given regex
1526 # Search for keys in each namespace that match the given regex
1527 # If a match is found, delete the key/value pair.
1527 # If a match is found, delete the key/value pair.
1528 for ns in self.all_ns_refs:
1528 for ns in self.all_ns_refs:
@@ -3614,13 +3614,13 b' class InteractiveShell(SingletonConfigurable):'
3614 try:
3614 try:
3615 if target.startswith(('http://', 'https://')):
3615 if target.startswith(('http://', 'https://')):
3616 return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
3616 return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
3617 except UnicodeDecodeError:
3617 except UnicodeDecodeError as e:
3618 if not py_only :
3618 if not py_only :
3619 # Deferred import
3619 # Deferred import
3620 from urllib.request import urlopen
3620 from urllib.request import urlopen
3621 response = urlopen(target)
3621 response = urlopen(target)
3622 return response.read().decode('latin1')
3622 return response.read().decode('latin1')
3623 raise ValueError(("'%s' seem to be unreadable.") % target)
3623 raise ValueError(("'%s' seem to be unreadable.") % target) from e
3624
3624
3625 potential_target = [target]
3625 potential_target = [target]
3626 try :
3626 try :
@@ -3632,11 +3632,11 b' class InteractiveShell(SingletonConfigurable):'
3632 if os.path.isfile(tgt): # Read file
3632 if os.path.isfile(tgt): # Read file
3633 try :
3633 try :
3634 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3634 return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
3635 except UnicodeDecodeError :
3635 except UnicodeDecodeError as e:
3636 if not py_only :
3636 if not py_only :
3637 with io_open(tgt,'r', encoding='latin1') as f :
3637 with io_open(tgt,'r', encoding='latin1') as f :
3638 return f.read()
3638 return f.read()
3639 raise ValueError(("'%s' seem to be unreadable.") % target)
3639 raise ValueError(("'%s' seem to be unreadable.") % target) from e
3640 elif os.path.isdir(os.path.expanduser(tgt)):
3640 elif os.path.isdir(os.path.expanduser(tgt)):
3641 raise ValueError("'%s' is a directory, not a regular file." % target)
3641 raise ValueError("'%s' is a directory, not a regular file." % target)
3642
3642
@@ -3648,9 +3648,9 b' class InteractiveShell(SingletonConfigurable):'
3648
3648
3649 try: # User namespace
3649 try: # User namespace
3650 codeobj = eval(target, self.user_ns)
3650 codeobj = eval(target, self.user_ns)
3651 except Exception:
3651 except Exception as e:
3652 raise ValueError(("'%s' was not found in history, as a file, url, "
3652 raise ValueError(("'%s' was not found in history, as a file, url, "
3653 "nor in the user namespace.") % target)
3653 "nor in the user namespace.") % target) from e
3654
3654
3655 if isinstance(codeobj, str):
3655 if isinstance(codeobj, str):
3656 return codeobj
3656 return codeobj
@@ -630,7 +630,7 b' class Magics(Configurable):'
630 opts,args = getopt(argv, opt_str, long_opts)
630 opts,args = getopt(argv, opt_str, long_opts)
631 except GetoptError as e:
631 except GetoptError as e:
632 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
632 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
633 " ".join(long_opts)))
633 " ".join(long_opts))) from e
634 for o,a in opts:
634 for o,a in opts:
635 if o.startswith('--'):
635 if o.startswith('--'):
636 o = o[2:]
636 o = o[2:]
@@ -440,10 +440,10 b' class CodeMagics(Magics):'
440 return (None, None, None)
440 return (None, None, None)
441 use_temp = False
441 use_temp = False
442
442
443 except DataIsObject:
443 except DataIsObject as e:
444 # macros have a special edit function
444 # macros have a special edit function
445 if isinstance(data, Macro):
445 if isinstance(data, Macro):
446 raise MacroToEdit(data)
446 raise MacroToEdit(data) from e
447
447
448 # For objects, try to edit the file where they are defined
448 # For objects, try to edit the file where they are defined
449 filename = find_file(data)
449 filename = find_file(data)
@@ -467,8 +467,8 b' class CodeMagics(Magics):'
467
467
468 m = ipython_input_pat.match(os.path.basename(filename))
468 m = ipython_input_pat.match(os.path.basename(filename))
469 if m:
469 if m:
470 raise InteractivelyDefined(int(m.groups()[0]))
470 raise InteractivelyDefined(int(m.groups()[0])) from e
471
471
472 datafile = 1
472 datafile = 1
473 if filename is None:
473 if filename is None:
474 filename = make_filename(args)
474 filename = make_filename(args)
@@ -679,9 +679,9 b' class ExecutionMagics(Magics):'
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 as e:
683 msg = 'you must provide at least a filename.'
683 msg = 'you must provide at least a filename.'
684 raise Exception(msg)
684 raise Exception(msg) from e
685 except IOError as e:
685 except IOError as e:
686 try:
686 try:
687 msg = str(e)
687 msg = str(e)
@@ -689,7 +689,7 b' class ExecutionMagics(Magics):'
689 msg = e.message
689 msg = e.message
690 if os.name == 'nt' and re.match(r"^'.*'$",fpath):
690 if os.name == 'nt' and re.match(r"^'.*'$",fpath):
691 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"')
692 raise Exception(msg)
692 raise Exception(msg) from e
693 except TypeError:
693 except TypeError:
694 if fpath in sys.meta_path:
694 if fpath in sys.meta_path:
695 filename = ""
695 filename = ""
@@ -683,8 +683,8 b' class NamespaceMagics(Magics):'
683 else:
683 else:
684 try:
684 try:
685 m = re.compile(regex)
685 m = re.compile(regex)
686 except TypeError:
686 except TypeError as e:
687 raise TypeError('regex must be a string or compiled pattern')
687 raise TypeError('regex must be a string or compiled pattern') from e
688 for i in self.who_ls():
688 for i in self.who_ls():
689 if m.search(i):
689 if m.search(i):
690 del(user_ns[i])
690 del(user_ns[i])
@@ -293,8 +293,8 b' class OSMagics(Magics):'
293 """
293 """
294 try:
294 try:
295 return os.getcwd()
295 return os.getcwd()
296 except FileNotFoundError:
296 except FileNotFoundError as e:
297 raise UsageError("CWD no longer exists - please use %cd to change directory.")
297 raise UsageError("CWD no longer exists - please use %cd to change directory.") from e
298
298
299 @skip_doctest
299 @skip_doctest
300 @line_magic
300 @line_magic
@@ -386,8 +386,8 b' class OSMagics(Magics):'
386 if ps == '-':
386 if ps == '-':
387 try:
387 try:
388 ps = self.shell.user_ns['_dh'][-2]
388 ps = self.shell.user_ns['_dh'][-2]
389 except IndexError:
389 except IndexError as e:
390 raise UsageError('%cd -: No previous directory to change to.')
390 raise UsageError('%cd -: No previous directory to change to.') from e
391 # jump to bookmark if needed
391 # jump to bookmark if needed
392 else:
392 else:
393 if not os.path.isdir(ps) or 'b' in opts:
393 if not os.path.isdir(ps) or 'b' in opts:
@@ -764,15 +764,15 b' class OSMagics(Magics):'
764 if 'd' in opts:
764 if 'd' in opts:
765 try:
765 try:
766 todel = args[0]
766 todel = args[0]
767 except IndexError:
767 except IndexError as e:
768 raise UsageError(
768 raise UsageError(
769 "%bookmark -d: must provide a bookmark to delete")
769 "%bookmark -d: must provide a bookmark to delete") from e
770 else:
770 else:
771 try:
771 try:
772 del bkms[todel]
772 del bkms[todel]
773 except KeyError:
773 except KeyError as e:
774 raise UsageError(
774 raise UsageError(
775 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
775 "%%bookmark -d: Can't delete bookmark '%s'" % todel) from e
776
776
777 elif 'r' in opts:
777 elif 'r' in opts:
778 bkms = {}
778 bkms = {}
General Comments 0
You need to be logged in to leave comments. Login now