This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author richardxia
Recipients richardxia
Date 2017-02-15.21:21:18
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1487193679.14.0.35599130933.issue29573@psf.upfronthosting.co.za>
In-reply-to
Content
Here is a very short program to demonstrate what I'm seeing:

>>> import tempfile
>>> import os
>>> with tempfile.NamedTemporaryFile(delete=True) as fp:
...     print(fp.name)
...     os.system('rm {}'.format(fp.name))
/tmp/tmpomw0udc6
0
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/usr/local/lib/python3.6/tempfile.py", line 502, in __exit__
    self.close()
  File "/usr/local/lib/python3.6/tempfile.py", line 509, in close
    self._closer.close()
  File "/usr/local/lib/python3.6/tempfile.py", line 446, in close
    unlink(self.name)
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpomw0udc6'


In my specific use case, I am shelling out to another program (binutils' objcopy) and passing the path of the NamedTemporaryFile as the output file. objcopy apparently deletes the output file if it encounters an error, which causes NamedTemporaryFile's exit method to fail when it tries to delete the file.

While I can work around this by using delete=False and manually doing the cleanup on my own, it's less elegant than being able to rely on the normal context manager exit.
History
Date User Action Args
2017-02-15 21:21:19richardxiasetrecipients: + richardxia
2017-02-15 21:21:19richardxiasetmessageid: <1487193679.14.0.35599130933.issue29573@psf.upfronthosting.co.za>
2017-02-15 21:21:19richardxialinkissue29573 messages
2017-02-15 21:21:18richardxiacreate