Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 8 additions & 3 deletions Lib/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,9 +922,14 @@ def onexc(func, path, exc):
raise

try:
if path != name:
_resetperms(_os.path.dirname(path))
_resetperms(path)
try:
if path != name:
_resetperms(_os.path.dirname(path))
_resetperms(path)
except PermissionError:
if ignore_errors:
return
raise

try:
_os.unlink(path)
Expand Down
31 changes: 31 additions & 0 deletions Lib/test/test_tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,37 @@ def test_explicit_cleanup_ignore_errors(self):
temp_path.exists(),
f"TemporaryDirectory {temp_path!s} exists after cleanup")

def test_explicit_cleanup_ignore_errors_resetperms(self):
with tempfile.TemporaryDirectory() as working_dir:
temp_dir = self.do_create(
dir=working_dir, ignore_cleanup_errors=True)

def fake_rmtree(path, onexc):
onexc(os.open, path, PermissionError())

def fake_resetperms(path):
raise PermissionError(path)

with support.swap_attr(tempfile._shutil, "rmtree", fake_rmtree), \
support.swap_attr(tempfile, "_resetperms", fake_resetperms):
temp_dir.cleanup()

def test_explicit_cleanup_resetperms_error(self):
with tempfile.TemporaryDirectory() as working_dir:
temp_dir = self.do_create(dir=working_dir)
self.addCleanup(temp_dir.cleanup)

def fake_rmtree(path, onexc):
onexc(os.open, path, PermissionError())

def fake_resetperms(path):
raise PermissionError(path)

with support.swap_attr(tempfile._shutil, "rmtree", fake_rmtree), \
support.swap_attr(tempfile, "_resetperms", fake_resetperms):
with self.assertRaises(PermissionError):
temp_dir.cleanup()

@unittest.skipUnless(os.name == "nt", "Only on Windows.")
def test_explicit_cleanup_correct_error(self):
with tempfile.TemporaryDirectory() as working_dir:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :class:`tempfile.TemporaryDirectory` so that
``ignore_cleanup_errors=True`` also ignores cleanup errors raised while
resetting permissions.
Loading