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

Appearance settings

Commit 8186159

Browse filesBrowse files
committed
Don't rely on __del__
1 parent 741edb5 commit 8186159
Copy full SHA for 8186159

File tree

Expand file treeCollapse file tree

2 files changed

+21
-12
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+21
-12
lines changed

‎git/index/base.py

Copy file name to clipboardExpand all lines: git/index/base.py
+8-9Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# This module is part of GitPython and is released under
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66

7+
from contextlib import ExitStack
78
import datetime
89
import glob
910
from io import BytesIO
@@ -360,20 +361,19 @@ def from_tree(cls, repo: "Repo", *treeish: Treeish, **kwargs: Any) -> "IndexFile
360361
# as it considers existing entries. moving it essentially clears the index.
361362
# Unfortunately there is no 'soft' way to do it.
362363
# The TemporaryFileSwap assure the original file get put back
363-
if repo.git_dir:
364-
index_handler = TemporaryFileSwap(join_path_native(repo.git_dir, "index"))
365364
try:
366-
repo.git.read_tree(*arg_list, **kwargs)
367-
index = cls(repo, tmp_index)
368-
index.entries # force it to read the file as we will delete the temp-file
369-
del index_handler # release as soon as possible
365+
with ExitStack() as stack:
366+
if repo.git_dir:
367+
stack.enter_context(TemporaryFileSwap(join_path_native(repo.git_dir, "index")))
368+
repo.git.read_tree(*arg_list, **kwargs)
369+
index = cls(repo, tmp_index)
370+
index.entries # force it to read the file as we will delete the temp-file
371+
return index
370372
finally:
371373
if osp.exists(tmp_index):
372374
os.remove(tmp_index)
373375
# END index merge handling
374376

375-
return index
376-
377377
# UTILITIES
378378
@unbare_repo
379379
def _iter_expand_paths(self: "IndexFile", paths: Sequence[PathLike]) -> Iterator[PathLike]:
@@ -1156,7 +1156,6 @@ def checkout(
11561156
unknown_lines = []
11571157

11581158
def handle_stderr(proc: "Popen[bytes]", iter_checked_out_files: Iterable[PathLike]) -> None:
1159-
11601159
stderr_IO = proc.stderr
11611160
if not stderr_IO:
11621161
return None # return early if stderr empty

‎git/index/util.py

Copy file name to clipboardExpand all lines: git/index/util.py
+13-3Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import struct
55
import tempfile
6+
from types import TracebackType
67

78
from git.compat import is_win
89

@@ -11,7 +12,7 @@
1112

1213
# typing ----------------------------------------------------------------------
1314

14-
from typing import Any, Callable, TYPE_CHECKING
15+
from typing import Any, Callable, TYPE_CHECKING, Optional, Type
1516

1617
from git.types import PathLike, _T
1718

@@ -47,12 +48,21 @@ def __init__(self, file_path: PathLike) -> None:
4748
except OSError:
4849
pass
4950

50-
def __del__(self) -> None:
51+
def __enter__(self) -> "TemporaryFileSwap":
52+
return self
53+
54+
def __exit__(
55+
self,
56+
exc_type: Optional[Type[BaseException]],
57+
exc_val: Optional[BaseException],
58+
exc_tb: Optional[TracebackType],
59+
) -> bool:
5160
if osp.isfile(self.tmp_file_path):
5261
if is_win and osp.exists(self.file_path):
5362
os.remove(self.file_path)
5463
os.rename(self.tmp_file_path, self.file_path)
55-
# END temp file exists
64+
65+
return False
5666

5767

5868
# { Decorators

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.