Author lilydjwg
Recipients lilydjwg
Date 2018-11-02.08:40:08
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1541148008.66.0.788709270274.issue35144@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2018-11-02 08:40:08lilydjwgsetrecipients: + lilydjwg
2018-11-02 08:40:08lilydjwgsetmessageid: <1541148008.66.0.788709270274.issue35144@psf.upfronthosting.co.za>
2018-11-02 08:40:08lilydjwglinkissue35144 messages
2018-11-02 08:40:08lilydjwgcreate