[Python-checkins] r53766 - in python/trunk: Doc/lib/libzipfile.tex Lib/test/test_zipfile.py Lib/zipfile.py Misc/NEWS

martin.v.loewis python-checkins at python.org
Tue Feb 13 11:10:40 CET 2007


Author: martin.v.loewis
Date: Tue Feb 13 11:10:39 2007
New Revision: 53766

Modified:
   python/trunk/Doc/lib/libzipfile.tex
   python/trunk/Lib/test/test_zipfile.py
   python/trunk/Lib/zipfile.py
   python/trunk/Misc/NEWS
Log:
Patch #1517891: Make 'a' create the file if it doesn't exist.
Fixes #1514451.


Modified: python/trunk/Doc/lib/libzipfile.tex
==============================================================================
--- python/trunk/Doc/lib/libzipfile.tex	(original)
+++ python/trunk/Doc/lib/libzipfile.tex	Tue Feb 13 11:10:39 2007
@@ -101,6 +101,8 @@
 \end{verbatim}
 
   also works, and at least \program{WinZip} can read such files.
+  If \var{mode} is \code{a} and the file does not exist at all,
+  it is created.
   \var{compression} is the ZIP compression method to use when writing
   the archive, and should be \constant{ZIP_STORED} or
   \constant{ZIP_DEFLATED}; unrecognized values will cause
@@ -114,6 +116,9 @@
   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.
+
+  \versionchanged[If the file does not exist, it is created if the
+  mode is 'a']{2.6}
 \end{classdesc}
 
 \begin{methoddesc}{close}{}

Modified: python/trunk/Lib/test/test_zipfile.py
==============================================================================
--- python/trunk/Lib/test/test_zipfile.py	(original)
+++ python/trunk/Lib/test/test_zipfile.py	Tue Feb 13 11:10:39 2007
@@ -307,6 +307,28 @@
 
 
 class OtherTests(unittest.TestCase):
+    def testCreateNonExistentFileForAppend(self):
+        if os.path.exists(TESTFN):
+            os.unlink(TESTFN)
+            
+        filename = 'testfile.txt'
+        content = 'hello, world. this is some content.'
+        
+        try:
+            zf = zipfile.ZipFile(TESTFN, 'a')
+            zf.writestr(filename, content)
+            zf.close()
+        except IOError, (errno, errmsg):
+            self.fail('Could not append data to a non-existent zip file.')
+
+        self.assert_(os.path.exists(TESTFN))
+
+        zf = zipfile.ZipFile(TESTFN, 'r')
+        self.assertEqual(zf.read(filename), content)
+        zf.close()
+        
+        os.unlink(TESTFN)
+        
     def testCloseErroneousFile(self):
         # This test checks that the ZipFile constructor closes the file object
         # it opens if there's an error in the file.  If it doesn't, the traceback

Modified: python/trunk/Lib/zipfile.py
==============================================================================
--- python/trunk/Lib/zipfile.py	(original)
+++ python/trunk/Lib/zipfile.py	Tue Feb 13 11:10:39 2007
@@ -396,7 +396,14 @@
             self._filePassed = 0
             self.filename = file
             modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'}
-            self.fp = open(file, modeDict[mode])
+            try:
+                self.fp = open(file, modeDict[mode])
+            except IOError:
+                if mode == 'a':
+                    mode = key = 'w'
+                    self.fp = open(file, modeDict[mode])
+                else:
+                    raise
         else:
             self._filePassed = 1
             self.fp = file

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Tue Feb 13 11:10:39 2007
@@ -128,6 +128,9 @@
 Library
 -------
 
+- Patch #1517891: Mode 'a' for ZipFile now creates the file if it
+  doesn't exist.
+
 - Patch #698833: Support file decryption in zipfile.
 
 - Patch #685268: Consider a package's __path__ in imputil.


More information about the Python-checkins mailing list