Try TemporaryDirectory

This should work everywhere except Win <=3.7.
This commit is contained in:
Paul O'Leary McCann 2023-01-26 14:41:14 +09:00
parent 13fe001b15
commit a36d0ff28f

View File

@ -1032,19 +1032,10 @@ def make_tempdir() -> Generator[Path, None, None]:
YIELDS (Path): The path of the temp directory.
"""
d = Path(tempfile.mkdtemp())
# On Windows, attempts to delete a git directory fail with permissions
# errors due to read only / hidden files. This mainly addresses that issue.
def _fix_perms(func, path, exc_info):
# IWUSR = give user write permission
os.chmod(path, stat.S_IWUSR)
# func is the function that gave the error, so it behaves like rmtree.
func(path)
yield d
try:
shutil.rmtree(str(d), ignore_errors=False, onerror=_fix_perms)
with tempfile.TemporaryDirectory() as td:
yield td
except PermissionError as e:
warnings.warn(Warnings.W091.format(dir=d, msg=e))