mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 19:34:08 +00:00 
			
		
		
		
	Merged revisions 77136 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r77136 | ezio.melotti | 2009-12-30 08:14:51 +0200 (Wed, 30 Dec 2009) | 1 line #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin. ........
This commit is contained in:
		
							parent
							
								
									588009e934
								
							
						
					
					
						commit
						faa6b7f421
					
				
					 4 changed files with 458 additions and 477 deletions
				
			
		| 
						 | 
				
			
			@ -102,25 +102,32 @@ 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 ::
 | 
			
		||||
   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`).  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.
 | 
			
		||||
 | 
			
		||||
      cat myzip.zip >> python.exe
 | 
			
		||||
   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::
 | 
			
		||||
 | 
			
		||||
   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.
 | 
			
		||||
      with ZipFile('spam.zip', 'w') as myzip:
 | 
			
		||||
          myzip.write('eggs.txt')
 | 
			
		||||
 | 
			
		||||
   .. versionadded:: 3.2
 | 
			
		||||
      Added the ability to use :class:`ZipFile` as a context manager.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
.. method:: ZipFile.close()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							| 
						 | 
				
			
			@ -719,6 +719,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."""
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -171,6 +171,9 @@ C-API
 | 
			
		|||
Library
 | 
			
		||||
-------
 | 
			
		||||
 | 
			
		||||
- Issue #5511: now zipfile.ZipFile can be used as a context manager.
 | 
			
		||||
  Initial patch by Brian Curtin.
 | 
			
		||||
 | 
			
		||||
- Issue #7556: Make sure Distutils' msvc9compile reads and writes the
 | 
			
		||||
  MSVC XML Manifest file in text mode so string patterns can be used 
 | 
			
		||||
  in regular expressions.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue