classification
Title: Equivalent to tempfile.NamedTemporaryFile that deletes file at context exit
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: duplicate
Dependencies: Superseder: tempfile.NamedTemporaryFile not particularly useful on Windows
View: 14243
Assigned To: Nosy List: eric.smith, ncoghlan, r.david.murray
Priority: normal Keywords:

Created on 2012-04-06 02:00 by r.david.murray, last changed 2012-04-10 01:31 by ncoghlan. This issue is now closed.

Messages (4)
msg157634 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-04-06 02:00
A common pattern (especially in writing tests) is to create a named temporary file, operate on it with tools that take the filename, and then delete the file.  This pattern would be facilitated by a version of NamedTemporaryFile that deleted the named file at the end of the context, but allowed the file to continue to exist when closed.  This latter requirement arises because the file cannot be opened a second time on Windows, and so needs to be closed before other operations that open it can be performed in order for the code to work cross platform.

It's tempting just to change delete=True to mean delete on exit (not close) if and only if NamedTemporaryFile is being used as a context manager, but there might be backward compatibility issues with that.  It might be acceptable as a feature change, though, since the likelyhood of someone closing such a tempfile inside the context and then depending on it *not* existing between then and the end of the context seems low.

If that it deemed unacceptable, this could be implemented as a new flag on NamedTemporaryFile, or by changing the delete flag to take additional values (True/False == current behavior, but add delete='onclose', delete='onexit', delete='no', and document True/False as deprecated.)
msg157637 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2012-04-06 02:46
See also issue 14243.
msg157638 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-04-06 02:52
Grr.  I did a search first, and even used google because I know search is currently broken, and I did not find that issue.
msg157926 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-04-10 01:31
I converted issue #14243 to a feature request, so this is now a duplicate.
History
Date User Action Args
2012-04-10 01:31:35ncoghlansetstatus: open -> closed

nosy: + ncoghlan
messages: + msg157926

superseder: tempfile.NamedTemporaryFile not particularly useful on Windows
resolution: duplicate
2012-04-06 02:52:13r.david.murraysetmessages: + msg157638
2012-04-06 02:46:24eric.smithsetnosy: + eric.smith
messages: + msg157637
2012-04-06 02:00:28r.david.murraycreate