Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
569e61f3
Commit
569e61f3
authored
Dec 30, 2009
by
Ezio Melotti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
parent
eb74da8e
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
489 additions
and
510 deletions
+489
-510
Doc/library/zipfile.rst
Doc/library/zipfile.rst
+27
-20
Lib/test/test_zipfile.py
Lib/test/test_zipfile.py
+452
-489
Lib/zipfile.py
Lib/zipfile.py
+6
-0
Misc/NEWS
Misc/NEWS
+4
-1
No files found.
Doc/library/zipfile.rst
View file @
569e61f3
...
...
@@ -105,28 +105,35 @@ ZipFile Objects
Open a ZIP file, where *file* can be either a path to a file (a string) or a
file-like object. The *mode* parameter should be ``'r'`` to read an existing
file, ``'w'`` to truncate and write a new file, or ``'a'`` to append to an
existing file. If *mode* is ``'a'`` and *file* refers to an existing ZIP file,
then additional files are added to it. If *file* does not refer to a ZIP file,
then a new ZIP archive is appended to the file. This is meant for adding a ZIP
archive to another file, such as :file:`python.exe`. Using ::
cat myzip.zip >> python.exe
also works, and at least :program:`WinZip` can read such files. If *mode* is
``a`` and the file does not exist at all, it is created. *compression* is the
ZIP compression method to use when writing the archive, and should be
:const:`ZIP_STORED` or :const:`ZIP_DEFLATED`; unrecognized values will cause
:exc:`RuntimeError` to be raised. If :const:`ZIP_DEFLATED` is specified but the
:mod:`zlib` module is not available, :exc:`RuntimeError` is also raised. The
default is :const:`ZIP_STORED`. If *allowZip64* is ``True`` zipfile will create
ZIP files that use the ZIP64 extensions when the zipfile is larger than 2 GB. If
it is false (the default) :mod:`zipfile` will raise an exception when the ZIP
file would require ZIP64 extensions. ZIP64 extensions are disabled by default
because the default :program:`zip` and :program:`unzip` commands on Unix (the
InfoZIP utilities) don't support these extensions.
existing file. If *mode* is ``'a'`` and *file* refers to an existing ZIP
file, then additional files are added to it. If *file* does not refer to a
ZIP file, then a new ZIP archive is appended to the file. This is meant for
adding a ZIP archive to another file (such as :file:`python.exe`).
.. versionchanged:: 2.6
If the file does not exist, it is created if the mode is 'a'.
If *mode* is ``a`` and the file does not exist at all, it is created.
*compression* is the ZIP compression method to use when writing the archive,
and should be :const:`ZIP_STORED` or :const:`ZIP_DEFLATED`; unrecognized
values will cause :exc:`RuntimeError` to be raised. If :const:`ZIP_DEFLATED`
is specified but the :mod:`zlib` module is not available, :exc:`RuntimeError`
is also raised. The default is :const:`ZIP_STORED`. If *allowZip64* is
``True`` zipfile will create ZIP files that use the ZIP64 extensions when
the zipfile is larger than 2 GB. If it is false (the default) :mod:`zipfile`
will raise an exception when the ZIP file would require ZIP64 extensions.
ZIP64 extensions are disabled by default because the default :program:`zip`
and :program:`unzip` commands on Unix (the InfoZIP utilities) don't support
these extensions.
ZipFile is also a context manager and therefore supports the
:keyword:`with` statement. In the example, *myzip* is closed after the
:keyword:`with` statement's suite is finished---even if an exception occurs::
with ZipFile('spam.zip', 'w') as myzip:
myzip.write('eggs.txt')
.. versionadded:: 2.7
Added the ability to use :class:`ZipFile` as a context manager.
.. method:: ZipFile.close()
...
...
Lib/test/test_zipfile.py
View file @
569e61f3
This diff is collapsed.
Click to expand it.
Lib/zipfile.py
View file @
569e61f3
...
...
@@ -721,6 +721,12 @@ class ZipFile:
self
.
fp
=
None
raise
RuntimeError
,
'Mode must be "r", "w" or "a"'
def
__enter__
(
self
):
return
self
def
__exit__
(
self
,
type
,
value
,
traceback
):
self
.
close
()
def
_GetContents
(
self
):
"""Read the directory, making sure we close the file if the format
is bad."""
...
...
Misc/NEWS
View file @
569e61f3
...
...
@@ -44,8 +44,11 @@ Core and Builtins
Library
-------
- Issue #5511: now zipfile.ZipFile can be used as a context manager.
Initial patch by Brian Curtin.
- Distutils now correctly identifies the build architecture as "x86_64"
when building on OSX 10.6 without "-arch" flags.
when building on OSX 10.6 without "-arch" flags.
- Issue #7556: Distutils' msvc9compiler now opens the MSVC Manifest
file in text mode.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment