classification
Title: GzipFile and BZ2File should support context manager protocol
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.1, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, hagen, haypo, pitrou, teamnoir
Priority: normal Keywords: patch

Created on 2008-09-13 19:29 by hagen, last changed 2011-02-12 19:32 by teamnoir. This issue is now closed.

Files
File name Uploaded Description Edit
withgzip.patch pitrou, 2008-12-18 17:53
Messages (8)
msg73194 - (view) Author: Hagen F├╝rstenau (hagen) Date: 2008-09-13 19:29
When you've become used to writing

with open("xzy", "w") as f: 

it's strange when it doesn't work for gzip.open or bz2.BZ2File.
Or is there a reason for them not being context managers?
msg78035 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-12-18 17:53
Here is a patch for the trunk. I'm waiting for the opinion other
developers before deciding to commit or no.
msg78037 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-12-18 18:43
Here's the StringIO discussion: #1286
msg78046 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-12-18 23:01
Thanks. The outcome of the StringIO discussion isn't clearly negative,
although Guido does not see a big benefit in it. If some people are
opposed to this, please stand up :) Otherwise I'll commit the patch to
trunk.
msg78319 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2008-12-26 23:45
I love "with ... as f:" and I want it for gzip/bz2 files!

Comment about the patch: 

(1) the IOError message is different for the two modules:
   "I/O operation on closed GzipFile object"
vs
   "I/O operation on closed file"

Specify the object name in both messages or remove it from both 
messages. I prefer verbose error message ("I/O operation on closed 
BZ2File object").

(2) For bz2module, Why don't you call BZ2File_close() directly instead 
of the generic version PyObject_CallMethod(... "close" ...)?
msg78324 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-12-27 00:46
> (1) the IOError message is different for the two modules:
>    "I/O operation on closed GzipFile object"
> vs
>    "I/O operation on closed file"

I've reused the same error message as used in other parts of each
module.

> (2) For bz2module, Why don't you call BZ2File_close() directly instead 
> of the generic version PyObject_CallMethod(... "close" ...)?

Because someone may subclass BZ2File and override the close() method. I
don't know if it's likely to happen, though.
msg79551 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-01-10 16:23
Committed in trunk and py3k. Thanks!
msg128458 - (view) Author: K Richard Pixley (teamnoir) Date: 2011-02-12 19:32
Documentation needs to be updated to state that these are now context managers.  This is important since they aren't in python-2.x.

I'm not sure whether this should be added to the "new in python" blurbs.
History
Date User Action Args
2011-02-12 19:32:25teamnoirsetnosy: + teamnoir
messages: + msg128458
2009-01-10 16:23:27pitrousetstatus: open -> closed
resolution: fixed
messages: + msg79551
2008-12-27 00:46:09pitrousetmessages: + msg78324
2008-12-26 23:45:13hayposetnosy: + haypo
messages: + msg78319
2008-12-18 23:01:42pitrousetmessages: + msg78046
2008-12-18 18:43:14benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg78037
2008-12-18 17:53:14pitrousetfiles: + withgzip.patch
keywords: + patch
messages: + msg78035
nosy: + pitrou
stage: patch review
2008-09-13 19:32:07benjamin.petersonsetpriority: normal
versions: + Python 3.1, Python 2.7, - Python 2.6, Python 3.0
2008-09-13 19:29:02hagencreate