Skip to content

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

Open
StephenGallagher mannequin opened this issue Mar 24, 2015 · 3 comments
Open

tempfile.NamedTemporaryFile should be able to toggle "delete" #67943

StephenGallagher mannequin opened this issue Mar 24, 2015 · 3 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@StephenGallagher
Copy link
Mannequin

StephenGallagher mannequin commented Mar 24, 2015

BPO 23755
Nosy @vadmium

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:

assignee = None
closed_at = None
created_at = <Date 2015-03-24.00:55:06.072>
labels = ['type-feature', 'library']
title = 'tempfile.NamedTemporaryFile should be able to toggle "delete"'
updated_at = <Date 2015-03-30.04:21:10.508>
user = 'https://bugs.python.org/StephenGallagher'

bugs.python.org fields:

activity = <Date 2015-03-30.04:21:10.508>
actor = 'martin.panter'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2015-03-24.00:55:06.072>
creator = 'Stephen Gallagher'
dependencies = []
files = []
hgrepos = []
issue_num = 23755
keywords = []
message_count = 3.0
messages = ['239082', '239083', '239565']
nosy_count = 2.0
nosy_names = ['martin.panter', 'Stephen Gallagher']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue23755'
versions = ['Python 2.7', 'Python 3.4']

@StephenGallagher
Copy link
Mannequin Author

StephenGallagher mannequin commented Mar 24, 2015

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'

@StephenGallagher StephenGallagher mannequin added the stdlib Python modules in the Lib dir label Mar 24, 2015
@StephenGallagher
Copy link
Mannequin Author

StephenGallagher mannequin commented Mar 24, 2015

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.

@vadmium
Copy link
Member

vadmium commented Mar 30, 2015

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>.

@vadmium vadmium added the type-feature A feature request or enhancement label Mar 30, 2015
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
Status: No status
Development

No branches or pull requests

1 participant