Skip to content

Fix TemporaryFileSwap regression where file_path could not be Path #1776

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 4 commits into from
Dec 21, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix TemporaryFileSwap bug when file_path is a Path
This fixes the regression introduced in 9e86053 (#1770) where the
file_path argument to TemporaryFileSwap.__init__ could no longer be
a Path object.

The change also makes this truer to the code from before #1770,
still without the race condition fixed there, in that str was
called on file_path then as well. However, it is not clear that
this is a good thing, because this is not an idiomatic use of
mkstemp. The reason the `prefix` cannot be a Path is that it is
expected to be a filename prefix, with leading directories given in
the `dir` argument.
  • Loading branch information
EliahKagan committed Dec 21, 2023
commit 1ddf953ea0d7625485ed02c90b03aae7f939ec46
2 changes: 1 addition & 1 deletion git/index/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TemporaryFileSwap:

def __init__(self, file_path: PathLike) -> None:
self.file_path = file_path
fd, self.tmp_file_path = tempfile.mkstemp(prefix=self.file_path, dir="")
fd, self.tmp_file_path = tempfile.mkstemp(prefix=str(self.file_path), dir="")
os.close(fd)
with contextlib.suppress(OSError): # It may be that the source does not exist.
os.replace(self.file_path, self.tmp_file_path)
Expand Down