From 66921af3fcf1cc578e6bcc467e90f65af620360d 2020-06-10 15:40:54 From: Matthias Bussonnier Date: 2020-06-10 15:40:54 Subject: [PATCH] Merge pull request #12381 from cool-RR/2020-06-09-raise-from --- diff --git a/IPython/core/formatters.py b/IPython/core/formatters.py index 237b959..839ef93 100644 --- a/IPython/core/formatters.py +++ b/IPython/core/formatters.py @@ -643,17 +643,17 @@ class PlainTextFormatter(BaseFormatter): fmt = new try: fmt%3.14159 - except Exception: - raise ValueError("Precision must be int or format string, not %r"%new) + except Exception as e: + raise ValueError("Precision must be int or format string, not %r"%new) from e elif new: # otherwise, should be an int try: i = int(new) assert i >= 0 - except ValueError: - raise ValueError("Precision must be int or format string, not %r"%new) - except AssertionError: - raise ValueError("int precision must be non-negative, not %r"%i) + except ValueError as e: + raise ValueError("Precision must be int or format string, not %r"%new) from e + except AssertionError as e: + raise ValueError("int precision must be non-negative, not %r"%i) from e fmt = '%%.%if'%i if 'numpy' in sys.modules: diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index f5fdba6..e52b419 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -1490,8 +1490,8 @@ class InteractiveShell(SingletonConfigurable): else: # Delete by object try: obj = self.user_ns[varname] - except KeyError: - raise NameError("name '%s' is not defined" % varname) + except KeyError as e: + raise NameError("name '%s' is not defined" % varname) from e # Also check in output history ns_refs.append(self.history_manager.output_hist) for ns in ns_refs: @@ -1521,8 +1521,8 @@ class InteractiveShell(SingletonConfigurable): if regex is not None: try: m = re.compile(regex) - except TypeError: - raise TypeError('regex must be a string or compiled pattern') + except TypeError as e: + raise TypeError('regex must be a string or compiled pattern') from e # Search for keys in each namespace that match the given regex # If a match is found, delete the key/value pair. for ns in self.all_ns_refs: @@ -3614,13 +3614,13 @@ class InteractiveShell(SingletonConfigurable): try: if target.startswith(('http://', 'https://')): return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie) - except UnicodeDecodeError: + except UnicodeDecodeError as e: if not py_only : # Deferred import from urllib.request import urlopen response = urlopen(target) return response.read().decode('latin1') - raise ValueError(("'%s' seem to be unreadable.") % target) + raise ValueError(("'%s' seem to be unreadable.") % target) from e potential_target = [target] try : @@ -3632,11 +3632,11 @@ class InteractiveShell(SingletonConfigurable): if os.path.isfile(tgt): # Read file try : return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie) - except UnicodeDecodeError : + except UnicodeDecodeError as e: if not py_only : with io_open(tgt,'r', encoding='latin1') as f : return f.read() - raise ValueError(("'%s' seem to be unreadable.") % target) + raise ValueError(("'%s' seem to be unreadable.") % target) from e elif os.path.isdir(os.path.expanduser(tgt)): raise ValueError("'%s' is a directory, not a regular file." % target) @@ -3648,9 +3648,9 @@ class InteractiveShell(SingletonConfigurable): try: # User namespace codeobj = eval(target, self.user_ns) - except Exception: + except Exception as e: raise ValueError(("'%s' was not found in history, as a file, url, " - "nor in the user namespace.") % target) + "nor in the user namespace.") % target) from e if isinstance(codeobj, str): return codeobj diff --git a/IPython/core/magic.py b/IPython/core/magic.py index bc51677..e64d3aa 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -630,7 +630,7 @@ class Magics(Configurable): opts,args = getopt(argv, opt_str, long_opts) except GetoptError as e: raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str, - " ".join(long_opts))) + " ".join(long_opts))) from e for o,a in opts: if o.startswith('--'): o = o[2:] diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index a184138..f1289b5 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -440,10 +440,10 @@ class CodeMagics(Magics): return (None, None, None) use_temp = False - except DataIsObject: + except DataIsObject as e: # macros have a special edit function if isinstance(data, Macro): - raise MacroToEdit(data) + raise MacroToEdit(data) from e # For objects, try to edit the file where they are defined filename = find_file(data) @@ -467,8 +467,8 @@ class CodeMagics(Magics): m = ipython_input_pat.match(os.path.basename(filename)) if m: - raise InteractivelyDefined(int(m.groups()[0])) - + raise InteractivelyDefined(int(m.groups()[0])) from e + datafile = 1 if filename is None: filename = make_filename(args) diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index 6f73b80..effedbd 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -679,9 +679,9 @@ class ExecutionMagics(Magics): fpath = None # initialize to make sure fpath is in scope later fpath = arg_lst[0] filename = file_finder(fpath) - except IndexError: + except IndexError as e: msg = 'you must provide at least a filename.' - raise Exception(msg) + raise Exception(msg) from e except IOError as e: try: msg = str(e) @@ -689,7 +689,7 @@ class ExecutionMagics(Magics): msg = e.message if os.name == 'nt' and re.match(r"^'.*'$",fpath): warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"') - raise Exception(msg) + raise Exception(msg) from e except TypeError: if fpath in sys.meta_path: filename = "" diff --git a/IPython/core/magics/namespace.py b/IPython/core/magics/namespace.py index acc4620..0f232a7 100644 --- a/IPython/core/magics/namespace.py +++ b/IPython/core/magics/namespace.py @@ -683,8 +683,8 @@ class NamespaceMagics(Magics): else: try: m = re.compile(regex) - except TypeError: - raise TypeError('regex must be a string or compiled pattern') + except TypeError as e: + raise TypeError('regex must be a string or compiled pattern') from e for i in self.who_ls(): if m.search(i): del(user_ns[i]) diff --git a/IPython/core/magics/osm.py b/IPython/core/magics/osm.py index e337c66..a6e3d64 100644 --- a/IPython/core/magics/osm.py +++ b/IPython/core/magics/osm.py @@ -293,8 +293,8 @@ class OSMagics(Magics): """ try: return os.getcwd() - except FileNotFoundError: - raise UsageError("CWD no longer exists - please use %cd to change directory.") + except FileNotFoundError as e: + raise UsageError("CWD no longer exists - please use %cd to change directory.") from e @skip_doctest @line_magic @@ -386,8 +386,8 @@ class OSMagics(Magics): if ps == '-': try: ps = self.shell.user_ns['_dh'][-2] - except IndexError: - raise UsageError('%cd -: No previous directory to change to.') + except IndexError as e: + raise UsageError('%cd -: No previous directory to change to.') from e # jump to bookmark if needed else: if not os.path.isdir(ps) or 'b' in opts: @@ -764,15 +764,15 @@ class OSMagics(Magics): if 'd' in opts: try: todel = args[0] - except IndexError: + except IndexError as e: raise UsageError( - "%bookmark -d: must provide a bookmark to delete") + "%bookmark -d: must provide a bookmark to delete") from e else: try: del bkms[todel] - except KeyError: + except KeyError as e: raise UsageError( - "%%bookmark -d: Can't delete bookmark '%s'" % todel) + "%%bookmark -d: Can't delete bookmark '%s'" % todel) from e elif 'r' in opts: bkms = {}