Author: Matti Picus <[email protected]>
Branch: 
Changeset: r69808:0e3a28693bc6
Date: 2014-03-08 23:30 +0200
http://bitbucket.org/pypy/pypy/changeset/0e3a28693bc6/

Log:    win32 - use test_support.rmtree to prevent race conditions, fix
        open() calls

diff --git a/lib-python/2.7/test/test_zipfile.py 
b/lib-python/2.7/test/test_zipfile.py
--- a/lib-python/2.7/test/test_zipfile.py
+++ b/lib-python/2.7/test/test_zipfile.py
@@ -19,7 +19,7 @@
 from unittest import skipUnless
 
 from test.test_support import TESTFN, TESTFN_UNICODE, TESTFN_ENCODING, \
-                              run_unittest, findfile, unlink
+                              run_unittest, findfile, unlink, rmtree
 try:
     TESTFN_UNICODE.encode(TESTFN_ENCODING)
 except (UnicodeError, TypeError):
@@ -365,7 +365,8 @@
         produces the expected result."""
         with zipfile.ZipFile(TESTFN2, "w") as zipfp:
             zipfp.write(TESTFN)
-            self.assertEqual(zipfp.read(TESTFN), open(TESTFN).read())
+            with open(TESTFN,'r') as fid:
+                self.assertEqual(zipfp.read(TESTFN), fid.read())
 
     @skipUnless(zlib, "requires zlib")
     def test_per_file_compression(self):
@@ -404,11 +405,12 @@
                 self.assertEqual(writtenfile, correctfile)
 
                 # make sure correct data is in correct file
-                self.assertEqual(fdata, open(writtenfile, "rb").read())
+                with open(writtenfile, "rb") as fid:
+                    self.assertEqual(fdata, fid.read())
                 os.remove(writtenfile)
 
         # remove the test file subdirectories
-        shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
+        rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
 
     def test_extract_all(self):
         with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
@@ -419,12 +421,13 @@
             zipfp.extractall()
             for fpath, fdata in SMALL_TEST_DATA:
                 outfile = os.path.join(os.getcwd(), fpath)
-
-                self.assertEqual(fdata, open(outfile, "rb").read())
+                
+                with open(outfile, "rb") as fid:
+                    self.assertEqual(fdata, fid.read())
                 os.remove(outfile)
 
         # remove the test file subdirectories
-        shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
+        rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
 
     def check_file(self, filename, content):
         self.assertTrue(os.path.isfile(filename))
@@ -509,12 +512,12 @@
                 self.assertEqual(writtenfile, correctfile,
                                  msg="extract %r" % arcname)
             self.check_file(correctfile, content)
-            shutil.rmtree('target')
+            rmtree('target')
 
             with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
                 zipfp.extractall(targetpath)
             self.check_file(correctfile, content)
-            shutil.rmtree('target')
+            rmtree('target')
 
             correctfile = os.path.join(os.getcwd(), *fixedname.split('/'))
 
@@ -523,12 +526,12 @@
                 self.assertEqual(writtenfile, correctfile,
                                  msg="extract %r" % arcname)
             self.check_file(correctfile, content)
-            shutil.rmtree(fixedname.split('/')[0])
+            rmtree(fixedname.split('/')[0])
 
             with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
                 zipfp.extractall()
             self.check_file(correctfile, content)
-            shutil.rmtree(fixedname.split('/')[0])
+            rmtree(fixedname.split('/')[0])
 
             os.remove(TESTFN2)
 
@@ -593,6 +596,8 @@
     def tearDown(self):
         unlink(TESTFN)
         unlink(TESTFN2)
+        if os.path.exists(TESTFN):
+            os.remove(TESTFN)
 
 
 class TestZip64InSmallFiles(unittest.TestCase):
@@ -712,6 +717,12 @@
 
 
 class PyZipFileTests(unittest.TestCase):
+    def teardown(self):
+        if os.path.exists(TESTFN):
+            os.remove(TESTFN)
+        if os.path.exists(TESTFN2):
+            os.remove(TESTFN2)
+
     def test_write_pyfile(self):
         with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
             fn = __file__
@@ -773,11 +784,14 @@
             self.assertNotIn('mod2.txt', names)
 
         finally:
-            shutil.rmtree(TESTFN2)
+            rmtree(TESTFN2)
 
     def test_write_non_pyfile(self):
+        if os.path.exists(TESTFN):
+            os.remove(TESTFN)
         with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
-            open(TESTFN, 'w').write('most definitely not a python file')
+            with open(TESTFN, 'w') as fid:
+                fid.write('most definitely not a python file')
             self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
             os.remove(TESTFN)
 
@@ -940,8 +954,9 @@
         self.assertRaises(RuntimeError, zipf.open, "foo.txt")
         self.assertRaises(RuntimeError, zipf.testzip)
         self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
-        open(TESTFN, 'w').write('zipfile test data')
-        self.assertRaises(RuntimeError, zipf.write, TESTFN)
+        with open(TESTFN, 'w') as fid:
+            fid.write('zipfile test data')
+            self.assertRaises(RuntimeError, zipf.write, TESTFN)
 
     def test_bad_constructor_mode(self):
         """Check that bad modes passed to ZipFile constructor are caught."""
@@ -1126,6 +1141,7 @@
             pass
         try:
             zipf = zipfile.ZipFile(TESTFN, mode="r")
+            zipf.close()
         except zipfile.BadZipfile:
             self.fail("Unable to create empty ZIP file in 'w' mode")
 
@@ -1133,6 +1149,7 @@
             pass
         try:
             zipf = zipfile.ZipFile(TESTFN, mode="r")
+            zipf.close()
         except:
             self.fail("Unable to create empty ZIP file in 'a' mode")
 
@@ -1151,6 +1168,8 @@
     def tearDown(self):
         unlink(TESTFN)
         unlink(TESTFN2)
+        if os.path.exists(TESTFN):
+            os.remove(TESTFN)
 
 
 class DecryptionTests(unittest.TestCase):
@@ -1201,16 +1220,28 @@
 
     def test_bad_password(self):
         self.zip.setpassword("perl")
-        self.assertRaises(RuntimeError, self.zip.read, "test.txt")
+        try:
+            self.assertRaises(RuntimeError, self.zip.read, "test.txt")
+        finally:
+            self.zip.close()
         self.zip2.setpassword("perl")
-        self.assertRaises(RuntimeError, self.zip2.read, "zero")
+        try:
+            self.assertRaises(RuntimeError, self.zip2.read, "zero")
+        finally:    
+            self.zip2.close()
 
     @skipUnless(zlib, "requires zlib")
     def test_good_password(self):
         self.zip.setpassword("python")
-        self.assertEqual(self.zip.read("test.txt"), self.plain)
+        try:
+            self.assertEqual(self.zip.read("test.txt"), self.plain)
+        finally:
+            self.zip.close()
         self.zip2.setpassword("12345")
-        self.assertEqual(self.zip2.read("zero"), self.plain2)
+        try:
+            self.assertEqual(self.zip2.read("zero"), self.plain2)
+        finally:
+            self.zip2.close()
 
 
 class TestsWithRandomBinaryFiles(unittest.TestCase):
@@ -1224,8 +1255,10 @@
             fp.write(self.data)
 
     def tearDown(self):
-        unlink(TESTFN)
-        unlink(TESTFN2)
+        if os.path.exists(TESTFN):
+            os.remove(TESTFN)
+        if os.path.exists(TESTFN2):
+            os.remove(TESTFN2)
 
     def make_test_archive(self, f, compression):
         # Create the ZIP archive
@@ -1329,12 +1362,11 @@
         # Verify that (when the ZipFile is in control of creating file objects)
         # multiple open() calls can be made without interfering with each 
other.
         with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
-            zopen1 = zipf.open('ones')
-            zopen2 = zipf.open('ones')
-            data1 = zopen1.read(500)
-            data2 = zopen2.read(500)
-            data1 += zopen1.read(500)
-            data2 += zopen2.read(500)
+            with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2:
+                data1 = zopen1.read(500)
+                data2 = zopen2.read(500)
+                data1 += zopen1.read(500)
+                data2 += zopen2.read(500)
             self.assertEqual(data1, data2)
 
     def test_different_file(self):
@@ -1394,14 +1426,14 @@
 
     def test_store_dir(self):
         os.mkdir(os.path.join(TESTFN2, "x"))
-        zipf = zipfile.ZipFile(TESTFN, "w")
-        zipf.write(os.path.join(TESTFN2, "x"), "x")
-        self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
+        with zipfile.ZipFile(TESTFN, "w") as zipf:
+            zipf.write(os.path.join(TESTFN2, "x"), "x")
+            self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
 
     def tearDown(self):
-        shutil.rmtree(TESTFN2)
+        rmtree(TESTFN2)
         if os.path.exists(TESTFN):
-            unlink(TESTFN)
+            os.remove(TESTFN)
 
 
 class UniversalNewlineTests(unittest.TestCase):
@@ -1413,7 +1445,8 @@
         for n, s in enumerate(self.seps):
             self.arcdata[s] = s.join(self.line_gen) + s
             self.arcfiles[s] = '%s-%d' % (TESTFN, n)
-            open(self.arcfiles[s], "wb").write(self.arcdata[s])
+            with open(self.arcfiles[s], "wb") as fid:
+                fid.write(self.arcdata[s])
 
     def make_test_archive(self, f, compression):
         # Create the ZIP archive
@@ -1482,8 +1515,9 @@
         # Read the ZIP archive
         with zipfile.ZipFile(f, "r") as zipfp:
             for sep, fn in self.arcfiles.items():
-                for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
-                    self.assertEqual(zipline, line + '\n')
+                with zipfp.open(fn, "rU") as fid:
+                    for line, zipline in zip(self.line_gen, fid):
+                        self.assertEqual(zipline, line + '\n')
 
     def test_read_stored(self):
         for f in (TESTFN2, TemporaryFile(), StringIO()):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to