make file closing more robust
[python/dscho.git] / Lib / test / test_zipfile64.py
blob0e7d73f1fffd0f65692e713d6ce7000a39e0dd3b
1 # Tests of the full ZIP64 functionality of zipfile
2 # The support.requires call is the only reason for keeping this separate
3 # from test_zipfile
4 from test import support
6 # XXX(nnorwitz): disable this test by looking for extra largfile resource
7 # which doesn't exist. This test takes over 30 minutes to run in general
8 # and requires more disk space than most of the buildbots.
9 support.requires(
10 'extralargefile',
11 'test requires loads of disk-space bytes and a long time to run'
14 # We can test part of the module without zlib.
15 try:
16 import zlib
17 except ImportError:
18 zlib = None
20 import zipfile, os, unittest
21 import time
22 import sys
24 from io import StringIO
25 from tempfile import TemporaryFile
27 from test.support import TESTFN, run_unittest
29 TESTFN2 = TESTFN + "2"
31 # How much time in seconds can pass before we print a 'Still working' message.
32 _PRINT_WORKING_MSG_INTERVAL = 5 * 60
34 class TestsWithSourceFile(unittest.TestCase):
35 def setUp(self):
36 # Create test data.
37 line_gen = ("Test of zipfile line %d." % i for i in range(1000000))
38 self.data = '\n'.join(line_gen).encode('ascii')
40 # And write it to a file.
41 fp = open(TESTFN, "wb")
42 fp.write(self.data)
43 fp.close()
45 def zipTest(self, f, compression):
46 # Create the ZIP archive.
47 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
49 # It will contain enough copies of self.data to reach about 6GB of
50 # raw data to store.
51 filecount = 6*1024**3 // len(self.data)
53 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
54 for num in range(filecount):
55 zipfp.writestr("testfn%d" % num, self.data)
56 # Print still working message since this test can be really slow
57 if next_time <= time.time():
58 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
59 print((
60 ' zipTest still writing %d of %d, be patient...' %
61 (num, filecount)), file=sys.__stdout__)
62 sys.__stdout__.flush()
63 zipfp.close()
65 # Read the ZIP archive
66 zipfp = zipfile.ZipFile(f, "r", compression)
67 for num in range(filecount):
68 self.assertEqual(zipfp.read("testfn%d" % num), self.data)
69 # Print still working message since this test can be really slow
70 if next_time <= time.time():
71 next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL
72 print((
73 ' zipTest still reading %d of %d, be patient...' %
74 (num, filecount)), file=sys.__stdout__)
75 sys.__stdout__.flush()
76 zipfp.close()
78 def testStored(self):
79 # Try the temp file first. If we do TESTFN2 first, then it hogs
80 # gigabytes of disk space for the duration of the test.
81 for f in TemporaryFile(), TESTFN2:
82 self.zipTest(f, zipfile.ZIP_STORED)
84 if zlib:
85 def testDeflated(self):
86 # Try the temp file first. If we do TESTFN2 first, then it hogs
87 # gigabytes of disk space for the duration of the test.
88 for f in TemporaryFile(), TESTFN2:
89 self.zipTest(f, zipfile.ZIP_DEFLATED)
91 def tearDown(self):
92 for fname in TESTFN, TESTFN2:
93 if os.path.exists(fname):
94 os.remove(fname)
97 class OtherTests(unittest.TestCase):
98 def testMoreThan64kFiles(self):
99 # This test checks that more than 64k files can be added to an archive,
100 # and that the resulting archive can be read properly by ZipFile
101 zipf = zipfile.ZipFile(TESTFN, mode="w")
102 zipf.debug = 100
103 numfiles = (1 << 16) * 3//2
104 for i in range(numfiles):
105 zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
106 self.assertEqual(len(zipf.namelist()), numfiles)
107 zipf.close()
109 zipf2 = zipfile.ZipFile(TESTFN, mode="r")
110 self.assertEqual(len(zipf2.namelist()), numfiles)
111 for i in range(numfiles):
112 content = zipf2.read("foo%08d" % i).decode('ascii')
113 self.assertEqual(content, "%d" % (i**3 % 57))
114 zipf.close()
116 def tearDown(self):
117 support.unlink(TESTFN)
118 support.unlink(TESTFN2)
120 def test_main():
121 run_unittest(TestsWithSourceFile, OtherTests)
123 if __name__ == "__main__":
124 test_main()