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: ZipFile.open context manager support
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Mike.Short, Ralph.Broenink, berker.peksag, docs@python, python-dev, serhiy.storchaka, tuomas.suutari
Priority: low Keywords: easy, patch

Created on 2014-08-06 07:56 by Ralph.Broenink, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
zipfile.patch Mike.Short, 2014-09-01 21:55 review
Messages (8)
msg224914 - (view) Author: Ralph Broenink (Ralph.Broenink) Date: 2014-08-06 07:56
In Python 3.2, context manager support for ZipFile was added. However, I would also love the ability for ``ZipFile.open`` to be used as a context manager, e.g.:

    from zipfile import ZipFile
    with ZipFile("test.zip", "r") as z:
        with z.open("test.txt", "r") as f:
            print(f.read())
msg224915 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-08-06 08:31
This is already implemented.

>>> with ZipFile("Lib/test/zip_cp437_header.zip", "r") as z:
...     with z.open("filename_without.txt", "r") as f:
...         print(f.read())
...     print(f.closed)
... 
b'EOF\r\n'
True
msg224920 - (view) Author: Ralph Broenink (Ralph.Broenink) Date: 2014-08-06 09:42
Perhaps this should be documented then :)
msg224921 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-08-06 10:03
May be.
msg226246 - (view) Author: Mike Short (Mike.Short) * Date: 2014-09-01 21:55
Context manager comment & code snippet added to zipfile doc - patch attached.
msg237437 - (view) Author: Tuomas Suutari (tuomas.suutari) * Date: 2015-03-07 11:03
The context manager support was added on issue #5511, so the documentation should go to 2.7, 3.4 and default. Also the version added notes should be added.
msg237996 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-03-13 00:29
New changeset b6fdf8c7a74d by Berker Peksag in branch '3.4':
Issue #22154: Add an example to show context management protocol support of ZipFile.open().
https://hg.python.org/cpython/rev/b6fdf8c7a74d

New changeset 390a2f4dfd4a by Berker Peksag in branch 'default':
Issue #22154: Add an example to show context management protocol support of ZipFile.open().
https://hg.python.org/cpython/rev/390a2f4dfd4a
msg237997 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2015-03-13 00:31
Thanks for the patch, Mike.
History
Date User Action Args
2022-04-11 14:58:06adminsetgithub: 66350
2015-03-13 00:31:52berker.peksagsetstatus: open -> closed

type: behavior -> enhancement
versions: + Python 3.4
nosy: + berker.peksag

messages: + msg237997
resolution: fixed
stage: needs patch -> resolved
2015-03-13 00:29:14python-devsetnosy: + python-dev
messages: + msg237996
2015-03-07 11:03:28tuomas.suutarisetnosy: + tuomas.suutari
messages: + msg237437
2014-09-01 21:55:14Mike.Shortsetfiles: + zipfile.patch

nosy: + Mike.Short
messages: + msg226246

keywords: + patch
2014-08-06 10:03:36serhiy.storchakasetstatus: closed -> open

resolution: out of date -> (no value)

assignee: docs@python
keywords: + easy
stage: resolved -> needs patch
nosy: + docs@python
messages: + msg224921
priority: normal -> low
components: + Documentation, - Library (Lib)
type: enhancement -> behavior
2014-08-06 09:42:57Ralph.Broeninksetmessages: + msg224920
2014-08-06 08:31:21serhiy.storchakasetstatus: open -> closed

components: + Library (Lib), - Extension Modules

nosy: + serhiy.storchaka
messages: + msg224915
resolution: out of date
stage: resolved
2014-08-06 07:56:40Ralph.Broeninkcreate