-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
tempfile.NamedTemporaryFile should be able to toggle "delete" #67943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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: {{{ 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' |
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. |
I think I have often passed delete=False because of the documented deficiency with Windows (see bpo-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>. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: