Issue 2021: Turn NamedTemporaryFile into a context manager (original) (raw)

Issue2021

Created on 2008-02-06 16:14 by belopolsky, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
with_NamedTemporaryFile.diff belopolsky,2008-02-06 16:14 Diff against revision 60593
subclass-file.diff belopolsky,2008-02-10 22:57 diff against revision 60703
Messages (6)
msg62102 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2008-02-06 16:14
In the spirit of files becoming context managers in 2.5, the attached patch defines __enter__ and __exit__ methods for tempfile.NamedTemporaryFile. BTW, I was not able to add a "patch" keyword which seems appropriate here.
msg62111 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-02-06 18:18
Thanks for the patch! It even has a unit test, very good. :) The __future__ statement isn't necessary for Python 2.6. The with statement is always available.
msg62223 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2008-02-09 15:32
I've changed the issue type from rfe to behaviour. NamedTemporaryFile actually provides __enter__ and __exit__ methods in 2.5, they just don't work (it tries to use the methods from the underlying file object directly which turns out to be a doomed exercise for a couple of different reasons). Fixed on the trunk in r60695. Leaving issue as pending until the NamedTemporaryFile fix is backported to 2.5 (or we decide not to backport it). P.S. Alexander's patch worked as written, but in figuring out *why* it worked I ended up moving things around a bit (main change was to only override __exit__ when it was actually necessary to do so) and adding some more test cases (e.g. to also cover 2.6's new SpooledTemporaryFile).
msg62264 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2008-02-10 22:57
Nick's comment made me think why NamedTemporaryFile can't simply subclass file and get properly working context manager's methods for free. It turned out that although file is subclassable, in its present form, it does not allow NamedTemporaryFile implementation for the following reasons: 1. os.fdopen cannot create a subclass of file. 2. file.__exit__ does not call subclass' close method. The attached patch fixes both issues and reimplements NamedTemporaryFile. I understand that adding new functionality to file objects should be brought up on python-dev, but I would like to hear comments from the "nosy list" first. The patch is proof-of-concept quality at the moment and may not work non-posix platforms.
msg62278 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2008-02-11 10:58
The wrapper approach has the virtue of providing easy access to the underlying file object and its methods. That actually gets a bit more difficult when you switch to a subclassing approach (you need to either explicitly qualify the methods or play around with super()). The wrapper approach also has the virtue of being a valid candidate for backport to 2.5.2, while that is most definitely *not* the case for making file fully subclassable. If you would like to pursue that idea further, I suggest opening a separate issue for it.
msg62285 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2008-02-11 12:55
Backported to 2.5 in r60728 P.S. To elaborate a bit more on why converting NamedTemporaryFile to subclass file would be a much bigger deal than it is to just fix its __enter__ and __exit__ methods: - converts from old-style to new-style class - imposes the constraints of a C base class on any subclasses - file attribute is currently part of the public API - need to retain wrapper approach in tempfile anyway for related type SpooledTemporaryFile in 2.6 (as that may contain a real file or a string IO object at different points in its lifecycle) - no compelling use case for the change (the wrapper approach works, what real advantage do we gain from subclassing file instead?) - wrapper approach is much easier to reconcile with Python 3.0's io module
History
Date User Action Args
2022-04-11 14:56:30 admin set github: 46305
2008-02-11 12:55:58 ncoghlan set status: pending -> closedmessages: +
2008-02-11 10:58:59 ncoghlan set messages: +
2008-02-10 22:57:18 belopolsky set files: + subclass-file.diffmessages: +
2008-02-09 15:32:01 ncoghlan set status: open -> pendingtype: enhancement -> behaviorresolution: fixedmessages: +
2008-02-09 13:21:46 ncoghlan set assignee: ncoghlannosy: + ncoghlan
2008-02-06 18🔞20 christian.heimes set priority: normaltype: behavior -> enhancementmessages: + nosy: + christian.heimes
2008-02-06 16:20:56 draghuram set keywords: + patch
2008-02-06 16:14:23 belopolsky create