[issue35144] TemporaryDirectory can't be cleaned up if there are unsearchable directories
lilydjwg
report at bugs.python.org
Fri Nov 2 04:40:08 EDT 2018
New submission from lilydjwg <lilydjwg at gmail.com>:
If the title doesn't explain clearly, here's a demo program that will fail:
import tempfile
import pathlib
def test():
with tempfile.TemporaryDirectory(prefix='test-bad-') as tmpdir:
tmpdir = pathlib.Path(tmpdir)
subdir = tmpdir / 'sub'
subdir.mkdir()
with open(subdir / 'file', 'w'):
pass
subdir.chmod(0o600)
if __name__ == '__main__':
test()
I didn't expect this, and I didn't find an easy way to handle this except not using TemporaryDirectory at all:
import tempfile
import pathlib
import shutil
import os
def rmtree_error(func, path, excinfo):
if isinstance(excinfo[1], PermissionError):
os.chmod(os.path.dirname(path), 0o700)
os.unlink(path)
print(func, path, excinfo)
def test():
tmpdir = tempfile.mkdtemp(prefix='test-good-')
try:
tmpdir = pathlib.Path(tmpdir)
subdir = tmpdir / 'sub'
subdir.mkdir()
with open(subdir / 'file', 'w'):
pass
subdir.chmod(0o600)
finally:
shutil.rmtree(tmpdir, onerror=rmtree_error)
if __name__ == '__main__':
test()
This works around the issue, but the dirfd is missing in the onerror callback.
I have this issue because my program extracts tarballs to a temporary directory for examination. I expected that TemporaryDirectory cleaned up things when it could.
What do you think? rm -rf can't remove such a directory either but this is annoying and I think Python can do better.
----------
components: Library (Lib)
messages: 329116
nosy: lilydjwg
priority: normal
severity: normal
status: open
title: TemporaryDirectory can't be cleaned up if there are unsearchable directories
type: behavior
versions: Python 3.7
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35144>
_______________________________________
More information about the Python-bugs-list
mailing list