Issue 3860: GzipFile and BZ2File should support context manager protocol (original) (raw)

Created on 2008-09-13 19:29 by hagen, last changed 2022-04-11 14:56 by admin. 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 (vstinner) * (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
2022-04-11 14:56:39 admin set github: 48110
2011-02-12 19:32:25 teamnoir set nosy: + teamnoirmessages: +
2009-01-10 16:23:27 pitrou set status: open -> closedresolution: fixedmessages: +
2008-12-27 00:46:09 pitrou set messages: +
2008-12-26 23:45:13 vstinner set nosy: + vstinnermessages: +
2008-12-18 23:01:42 pitrou set messages: +
2008-12-18 18:43:14 benjamin.peterson set nosy: + benjamin.petersonmessages: +
2008-12-18 17:53:14 pitrou set files: + withgzip.patchkeywords: + patchmessages: + nosy: + pitroustage: patch review
2008-09-13 19:32:07 benjamin.peterson set priority: normalversions: + Python 3.1, Python 2.7, - Python 2.6, Python 3.0
2008-09-13 19:29:02 hagen create