Skip to content
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

Don't rely on __del__ #1606

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
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
17 changes: 8 additions & 9 deletions git/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php

from contextlib import ExitStack
import datetime
import glob
from io import BytesIO
Expand Down Expand Up @@ -360,20 +361,19 @@ def from_tree(cls, repo: "Repo", *treeish: Treeish, **kwargs: Any) -> "IndexFile
# as it considers existing entries. moving it essentially clears the index.
# Unfortunately there is no 'soft' way to do it.
# The TemporaryFileSwap assure the original file get put back
if repo.git_dir:
index_handler = TemporaryFileSwap(join_path_native(repo.git_dir, "index"))
try:
repo.git.read_tree(*arg_list, **kwargs)
index = cls(repo, tmp_index)
index.entries # force it to read the file as we will delete the temp-file
del index_handler # release as soon as possible
with ExitStack() as stack:
if repo.git_dir:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why that was necessary - wouldn't git_dir always be set? Otherwise it wouldn't be usable, right?
If that check was unnecessary, I assume the ExitStack could be left out as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made some refactoring here to remove the finally block

stack.enter_context(TemporaryFileSwap(join_path_native(repo.git_dir, "index")))
repo.git.read_tree(*arg_list, **kwargs)
index = cls(repo, tmp_index)
index.entries # force it to read the file as we will delete the temp-file
return index
finally:
if osp.exists(tmp_index):
os.remove(tmp_index)
# END index merge handling

return index

# UTILITIES
@unbare_repo
def _iter_expand_paths(self: "IndexFile", paths: Sequence[PathLike]) -> Iterator[PathLike]:
Expand Down Expand Up @@ -1156,7 +1156,6 @@ def checkout(
unknown_lines = []

def handle_stderr(proc: "Popen[bytes]", iter_checked_out_files: Iterable[PathLike]) -> None:

stderr_IO = proc.stderr
if not stderr_IO:
return None # return early if stderr empty
Expand Down
16 changes: 13 additions & 3 deletions git/index/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import struct
import tempfile
from types import TracebackType

from git.compat import is_win

Expand All @@ -11,7 +12,7 @@

# typing ----------------------------------------------------------------------

from typing import Any, Callable, TYPE_CHECKING
from typing import Any, Callable, TYPE_CHECKING, Optional, Type

from git.types import PathLike, _T

Expand Down Expand Up @@ -47,12 +48,21 @@ def __init__(self, file_path: PathLike) -> None:
except OSError:
pass

def __del__(self) -> None:
def __enter__(self) -> "TemporaryFileSwap":
return self

def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> bool:
if osp.isfile(self.tmp_file_path):
if is_win and osp.exists(self.file_path):
os.remove(self.file_path)
os.rename(self.tmp_file_path, self.file_path)
# END temp file exists

return False
Byron marked this conversation as resolved.
Show resolved Hide resolved


# { Decorators
Expand Down