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

Created on 2017-02-15 21:21 by richardxia, last changed 2017-02-28 00:46 by jwilk.

Pull Requests
URL Status Linked Edit
PR 134 open andrewnester, 2017-02-16 13:57
Messages (5)
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(
...     os.system('rm {}'.format(
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/usr/local/lib/python3.6/", line 502, in __exit__
  File "/usr/local/lib/python3.6/", line 509, in close
  File "/usr/local/lib/python3.6/", line 446, in close
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?
msg288686 - (view) Author: Jakub Wilk (jwilk) Date: 2017-02-28 00:46
Deleting non-existent files in /tmp is an indicator of a security hole.
This kind of error must never pass silently.
Date User Action Args
2017-02-28 00:46:23jwilksetnosy: + jwilk
messages: + msg288686
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