Skip to content

Commit

Permalink
Refactor try-finally cleanup in git/
Browse files Browse the repository at this point in the history
This is, in part, to help avoid (or be able to notice) other bugs
like the rollback bug that affected SymbolicReference. However, it
also includes a simplification of try-(try-except)-finally to
try-except-finally.
  • Loading branch information
EliahKagan committed Sep 21, 2023
1 parent 592ec84 commit ff84b26
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
17 changes: 8 additions & 9 deletions git/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,15 +406,14 @@ def release(self) -> None:
return

try:
try:
self.write()
except IOError:
log.error("Exception during destruction of GitConfigParser", exc_info=True)
except ReferenceError:
# This happens in PY3 ... and usually means that some state cannot be written
# as the sections dict cannot be iterated
# Usually when shutting down the interpreter, don'y know how to fix this
pass
self.write()
except IOError:
log.error("Exception during destruction of GitConfigParser", exc_info=True)
except ReferenceError:
# This happens in PY3 ... and usually means that some state cannot be
# written as the sections dict cannot be iterated
# Usually when shutting down the interpreter, don't know how to fix this
pass
finally:
if self._lock is not None:
self._lock._release_lock()
Expand Down
8 changes: 3 additions & 5 deletions git/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,11 @@ def write(
lfd = LockedFD(file_path or self._file_path)
stream = lfd.open(write=True, stream=True)

ok = False
try:
self._serialize(stream, ignore_extension_data)
ok = True
finally:
if not ok:
lfd.rollback()
except BaseException:
lfd.rollback()
raise

lfd.commit()

Expand Down
8 changes: 3 additions & 5 deletions git/refs/symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,12 @@ def set_reference(

lfd = LockedFD(fpath)
fd = lfd.open(write=True, stream=True)
ok = False
try:
fd.write(write_value.encode("utf-8") + b"\n")
lfd.commit()
ok = True
finally:
if not ok:
lfd.rollback()
except BaseException:
lfd.rollback()
raise
# Adjust the reflog
if logmsg is not None:
self.log_append(oldbinsha, logmsg)
Expand Down

0 comments on commit ff84b26

Please sign in to comment.