Skip to content

Fix exception causes all over the codebase #1024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions git/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def _init_externals():

try:
import gitdb
except ImportError:
raise ImportError("'gitdb' could not be found in your PYTHONPATH")
except ImportError as e:
raise ImportError("'gitdb' could not be found in your PYTHONPATH") from e
# END verify import

#} END initialization
Expand Down Expand Up @@ -54,7 +54,7 @@ def _init_externals():
rmtree,
)
except GitError as exc:
raise ImportError('%s: %s' % (exc.__class__.__name__, exc))
raise ImportError('%s: %s' % (exc.__class__.__name__, exc)) from exc

#} END imports

Expand Down Expand Up @@ -82,5 +82,5 @@ def refresh(path=None):
try:
refresh()
except Exception as exc:
raise ImportError('Failed to initialize: {0}'.format(exc))
raise ImportError('Failed to initialize: {0}'.format(exc)) from exc
#################
4 changes: 2 additions & 2 deletions git/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def partial_to_complete_sha_hex(self, partial_hexsha):
try:
hexsha, _typename, _size = self._git.get_object_header(partial_hexsha)
return hex_to_bin(hexsha)
except (GitCommandError, ValueError):
raise BadObject(partial_hexsha)
except (GitCommandError, ValueError) as e:
raise BadObject(partial_hexsha) from e
# END handle exceptions

#} END interface
6 changes: 3 additions & 3 deletions git/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,9 @@ def _write_path_to_stdin(self, proc, filepath, item, fmakeexc, fprogress,
rval = None
try:
proc.stdin.write(("%s\n" % filepath).encode(defenc))
except IOError:
except IOError as e:
# pipe broke, usually because some error happened
raise fmakeexc()
raise fmakeexc() from e
# END write exception handling
proc.stdin.flush()
if read_from_stdout:
Expand Down Expand Up @@ -954,7 +954,7 @@ def commit(self, message, parent_commits=None, head=True, author=None,
if not skip_hooks:
run_commit_hook('post-commit', self)
return rval

def _write_commit_editmsg(self, message):
with open(self._commit_editmsg_filepath(), "wb") as commit_editmsg_file:
commit_editmsg_file.write(message.encode(defenc))
Expand Down
2 changes: 1 addition & 1 deletion git/index/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def run_commit_hook(name, index, *args):
close_fds=is_posix,
creationflags=PROC_CREATIONFLAGS,)
except Exception as ex:
raise HookExecutionError(hp, ex)
raise HookExecutionError(hp, ex) from ex
else:
stdout = []
stderr = []
Expand Down
24 changes: 12 additions & 12 deletions git/objects/submodule/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ def _set_cache_(self, attr):
# default submodule values
try:
self.path = reader.get('path')
except cp.NoSectionError:
except cp.NoSectionError as e:
raise ValueError("This submodule instance does not exist anymore in '%s' file"
% osp.join(self.repo.working_tree_dir, '.gitmodules'))
% osp.join(self.repo.working_tree_dir, '.gitmodules')) from e
# end
self._url = reader.get('url')
# git-python extension values - optional
Expand Down Expand Up @@ -189,9 +189,9 @@ def _config_parser(cls, repo, parent_commit, read_only):
assert parent_commit is not None, "need valid parent_commit in bare repositories"
try:
fp_module = cls._sio_modules(parent_commit)
except KeyError:
except KeyError as e:
raise IOError("Could not find %s file in the tree of parent commit %s" %
(cls.k_modules_file, parent_commit))
(cls.k_modules_file, parent_commit)) from e
# END handle exceptions
# END handle non-bare working tree

Expand Down Expand Up @@ -516,9 +516,9 @@ def update(self, recursive=False, init=True, to_latest_revision=False, progress=
if not dry_run and osp.isdir(checkout_module_abspath):
try:
os.rmdir(checkout_module_abspath)
except OSError:
except OSError as e:
raise OSError("Module directory at %r does already exist and is non-empty"
% checkout_module_abspath)
% checkout_module_abspath) from e
# END handle OSError
# END handle directory removal

Expand Down Expand Up @@ -737,8 +737,8 @@ def move(self, module_path, configuration=True, module=True):
del(index.entries[ekey])
nentry = git.IndexEntry(entry[:3] + (module_checkout_path,) + entry[4:])
index.entries[tekey] = nentry
except KeyError:
raise InvalidGitRepositoryError("Submodule's entry at %r did not exist" % (self.path))
except KeyError as e:
raise InvalidGitRepositoryError("Submodule's entry at %r did not exist" % (self.path)) from e
# END handle submodule doesn't exist

# update configuration
Expand Down Expand Up @@ -871,7 +871,7 @@ def remove(self, module=True, force=False, configuration=True, dry_run=False):
rmtree(wtd)
except Exception as ex:
if HIDE_WINDOWS_KNOWN_ERRORS:
raise SkipTest("FIXME: fails with: PermissionError\n {}".format(ex))
raise SkipTest("FIXME: fails with: PermissionError\n {}".format(ex)) from ex
raise
# END delete tree if possible
# END handle force
Expand All @@ -882,7 +882,7 @@ def remove(self, module=True, force=False, configuration=True, dry_run=False):
rmtree(git_dir)
except Exception as ex:
if HIDE_WINDOWS_KNOWN_ERRORS:
raise SkipTest("FIXME: fails with: PermissionError\n %s", ex)
raise SkipTest("FIXME: fails with: PermissionError\n %s", ex) from ex
else:
raise
# end handle separate bare repository
Expand Down Expand Up @@ -1046,8 +1046,8 @@ def module(self):
if repo != self.repo:
return repo
# END handle repo uninitialized
except (InvalidGitRepositoryError, NoSuchPathError):
raise InvalidGitRepositoryError("No valid repository at %s" % module_checkout_abspath)
except (InvalidGitRepositoryError, NoSuchPathError) as e:
raise InvalidGitRepositoryError("No valid repository at %s" % module_checkout_abspath) from e
else:
raise InvalidGitRepositoryError("Repository at %r was not yet checked out" % module_checkout_abspath)
# END handle exceptions
Expand Down
4 changes: 2 additions & 2 deletions git/objects/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ def _iter_convert_to_object(self, iterable):
path = join_path(self.path, name)
try:
yield self._map_id_to_type[mode >> 12](self.repo, binsha, mode, path)
except KeyError:
raise TypeError("Unknown mode %o found in tree data for path '%s'" % (mode, path))
except KeyError as e:
raise TypeError("Unknown mode %o found in tree data for path '%s'" % (mode, path)) from e
# END for each item

def join(self, file):
Expand Down
4 changes: 2 additions & 2 deletions git/objects/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ def parse_date(string_date):
# still here ? fail
raise ValueError("no format matched")
# END handle format
except Exception:
raise ValueError("Unsupported date format: %s" % string_date)
except Exception as e:
raise ValueError("Unsupported date format: %s" % string_date) from e
# END handle exceptions


Expand Down