Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb438c45

Browse files
committed
Refactor TemporaryFileSwap.__init__ for clarity
This changes the mkstemp call TemporaryFileSwap uses to atomicallycreate (and thus reserve) the temporary file, so that it passesonly a filename (i.e., basename) prefix as `prefix`, and passes allother path components (i.e., the directory) as `dir`. (If theoriginal path has no directory, then `dir` is "" as before.)This makes the mkstemp call *slightly* more idiomatic. This alsomakes it clearer, as it is no longer using `prefix` for somethingthat feels like it should be possible to pass as a Path object.Although mkstemp does not accept a Path as `prefix`, it does (asexpected) accept one as `dir`. However, to keep the code simple,I'm passing str for both. The os.path.split function accepts bothstr and Path (since Python 3.6), and returns str objects, which arenow used for the `dir` and `prefix` arguments to mkstemp.For unusual cases, this may technically not be a refactoring. Forexample, a file_path of "a/b//c" will be split into "a/b" and "c".If the automatically generated temporary file suffix is "xyz", thenthat results in a tmp_file_path of "a/b/cxyz" where "a/b//cxyz"would have been used before. The tmp_file_path attribute of aTemporaryFileSwap object is public (and used in filesystem calls).However, no guarantee has ever been given that the temporary filepath have the original path as an exact string prefix. I believethe slightly weaker relationship I expressed in the recentlyintroduced test_temporary_file_swap -- another file in the samedirectory, named with the original filename with more characters,consisting of equivalent path components in the same order -- hasalways been the intended one.Note that this slight possible variation does not apply to thefile_path attribute. That attribute is always kept exactly as itwas, both in its type and its value, and it always used unmodifiedin calls that access the filesystem.
1 parent1ddf953 commitb438c45

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

‎git/index/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class TemporaryFileSwap:
4141

4242
def__init__(self,file_path:PathLike)->None:
4343
self.file_path=file_path
44-
fd,self.tmp_file_path=tempfile.mkstemp(prefix=str(self.file_path),dir="")
44+
dirname,basename=osp.split(file_path)
45+
fd,self.tmp_file_path=tempfile.mkstemp(prefix=basename,dir=dirname)
4546
os.close(fd)
4647
withcontextlib.suppress(OSError):# It may be that the source does not exist.
4748
os.replace(self.file_path,self.tmp_file_path)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp