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.

classification
Title: tempfile.NamedTemporaryFile should be able to toggle "delete"
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.4, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Stephen Gallagher, martin.panter
Priority: normal Keywords:

Created on 2015-03-24 00:55 by Stephen Gallagher, last changed 2022-04-11 14:58 by admin.

Messages (3)
msg239082 - (view) Author: Stephen Gallagher (Stephen Gallagher) Date: 2015-03-24 00:55
Currently, NamedTemporaryFile takes an attribute at initialization that allows it to remove the temporary file on going out of scope or else leave it around. However, it's not possible to change this after the fact.

It would be a much more sensible pattern to be able to operate with auto-deletion enabled while constructing the file and then to be able to toggle this option off once the file is completed.

For example, the use-case I have in mind is that I am creating a file that, once complete, will go into a well-known location. Because of known attacks, the only secure way to create this file is to generate it in a temporary location and then atomically move (os.rename()) it into its final location. This avoids time-of-check-time-of-use risks as well as avoiding overwriting the old file if something goes wrong.

It would be handy if tempfile could be extended to support this operation.

Additionally, I attempted to solve this by monkey-patching tempfile and overriding the __del__ function on the _TemporaryFileWrapper object to be a no-op. This works in python 2.7.9, but seems to be ignored on python 3.4.2.

Example code:

{{{
import tempfile
import os

f = tempfile.NamedTemporaryFile()
os.unlink(f.name)
f.unlink = lambda x: None
}}}

If you run that under python2, it will succeed. On Python 3, it will noisily report:
Exception ignored in: <bound method _TemporaryFileCloser.__del__ of <tempfile._TemporaryFileCloser object at 0x7f7a24548c88>>
Traceback (most recent call last):
  File "/usr/lib64/python3.4/tempfile.py", line 366, in __del__
  File "/usr/lib64/python3.4/tempfile.py", line 362, in close
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpqs5k6w7q'
msg239083 - (view) Author: Stephen Gallagher (Stephen Gallagher) Date: 2015-03-24 00:57
Oops, the temporary code I sent indicated that I was overriding the unlink() function (which I also tried, just in case __del__ was somehow protected). Neither monkeypatching unlink nor __del__ actually worked.
msg239565 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-03-30 04:21
I think I have often passed delete=False because of the documented deficiency with Windows (see Issue 14243). Depending on the outcome of that issue, allowing for deletion after close() might be useful too.

BTW, monkey-patching __del__() probably won’t work if you only set an attribute on the “f” instance, instead of modifying the class itself. See <file:///home/proj/python/cpython/Doc/build/html/reference/datamodel.html#special-method-lookup>.
History
Date User Action Args
2022-04-11 14:58:14adminsetgithub: 67943
2015-03-30 04:21:10martin.pantersetnosy: + martin.panter
type: enhancement
messages: + msg239565
2015-03-24 00:57:01Stephen Gallaghersetmessages: + msg239083
2015-03-24 00:55:06Stephen Gallaghercreate