homepage

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: Turn NamedTemporaryFile into a context manager
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ncoghlan Nosy List: belopolsky, christian.heimes, ncoghlan
Priority: normal Keywords: patch

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:30adminsetgithub: 46305
2008年02月11日 12:55:58ncoghlansetstatus: pending -> closed
messages: + msg62285
2008年02月11日 10:58:59ncoghlansetmessages: + msg62278
2008年02月10日 22:57:18belopolskysetfiles: + subclass-file.diff
messages: + msg62264
2008年02月09日 15:32:01ncoghlansetstatus: open -> pending
type: enhancement -> behavior
resolution: fixed
messages: + msg62223
2008年02月09日 13:21:46ncoghlansetassignee: ncoghlan
nosy: + ncoghlan
2008年02月06日 18:18:20christian.heimessetpriority: normal
type: behavior -> enhancement
messages: + msg62111
nosy: + christian.heimes
2008年02月06日 16:20:56draghuramsetkeywords: + patch
2008年02月06日 16:14:23belopolskycreate

AltStyle によって変換されたページ (->オリジナル) /