classification
Title: NamedTemporaryFile with delete=True should not fail if file already deleted
Type: behavior Stage:
Components: IO Versions: Python 3.6, Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: andrewnester, richardxia
Priority: normal Keywords:

Created on 2017-02-15 21:21 by richardxia, last changed 2017-02-26 21:04 by andrewnester.

Pull Requests
URL Status Linked Edit
PR 134 open andrewnester, 2017-02-16 13:57
Messages (4)
msg287888 - (view) Author: Richard Xia (richardxia) Date: 2017-02-15 21:21
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.
msg287946 - (view) Author: Andrew Nester (andrewnester) * Date: 2017-02-16 13:58
I've just added PR fixing this issue.
msg288156 - (view) Author: Andrew Nester (andrewnester) * Date: 2017-02-19 19:19
any updates on this? :)
msg288612 - (view) Author: Andrew Nester (andrewnester) * Date: 2017-02-26 21:04
some updates?
History
Date User Action Args
2017-02-26 21:04:16andrewnestersetmessages: + msg288612
2017-02-19 19:19:35andrewnestersetmessages: + msg288156
2017-02-16 13:58:04andrewnestersetnosy: + andrewnester
messages: + msg287946
2017-02-16 13:57:47andrewnestersetpull_requests: + pull_request96
2017-02-15 21:21:19richardxiacreate