Skip to content

Commit 411635f

Browse files
cool-RRByron
authored andcommitted
Fix exception causes all over the codebase
1 parent 99ba753 commit 411635f

File tree

7 files changed

+26
-26
lines changed

7 files changed

+26
-26
lines changed

Diff for: git/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def _init_externals():
2323

2424
try:
2525
import gitdb
26-
except ImportError:
27-
raise ImportError("'gitdb' could not be found in your PYTHONPATH")
26+
except ImportError as e:
27+
raise ImportError("'gitdb' could not be found in your PYTHONPATH") from e
2828
# END verify import
2929

3030
#} END initialization
@@ -54,7 +54,7 @@ def _init_externals():
5454
rmtree,
5555
)
5656
except GitError as exc:
57-
raise ImportError('%s: %s' % (exc.__class__.__name__, exc))
57+
raise ImportError('%s: %s' % (exc.__class__.__name__, exc)) from exc
5858

5959
#} END imports
6060

@@ -82,5 +82,5 @@ def refresh(path=None):
8282
try:
8383
refresh()
8484
except Exception as exc:
85-
raise ImportError('Failed to initialize: {0}'.format(exc))
85+
raise ImportError('Failed to initialize: {0}'.format(exc)) from exc
8686
#################

Diff for: git/db.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def partial_to_complete_sha_hex(self, partial_hexsha):
5353
try:
5454
hexsha, _typename, _size = self._git.get_object_header(partial_hexsha)
5555
return hex_to_bin(hexsha)
56-
except (GitCommandError, ValueError):
57-
raise BadObject(partial_hexsha)
56+
except (GitCommandError, ValueError) as e:
57+
raise BadObject(partial_hexsha) from e
5858
# END handle exceptions
5959

6060
#} END interface

Diff for: git/index/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,9 @@ def _write_path_to_stdin(self, proc, filepath, item, fmakeexc, fprogress,
420420
rval = None
421421
try:
422422
proc.stdin.write(("%s\n" % filepath).encode(defenc))
423-
except IOError:
423+
except IOError as e:
424424
# pipe broke, usually because some error happened
425-
raise fmakeexc()
425+
raise fmakeexc() from e
426426
# END write exception handling
427427
proc.stdin.flush()
428428
if read_from_stdout:
@@ -954,7 +954,7 @@ def commit(self, message, parent_commits=None, head=True, author=None,
954954
if not skip_hooks:
955955
run_commit_hook('post-commit', self)
956956
return rval
957-
957+
958958
def _write_commit_editmsg(self, message):
959959
with open(self._commit_editmsg_filepath(), "wb") as commit_editmsg_file:
960960
commit_editmsg_file.write(message.encode(defenc))

Diff for: git/index/fun.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def run_commit_hook(name, index, *args):
8282
close_fds=is_posix,
8383
creationflags=PROC_CREATIONFLAGS,)
8484
except Exception as ex:
85-
raise HookExecutionError(hp, ex)
85+
raise HookExecutionError(hp, ex) from ex
8686
else:
8787
stdout = []
8888
stderr = []

Diff for: git/objects/submodule/base.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ def _set_cache_(self, attr):
121121
# default submodule values
122122
try:
123123
self.path = reader.get('path')
124-
except cp.NoSectionError:
124+
except cp.NoSectionError as e:
125125
raise ValueError("This submodule instance does not exist anymore in '%s' file"
126-
% osp.join(self.repo.working_tree_dir, '.gitmodules'))
126+
% osp.join(self.repo.working_tree_dir, '.gitmodules')) from e
127127
# end
128128
self._url = reader.get('url')
129129
# git-python extension values - optional
@@ -189,9 +189,9 @@ def _config_parser(cls, repo, parent_commit, read_only):
189189
assert parent_commit is not None, "need valid parent_commit in bare repositories"
190190
try:
191191
fp_module = cls._sio_modules(parent_commit)
192-
except KeyError:
192+
except KeyError as e:
193193
raise IOError("Could not find %s file in the tree of parent commit %s" %
194-
(cls.k_modules_file, parent_commit))
194+
(cls.k_modules_file, parent_commit)) from e
195195
# END handle exceptions
196196
# END handle non-bare working tree
197197

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

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

744744
# update configuration
@@ -871,7 +871,7 @@ def remove(self, module=True, force=False, configuration=True, dry_run=False):
871871
rmtree(wtd)
872872
except Exception as ex:
873873
if HIDE_WINDOWS_KNOWN_ERRORS:
874-
raise SkipTest("FIXME: fails with: PermissionError\n {}".format(ex))
874+
raise SkipTest("FIXME: fails with: PermissionError\n {}".format(ex)) from ex
875875
raise
876876
# END delete tree if possible
877877
# END handle force
@@ -882,7 +882,7 @@ def remove(self, module=True, force=False, configuration=True, dry_run=False):
882882
rmtree(git_dir)
883883
except Exception as ex:
884884
if HIDE_WINDOWS_KNOWN_ERRORS:
885-
raise SkipTest("FIXME: fails with: PermissionError\n %s", ex)
885+
raise SkipTest("FIXME: fails with: PermissionError\n %s", ex) from ex
886886
else:
887887
raise
888888
# end handle separate bare repository
@@ -1046,8 +1046,8 @@ def module(self):
10461046
if repo != self.repo:
10471047
return repo
10481048
# END handle repo uninitialized
1049-
except (InvalidGitRepositoryError, NoSuchPathError):
1050-
raise InvalidGitRepositoryError("No valid repository at %s" % module_checkout_abspath)
1049+
except (InvalidGitRepositoryError, NoSuchPathError) as e:
1050+
raise InvalidGitRepositoryError("No valid repository at %s" % module_checkout_abspath) from e
10511051
else:
10521052
raise InvalidGitRepositoryError("Repository at %r was not yet checked out" % module_checkout_abspath)
10531053
# END handle exceptions

Diff for: git/objects/tree.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ def _iter_convert_to_object(self, iterable):
203203
path = join_path(self.path, name)
204204
try:
205205
yield self._map_id_to_type[mode >> 12](self.repo, binsha, mode, path)
206-
except KeyError:
207-
raise TypeError("Unknown mode %o found in tree data for path '%s'" % (mode, path))
206+
except KeyError as e:
207+
raise TypeError("Unknown mode %o found in tree data for path '%s'" % (mode, path)) from e
208208
# END for each item
209209

210210
def join(self, file):

Diff for: git/objects/util.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ def parse_date(string_date):
203203
# still here ? fail
204204
raise ValueError("no format matched")
205205
# END handle format
206-
except Exception:
207-
raise ValueError("Unsupported date format: %s" % string_date)
206+
except Exception as e:
207+
raise ValueError("Unsupported date format: %s" % string_date) from e
208208
# END handle exceptions
209209

210210

0 commit comments

Comments
 (0)