Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_zipfile.py

    r2 r391  
    55    zlib = None
    66
    7 import zipfile, os, unittest, sys, shutil, struct
     7import os
     8import io
     9import sys
     10import time
     11import shutil
     12import struct
     13import zipfile
     14import unittest
    815
    916from StringIO import StringIO
    1017from tempfile import TemporaryFile
    1118from random import randint, random
    12 
    13 import test.test_support as support
    14 from test.test_support import TESTFN, run_unittest, findfile
     19from unittest import skipUnless
     20
     21from test.test_support import TESTFN, TESTFN_UNICODE, TESTFN_ENCODING, \
     22                              run_unittest, findfile, unlink
     23try:
     24    TESTFN_UNICODE.encode(TESTFN_ENCODING)
     25except (UnicodeError, TypeError):
     26    # Either the file system encoding is None, or the file name
     27    # cannot be encoded in the file system encoding.
     28    TESTFN_UNICODE = None
    1529
    1630TESTFN2 = TESTFN + "2"
     
    2034SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
    2135                   ('ziptest2dir/_ziptest2', 'qawsedrftg'),
    22                    ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
     36                   ('ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
    2337                   ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
     38
    2439
    2540class TestsWithSourceFile(unittest.TestCase):
    2641    def setUp(self):
    2742        self.line_gen = ["Zipfile test line %d. random float: %f" % (i, random())
    28                           for i in xrange(FIXEDTEST_SIZE)]
     43                         for i in xrange(FIXEDTEST_SIZE)]
    2944        self.data = '\n'.join(self.line_gen) + '\n'
    3045
    3146        # Make a source file with some lines
    32         fp = open(TESTFN, "wb")
    33         fp.write(self.data)
    34         fp.close()
    35 
    36     def makeTestArchive(self, f, compression):
     47        with open(TESTFN, "wb") as fp:
     48            fp.write(self.data)
     49
     50    def make_test_archive(self, f, compression):
    3751        # Create the ZIP archive
    38         zipfp = zipfile.ZipFile(f, "w", compression)
    39         zipfp.write(TESTFN, "another"+os.extsep+"name")
    40         zipfp.write(TESTFN, TESTFN)
    41         zipfp.writestr("strfile", self.data)
    42         zipfp.close()
    43 
    44     def zipTest(self, f, compression):
    45         self.makeTestArchive(f, compression)
     52        with zipfile.ZipFile(f, "w", compression) as zipfp:
     53            zipfp.write(TESTFN, "another.name")
     54            zipfp.write(TESTFN, TESTFN)
     55            zipfp.writestr("strfile", self.data)
     56
     57    def zip_test(self, f, compression):
     58        self.make_test_archive(f, compression)
    4659
    4760        # Read the ZIP archive
    48         zipfp = zipfile.ZipFile(f, "r", compression)
    49         self.assertEqual(zipfp.read(TESTFN), self.data)
    50         self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
    51         self.assertEqual(zipfp.read("strfile"), self.data)
    52 
    53         # Print the ZIP directory
    54         fp = StringIO()
    55         stdout = sys.stdout
    56         try:
    57             sys.stdout = fp
    58 
    59             zipfp.printdir()
    60         finally:
    61             sys.stdout = stdout
    62 
    63         directory = fp.getvalue()
    64         lines = directory.splitlines()
    65         self.assertEquals(len(lines), 4) # Number of files + header
    66 
    67         self.assert_('File Name' in lines[0])
    68         self.assert_('Modified' in lines[0])
    69         self.assert_('Size' in lines[0])
    70 
    71         fn, date, time, size = lines[1].split()
    72         self.assertEquals(fn, 'another.name')
    73         # XXX: timestamp is not tested
    74         self.assertEquals(size, str(len(self.data)))
    75 
    76         # Check the namelist
    77         names = zipfp.namelist()
    78         self.assertEquals(len(names), 3)
    79         self.assert_(TESTFN in names)
    80         self.assert_("another"+os.extsep+"name" in names)
    81         self.assert_("strfile" in names)
    82 
    83         # Check infolist
    84         infos = zipfp.infolist()
    85         names = [ i.filename for i in infos ]
    86         self.assertEquals(len(names), 3)
    87         self.assert_(TESTFN in names)
    88         self.assert_("another"+os.extsep+"name" in names)
    89         self.assert_("strfile" in names)
    90         for i in infos:
    91             self.assertEquals(i.file_size, len(self.data))
    92 
    93         # check getinfo
    94         for nm in (TESTFN, "another"+os.extsep+"name", "strfile"):
    95             info = zipfp.getinfo(nm)
    96             self.assertEquals(info.filename, nm)
    97             self.assertEquals(info.file_size, len(self.data))
    98 
    99         # Check that testzip doesn't raise an exception
    100         zipfp.testzip()
    101         zipfp.close()
    102 
    103     def testStored(self):
    104         for f in (TESTFN2, TemporaryFile(), StringIO()):
    105             self.zipTest(f, zipfile.ZIP_STORED)
    106 
    107     def zipOpenTest(self, f, compression):
    108         self.makeTestArchive(f, compression)
     61        with zipfile.ZipFile(f, "r", compression) as zipfp:
     62            self.assertEqual(zipfp.read(TESTFN), self.data)
     63            self.assertEqual(zipfp.read("another.name"), self.data)
     64            self.assertEqual(zipfp.read("strfile"), self.data)
     65
     66            # Print the ZIP directory
     67            fp = StringIO()
     68            stdout = sys.stdout
     69            try:
     70                sys.stdout = fp
     71                zipfp.printdir()
     72            finally:
     73                sys.stdout = stdout
     74
     75            directory = fp.getvalue()
     76            lines = directory.splitlines()
     77            self.assertEqual(len(lines), 4) # Number of files + header
     78
     79            self.assertIn('File Name', lines[0])
     80            self.assertIn('Modified', lines[0])
     81            self.assertIn('Size', lines[0])
     82
     83            fn, date, time_, size = lines[1].split()
     84            self.assertEqual(fn, 'another.name')
     85            self.assertTrue(time.strptime(date, '%Y-%m-%d'))
     86            self.assertTrue(time.strptime(time_, '%H:%M:%S'))
     87            self.assertEqual(size, str(len(self.data)))
     88
     89            # Check the namelist
     90            names = zipfp.namelist()
     91            self.assertEqual(len(names), 3)
     92            self.assertIn(TESTFN, names)
     93            self.assertIn("another.name", names)
     94            self.assertIn("strfile", names)
     95
     96            # Check infolist
     97            infos = zipfp.infolist()
     98            names = [i.filename for i in infos]
     99            self.assertEqual(len(names), 3)
     100            self.assertIn(TESTFN, names)
     101            self.assertIn("another.name", names)
     102            self.assertIn("strfile", names)
     103            for i in infos:
     104                self.assertEqual(i.file_size, len(self.data))
     105
     106            # check getinfo
     107            for nm in (TESTFN, "another.name", "strfile"):
     108                info = zipfp.getinfo(nm)
     109                self.assertEqual(info.filename, nm)
     110                self.assertEqual(info.file_size, len(self.data))
     111
     112            # Check that testzip doesn't raise an exception
     113            zipfp.testzip()
     114
     115    def test_stored(self):
     116        for f in (TESTFN2, TemporaryFile(), StringIO()):
     117            self.zip_test(f, zipfile.ZIP_STORED)
     118
     119    def zip_open_test(self, f, compression):
     120        self.make_test_archive(f, compression)
    109121
    110122        # Read the ZIP archive
    111         zipfp = zipfile.ZipFile(f, "r", compression)
    112         zipdata1 = []
    113         zipopen1 = zipfp.open(TESTFN)
    114         while 1:
    115             read_data = zipopen1.read(256)
    116             if not read_data:
    117                 break
    118             zipdata1.append(read_data)
    119 
    120         zipdata2 = []
    121         zipopen2 = zipfp.open("another"+os.extsep+"name")
    122         while 1:
    123             read_data = zipopen2.read(256)
    124             if not read_data:
    125                 break
    126             zipdata2.append(read_data)
    127 
    128         self.assertEqual(''.join(zipdata1), self.data)
    129         self.assertEqual(''.join(zipdata2), self.data)
    130         zipfp.close()
    131 
    132     def testOpenStored(self):
    133         for f in (TESTFN2, TemporaryFile(), StringIO()):
    134             self.zipOpenTest(f, zipfile.ZIP_STORED)
    135 
    136     def testOpenViaZipInfo(self):
     123        with zipfile.ZipFile(f, "r", compression) as zipfp:
     124            zipdata1 = []
     125            with zipfp.open(TESTFN) as zipopen1:
     126                while True:
     127                    read_data = zipopen1.read(256)
     128                    if not read_data:
     129                        break
     130                    zipdata1.append(read_data)
     131
     132            zipdata2 = []
     133            with zipfp.open("another.name") as zipopen2:
     134                while True:
     135                    read_data = zipopen2.read(256)
     136                    if not read_data:
     137                        break
     138                    zipdata2.append(read_data)
     139
     140            self.assertEqual(''.join(zipdata1), self.data)
     141            self.assertEqual(''.join(zipdata2), self.data)
     142
     143    def test_open_stored(self):
     144        for f in (TESTFN2, TemporaryFile(), StringIO()):
     145            self.zip_open_test(f, zipfile.ZIP_STORED)
     146
     147    def test_open_via_zip_info(self):
    137148        # Create the ZIP archive
    138         zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
    139         zipfp.writestr("name", "foo")
    140         zipfp.writestr("name", "bar")
    141         zipfp.close()
    142 
    143         zipfp = zipfile.ZipFile(TESTFN2, "r")
    144         infos = zipfp.infolist()
    145         data = ""
    146         for info in infos:
    147             data += zipfp.open(info).read()
    148         self.assert_(data == "foobar" or data == "barfoo")
    149         data = ""
    150         for info in infos:
    151             data += zipfp.read(info)
    152         self.assert_(data == "foobar" or data == "barfoo")
    153         zipfp.close()
    154 
    155     def zipRandomOpenTest(self, f, compression):
    156         self.makeTestArchive(f, compression)
     149        with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
     150            zipfp.writestr("name", "foo")
     151            zipfp.writestr("name", "bar")
     152
     153        with zipfile.ZipFile(TESTFN2, "r") as zipfp:
     154            infos = zipfp.infolist()
     155            data = ""
     156            for info in infos:
     157                with zipfp.open(info) as f:
     158                    data += f.read()
     159            self.assertTrue(data == "foobar" or data == "barfoo")
     160            data = ""
     161            for info in infos:
     162                data += zipfp.read(info)
     163            self.assertTrue(data == "foobar" or data == "barfoo")
     164
     165    def zip_random_open_test(self, f, compression):
     166        self.make_test_archive(f, compression)
    157167
    158168        # Read the ZIP archive
    159         zipfp = zipfile.ZipFile(f, "r", compression)
    160         zipdata1 = []
    161         zipopen1 = zipfp.open(TESTFN)
    162         while 1:
    163             read_data = zipopen1.read(randint(1, 1024))
    164             if not read_data:
    165                 break
    166             zipdata1.append(read_data)
    167 
    168         self.assertEqual(''.join(zipdata1), self.data)
    169         zipfp.close()
    170 
    171     def testRandomOpenStored(self):
    172         for f in (TESTFN2, TemporaryFile(), StringIO()):
    173             self.zipRandomOpenTest(f, zipfile.ZIP_STORED)
    174 
    175     def zipReadlineTest(self, f, compression):
    176         self.makeTestArchive(f, compression)
     169        with zipfile.ZipFile(f, "r", compression) as zipfp:
     170            zipdata1 = []
     171            with zipfp.open(TESTFN) as zipopen1:
     172                while True:
     173                    read_data = zipopen1.read(randint(1, 1024))
     174                    if not read_data:
     175                        break
     176                    zipdata1.append(read_data)
     177
     178            self.assertEqual(''.join(zipdata1), self.data)
     179
     180    def test_random_open_stored(self):
     181        for f in (TESTFN2, TemporaryFile(), StringIO()):
     182            self.zip_random_open_test(f, zipfile.ZIP_STORED)
     183
     184    def test_univeral_readaheads(self):
     185        f = StringIO()
     186
     187        data = 'a\r\n' * 16 * 1024
     188        with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as zipfp:
     189            zipfp.writestr(TESTFN, data)
     190
     191        data2 = ''
     192        with zipfile.ZipFile(f, 'r') as zipfp:
     193            with zipfp.open(TESTFN, 'rU') as zipopen:
     194                for line in zipopen:
     195                    data2 += line
     196
     197        self.assertEqual(data, data2.replace('\n', '\r\n'))
     198
     199    def zip_readline_read_test(self, f, compression):
     200        self.make_test_archive(f, compression)
    177201
    178202        # Read the ZIP archive
    179         zipfp = zipfile.ZipFile(f, "r")
    180         zipopen = zipfp.open(TESTFN)
    181         for line in self.line_gen:
    182             linedata = zipopen.readline()
    183             self.assertEqual(linedata, line + '\n')
    184 
    185         zipfp.close()
    186 
    187     def zipReadlinesTest(self, f, compression):
    188         self.makeTestArchive(f, compression)
     203        with zipfile.ZipFile(f, "r") as zipfp:
     204            with zipfp.open(TESTFN) as zipopen:
     205                data = ''
     206                while True:
     207                    read = zipopen.readline()
     208                    if not read:
     209                        break
     210                    data += read
     211
     212                    read = zipopen.read(100)
     213                    if not read:
     214                        break
     215                    data += read
     216
     217        self.assertEqual(data, self.data)
     218
     219    def zip_readline_test(self, f, compression):
     220        self.make_test_archive(f, compression)
    189221
    190222        # Read the ZIP archive
    191         zipfp = zipfile.ZipFile(f, "r")
    192         ziplines = zipfp.open(TESTFN).readlines()
    193         for line, zipline in zip(self.line_gen, ziplines):
    194             self.assertEqual(zipline, line + '\n')
    195 
    196         zipfp.close()
    197 
    198     def zipIterlinesTest(self, f, compression):
    199         self.makeTestArchive(f, compression)
     223        with zipfile.ZipFile(f, "r") as zipfp:
     224            with zipfp.open(TESTFN) as zipopen:
     225                for line in self.line_gen:
     226                    linedata = zipopen.readline()
     227                    self.assertEqual(linedata, line + '\n')
     228
     229    def zip_readlines_test(self, f, compression):
     230        self.make_test_archive(f, compression)
    200231
    201232        # Read the ZIP archive
    202         zipfp = zipfile.ZipFile(f, "r")
    203         for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
    204             self.assertEqual(zipline, line + '\n')
    205 
    206         zipfp.close()
    207 
    208     def testReadlineStored(self):
    209         for f in (TESTFN2, TemporaryFile(), StringIO()):
    210             self.zipReadlineTest(f, zipfile.ZIP_STORED)
    211 
    212     def testReadlinesStored(self):
    213         for f in (TESTFN2, TemporaryFile(), StringIO()):
    214             self.zipReadlinesTest(f, zipfile.ZIP_STORED)
    215 
    216     def testIterlinesStored(self):
    217         for f in (TESTFN2, TemporaryFile(), StringIO()):
    218             self.zipIterlinesTest(f, zipfile.ZIP_STORED)
    219 
    220     if zlib:
    221         def testDeflated(self):
    222             for f in (TESTFN2, TemporaryFile(), StringIO()):
    223                 self.zipTest(f, zipfile.ZIP_DEFLATED)
    224 
    225         def testOpenDeflated(self):
    226             for f in (TESTFN2, TemporaryFile(), StringIO()):
    227                 self.zipOpenTest(f, zipfile.ZIP_DEFLATED)
    228 
    229         def testRandomOpenDeflated(self):
    230             for f in (TESTFN2, TemporaryFile(), StringIO()):
    231                 self.zipRandomOpenTest(f, zipfile.ZIP_DEFLATED)
    232 
    233         def testReadlineDeflated(self):
    234             for f in (TESTFN2, TemporaryFile(), StringIO()):
    235                 self.zipReadlineTest(f, zipfile.ZIP_DEFLATED)
    236 
    237         def testReadlinesDeflated(self):
    238             for f in (TESTFN2, TemporaryFile(), StringIO()):
    239                 self.zipReadlinesTest(f, zipfile.ZIP_DEFLATED)
    240 
    241         def testIterlinesDeflated(self):
    242             for f in (TESTFN2, TemporaryFile(), StringIO()):
    243                 self.zipIterlinesTest(f, zipfile.ZIP_DEFLATED)
    244 
    245         def testLowCompression(self):
    246             # Checks for cases where compressed data is larger than original
    247             # Create the ZIP archive
    248             zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
     233        with zipfile.ZipFile(f, "r") as zipfp:
     234            with zipfp.open(TESTFN) as zo:
     235                ziplines = zo.readlines()
     236                for line, zipline in zip(self.line_gen, ziplines):
     237                    self.assertEqual(zipline, line + '\n')
     238
     239    def zip_iterlines_test(self, f, compression):
     240        self.make_test_archive(f, compression)
     241
     242        # Read the ZIP archive
     243        with zipfile.ZipFile(f, "r") as zipfp:
     244            for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
     245                self.assertEqual(zipline, line + '\n')
     246
     247    def test_readline_read_stored(self):
     248        # Issue #7610: calls to readline() interleaved with calls to read().
     249        for f in (TESTFN2, TemporaryFile(), StringIO()):
     250            self.zip_readline_read_test(f, zipfile.ZIP_STORED)
     251
     252    def test_readline_stored(self):
     253        for f in (TESTFN2, TemporaryFile(), StringIO()):
     254            self.zip_readline_test(f, zipfile.ZIP_STORED)
     255
     256    def test_readlines_stored(self):
     257        for f in (TESTFN2, TemporaryFile(), StringIO()):
     258            self.zip_readlines_test(f, zipfile.ZIP_STORED)
     259
     260    def test_iterlines_stored(self):
     261        for f in (TESTFN2, TemporaryFile(), StringIO()):
     262            self.zip_iterlines_test(f, zipfile.ZIP_STORED)
     263
     264    @skipUnless(zlib, "requires zlib")
     265    def test_deflated(self):
     266        for f in (TESTFN2, TemporaryFile(), StringIO()):
     267            self.zip_test(f, zipfile.ZIP_DEFLATED)
     268
     269    @skipUnless(zlib, "requires zlib")
     270    def test_open_deflated(self):
     271        for f in (TESTFN2, TemporaryFile(), StringIO()):
     272            self.zip_open_test(f, zipfile.ZIP_DEFLATED)
     273
     274    @skipUnless(zlib, "requires zlib")
     275    def test_random_open_deflated(self):
     276        for f in (TESTFN2, TemporaryFile(), StringIO()):
     277            self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
     278
     279    @skipUnless(zlib, "requires zlib")
     280    def test_readline_read_deflated(self):
     281        # Issue #7610: calls to readline() interleaved with calls to read().
     282        for f in (TESTFN2, TemporaryFile(), StringIO()):
     283            self.zip_readline_read_test(f, zipfile.ZIP_DEFLATED)
     284
     285    @skipUnless(zlib, "requires zlib")
     286    def test_readline_deflated(self):
     287        for f in (TESTFN2, TemporaryFile(), StringIO()):
     288            self.zip_readline_test(f, zipfile.ZIP_DEFLATED)
     289
     290    @skipUnless(zlib, "requires zlib")
     291    def test_readlines_deflated(self):
     292        for f in (TESTFN2, TemporaryFile(), StringIO()):
     293            self.zip_readlines_test(f, zipfile.ZIP_DEFLATED)
     294
     295    @skipUnless(zlib, "requires zlib")
     296    def test_iterlines_deflated(self):
     297        for f in (TESTFN2, TemporaryFile(), StringIO()):
     298            self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED)
     299
     300    @skipUnless(zlib, "requires zlib")
     301    def test_low_compression(self):
     302        """Check for cases where compressed data is larger than original."""
     303        # Create the ZIP archive
     304        with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
    249305            zipfp.writestr("strfile", '12')
    250             zipfp.close()
    251 
    252             # Get an open object for strfile
    253             zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED)
    254             openobj = zipfp.open("strfile")
    255             self.assertEqual(openobj.read(1), '1')
    256             self.assertEqual(openobj.read(1), '2')
    257 
    258     def testAbsoluteArcnames(self):
    259         zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
    260         zipfp.write(TESTFN, "/absolute")
    261         zipfp.close()
    262 
    263         zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
    264         self.assertEqual(zipfp.namelist(), ["absolute"])
    265         zipfp.close()
    266 
    267     def testAppendToZipFile(self):
    268         # Test appending to an existing zipfile
    269         zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
    270         zipfp.write(TESTFN, TESTFN)
    271         zipfp.close()
    272         zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
    273         zipfp.writestr("strfile", self.data)
    274         self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
    275         zipfp.close()
    276 
    277     def testAppendToNonZipFile(self):
    278         # Test appending to an existing file that is not a zipfile
     306
     307        # Get an open object for strfile
     308        with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp:
     309            with zipfp.open("strfile") as openobj:
     310                self.assertEqual(openobj.read(1), '1')
     311                self.assertEqual(openobj.read(1), '2')
     312
     313    def test_absolute_arcnames(self):
     314        with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
     315            zipfp.write(TESTFN, "/absolute")
     316
     317        with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
     318            self.assertEqual(zipfp.namelist(), ["absolute"])
     319
     320    def test_append_to_zip_file(self):
     321        """Test appending to an existing zipfile."""
     322        with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
     323            zipfp.write(TESTFN, TESTFN)
     324
     325        with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
     326            zipfp.writestr("strfile", self.data)
     327            self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
     328
     329    def test_append_to_non_zip_file(self):
     330        """Test appending to an existing file that is not a zipfile."""
    279331        # NOTE: this test fails if len(d) < 22 because of the first
    280332        # line "fpin.seek(-22, 2)" in _EndRecData
    281         d = 'I am not a ZipFile!'*10
    282         f = file(TESTFN2, 'wb')
    283         f.write(d)
    284         f.close()
    285         zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
    286         zipfp.write(TESTFN, TESTFN)
    287         zipfp.close()
    288 
    289         f = file(TESTFN2, 'rb')
    290         f.seek(len(d))
    291         zipfp = zipfile.ZipFile(f, "r")
    292         self.assertEqual(zipfp.namelist(), [TESTFN])
    293         zipfp.close()
    294         f.close()
    295 
    296     def test_WriteDefaultName(self):
    297         # Check that calling ZipFile.write without arcname specified produces the expected result
    298         zipfp = zipfile.ZipFile(TESTFN2, "w")
    299         zipfp.write(TESTFN)
    300         self.assertEqual(zipfp.read(TESTFN), file(TESTFN).read())
    301         zipfp.close()
    302 
    303     def test_PerFileCompression(self):
    304         # Check that files within a Zip archive can have different compression options
    305         zipfp = zipfile.ZipFile(TESTFN2, "w")
    306         zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
    307         zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
    308         sinfo = zipfp.getinfo('storeme')
    309         dinfo = zipfp.getinfo('deflateme')
    310         self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
    311         self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
    312         zipfp.close()
    313 
    314     def test_WriteToReadonly(self):
    315         # Check that trying to call write() on a readonly ZipFile object
    316         # raises a RuntimeError
    317         zipf = zipfile.ZipFile(TESTFN2, mode="w")
    318         zipf.writestr("somefile.txt", "bogus")
    319         zipf.close()
    320         zipf = zipfile.ZipFile(TESTFN2, mode="r")
    321         self.assertRaises(RuntimeError, zipf.write, TESTFN)
    322         zipf.close()
    323 
    324     def testExtract(self):
    325         zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
    326         for fpath, fdata in SMALL_TEST_DATA:
    327             zipfp.writestr(fpath, fdata)
    328         zipfp.close()
    329 
    330         zipfp = zipfile.ZipFile(TESTFN2, "r")
    331         for fpath, fdata in SMALL_TEST_DATA:
    332             writtenfile = zipfp.extract(fpath)
    333 
    334             # make sure it was written to the right place
    335             if os.path.isabs(fpath):
    336                 correctfile = os.path.join(os.getcwd(), fpath[1:])
    337             else:
     333        data = 'I am not a ZipFile!'*10
     334        with open(TESTFN2, 'wb') as f:
     335            f.write(data)
     336
     337        with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
     338            zipfp.write(TESTFN, TESTFN)
     339
     340        with open(TESTFN2, 'rb') as f:
     341            f.seek(len(data))
     342            with zipfile.ZipFile(f, "r") as zipfp:
     343                self.assertEqual(zipfp.namelist(), [TESTFN])
     344
     345    def test_ignores_newline_at_end(self):
     346        with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
     347            zipfp.write(TESTFN, TESTFN)
     348        with open(TESTFN2, 'a') as f:
     349            f.write("\r\n\00\00\00")
     350        with zipfile.ZipFile(TESTFN2, "r") as zipfp:
     351            self.assertIsInstance(zipfp, zipfile.ZipFile)
     352
     353    def test_ignores_stuff_appended_past_comments(self):
     354        with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
     355            zipfp.comment = b"this is a comment"
     356            zipfp.write(TESTFN, TESTFN)
     357        with open(TESTFN2, 'a') as f:
     358            f.write("abcdef\r\n")
     359        with zipfile.ZipFile(TESTFN2, "r") as zipfp:
     360            self.assertIsInstance(zipfp, zipfile.ZipFile)
     361            self.assertEqual(zipfp.comment, b"this is a comment")
     362
     363    def test_write_default_name(self):
     364        """Check that calling ZipFile.write without arcname specified
     365        produces the expected result."""
     366        with zipfile.ZipFile(TESTFN2, "w") as zipfp:
     367            zipfp.write(TESTFN)
     368            self.assertEqual(zipfp.read(TESTFN), open(TESTFN).read())
     369
     370    @skipUnless(zlib, "requires zlib")
     371    def test_per_file_compression(self):
     372        """Check that files within a Zip archive can have different
     373        compression options."""
     374        with zipfile.ZipFile(TESTFN2, "w") as zipfp:
     375            zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
     376            zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
     377            sinfo = zipfp.getinfo('storeme')
     378            dinfo = zipfp.getinfo('deflateme')
     379            self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
     380            self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
     381
     382    def test_write_to_readonly(self):
     383        """Check that trying to call write() on a readonly ZipFile object
     384        raises a RuntimeError."""
     385        with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
     386            zipfp.writestr("somefile.txt", "bogus")
     387
     388        with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
     389            self.assertRaises(RuntimeError, zipfp.write, TESTFN)
     390
     391    def test_extract(self):
     392        with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
     393            for fpath, fdata in SMALL_TEST_DATA:
     394                zipfp.writestr(fpath, fdata)
     395
     396        with zipfile.ZipFile(TESTFN2, "r") as zipfp:
     397            for fpath, fdata in SMALL_TEST_DATA:
     398                writtenfile = zipfp.extract(fpath)
     399
     400                # make sure it was written to the right place
    338401                correctfile = os.path.join(os.getcwd(), fpath)
    339             correctfile = os.path.normpath(correctfile)
    340 
    341             self.assertEqual(writtenfile, correctfile)
    342 
    343             # make sure correct data is in correct file
    344             self.assertEqual(fdata, file(writtenfile, "rb").read())
    345 
    346             os.remove(writtenfile)
    347 
    348         zipfp.close()
     402                correctfile = os.path.normpath(correctfile)
     403
     404                self.assertEqual(writtenfile, correctfile)
     405
     406                # make sure correct data is in correct file
     407                self.assertEqual(fdata, open(writtenfile, "rb").read())
     408                os.remove(writtenfile)
    349409
    350410        # remove the test file subdirectories
    351411        shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
    352412
    353     def testExtractAll(self):
    354         zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
    355         for fpath, fdata in SMALL_TEST_DATA:
    356             zipfp.writestr(fpath, fdata)
    357         zipfp.close()
    358 
    359         zipfp = zipfile.ZipFile(TESTFN2, "r")
    360         zipfp.extractall()
    361         for fpath, fdata in SMALL_TEST_DATA:
    362             if os.path.isabs(fpath):
    363                 outfile = os.path.join(os.getcwd(), fpath[1:])
    364             else:
     413    def test_extract_all(self):
     414        with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
     415            for fpath, fdata in SMALL_TEST_DATA:
     416                zipfp.writestr(fpath, fdata)
     417
     418        with zipfile.ZipFile(TESTFN2, "r") as zipfp:
     419            zipfp.extractall()
     420            for fpath, fdata in SMALL_TEST_DATA:
    365421                outfile = os.path.join(os.getcwd(), fpath)
    366422
    367             self.assertEqual(fdata, file(outfile, "rb").read())
    368 
    369             os.remove(outfile)
    370 
    371         zipfp.close()
     423                self.assertEqual(fdata, open(outfile, "rb").read())
     424                os.remove(outfile)
    372425
    373426        # remove the test file subdirectories
    374427        shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
     428
     429    def check_file(self, filename, content):
     430        self.assertTrue(os.path.isfile(filename))
     431        with open(filename, 'rb') as f:
     432            self.assertEqual(f.read(), content)
     433
     434    @skipUnless(TESTFN_UNICODE, "No Unicode filesystem semantics on this platform.")
     435    def test_extract_unicode_filenames(self):
     436        fnames = [u'foo.txt', os.path.basename(TESTFN_UNICODE)]
     437        content = 'Test for unicode filename'
     438        with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
     439            for fname in fnames:
     440                zipfp.writestr(fname, content)
     441
     442        with zipfile.ZipFile(TESTFN2, "r") as zipfp:
     443            for fname in fnames:
     444                writtenfile = zipfp.extract(fname)
     445
     446                # make sure it was written to the right place
     447                correctfile = os.path.join(os.getcwd(), fname)
     448                correctfile = os.path.normpath(correctfile)
     449                self.assertEqual(writtenfile, correctfile)
     450
     451                self.check_file(writtenfile, content)
     452                os.remove(writtenfile)
     453
     454    def test_extract_hackers_arcnames(self):
     455        hacknames = [
     456            ('../foo/bar', 'foo/bar'),
     457            ('foo/../bar', 'foo/bar'),
     458            ('foo/../../bar', 'foo/bar'),
     459            ('foo/bar/..', 'foo/bar'),
     460            ('./../foo/bar', 'foo/bar'),
     461            ('/foo/bar', 'foo/bar'),
     462            ('/foo/../bar', 'foo/bar'),
     463            ('/foo/../../bar', 'foo/bar'),
     464        ]
     465        if os.path.sep == '\\':
     466            hacknames.extend([
     467                (r'..\foo\bar', 'foo/bar'),
     468                (r'..\/foo\/bar', 'foo/bar'),
     469                (r'foo/\..\/bar', 'foo/bar'),
     470                (r'foo\/../\bar', 'foo/bar'),
     471                (r'C:foo/bar', 'foo/bar'),
     472                (r'C:/foo/bar', 'foo/bar'),
     473                (r'C://foo/bar', 'foo/bar'),
     474                (r'C:\foo\bar', 'foo/bar'),
     475                (r'//conky/mountpoint/foo/bar', 'conky/mountpoint/foo/bar'),
     476                (r'\\conky\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'),
     477                (r'///conky/mountpoint/foo/bar', 'conky/mountpoint/foo/bar'),
     478                (r'\\\conky\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'),
     479                (r'//conky//mountpoint/foo/bar', 'conky/mountpoint/foo/bar'),
     480                (r'\\conky\\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'),
     481                (r'//?/C:/foo/bar', '_/C_/foo/bar'),
     482                (r'\\?\C:\foo\bar', '_/C_/foo/bar'),
     483                (r'C:/../C:/foo/bar', 'C_/foo/bar'),
     484                (r'a:b\c<d>e|f"g?h*i', 'b/c_d_e_f_g_h_i'),
     485                ('../../foo../../ba..r', 'foo/ba..r'),
     486            ])
     487        else:  # Unix
     488            hacknames.extend([
     489                ('//foo/bar', 'foo/bar'),
     490                ('../../foo../../ba..r', 'foo../ba..r'),
     491                (r'foo/..\bar', r'foo/..\bar'),
     492            ])
     493
     494        for arcname, fixedname in hacknames:
     495            content = b'foobar' + arcname.encode()
     496            with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipfp:
     497                zinfo = zipfile.ZipInfo()
     498                # preserve backslashes
     499                zinfo.filename = arcname
     500                zinfo.external_attr = 0o600 << 16
     501                zipfp.writestr(zinfo, content)
     502
     503            arcname = arcname.replace(os.sep, "/")
     504            targetpath = os.path.join('target', 'subdir', 'subsub')
     505            correctfile = os.path.join(targetpath, *fixedname.split('/'))
     506
     507            with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
     508                writtenfile = zipfp.extract(arcname, targetpath)
     509                self.assertEqual(writtenfile, correctfile,
     510                                 msg="extract %r" % arcname)
     511            self.check_file(correctfile, content)
     512            shutil.rmtree('target')
     513
     514            with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
     515                zipfp.extractall(targetpath)
     516            self.check_file(correctfile, content)
     517            shutil.rmtree('target')
     518
     519            correctfile = os.path.join(os.getcwd(), *fixedname.split('/'))
     520
     521            with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
     522                writtenfile = zipfp.extract(arcname)
     523                self.assertEqual(writtenfile, correctfile,
     524                                 msg="extract %r" % arcname)
     525            self.check_file(correctfile, content)
     526            shutil.rmtree(fixedname.split('/')[0])
     527
     528            with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
     529                zipfp.extractall()
     530            self.check_file(correctfile, content)
     531            shutil.rmtree(fixedname.split('/')[0])
     532
     533            os.remove(TESTFN2)
     534
     535    def test_writestr_compression(self):
     536        zipfp = zipfile.ZipFile(TESTFN2, "w")
     537        zipfp.writestr("a.txt", "hello world", compress_type=zipfile.ZIP_STORED)
     538        if zlib:
     539            zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_DEFLATED)
     540
     541        info = zipfp.getinfo('a.txt')
     542        self.assertEqual(info.compress_type, zipfile.ZIP_STORED)
     543
     544        if zlib:
     545            info = zipfp.getinfo('b.txt')
     546            self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED)
     547
    375548
    376549    def zip_test_writestr_permissions(self, f, compression):
     
    378551        # when it is passed a name rather than a ZipInfo instance.
    379552
    380         self.makeTestArchive(f, compression)
    381         zipfp = zipfile.ZipFile(f, "r")
    382         zinfo = zipfp.getinfo('strfile')
    383         self.assertEqual(zinfo.external_attr, 0600 << 16)
     553        self.make_test_archive(f, compression)
     554        with zipfile.ZipFile(f, "r") as zipfp:
     555            zinfo = zipfp.getinfo('strfile')
     556            self.assertEqual(zinfo.external_attr, 0600 << 16)
    384557
    385558    def test_writestr_permissions(self):
     
    387560            self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
    388561
     562    def test_close(self):
     563        """Check that the zipfile is closed after the 'with' block."""
     564        with zipfile.ZipFile(TESTFN2, "w") as zipfp:
     565            for fpath, fdata in SMALL_TEST_DATA:
     566                zipfp.writestr(fpath, fdata)
     567                self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
     568        self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
     569
     570        with zipfile.ZipFile(TESTFN2, "r") as zipfp:
     571            self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
     572        self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
     573
     574    def test_close_on_exception(self):
     575        """Check that the zipfile is closed if an exception is raised in the
     576        'with' block."""
     577        with zipfile.ZipFile(TESTFN2, "w") as zipfp:
     578            for fpath, fdata in SMALL_TEST_DATA:
     579                zipfp.writestr(fpath, fdata)
     580
     581        try:
     582            with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
     583                raise zipfile.BadZipfile()
     584        except zipfile.BadZipfile:
     585            self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')
     586
     587    def test_add_file_before_1980(self):
     588        # Set atime and mtime to 1970-01-01
     589        os.utime(TESTFN, (0, 0))
     590        with zipfile.ZipFile(TESTFN2, "w") as zipfp:
     591            self.assertRaises(ValueError, zipfp.write, TESTFN)
     592
    389593    def tearDown(self):
    390         os.remove(TESTFN)
    391         os.remove(TESTFN2)
     594        unlink(TESTFN)
     595        unlink(TESTFN2)
     596
    392597
    393598class TestZip64InSmallFiles(unittest.TestCase):
     
    399604        zipfile.ZIP64_LIMIT = 5
    400605
    401         line_gen = ("Test of zipfile line %d." % i for i in range(0, FIXEDTEST_SIZE))
     606        line_gen = ("Test of zipfile line %d." % i
     607                    for i in range(0, FIXEDTEST_SIZE))
    402608        self.data = '\n'.join(line_gen)
    403609
    404610        # Make a source file with some lines
    405         fp = open(TESTFN, "wb")
    406         fp.write(self.data)
    407         fp.close()
    408 
    409     def largeFileExceptionTest(self, f, compression):
    410         zipfp = zipfile.ZipFile(f, "w", compression)
    411         self.assertRaises(zipfile.LargeZipFile,
    412                 zipfp.write, TESTFN, "another"+os.extsep+"name")
    413         zipfp.close()
    414 
    415     def largeFileExceptionTest2(self, f, compression):
    416         zipfp = zipfile.ZipFile(f, "w", compression)
    417         self.assertRaises(zipfile.LargeZipFile,
    418                 zipfp.writestr, "another"+os.extsep+"name", self.data)
    419         zipfp.close()
    420 
    421     def testLargeFileException(self):
    422         for f in (TESTFN2, TemporaryFile(), StringIO()):
    423             self.largeFileExceptionTest(f, zipfile.ZIP_STORED)
    424             self.largeFileExceptionTest2(f, zipfile.ZIP_STORED)
    425 
    426     def zipTest(self, f, compression):
     611        with open(TESTFN, "wb") as fp:
     612            fp.write(self.data)
     613
     614    def large_file_exception_test(self, f, compression):
     615        with zipfile.ZipFile(f, "w", compression) as zipfp:
     616            self.assertRaises(zipfile.LargeZipFile,
     617                              zipfp.write, TESTFN, "another.name")
     618
     619    def large_file_exception_test2(self, f, compression):
     620        with zipfile.ZipFile(f, "w", compression) as zipfp:
     621            self.assertRaises(zipfile.LargeZipFile,
     622                              zipfp.writestr, "another.name", self.data)
     623
     624    def test_large_file_exception(self):
     625        for f in (TESTFN2, TemporaryFile(), StringIO()):
     626            self.large_file_exception_test(f, zipfile.ZIP_STORED)
     627            self.large_file_exception_test2(f, zipfile.ZIP_STORED)
     628
     629    def zip_test(self, f, compression):
    427630        # Create the ZIP archive
    428         zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
    429         zipfp.write(TESTFN, "another"+os.extsep+"name")
    430         zipfp.write(TESTFN, TESTFN)
    431         zipfp.writestr("strfile", self.data)
    432         zipfp.close()
     631        with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp:
     632            zipfp.write(TESTFN, "another.name")
     633            zipfp.write(TESTFN, TESTFN)
     634            zipfp.writestr("strfile", self.data)
    433635
    434636        # Read the ZIP archive
    435         zipfp = zipfile.ZipFile(f, "r", compression)
    436         self.assertEqual(zipfp.read(TESTFN), self.data)
    437         self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
    438         self.assertEqual(zipfp.read("strfile"), self.data)
    439 
    440         # Print the ZIP directory
    441         fp = StringIO()
    442         stdout = sys.stdout
    443         try:
    444             sys.stdout = fp
    445 
    446             zipfp.printdir()
    447         finally:
    448             sys.stdout = stdout
    449 
    450         directory = fp.getvalue()
    451         lines = directory.splitlines()
    452         self.assertEquals(len(lines), 4) # Number of files + header
    453 
    454         self.assert_('File Name' in lines[0])
    455         self.assert_('Modified' in lines[0])
    456         self.assert_('Size' in lines[0])
    457 
    458         fn, date, time, size = lines[1].split()
    459         self.assertEquals(fn, 'another.name')
    460         # XXX: timestamp is not tested
    461         self.assertEquals(size, str(len(self.data)))
    462 
    463         # Check the namelist
    464         names = zipfp.namelist()
    465         self.assertEquals(len(names), 3)
    466         self.assert_(TESTFN in names)
    467         self.assert_("another"+os.extsep+"name" in names)
    468         self.assert_("strfile" in names)
    469 
    470         # Check infolist
    471         infos = zipfp.infolist()
    472         names = [ i.filename for i in infos ]
    473         self.assertEquals(len(names), 3)
    474         self.assert_(TESTFN in names)
    475         self.assert_("another"+os.extsep+"name" in names)
    476         self.assert_("strfile" in names)
    477         for i in infos:
    478             self.assertEquals(i.file_size, len(self.data))
    479 
    480         # check getinfo
    481         for nm in (TESTFN, "another"+os.extsep+"name", "strfile"):
    482             info = zipfp.getinfo(nm)
    483             self.assertEquals(info.filename, nm)
    484             self.assertEquals(info.file_size, len(self.data))
    485 
    486         # Check that testzip doesn't raise an exception
    487         zipfp.testzip()
    488 
    489 
    490         zipfp.close()
    491 
    492     def testStored(self):
    493         for f in (TESTFN2, TemporaryFile(), StringIO()):
    494             self.zipTest(f, zipfile.ZIP_STORED)
    495 
    496 
    497     if zlib:
    498         def testDeflated(self):
    499             for f in (TESTFN2, TemporaryFile(), StringIO()):
    500                 self.zipTest(f, zipfile.ZIP_DEFLATED)
    501 
    502     def testAbsoluteArcnames(self):
    503         zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True)
    504         zipfp.write(TESTFN, "/absolute")
    505         zipfp.close()
    506 
    507         zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
    508         self.assertEqual(zipfp.namelist(), ["absolute"])
    509         zipfp.close()
     637        with zipfile.ZipFile(f, "r", compression) as zipfp:
     638            self.assertEqual(zipfp.read(TESTFN), self.data)
     639            self.assertEqual(zipfp.read("another.name"), self.data)
     640            self.assertEqual(zipfp.read("strfile"), self.data)
     641
     642            # Print the ZIP directory
     643            fp = StringIO()
     644            stdout = sys.stdout
     645            try:
     646                sys.stdout = fp
     647                zipfp.printdir()
     648            finally:
     649                sys.stdout = stdout
     650
     651            directory = fp.getvalue()
     652            lines = directory.splitlines()
     653            self.assertEqual(len(lines), 4) # Number of files + header
     654
     655            self.assertIn('File Name', lines[0])
     656            self.assertIn('Modified', lines[0])
     657            self.assertIn('Size', lines[0])
     658
     659            fn, date, time_, size = lines[1].split()
     660            self.assertEqual(fn, 'another.name')
     661            self.assertTrue(time.strptime(date, '%Y-%m-%d'))
     662            self.assertTrue(time.strptime(time_, '%H:%M:%S'))
     663            self.assertEqual(size, str(len(self.data)))
     664
     665            # Check the namelist
     666            names = zipfp.namelist()
     667            self.assertEqual(len(names), 3)
     668            self.assertIn(TESTFN, names)
     669            self.assertIn("another.name", names)
     670            self.assertIn("strfile", names)
     671
     672            # Check infolist
     673            infos = zipfp.infolist()
     674            names = [i.filename for i in infos]
     675            self.assertEqual(len(names), 3)
     676            self.assertIn(TESTFN, names)
     677            self.assertIn("another.name", names)
     678            self.assertIn("strfile", names)
     679            for i in infos:
     680                self.assertEqual(i.file_size, len(self.data))
     681
     682            # check getinfo
     683            for nm in (TESTFN, "another.name", "strfile"):
     684                info = zipfp.getinfo(nm)
     685                self.assertEqual(info.filename, nm)
     686                self.assertEqual(info.file_size, len(self.data))
     687
     688            # Check that testzip doesn't raise an exception
     689            zipfp.testzip()
     690
     691    def test_stored(self):
     692        for f in (TESTFN2, TemporaryFile(), StringIO()):
     693            self.zip_test(f, zipfile.ZIP_STORED)
     694
     695    @skipUnless(zlib, "requires zlib")
     696    def test_deflated(self):
     697        for f in (TESTFN2, TemporaryFile(), StringIO()):
     698            self.zip_test(f, zipfile.ZIP_DEFLATED)
     699
     700    def test_absolute_arcnames(self):
     701        with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED,
     702                             allowZip64=True) as zipfp:
     703            zipfp.write(TESTFN, "/absolute")
     704
     705        with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
     706            self.assertEqual(zipfp.namelist(), ["absolute"])
    510707
    511708    def tearDown(self):
    512709        zipfile.ZIP64_LIMIT = self._limit
    513         os.remove(TESTFN)
    514         os.remove(TESTFN2)
     710        unlink(TESTFN)
     711        unlink(TESTFN2)
     712
    515713
    516714class PyZipFileTests(unittest.TestCase):
    517     def testWritePyfile(self):
    518         zipfp  = zipfile.PyZipFile(TemporaryFile(), "w")
    519         fn = __file__
    520         if fn.endswith('.pyc') or fn.endswith('.pyo'):
    521             fn = fn[:-1]
    522 
    523         zipfp.writepy(fn)
    524 
    525         bn = os.path.basename(fn)
    526         self.assert_(bn not in zipfp.namelist())
    527         self.assert_(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
    528         zipfp.close()
    529 
    530 
    531         zipfp  = zipfile.PyZipFile(TemporaryFile(), "w")
    532         fn = __file__
    533         if fn.endswith('.pyc') or fn.endswith('.pyo'):
    534             fn = fn[:-1]
    535 
    536         zipfp.writepy(fn, "testpackage")
    537 
    538         bn = "%s/%s"%("testpackage", os.path.basename(fn))
    539         self.assert_(bn not in zipfp.namelist())
    540         self.assert_(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
    541         zipfp.close()
    542 
    543     def testWritePythonPackage(self):
     715    def test_write_pyfile(self):
     716        with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
     717            fn = __file__
     718            if fn.endswith('.pyc') or fn.endswith('.pyo'):
     719                fn = fn[:-1]
     720
     721            zipfp.writepy(fn)
     722
     723            bn = os.path.basename(fn)
     724            self.assertNotIn(bn, zipfp.namelist())
     725            self.assertTrue(bn + 'o' in zipfp.namelist() or
     726                            bn + 'c' in zipfp.namelist())
     727
     728        with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
     729            fn = __file__
     730            if fn.endswith(('.pyc', '.pyo')):
     731                fn = fn[:-1]
     732
     733            zipfp.writepy(fn, "testpackage")
     734
     735            bn = "%s/%s" % ("testpackage", os.path.basename(fn))
     736            self.assertNotIn(bn, zipfp.namelist())
     737            self.assertTrue(bn + 'o' in zipfp.namelist() or
     738                            bn + 'c' in zipfp.namelist())
     739
     740    def test_write_python_package(self):
    544741        import email
    545742        packagedir = os.path.dirname(email.__file__)
    546743
    547         zipfp  = zipfile.PyZipFile(TemporaryFile(), "w")
    548         zipfp.writepy(packagedir)
    549 
    550         # Check for a couple of modules at different levels of the hieararchy
    551         names = zipfp.namelist()
    552         self.assert_('email/__init__.pyo' in names or 'email/__init__.pyc' in names)
    553         self.assert_('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names)
    554 
    555     def testWritePythonDirectory(self):
     744        with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
     745            zipfp.writepy(packagedir)
     746
     747            # Check for a couple of modules at different levels of the
     748            # hierarchy
     749            names = zipfp.namelist()
     750            self.assertTrue('email/__init__.pyo' in names or
     751                            'email/__init__.pyc' in names)
     752            self.assertTrue('email/mime/text.pyo' in names or
     753                            'email/mime/text.pyc' in names)
     754
     755    def test_write_python_directory(self):
    556756        os.mkdir(TESTFN2)
    557757        try:
    558             fp = open(os.path.join(TESTFN2, "mod1.py"), "w")
    559             fp.write("print 42\n")
    560             fp.close()
    561 
    562             fp = open(os.path.join(TESTFN2, "mod2.py"), "w")
    563             fp.write("print 42 * 42\n")
    564             fp.close()
    565 
    566             fp = open(os.path.join(TESTFN2, "mod2.txt"), "w")
    567             fp.write("bla bla bla\n")
    568             fp.close()
     758            with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
     759                fp.write("print(42)\n")
     760
     761            with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp:
     762                fp.write("print(42 * 42)\n")
     763
     764            with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp:
     765                fp.write("bla bla bla\n")
    569766
    570767            zipfp  = zipfile.PyZipFile(TemporaryFile(), "w")
     
    572769
    573770            names = zipfp.namelist()
    574             self.assert_('mod1.pyc' in names or 'mod1.pyo' in names)
    575             self.assert_('mod2.pyc' in names or 'mod2.pyo' in names)
    576             self.assert_('mod2.txt' not in names)
     771            self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
     772            self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
     773            self.assertNotIn('mod2.txt', names)
    577774
    578775        finally:
    579776            shutil.rmtree(TESTFN2)
    580777
    581     def testWriteNonPyfile(self):
    582         zipfp  = zipfile.PyZipFile(TemporaryFile(), "w")
    583         file(TESTFN, 'w').write('most definitely not a python file')
    584         self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
    585         os.remove(TESTFN)
     778    def test_write_non_pyfile(self):
     779        with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
     780            open(TESTFN, 'w').write('most definitely not a python file')
     781            self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
     782            os.remove(TESTFN)
    586783
    587784
    588785class OtherTests(unittest.TestCase):
    589     def testUnicodeFilenames(self):
    590         zf = zipfile.ZipFile(TESTFN, "w")
    591         zf.writestr(u"foo.txt", "Test for unicode filename")
    592         zf.writestr(u"\xf6.txt", "Test for unicode filename")
    593         self.assertTrue(isinstance(zf.infolist()[0].filename, unicode))
    594         zf.close()
    595         zf = zipfile.ZipFile(TESTFN, "r")
    596         self.assertEqual(zf.filelist[0].filename, "foo.txt")
    597         self.assertEqual(zf.filelist[1].filename, u"\xf6.txt")
    598         zf.close()
    599 
    600     def testCreateNonExistentFileForAppend(self):
     786    zips_with_bad_crc = {
     787        zipfile.ZIP_STORED: (
     788            b'PK\003\004\024\0\0\0\0\0 \213\212;:r'
     789            b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af'
     790            b'ilehello,AworldP'
     791            b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:'
     792            b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0'
     793            b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi'
     794            b'lePK\005\006\0\0\0\0\001\0\001\0003\000'
     795            b'\0\0/\0\0\0\0\0'),
     796        zipfile.ZIP_DEFLATED: (
     797            b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA'
     798            b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
     799            b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0'
     800            b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n'
     801            b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05'
     802            b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00'
     803            b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00'
     804            b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00'),
     805    }
     806
     807    def test_unicode_filenames(self):
     808        with zipfile.ZipFile(TESTFN, "w") as zf:
     809            zf.writestr(u"foo.txt", "Test for unicode filename")
     810            zf.writestr(u"\xf6.txt", "Test for unicode filename")
     811            self.assertIsInstance(zf.infolist()[0].filename, unicode)
     812
     813        with zipfile.ZipFile(TESTFN, "r") as zf:
     814            self.assertEqual(zf.filelist[0].filename, "foo.txt")
     815            self.assertEqual(zf.filelist[1].filename, u"\xf6.txt")
     816
     817    def test_create_non_existent_file_for_append(self):
    601818        if os.path.exists(TESTFN):
    602819            os.unlink(TESTFN)
     
    606823
    607824        try:
    608             zf = zipfile.ZipFile(TESTFN, 'a')
    609             zf.writestr(filename, content)
    610             zf.close()
    611         except IOError, (errno, errmsg):
     825            with zipfile.ZipFile(TESTFN, 'a') as zf:
     826                zf.writestr(filename, content)
     827        except IOError:
    612828            self.fail('Could not append data to a non-existent zip file.')
    613829
    614         self.assert_(os.path.exists(TESTFN))
    615 
    616         zf = zipfile.ZipFile(TESTFN, 'r')
    617         self.assertEqual(zf.read(filename), content)
    618         zf.close()
    619 
    620     def testCloseErroneousFile(self):
     830        self.assertTrue(os.path.exists(TESTFN))
     831
     832        with zipfile.ZipFile(TESTFN, 'r') as zf:
     833            self.assertEqual(zf.read(filename), content)
     834
     835    def test_close_erroneous_file(self):
    621836        # This test checks that the ZipFile constructor closes the file object
    622         # it opens if there's an error in the file.  If it doesn't, the traceback
    623         # holds a reference to the ZipFile object and, indirectly, the file object.
     837        # it opens if there's an error in the file.  If it doesn't, the
     838        # traceback holds a reference to the ZipFile object and, indirectly,
     839        # the file object.
    624840        # On Windows, this causes the os.unlink() call to fail because the
    625841        # underlying file is still open.  This is SF bug #412214.
    626842        #
    627         fp = open(TESTFN, "w")
    628         fp.write("this is not a legal zip file\n")
    629         fp.close()
     843        with open(TESTFN, "w") as fp:
     844            fp.write("this is not a legal zip file\n")
    630845        try:
    631846            zf = zipfile.ZipFile(TESTFN)
     
    633848            pass
    634849
    635     def testIsZipErroneousFile(self):
    636         # This test checks that the is_zipfile function correctly identifies
    637         # a file that is not a zip file
    638         fp = open(TESTFN, "w")
     850    def test_is_zip_erroneous_file(self):
     851        """Check that is_zipfile() correctly identifies non-zip files."""
     852        # - passing a filename
     853        with open(TESTFN, "w") as fp:
     854            fp.write("this is not a legal zip file\n")
     855        chk = zipfile.is_zipfile(TESTFN)
     856        self.assertFalse(chk)
     857        # - passing a file object
     858        with open(TESTFN, "rb") as fp:
     859            chk = zipfile.is_zipfile(fp)
     860            self.assertTrue(not chk)
     861        # - passing a file-like object
     862        fp = StringIO()
    639863        fp.write("this is not a legal zip file\n")
    640         fp.close()
     864        chk = zipfile.is_zipfile(fp)
     865        self.assertTrue(not chk)
     866        fp.seek(0, 0)
     867        chk = zipfile.is_zipfile(fp)
     868        self.assertTrue(not chk)
     869
     870    def test_damaged_zipfile(self):
     871        """Check that zipfiles with missing bytes at the end raise BadZipFile."""
     872        # - Create a valid zip file
     873        fp = io.BytesIO()
     874        with zipfile.ZipFile(fp, mode="w") as zipf:
     875            zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
     876        zipfiledata = fp.getvalue()
     877
     878        # - Now create copies of it missing the last N bytes and make sure
     879        #   a BadZipFile exception is raised when we try to open it
     880        for N in range(len(zipfiledata)):
     881            fp = io.BytesIO(zipfiledata[:N])
     882            self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, fp)
     883
     884    def test_is_zip_valid_file(self):
     885        """Check that is_zipfile() correctly identifies zip files."""
     886        # - passing a filename
     887        with zipfile.ZipFile(TESTFN, mode="w") as zipf:
     888            zipf.writestr("foo.txt", "O, for a Muse of Fire!")
    641889        chk = zipfile.is_zipfile(TESTFN)
    642         self.assert_(chk is False)
    643 
    644     def testIsZipValidFile(self):
    645         # This test checks that the is_zipfile function correctly identifies
    646         # a file that is a zip file
    647         zipf = zipfile.ZipFile(TESTFN, mode="w")
    648         zipf.writestr("foo.txt", "O, for a Muse of Fire!")
    649         zipf.close()
    650         chk = zipfile.is_zipfile(TESTFN)
    651         self.assert_(chk is True)
    652 
    653     def testNonExistentFileRaisesIOError(self):
     890        self.assertTrue(chk)
     891        # - passing a file object
     892        with open(TESTFN, "rb") as fp:
     893            chk = zipfile.is_zipfile(fp)
     894            self.assertTrue(chk)
     895            fp.seek(0, 0)
     896            zip_contents = fp.read()
     897        # - passing a file-like object
     898        fp = StringIO()
     899        fp.write(zip_contents)
     900        chk = zipfile.is_zipfile(fp)
     901        self.assertTrue(chk)
     902        fp.seek(0, 0)
     903        chk = zipfile.is_zipfile(fp)
     904        self.assertTrue(chk)
     905
     906    def test_non_existent_file_raises_IOError(self):
    654907        # make sure we don't raise an AttributeError when a partially-constructed
    655908        # ZipFile instance is finalized; this tests for regression on SF tracker
     
    666919
    667920    def test_empty_file_raises_BadZipFile(self):
    668         f = open(TESTFN, 'w')
    669         f.close()
     921        with open(TESTFN, 'w') as f:
     922            pass
    670923        self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
    671924
    672         f = open(TESTFN, 'w')
    673         f.write("short file")
    674         f.close()
     925        with open(TESTFN, 'w') as fp:
     926            fp.write("short file")
    675927        self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
    676928
    677     def testClosedZipRaisesRuntimeError(self):
    678         # Verify that testzip() doesn't swallow inappropriate exceptions.
     929    def test_closed_zip_raises_RuntimeError(self):
     930        """Verify that testzip() doesn't swallow inappropriate exceptions."""
    679931        data = StringIO()
    680         zipf = zipfile.ZipFile(data, mode="w")
    681         zipf.writestr("foo.txt", "O, for a Muse of Fire!")
    682         zipf.close()
    683 
    684         # This is correct; calling .read on a closed ZipFile should throw
     932        with zipfile.ZipFile(data, mode="w") as zipf:
     933            zipf.writestr("foo.txt", "O, for a Muse of Fire!")
     934
     935        # This is correct; calling .read on a closed ZipFile should raise
    685936        # a RuntimeError, and so should calling .testzip.  An earlier
    686937        # version of .testzip would swallow this exception (and any other)
     
    690941        self.assertRaises(RuntimeError, zipf.testzip)
    691942        self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
    692         file(TESTFN, 'w').write('zipfile test data')
     943        open(TESTFN, 'w').write('zipfile test data')
    693944        self.assertRaises(RuntimeError, zipf.write, TESTFN)
    694945
    695     def test_BadConstructorMode(self):
    696         # Check that bad modes passed to ZipFile constructor are caught
     946    def test_bad_constructor_mode(self):
     947        """Check that bad modes passed to ZipFile constructor are caught."""
    697948        self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
    698949
    699     def test_BadOpenMode(self):
    700         # Check that bad modes passed to ZipFile.open are caught
    701         zipf = zipfile.ZipFile(TESTFN, mode="w")
    702         zipf.writestr("foo.txt", "O, for a Muse of Fire!")
    703         zipf.close()
    704         zipf = zipfile.ZipFile(TESTFN, mode="r")
     950    def test_bad_open_mode(self):
     951        """Check that bad modes passed to ZipFile.open are caught."""
     952        with zipfile.ZipFile(TESTFN, mode="w") as zipf:
     953            zipf.writestr("foo.txt", "O, for a Muse of Fire!")
     954
     955        with zipfile.ZipFile(TESTFN, mode="r") as zipf:
    705956        # read the data to make sure the file is there
    706         zipf.read("foo.txt")
    707         self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
    708         zipf.close()
    709 
    710     def test_Read0(self):
    711         # Check that calling read(0) on a ZipExtFile object returns an empty
    712         # string and doesn't advance file pointer
    713         zipf = zipfile.ZipFile(TESTFN, mode="w")
    714         zipf.writestr("foo.txt", "O, for a Muse of Fire!")
    715         # read the data to make sure the file is there
    716         f = zipf.open("foo.txt")
    717         for i in xrange(FIXEDTEST_SIZE):
    718             self.assertEqual(f.read(0), '')
    719 
    720         self.assertEqual(f.read(), "O, for a Muse of Fire!")
    721         zipf.close()
    722 
    723     def test_OpenNonexistentItem(self):
    724         # Check that attempting to call open() for an item that doesn't
    725         # exist in the archive raises a RuntimeError
    726         zipf = zipfile.ZipFile(TESTFN, mode="w")
    727         self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
    728 
    729     def test_BadCompressionMode(self):
    730         # Check that bad compression methods passed to ZipFile.open are caught
     957            zipf.read("foo.txt")
     958            self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
     959
     960    def test_read0(self):
     961        """Check that calling read(0) on a ZipExtFile object returns an empty
     962        string and doesn't advance file pointer."""
     963        with zipfile.ZipFile(TESTFN, mode="w") as zipf:
     964            zipf.writestr("foo.txt", "O, for a Muse of Fire!")
     965            # read the data to make sure the file is there
     966            with zipf.open("foo.txt") as f:
     967                for i in xrange(FIXEDTEST_SIZE):
     968                    self.assertEqual(f.read(0), '')
     969
     970                self.assertEqual(f.read(), "O, for a Muse of Fire!")
     971
     972    def test_open_non_existent_item(self):
     973        """Check that attempting to call open() for an item that doesn't
     974        exist in the archive raises a RuntimeError."""
     975        with zipfile.ZipFile(TESTFN, mode="w") as zipf:
     976            self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
     977
     978    def test_bad_compression_mode(self):
     979        """Check that bad compression methods passed to ZipFile.open are
     980        caught."""
    731981        self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
    732982
    733     def test_NullByteInFilename(self):
    734         # Check that a filename containing a null byte is properly terminated
    735         zipf = zipfile.ZipFile(TESTFN, mode="w")
    736         zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!")
    737         self.assertEqual(zipf.namelist(), ['foo.txt'])
    738 
    739     def test_StructSizes(self):
    740         # check that ZIP internal structure sizes are calculated correctly
     983    def test_unsupported_compression(self):
     984        # data is declared as shrunk, but actually deflated
     985        data = (b'PK\x03\x04.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00'
     986        b'\x00\x02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00x\x03\x00PK\x01'
     987        b'\x02.\x03.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00\x00\x02\x00\x00'
     988        b'\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
     989        b'\x80\x01\x00\x00\x00\x00xPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00'
     990        b'/\x00\x00\x00!\x00\x00\x00\x00\x00')
     991        with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
     992            self.assertRaises(NotImplementedError, zipf.open, 'x')
     993
     994    def test_null_byte_in_filename(self):
     995        """Check that a filename containing a null byte is properly
     996        terminated."""
     997        with zipfile.ZipFile(TESTFN, mode="w") as zipf:
     998            zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!")
     999            self.assertEqual(zipf.namelist(), ['foo.txt'])
     1000
     1001    def test_struct_sizes(self):
     1002        """Check that ZIP internal structure sizes are calculated correctly."""
    7411003        self.assertEqual(zipfile.sizeEndCentDir, 22)
    7421004        self.assertEqual(zipfile.sizeCentralDir, 46)
     
    7441006        self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
    7451007
    746     def testComments(self):
    747         # This test checks that comments on the archive are handled properly
     1008    def test_comments(self):
     1009        """Check that comments on the archive are handled properly."""
    7481010
    7491011        # check default comment is empty
    750         zipf = zipfile.ZipFile(TESTFN, mode="w")
    751         self.assertEqual(zipf.comment, '')
    752         zipf.writestr("foo.txt", "O, for a Muse of Fire!")
    753         zipf.close()
    754         zipfr = zipfile.ZipFile(TESTFN, mode="r")
    755         self.assertEqual(zipfr.comment, '')
    756         zipfr.close()
     1012        with zipfile.ZipFile(TESTFN, mode="w") as zipf:
     1013            self.assertEqual(zipf.comment, '')
     1014            zipf.writestr("foo.txt", "O, for a Muse of Fire!")
     1015
     1016        with zipfile.ZipFile(TESTFN, mode="r") as zipf:
     1017            self.assertEqual(zipf.comment, '')
    7571018
    7581019        # check a simple short comment
    7591020        comment = 'Bravely taking to his feet, he beat a very brave retreat.'
    760         zipf = zipfile.ZipFile(TESTFN, mode="w")
    761         zipf.comment = comment
    762         zipf.writestr("foo.txt", "O, for a Muse of Fire!")
    763         zipf.close()
    764         zipfr = zipfile.ZipFile(TESTFN, mode="r")
    765         self.assertEqual(zipfr.comment, comment)
    766         zipfr.close()
     1021        with zipfile.ZipFile(TESTFN, mode="w") as zipf:
     1022            zipf.comment = comment
     1023            zipf.writestr("foo.txt", "O, for a Muse of Fire!")
     1024        with zipfile.ZipFile(TESTFN, mode="r") as zipf:
     1025            self.assertEqual(zipf.comment, comment)
    7671026
    7681027        # check a comment of max length
    7691028        comment2 = ''.join(['%d' % (i**3 % 10) for i in xrange((1 << 16)-1)])
    770         zipf = zipfile.ZipFile(TESTFN, mode="w")
    771         zipf.comment = comment2
    772         zipf.writestr("foo.txt", "O, for a Muse of Fire!")
    773         zipf.close()
    774         zipfr = zipfile.ZipFile(TESTFN, mode="r")
    775         self.assertEqual(zipfr.comment, comment2)
    776         zipfr.close()
     1029        with zipfile.ZipFile(TESTFN, mode="w") as zipf:
     1030            zipf.comment = comment2
     1031            zipf.writestr("foo.txt", "O, for a Muse of Fire!")
     1032
     1033        with zipfile.ZipFile(TESTFN, mode="r") as zipf:
     1034            self.assertEqual(zipf.comment, comment2)
    7771035
    7781036        # check a comment that is too long is truncated
    779         zipf = zipfile.ZipFile(TESTFN, mode="w")
    780         zipf.comment = comment2 + 'oops'
    781         zipf.writestr("foo.txt", "O, for a Muse of Fire!")
    782         zipf.close()
    783         zipfr = zipfile.ZipFile(TESTFN, mode="r")
    784         self.assertEqual(zipfr.comment, comment2)
    785         zipfr.close()
     1037        with zipfile.ZipFile(TESTFN, mode="w") as zipf:
     1038            zipf.comment = comment2 + 'oops'
     1039            zipf.writestr("foo.txt", "O, for a Muse of Fire!")
     1040        with zipfile.ZipFile(TESTFN, mode="r") as zipf:
     1041            self.assertEqual(zipf.comment, comment2)
     1042
     1043    def test_change_comment_in_empty_archive(self):
     1044        with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
     1045            self.assertFalse(zipf.filelist)
     1046            zipf.comment = b"this is a comment"
     1047        with zipfile.ZipFile(TESTFN, "r") as zipf:
     1048            self.assertEqual(zipf.comment, b"this is a comment")
     1049
     1050    def test_change_comment_in_nonempty_archive(self):
     1051        with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:
     1052            zipf.writestr("foo.txt", "O, for a Muse of Fire!")
     1053        with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
     1054            self.assertTrue(zipf.filelist)
     1055            zipf.comment = b"this is a comment"
     1056        with zipfile.ZipFile(TESTFN, "r") as zipf:
     1057            self.assertEqual(zipf.comment, b"this is a comment")
     1058
     1059    def check_testzip_with_bad_crc(self, compression):
     1060        """Tests that files with bad CRCs return their name from testzip."""
     1061        zipdata = self.zips_with_bad_crc[compression]
     1062
     1063        with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
     1064            # testzip returns the name of the first corrupt file, or None
     1065            self.assertEqual('afile', zipf.testzip())
     1066
     1067    def test_testzip_with_bad_crc_stored(self):
     1068        self.check_testzip_with_bad_crc(zipfile.ZIP_STORED)
     1069
     1070    @skipUnless(zlib, "requires zlib")
     1071    def test_testzip_with_bad_crc_deflated(self):
     1072        self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED)
     1073
     1074    def check_read_with_bad_crc(self, compression):
     1075        """Tests that files with bad CRCs raise a BadZipfile exception when read."""
     1076        zipdata = self.zips_with_bad_crc[compression]
     1077
     1078        # Using ZipFile.read()
     1079        with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
     1080            self.assertRaises(zipfile.BadZipfile, zipf.read, 'afile')
     1081
     1082        # Using ZipExtFile.read()
     1083        with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
     1084            with zipf.open('afile', 'r') as corrupt_file:
     1085                self.assertRaises(zipfile.BadZipfile, corrupt_file.read)
     1086
     1087        # Same with small reads (in order to exercise the buffering logic)
     1088        with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
     1089            with zipf.open('afile', 'r') as corrupt_file:
     1090                corrupt_file.MIN_READ_SIZE = 2
     1091                with self.assertRaises(zipfile.BadZipfile):
     1092                    while corrupt_file.read(2):
     1093                        pass
     1094
     1095    def test_read_with_bad_crc_stored(self):
     1096        self.check_read_with_bad_crc(zipfile.ZIP_STORED)
     1097
     1098    @skipUnless(zlib, "requires zlib")
     1099    def test_read_with_bad_crc_deflated(self):
     1100        self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED)
     1101
     1102    def check_read_return_size(self, compression):
     1103        # Issue #9837: ZipExtFile.read() shouldn't return more bytes
     1104        # than requested.
     1105        for test_size in (1, 4095, 4096, 4097, 16384):
     1106            file_size = test_size + 1
     1107            junk = b''.join(struct.pack('B', randint(0, 255))
     1108                            for x in range(file_size))
     1109            with zipfile.ZipFile(io.BytesIO(), "w", compression) as zipf:
     1110                zipf.writestr('foo', junk)
     1111                with zipf.open('foo', 'r') as fp:
     1112                    buf = fp.read(test_size)
     1113                    self.assertEqual(len(buf), test_size)
     1114
     1115    def test_read_return_size_stored(self):
     1116        self.check_read_return_size(zipfile.ZIP_STORED)
     1117
     1118    @skipUnless(zlib, "requires zlib")
     1119    def test_read_return_size_deflated(self):
     1120        self.check_read_return_size(zipfile.ZIP_DEFLATED)
     1121
     1122    def test_empty_zipfile(self):
     1123        # Check that creating a file in 'w' or 'a' mode and closing without
     1124        # adding any files to the archives creates a valid empty ZIP file
     1125        with zipfile.ZipFile(TESTFN, mode="w") as zipf:
     1126            pass
     1127        try:
     1128            zipf = zipfile.ZipFile(TESTFN, mode="r")
     1129        except zipfile.BadZipfile:
     1130            self.fail("Unable to create empty ZIP file in 'w' mode")
     1131
     1132        with zipfile.ZipFile(TESTFN, mode="a") as zipf:
     1133            pass
     1134        try:
     1135            zipf = zipfile.ZipFile(TESTFN, mode="r")
     1136        except:
     1137            self.fail("Unable to create empty ZIP file in 'a' mode")
     1138
     1139    def test_open_empty_file(self):
     1140        # Issue 1710703: Check that opening a file with less than 22 bytes
     1141        # raises a BadZipfile exception (rather than the previously unhelpful
     1142        # IOError)
     1143        with open(TESTFN, 'w') as f:
     1144            pass
     1145        self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN, 'r')
     1146
     1147    def test_create_zipinfo_before_1980(self):
     1148        self.assertRaises(ValueError,
     1149                          zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0))
    7861150
    7871151    def tearDown(self):
    788         support.unlink(TESTFN)
    789         support.unlink(TESTFN2)
     1152        unlink(TESTFN)
     1153        unlink(TESTFN2)
     1154
    7901155
    7911156class DecryptionTests(unittest.TestCase):
    792     # This test checks that ZIP decryption works. Since the library does not
    793     # support encryption at the moment, we use a pre-generated encrypted
    794     # ZIP file
     1157    """Check that ZIP decryption works. Since the library does not
     1158    support encryption at the moment, we use a pre-generated encrypted
     1159    ZIP file."""
    7951160
    7961161    data = (
     
    8161181
    8171182    def setUp(self):
    818         fp = open(TESTFN, "wb")
    819         fp.write(self.data)
    820         fp.close()
     1183        with open(TESTFN, "wb") as fp:
     1184            fp.write(self.data)
    8211185        self.zip = zipfile.ZipFile(TESTFN, "r")
    822         fp = open(TESTFN2, "wb")
    823         fp.write(self.data2)
    824         fp.close()
     1186        with open(TESTFN2, "wb") as fp:
     1187            fp.write(self.data2)
    8251188        self.zip2 = zipfile.ZipFile(TESTFN2, "r")
    8261189
     
    8311194        os.unlink(TESTFN2)
    8321195
    833     def testNoPassword(self):
     1196    def test_no_password(self):
    8341197        # Reading the encrypted file without password
    8351198        # must generate a RunTime exception
     
    8371200        self.assertRaises(RuntimeError, self.zip2.read, "zero")
    8381201
    839     def testBadPassword(self):
     1202    def test_bad_password(self):
    8401203        self.zip.setpassword("perl")
    8411204        self.assertRaises(RuntimeError, self.zip.read, "test.txt")
     
    8431206        self.assertRaises(RuntimeError, self.zip2.read, "zero")
    8441207
    845     def testGoodPassword(self):
     1208    @skipUnless(zlib, "requires zlib")
     1209    def test_good_password(self):
    8461210        self.zip.setpassword("python")
    847         self.assertEquals(self.zip.read("test.txt"), self.plain)
     1211        self.assertEqual(self.zip.read("test.txt"), self.plain)
    8481212        self.zip2.setpassword("12345")
    849         self.assertEquals(self.zip2.read("zero"), self.plain2)
     1213        self.assertEqual(self.zip2.read("zero"), self.plain2)
    8501214
    8511215
     
    8531217    def setUp(self):
    8541218        datacount = randint(16, 64)*1024 + randint(1, 1024)
    855         self.data = ''.join((struct.pack('<f', random()*randint(-1000, 1000)) for i in xrange(datacount)))
     1219        self.data = ''.join(struct.pack('<f', random()*randint(-1000, 1000))
     1220                            for i in xrange(datacount))
    8561221
    8571222        # Make a source file with some lines
    858         fp = open(TESTFN, "wb")
    859         fp.write(self.data)
    860         fp.close()
     1223        with open(TESTFN, "wb") as fp:
     1224            fp.write(self.data)
    8611225
    8621226    def tearDown(self):
    863         support.unlink(TESTFN)
    864         support.unlink(TESTFN2)
    865 
    866     def makeTestArchive(self, f, compression):
     1227        unlink(TESTFN)
     1228        unlink(TESTFN2)
     1229
     1230    def make_test_archive(self, f, compression):
    8671231        # Create the ZIP archive
    868         zipfp = zipfile.ZipFile(f, "w", compression)
    869         zipfp.write(TESTFN, "another"+os.extsep+"name")
    870         zipfp.write(TESTFN, TESTFN)
    871         zipfp.close()
    872 
    873     def zipTest(self, f, compression):
    874         self.makeTestArchive(f, compression)
     1232        with zipfile.ZipFile(f, "w", compression) as zipfp:
     1233            zipfp.write(TESTFN, "another.name")
     1234            zipfp.write(TESTFN, TESTFN)
     1235
     1236    def zip_test(self, f, compression):
     1237        self.make_test_archive(f, compression)
    8751238
    8761239        # Read the ZIP archive
    877         zipfp = zipfile.ZipFile(f, "r", compression)
    878         testdata = zipfp.read(TESTFN)
    879         self.assertEqual(len(testdata), len(self.data))
    880         self.assertEqual(testdata, self.data)
    881         self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
    882         zipfp.close()
    883 
    884     def testStored(self):
    885         for f in (TESTFN2, TemporaryFile(), StringIO()):
    886             self.zipTest(f, zipfile.ZIP_STORED)
    887 
    888     def zipOpenTest(self, f, compression):
    889         self.makeTestArchive(f, compression)
     1240        with zipfile.ZipFile(f, "r", compression) as zipfp:
     1241            testdata = zipfp.read(TESTFN)
     1242            self.assertEqual(len(testdata), len(self.data))
     1243            self.assertEqual(testdata, self.data)
     1244            self.assertEqual(zipfp.read("another.name"), self.data)
     1245
     1246    def test_stored(self):
     1247        for f in (TESTFN2, TemporaryFile(), StringIO()):
     1248            self.zip_test(f, zipfile.ZIP_STORED)
     1249
     1250    @skipUnless(zlib, "requires zlib")
     1251    def test_deflated(self):
     1252        for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
     1253            self.zip_test(f, zipfile.ZIP_DEFLATED)
     1254
     1255    def zip_open_test(self, f, compression):
     1256        self.make_test_archive(f, compression)
    8901257
    8911258        # Read the ZIP archive
    892         zipfp = zipfile.ZipFile(f, "r", compression)
    893         zipdata1 = []
    894         zipopen1 = zipfp.open(TESTFN)
    895         while 1:
    896             read_data = zipopen1.read(256)
    897             if not read_data:
    898                 break
    899             zipdata1.append(read_data)
    900 
    901         zipdata2 = []
    902         zipopen2 = zipfp.open("another"+os.extsep+"name")
    903         while 1:
    904             read_data = zipopen2.read(256)
    905             if not read_data:
    906                 break
    907             zipdata2.append(read_data)
    908 
    909         testdata1 = ''.join(zipdata1)
    910         self.assertEqual(len(testdata1), len(self.data))
    911         self.assertEqual(testdata1, self.data)
    912 
    913         testdata2 = ''.join(zipdata2)
    914         self.assertEqual(len(testdata1), len(self.data))
    915         self.assertEqual(testdata1, self.data)
    916         zipfp.close()
    917 
    918     def testOpenStored(self):
    919         for f in (TESTFN2, TemporaryFile(), StringIO()):
    920             self.zipOpenTest(f, zipfile.ZIP_STORED)
    921 
    922     def zipRandomOpenTest(self, f, compression):
    923         self.makeTestArchive(f, compression)
     1259        with zipfile.ZipFile(f, "r", compression) as zipfp:
     1260            zipdata1 = []
     1261            with zipfp.open(TESTFN) as zipopen1:
     1262                while True:
     1263                    read_data = zipopen1.read(256)
     1264                    if not read_data:
     1265                        break
     1266                    zipdata1.append(read_data)
     1267
     1268            zipdata2 = []
     1269            with zipfp.open("another.name") as zipopen2:
     1270                while True:
     1271                    read_data = zipopen2.read(256)
     1272                    if not read_data:
     1273                        break
     1274                    zipdata2.append(read_data)
     1275
     1276            testdata1 = ''.join(zipdata1)
     1277            self.assertEqual(len(testdata1), len(self.data))
     1278            self.assertEqual(testdata1, self.data)
     1279
     1280            testdata2 = ''.join(zipdata2)
     1281            self.assertEqual(len(testdata2), len(self.data))
     1282            self.assertEqual(testdata2, self.data)
     1283
     1284    def test_open_stored(self):
     1285        for f in (TESTFN2, TemporaryFile(), StringIO()):
     1286            self.zip_open_test(f, zipfile.ZIP_STORED)
     1287
     1288    @skipUnless(zlib, "requires zlib")
     1289    def test_open_deflated(self):
     1290        for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
     1291            self.zip_open_test(f, zipfile.ZIP_DEFLATED)
     1292
     1293    def zip_random_open_test(self, f, compression):
     1294        self.make_test_archive(f, compression)
    9241295
    9251296        # Read the ZIP archive
    926         zipfp = zipfile.ZipFile(f, "r", compression)
    927         zipdata1 = []
    928         zipopen1 = zipfp.open(TESTFN)
    929         while 1:
    930             read_data = zipopen1.read(randint(1, 1024))
    931             if not read_data:
    932                 break
    933             zipdata1.append(read_data)
    934 
    935         testdata = ''.join(zipdata1)
    936         self.assertEqual(len(testdata), len(self.data))
    937         self.assertEqual(testdata, self.data)
    938         zipfp.close()
    939 
    940     def testRandomOpenStored(self):
    941         for f in (TESTFN2, TemporaryFile(), StringIO()):
    942             self.zipRandomOpenTest(f, zipfile.ZIP_STORED)
    943 
     1297        with zipfile.ZipFile(f, "r", compression) as zipfp:
     1298            zipdata1 = []
     1299            with zipfp.open(TESTFN) as zipopen1:
     1300                while True:
     1301                    read_data = zipopen1.read(randint(1, 1024))
     1302                    if not read_data:
     1303                        break
     1304                    zipdata1.append(read_data)
     1305
     1306            testdata = ''.join(zipdata1)
     1307            self.assertEqual(len(testdata), len(self.data))
     1308            self.assertEqual(testdata, self.data)
     1309
     1310    def test_random_open_stored(self):
     1311        for f in (TESTFN2, TemporaryFile(), StringIO()):
     1312            self.zip_random_open_test(f, zipfile.ZIP_STORED)
     1313
     1314    @skipUnless(zlib, "requires zlib")
     1315    def test_random_open_deflated(self):
     1316        for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
     1317            self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
     1318
     1319
     1320@skipUnless(zlib, "requires zlib")
    9441321class TestsWithMultipleOpens(unittest.TestCase):
    9451322    def setUp(self):
    9461323        # Create the ZIP archive
    947         zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
    948         zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
    949         zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
    950         zipfp.close()
    951 
    952     def testSameFile(self):
     1324        with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
     1325            zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
     1326            zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
     1327
     1328    def test_same_file(self):
    9531329        # Verify that (when the ZipFile is in control of creating file objects)
    9541330        # multiple open() calls can be made without interfering with each other.
    955         zipf = zipfile.ZipFile(TESTFN2, mode="r")
    956         zopen1 = zipf.open('ones')
    957         zopen2 = zipf.open('ones')
    958         data1 = zopen1.read(500)
    959         data2 = zopen2.read(500)
    960         data1 += zopen1.read(500)
    961         data2 += zopen2.read(500)
    962         self.assertEqual(data1, data2)
    963         zipf.close()
    964 
    965     def testDifferentFile(self):
     1331        with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
     1332            zopen1 = zipf.open('ones')
     1333            zopen2 = zipf.open('ones')
     1334            data1 = zopen1.read(500)
     1335            data2 = zopen2.read(500)
     1336            data1 += zopen1.read(500)
     1337            data2 += zopen2.read(500)
     1338            self.assertEqual(data1, data2)
     1339
     1340    def test_different_file(self):
    9661341        # Verify that (when the ZipFile is in control of creating file objects)
    9671342        # multiple open() calls can be made without interfering with each other.
    968         zipf = zipfile.ZipFile(TESTFN2, mode="r")
    969         zopen1 = zipf.open('ones')
    970         zopen2 = zipf.open('twos')
    971         data1 = zopen1.read(500)
    972         data2 = zopen2.read(500)
    973         data1 += zopen1.read(500)
    974         data2 += zopen2.read(500)
    975         self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
    976         self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
    977         zipf.close()
    978 
    979     def testInterleaved(self):
     1343        with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
     1344            with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
     1345                data1 = zopen1.read(500)
     1346                data2 = zopen2.read(500)
     1347                data1 += zopen1.read(500)
     1348                data2 += zopen2.read(500)
     1349            self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
     1350            self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
     1351
     1352    def test_interleaved(self):
    9801353        # Verify that (when the ZipFile is in control of creating file objects)
    9811354        # multiple open() calls can be made without interfering with each other.
    982         zipf = zipfile.ZipFile(TESTFN2, mode="r")
    983         zopen1 = zipf.open('ones')
    984         data1 = zopen1.read(500)
    985         zopen2 = zipf.open('twos')
    986         data2 = zopen2.read(500)
    987         data1 += zopen1.read(500)
    988         data2 += zopen2.read(500)
    989         self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
    990         self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
    991         zipf.close()
     1355        with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
     1356            with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
     1357                data1 = zopen1.read(500)
     1358                data2 = zopen2.read(500)
     1359                data1 += zopen1.read(500)
     1360                data2 += zopen2.read(500)
     1361            self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
     1362            self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
    9921363
    9931364    def tearDown(self):
    994         os.remove(TESTFN2)
     1365        unlink(TESTFN2)
     1366
    9951367
    9961368class TestWithDirectory(unittest.TestCase):
     
    9981370        os.mkdir(TESTFN2)
    9991371
    1000     def testExtractDir(self):
    1001         zipf = zipfile.ZipFile(findfile("zipdir.zip"))
    1002         zipf.extractall(TESTFN2)
     1372    def test_extract_dir(self):
     1373        with zipfile.ZipFile(findfile("zipdir.zip")) as zipf:
     1374            zipf.extractall(TESTFN2)
    10031375        self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
    10041376        self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
     
    10081380        # Extraction should succeed if directories already exist
    10091381        os.mkdir(os.path.join(TESTFN2, "a"))
    1010         self.testExtractDir()
    1011 
    1012     def testStoreDir(self):
     1382        self.test_extract_dir()
     1383
     1384    def test_store_dir(self):
    10131385        os.mkdir(os.path.join(TESTFN2, "x"))
    10141386        zipf = zipfile.ZipFile(TESTFN, "w")
     
    10191391        shutil.rmtree(TESTFN2)
    10201392        if os.path.exists(TESTFN):
    1021             os.remove(TESTFN)
     1393            unlink(TESTFN)
    10221394
    10231395
    10241396class UniversalNewlineTests(unittest.TestCase):
    10251397    def setUp(self):
    1026         self.line_gen = ["Test of zipfile line %d." % i for i in xrange(FIXEDTEST_SIZE)]
     1398        self.line_gen = ["Test of zipfile line %d." % i
     1399                         for i in xrange(FIXEDTEST_SIZE)]
    10271400        self.seps = ('\r', '\r\n', '\n')
    10281401        self.arcdata, self.arcfiles = {}, {}
     
    10321405            open(self.arcfiles[s], "wb").write(self.arcdata[s])
    10331406
    1034     def makeTestArchive(self, f, compression):
     1407    def make_test_archive(self, f, compression):
    10351408        # Create the ZIP archive
    1036         zipfp = zipfile.ZipFile(f, "w", compression)
    1037         for fn in self.arcfiles.values():
    1038             zipfp.write(fn, fn)
    1039         zipfp.close()
    1040 
    1041     def readTest(self, f, compression):
    1042         self.makeTestArchive(f, compression)
     1409        with zipfile.ZipFile(f, "w", compression) as zipfp:
     1410            for fn in self.arcfiles.values():
     1411                zipfp.write(fn, fn)
     1412
     1413    def read_test(self, f, compression):
     1414        self.make_test_archive(f, compression)
     1415
     1416        # Read the ZIP archive
     1417        with zipfile.ZipFile(f, "r") as zipfp:
     1418            for sep, fn in self.arcfiles.items():
     1419                with zipfp.open(fn, "rU") as fp:
     1420                    zipdata = fp.read()
     1421                self.assertEqual(self.arcdata[sep], zipdata)
     1422
     1423    def readline_read_test(self, f, compression):
     1424        self.make_test_archive(f, compression)
    10431425
    10441426        # Read the ZIP archive
    10451427        zipfp = zipfile.ZipFile(f, "r")
    10461428        for sep, fn in self.arcfiles.items():
    1047             zipdata = zipfp.open(fn, "rU").read()
    1048             self.assertEqual(self.arcdata[sep], zipdata)
     1429            with zipfp.open(fn, "rU") as zipopen:
     1430                data = ''
     1431                while True:
     1432                    read = zipopen.readline()
     1433                    if not read:
     1434                        break
     1435                    data += read
     1436
     1437                    read = zipopen.read(5)
     1438                    if not read:
     1439                        break
     1440                    data += read
     1441
     1442            self.assertEqual(data, self.arcdata['\n'])
    10491443
    10501444        zipfp.close()
    10511445
    1052     def readlineTest(self, f, compression):
    1053         self.makeTestArchive(f, compression)
     1446    def readline_test(self, f, compression):
     1447        self.make_test_archive(f, compression)
    10541448
    10551449        # Read the ZIP archive
    1056         zipfp = zipfile.ZipFile(f, "r")
    1057         for sep, fn in self.arcfiles.items():
    1058             zipopen = zipfp.open(fn, "rU")
    1059             for line in self.line_gen:
    1060                 linedata = zipopen.readline()
    1061                 self.assertEqual(linedata, line + '\n')
    1062 
    1063         zipfp.close()
    1064 
    1065     def readlinesTest(self, f, compression):
    1066         self.makeTestArchive(f, compression)
     1450        with zipfile.ZipFile(f, "r") as zipfp:
     1451            for sep, fn in self.arcfiles.items():
     1452                with zipfp.open(fn, "rU") as zipopen:
     1453                    for line in self.line_gen:
     1454                        linedata = zipopen.readline()
     1455                        self.assertEqual(linedata, line + '\n')
     1456
     1457    def readlines_test(self, f, compression):
     1458        self.make_test_archive(f, compression)
    10671459
    10681460        # Read the ZIP archive
    1069         zipfp = zipfile.ZipFile(f, "r")
    1070         for sep, fn in self.arcfiles.items():
    1071             ziplines = zipfp.open(fn, "rU").readlines()
    1072             for line, zipline in zip(self.line_gen, ziplines):
    1073                 self.assertEqual(zipline, line + '\n')
    1074 
    1075         zipfp.close()
    1076 
    1077     def iterlinesTest(self, f, compression):
    1078         self.makeTestArchive(f, compression)
     1461        with zipfile.ZipFile(f, "r") as zipfp:
     1462            for sep, fn in self.arcfiles.items():
     1463                with zipfp.open(fn, "rU") as fp:
     1464                    ziplines = fp.readlines()
     1465                for line, zipline in zip(self.line_gen, ziplines):
     1466                    self.assertEqual(zipline, line + '\n')
     1467
     1468    def iterlines_test(self, f, compression):
     1469        self.make_test_archive(f, compression)
    10791470
    10801471        # Read the ZIP archive
    1081         zipfp = zipfile.ZipFile(f, "r")
    1082         for sep, fn in self.arcfiles.items():
    1083             for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
    1084                 self.assertEqual(zipline, line + '\n')
    1085 
    1086         zipfp.close()
    1087 
    1088     def testReadStored(self):
    1089         for f in (TESTFN2, TemporaryFile(), StringIO()):
    1090             self.readTest(f, zipfile.ZIP_STORED)
    1091 
    1092     def testReadlineStored(self):
    1093         for f in (TESTFN2, TemporaryFile(), StringIO()):
    1094             self.readlineTest(f, zipfile.ZIP_STORED)
    1095 
    1096     def testReadlinesStored(self):
    1097         for f in (TESTFN2, TemporaryFile(), StringIO()):
    1098             self.readlinesTest(f, zipfile.ZIP_STORED)
    1099 
    1100     def testIterlinesStored(self):
    1101         for f in (TESTFN2, TemporaryFile(), StringIO()):
    1102             self.iterlinesTest(f, zipfile.ZIP_STORED)
    1103 
    1104     if zlib:
    1105         def testReadDeflated(self):
    1106             for f in (TESTFN2, TemporaryFile(), StringIO()):
    1107                 self.readTest(f, zipfile.ZIP_DEFLATED)
    1108 
    1109         def testReadlineDeflated(self):
    1110             for f in (TESTFN2, TemporaryFile(), StringIO()):
    1111                 self.readlineTest(f, zipfile.ZIP_DEFLATED)
    1112 
    1113         def testReadlinesDeflated(self):
    1114             for f in (TESTFN2, TemporaryFile(), StringIO()):
    1115                 self.readlinesTest(f, zipfile.ZIP_DEFLATED)
    1116 
    1117         def testIterlinesDeflated(self):
    1118             for f in (TESTFN2, TemporaryFile(), StringIO()):
    1119                 self.iterlinesTest(f, zipfile.ZIP_DEFLATED)
     1472        with zipfile.ZipFile(f, "r") as zipfp:
     1473            for sep, fn in self.arcfiles.items():
     1474                for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
     1475                    self.assertEqual(zipline, line + '\n')
     1476
     1477    def test_read_stored(self):
     1478        for f in (TESTFN2, TemporaryFile(), StringIO()):
     1479            self.read_test(f, zipfile.ZIP_STORED)
     1480
     1481    def test_readline_read_stored(self):
     1482        # Issue #7610: calls to readline() interleaved with calls to read().
     1483        for f in (TESTFN2, TemporaryFile(), StringIO()):
     1484            self.readline_read_test(f, zipfile.ZIP_STORED)
     1485
     1486    def test_readline_stored(self):
     1487        for f in (TESTFN2, TemporaryFile(), StringIO()):
     1488            self.readline_test(f, zipfile.ZIP_STORED)
     1489
     1490    def test_readlines_stored(self):
     1491        for f in (TESTFN2, TemporaryFile(), StringIO()):
     1492            self.readlines_test(f, zipfile.ZIP_STORED)
     1493
     1494    def test_iterlines_stored(self):
     1495        for f in (TESTFN2, TemporaryFile(), StringIO()):
     1496            self.iterlines_test(f, zipfile.ZIP_STORED)
     1497
     1498    @skipUnless(zlib, "requires zlib")
     1499    def test_read_deflated(self):
     1500        for f in (TESTFN2, TemporaryFile(), StringIO()):
     1501            self.read_test(f, zipfile.ZIP_DEFLATED)
     1502
     1503    @skipUnless(zlib, "requires zlib")
     1504    def test_readline_read_deflated(self):
     1505        # Issue #7610: calls to readline() interleaved with calls to read().
     1506        for f in (TESTFN2, TemporaryFile(), StringIO()):
     1507            self.readline_read_test(f, zipfile.ZIP_DEFLATED)
     1508
     1509    @skipUnless(zlib, "requires zlib")
     1510    def test_readline_deflated(self):
     1511        for f in (TESTFN2, TemporaryFile(), StringIO()):
     1512            self.readline_test(f, zipfile.ZIP_DEFLATED)
     1513
     1514    @skipUnless(zlib, "requires zlib")
     1515    def test_readlines_deflated(self):
     1516        for f in (TESTFN2, TemporaryFile(), StringIO()):
     1517            self.readlines_test(f, zipfile.ZIP_DEFLATED)
     1518
     1519    @skipUnless(zlib, "requires zlib")
     1520    def test_iterlines_deflated(self):
     1521        for f in (TESTFN2, TemporaryFile(), StringIO()):
     1522            self.iterlines_test(f, zipfile.ZIP_DEFLATED)
    11201523
    11211524    def tearDown(self):
    11221525        for sep, fn in self.arcfiles.items():
    11231526            os.remove(fn)
    1124         support.unlink(TESTFN)
    1125         support.unlink(TESTFN2)
     1527        unlink(TESTFN)
     1528        unlink(TESTFN2)
    11261529
    11271530
     
    11291532    run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
    11301533                 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
    1131                  TestWithDirectory,
    1132                  UniversalNewlineTests, TestsWithRandomBinaryFiles)
     1534                 TestWithDirectory, UniversalNewlineTests,
     1535                 TestsWithRandomBinaryFiles)
    11331536
    11341537if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.