Skip to content

Fix exception causes in 7 modules #1023

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jun 13, 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/refs/symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ def set_commit(self, commit, logmsg=None):
else:
try:
invalid_type = self.repo.rev_parse(commit).type != Commit.type
except (BadObject, BadName):
raise ValueError("Invalid object: %s" % commit)
except (BadObject, BadName) as e:
raise ValueError("Invalid object: %s" % commit) from e
# END handle exception
# END verify type

Expand Down Expand Up @@ -301,8 +301,8 @@ def set_reference(self, ref, logmsg=None):
try:
obj = self.repo.rev_parse(ref + "^{}") # optionally deref tags
write_value = obj.hexsha
except (BadObject, BadName):
raise ValueError("Could not extract object from %s" % ref)
except (BadObject, BadName) as e:
raise ValueError("Could not extract object from %s" % ref) from e
# END end try string
else:
raise ValueError("Unrecognized Value: %r" % ref)
Expand Down
12 changes: 6 additions & 6 deletions git/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ def _from_line(cls, remote, line):
# control character handling
try:
flags |= cls._flag_map[control_character]
except KeyError:
raise ValueError("Control character %r unknown as parsed from line %r" % (control_character, line))
except KeyError as e:
raise ValueError("Control character %r unknown as parsed from line %r" % (control_character, line)) from e
# END handle control character

# from_to handling
Expand Down Expand Up @@ -296,15 +296,15 @@ def _from_line(cls, repo, line, fetch_line):
try:
_new_hex_sha, _fetch_operation, fetch_note = fetch_line.split("\t")
ref_type_name, fetch_note = fetch_note.split(' ', 1)
except ValueError: # unpack error
raise ValueError("Failed to parse FETCH_HEAD line: %r" % fetch_line)
except ValueError as e: # unpack error
raise ValueError("Failed to parse FETCH_HEAD line: %r" % fetch_line) from e

# parse flags from control_character
flags = 0
try:
flags |= cls._flag_map[control_character]
except KeyError:
raise ValueError("Control character %r unknown as parsed from line %r" % (control_character, line))
except KeyError as e:
raise ValueError("Control character %r unknown as parsed from line %r" % (control_character, line)) from e
# END control char exception handling

# parse operation string for more info - makes no sense for symbolic refs, but we parse it anyway
Expand Down
4 changes: 2 additions & 2 deletions git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ def submodule(self, name):
:raise ValueError: If no such submodule exists"""
try:
return self.submodules[name]
except IndexError:
raise ValueError("Didn't find submodule named %r" % name)
except IndexError as e:
raise ValueError("Didn't find submodule named %r" % name) from e
# END exception handling

def create_submodule(self, *args, **kwargs):
Expand Down
14 changes: 8 additions & 6 deletions git/repo/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,16 @@ def rev_parse(repo, rev):
try:
# transform reversed index into the format of our revlog
revlog_index = -(int(output_type) + 1)
except ValueError:
except ValueError as e:
# TODO: Try to parse the other date options, using parse_date
# maybe
raise NotImplementedError("Support for additional @{...} modes not implemented")
raise NotImplementedError("Support for additional @{...} modes not implemented") from e
# END handle revlog index

try:
entry = ref.log_entry(revlog_index)
except IndexError:
raise IndexError("Invalid revlog index: %i" % revlog_index)
except IndexError as e:
raise IndexError("Invalid revlog index: %i" % revlog_index) from e
# END handle index out of bound

obj = Object.new_from_sha(repo, hex_to_bin(entry.newhexsha))
Expand Down Expand Up @@ -324,8 +324,10 @@ def rev_parse(repo, rev):
else:
raise ValueError("Invalid token: %r" % token)
# END end handle tag
except (IndexError, AttributeError):
raise BadName("Invalid revision spec '%s' - not enough parent commits to reach '%s%i'" % (rev, token, num))
except (IndexError, AttributeError) as e:
raise BadName(
"Invalid revision spec '%s' - not enough "
"parent commits to reach '%s%i'" % (rev, token, num)) from e
# END exception handling
# END parse loop

Expand Down
4 changes: 2 additions & 2 deletions git/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ def test_add_unicode(self, rw_repo):
# verify first that we could encode file name in this environment
try:
file_path.encode(sys.getfilesystemencoding())
except UnicodeEncodeError:
raise SkipTest("Environment doesn't support unicode filenames")
except UnicodeEncodeError as e:
raise SkipTest("Environment doesn't support unicode filenames") from e

with open(file_path, "wb") as fp:
fp.write(b'something')
Expand Down
4 changes: 2 additions & 2 deletions git/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ def test_includes_order(self):
assert r_config.get_value('diff', 'tool') == "meld"
try:
assert r_config.get_value('sec', 'var1') == "value1_main"
except AssertionError:
except AssertionError as e:
raise SkipTest(
'Known failure -- included values are not in effect right away'
)
) from e

@with_rw_directory
def test_lock_reentry(self, rw_dir):
Expand Down
14 changes: 7 additions & 7 deletions git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def onerror(func, path, exc_info):
func(path) # Will scream if still not possible to delete.
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

return shutil.rmtree(path, False, onerror)
Expand Down Expand Up @@ -746,7 +746,7 @@ def _obtain_lock_or_raise(self):
fd = os.open(lock_file, flags, 0)
os.close(fd)
except OSError as e:
raise IOError(str(e))
raise IOError(str(e)) from e

self._owns_lock = True

Expand Down Expand Up @@ -801,19 +801,19 @@ def _obtain_lock(self):
while True:
try:
super(BlockingLockFile, self)._obtain_lock()
except IOError:
except IOError as e:
# synity check: if the directory leading to the lockfile is not
# readable anymore, raise an exception
curtime = time.time()
if not osp.isdir(osp.dirname(self._lock_file_path())):
msg = "Directory containing the lockfile %r was not readable anymore after waiting %g seconds" % (
self._lock_file_path(), curtime - starttime)
raise IOError(msg)
raise IOError(msg) from e
# END handle missing directory

if curtime >= maxtime:
msg = "Waited %g seconds for lock at %r" % (maxtime - starttime, self._lock_file_path())
raise IOError(msg)
raise IOError(msg) from e
# END abort if we wait too long
time.sleep(self._check_interval)
else:
Expand Down Expand Up @@ -878,8 +878,8 @@ def __getitem__(self, index):

try:
return getattr(self, index)
except AttributeError:
raise IndexError("No item found with id %r" % (self._prefix + index))
except AttributeError as e:
raise IndexError("No item found with id %r" % (self._prefix + index)) from e
# END handle getattr

def __delitem__(self, index):
Expand Down