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