annotate Lib/test/test_zipfile.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents fc38f2ecd2ac
children 3be37ec43c1b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
1 # We can test part of the module without zlib.
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
2 try:
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
3 import zlib
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
4 except ImportError:
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
5 zlib = None
33204
c18aca7e81cb Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33191
diff changeset
6
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
7 import zipfile, os, unittest, sys, shutil, struct
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16827
diff changeset
8
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
9 from StringIO import StringIO
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
10 from tempfile import TemporaryFile
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
11 from random import randint, random
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
12
41319
f57e8e325475 Make test_zipfile clean up its temporary files properly.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
13 import test.test_support as support
50999
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
14 from test.test_support import TESTFN, run_unittest, findfile
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
15
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
16 TESTFN2 = TESTFN + "2"
50999
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
17 TESTFNDIR = TESTFN + "d"
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
18 FIXEDTEST_SIZE = 1000
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
19
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
20 SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
21 ('ziptest2dir/_ziptest2', 'qawsedrftg'),
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
22 ('/ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
23 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
24
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
25 class TestsWithSourceFile(unittest.TestCase):
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
26 def setUp(self):
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
27 self.line_gen = ["Zipfile test line %d. random float: %f" % (i, random())
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
28 for i in xrange(FIXEDTEST_SIZE)]
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
29 self.data = '\n'.join(self.line_gen) + '\n'
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16827
diff changeset
30
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
31 # Make a source file with some lines
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
32 fp = open(TESTFN, "wb")
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
33 fp.write(self.data)
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
34 fp.close()
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
35
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
36 def makeTestArchive(self, f, compression):
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
37 # Create the ZIP archive
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
38 zipfp = zipfile.ZipFile(f, "w", compression)
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
39 zipfp.write(TESTFN, "another"+os.extsep+"name")
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
40 zipfp.write(TESTFN, TESTFN)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
41 zipfp.writestr("strfile", self.data)
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
42 zipfp.close()
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
43
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
44 def zipTest(self, f, compression):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
45 self.makeTestArchive(f, compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
46
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
47 # Read the ZIP archive
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
48 zipfp = zipfile.ZipFile(f, "r", compression)
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
49 self.assertEqual(zipfp.read(TESTFN), self.data)
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
50 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
51 self.assertEqual(zipfp.read("strfile"), self.data)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
52
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
53 # Print the ZIP directory
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
54 fp = StringIO()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
55 stdout = sys.stdout
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
56 try:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
57 sys.stdout = fp
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
58
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
59 zipfp.printdir()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
60 finally:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
61 sys.stdout = stdout
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
62
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
63 directory = fp.getvalue()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
64 lines = directory.splitlines()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
65 self.assertEquals(len(lines), 4) # Number of files + header
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
66
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
67 self.assert_('File Name' in lines[0])
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
68 self.assert_('Modified' in lines[0])
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
69 self.assert_('Size' in lines[0])
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
70
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
71 fn, date, time, size = lines[1].split()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
72 self.assertEquals(fn, 'another.name')
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
73 # XXX: timestamp is not tested
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
74 self.assertEquals(size, str(len(self.data)))
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
75
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
76 # Check the namelist
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
77 names = zipfp.namelist()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
78 self.assertEquals(len(names), 3)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
79 self.assert_(TESTFN in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
80 self.assert_("another"+os.extsep+"name" in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
81 self.assert_("strfile" in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
82
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
83 # Check infolist
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
84 infos = zipfp.infolist()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
85 names = [ i.filename for i in infos ]
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
86 self.assertEquals(len(names), 3)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
87 self.assert_(TESTFN in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
88 self.assert_("another"+os.extsep+"name" in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
89 self.assert_("strfile" in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
90 for i in infos:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
91 self.assertEquals(i.file_size, len(self.data))
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
92
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
93 # check getinfo
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
94 for nm in (TESTFN, "another"+os.extsep+"name", "strfile"):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
95 info = zipfp.getinfo(nm)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
96 self.assertEquals(info.filename, nm)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
97 self.assertEquals(info.file_size, len(self.data))
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
98
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
99 # Check that testzip doesn't raise an exception
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
100 zipfp.testzip()
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
101 zipfp.close()
12323
1a207c68e8b4 Test for zipfile.py, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
103 def testStored(self):
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
104 for f in (TESTFN2, TemporaryFile(), StringIO()):
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
105 self.zipTest(f, zipfile.ZIP_STORED)
16827
ce47498a3158 Added regression test for SF tracker bug #403871: AttributeError in
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
106
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
107 def zipOpenTest(self, f, compression):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
108 self.makeTestArchive(f, compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
109
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
110 # Read the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
111 zipfp = zipfile.ZipFile(f, "r", compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
112 zipdata1 = []
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
113 zipopen1 = zipfp.open(TESTFN)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
114 while 1:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
115 read_data = zipopen1.read(256)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
116 if not read_data:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
117 break
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
118 zipdata1.append(read_data)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
119
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
120 zipdata2 = []
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
121 zipopen2 = zipfp.open("another"+os.extsep+"name")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
122 while 1:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
123 read_data = zipopen2.read(256)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
124 if not read_data:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
125 break
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
126 zipdata2.append(read_data)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
127
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
128 self.assertEqual(''.join(zipdata1), self.data)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
129 self.assertEqual(''.join(zipdata2), self.data)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
130 zipfp.close()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
131
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
132 def testOpenStored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
133 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
134 self.zipOpenTest(f, zipfile.ZIP_STORED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
135
47195
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
136 def testOpenViaZipInfo(self):
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
137 # Create the ZIP archive
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
138 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
139 zipfp.writestr("name", "foo")
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
140 zipfp.writestr("name", "bar")
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
141 zipfp.close()
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
142
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
143 zipfp = zipfile.ZipFile(TESTFN2, "r")
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
144 infos = zipfp.infolist()
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
145 data = ""
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
146 for info in infos:
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
147 data += zipfp.open(info).read()
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
148 self.assert_(data == "foobar" or data == "barfoo")
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
149 data = ""
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
150 for info in infos:
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
151 data += zipfp.read(info)
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
152 self.assert_(data == "foobar" or data == "barfoo")
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
153 zipfp.close()
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
154
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
155 def zipRandomOpenTest(self, f, compression):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
156 self.makeTestArchive(f, compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
157
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
158 # Read the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
159 zipfp = zipfile.ZipFile(f, "r", compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
160 zipdata1 = []
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
161 zipopen1 = zipfp.open(TESTFN)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
162 while 1:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
163 read_data = zipopen1.read(randint(1, 1024))
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
164 if not read_data:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
165 break
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
166 zipdata1.append(read_data)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
167
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
168 self.assertEqual(''.join(zipdata1), self.data)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
169 zipfp.close()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
170
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
171 def testRandomOpenStored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
172 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
173 self.zipRandomOpenTest(f, zipfile.ZIP_STORED)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
174
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
175 def zipReadlineTest(self, f, compression):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
176 self.makeTestArchive(f, compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
177
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
178 # Read the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
179 zipfp = zipfile.ZipFile(f, "r")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
180 zipopen = zipfp.open(TESTFN)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
181 for line in self.line_gen:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
182 linedata = zipopen.readline()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
183 self.assertEqual(linedata, line + '\n')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
184
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
185 zipfp.close()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
186
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
187 def zipReadlinesTest(self, f, compression):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
188 self.makeTestArchive(f, compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
189
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
190 # Read the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
191 zipfp = zipfile.ZipFile(f, "r")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
192 ziplines = zipfp.open(TESTFN).readlines()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
193 for line, zipline in zip(self.line_gen, ziplines):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
194 self.assertEqual(zipline, line + '\n')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
195
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
196 zipfp.close()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
197
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
198 def zipIterlinesTest(self, f, compression):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
199 self.makeTestArchive(f, compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
200
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
201 # Read the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
202 zipfp = zipfile.ZipFile(f, "r")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
203 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
204 self.assertEqual(zipline, line + '\n')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
205
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
206 zipfp.close()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
207
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
208 def testReadlineStored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
209 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
210 self.zipReadlineTest(f, zipfile.ZIP_STORED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
211
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
212 def testReadlinesStored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
213 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
214 self.zipReadlinesTest(f, zipfile.ZIP_STORED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
215
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
216 def testIterlinesStored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
217 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
218 self.zipIterlinesTest(f, zipfile.ZIP_STORED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
219
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
220 if zlib:
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
221 def testDeflated(self):
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
222 for f in (TESTFN2, TemporaryFile(), StringIO()):
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
223 self.zipTest(f, zipfile.ZIP_DEFLATED)
17312
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
224
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
225 def testOpenDeflated(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
226 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
227 self.zipOpenTest(f, zipfile.ZIP_DEFLATED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
228
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
229 def testRandomOpenDeflated(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
230 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
231 self.zipRandomOpenTest(f, zipfile.ZIP_DEFLATED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
232
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
233 def testReadlineDeflated(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
234 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
235 self.zipReadlineTest(f, zipfile.ZIP_DEFLATED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
236
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
237 def testReadlinesDeflated(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
238 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
239 self.zipReadlinesTest(f, zipfile.ZIP_DEFLATED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
240
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
241 def testIterlinesDeflated(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
242 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
243 self.zipIterlinesTest(f, zipfile.ZIP_DEFLATED)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
244
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
245 def testLowCompression(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
246 # Checks for cases where compressed data is larger than original
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
247 # Create the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
248 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
249 zipfp.writestr("strfile", '12')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
250 zipfp.close()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
251
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
252 # Get an open object for strfile
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
253 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
254 openobj = zipfp.open("strfile")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
255 self.assertEqual(openobj.read(1), '1')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
256 self.assertEqual(openobj.read(1), '2')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
257
36609
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 33204
diff changeset
258 def testAbsoluteArcnames(self):
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 33204
diff changeset
259 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 33204
diff changeset
260 zipfp.write(TESTFN, "/absolute")
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 33204
diff changeset
261 zipfp.close()
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 33204
diff changeset
262
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 33204
diff changeset
263 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 33204
diff changeset
264 self.assertEqual(zipfp.namelist(), ["absolute"])
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 33204
diff changeset
265 zipfp.close()
36630
eb0977932e2d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36609
diff changeset
266
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
267 def testAppendToZipFile(self):
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
268 # Test appending to an existing zipfile
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
269 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
270 zipfp.write(TESTFN, TESTFN)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
271 zipfp.close()
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
272 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
273 zipfp.writestr("strfile", self.data)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
274 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
275 zipfp.close()
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
276
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
277 def testAppendToNonZipFile(self):
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
278 # Test appending to an existing file that is not a zipfile
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
279 # NOTE: this test fails if len(d) < 22 because of the first
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
280 # line "fpin.seek(-22, 2)" in _EndRecData
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
281 d = 'I am not a ZipFile!'*10
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
282 f = file(TESTFN2, 'wb')
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
283 f.write(d)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
284 f.close()
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
285 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
286 zipfp.write(TESTFN, TESTFN)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
287 zipfp.close()
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
288
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
289 f = file(TESTFN2, 'rb')
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
290 f.seek(len(d))
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
291 zipfp = zipfile.ZipFile(f, "r")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
292 self.assertEqual(zipfp.namelist(), [TESTFN])
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
293 zipfp.close()
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
294 f.close()
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
295
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
296 def test_WriteDefaultName(self):
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
297 # Check that calling ZipFile.write without arcname specified produces the expected result
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
298 zipfp = zipfile.ZipFile(TESTFN2, "w")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
299 zipfp.write(TESTFN)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
300 self.assertEqual(zipfp.read(TESTFN), file(TESTFN).read())
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
301 zipfp.close()
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
302
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
303 def test_PerFileCompression(self):
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
304 # Check that files within a Zip archive can have different compression options
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
305 zipfp = zipfile.ZipFile(TESTFN2, "w")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
306 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
307 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
308 sinfo = zipfp.getinfo('storeme')
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
309 dinfo = zipfp.getinfo('deflateme')
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
310 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
311 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
312 zipfp.close()
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
313
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
314 def test_WriteToReadonly(self):
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
315 # Check that trying to call write() on a readonly ZipFile object
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
316 # raises a RuntimeError
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
317 zipf = zipfile.ZipFile(TESTFN2, mode="w")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
318 zipf.writestr("somefile.txt", "bogus")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
319 zipf.close()
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
320 zipf = zipfile.ZipFile(TESTFN2, mode="r")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
321 self.assertRaises(RuntimeError, zipf.write, TESTFN)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
322 zipf.close()
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
323
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
324 def testExtract(self):
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
325 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
326 for fpath, fdata in SMALL_TEST_DATA:
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
327 zipfp.writestr(fpath, fdata)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
328 zipfp.close()
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
329
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
330 zipfp = zipfile.ZipFile(TESTFN2, "r")
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
331 for fpath, fdata in SMALL_TEST_DATA:
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
332 writtenfile = zipfp.extract(fpath)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
333
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
334 # make sure it was written to the right place
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
335 if os.path.isabs(fpath):
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
336 correctfile = os.path.join(os.getcwd(), fpath[1:])
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
337 else:
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
338 correctfile = os.path.join(os.getcwd(), fpath)
44978
699b37170cad Unit test fix from Giampaolo Rodola, #1938
Christian Heimes <christian@cheimes.de>
parents: 44807
diff changeset
339 correctfile = os.path.normpath(correctfile)
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
340
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
341 self.assertEqual(writtenfile, correctfile)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
342
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
343 # make sure correct data is in correct file
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
344 self.assertEqual(fdata, file(writtenfile, "rb").read())
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
345
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
346 os.remove(writtenfile)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
347
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
348 zipfp.close()
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
349
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
350 # remove the test file subdirectories
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
351 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
352
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
353 def testExtractAll(self):
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
354 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
355 for fpath, fdata in SMALL_TEST_DATA:
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
356 zipfp.writestr(fpath, fdata)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
357 zipfp.close()
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
358
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
359 zipfp = zipfile.ZipFile(TESTFN2, "r")
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
360 zipfp.extractall()
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
361 for fpath, fdata in SMALL_TEST_DATA:
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
362 if os.path.isabs(fpath):
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
363 outfile = os.path.join(os.getcwd(), fpath[1:])
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
364 else:
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
365 outfile = os.path.join(os.getcwd(), fpath)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
366
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
367 self.assertEqual(fdata, file(outfile, "rb").read())
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
368
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
369 os.remove(outfile)
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
370
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
371 zipfp.close()
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
372
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
373 # remove the test file subdirectories
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
374 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
375
48073
972f489ae356 #3394: zipfile.writestr doesn't set external attributes, so files are extracted mode 000 on Unix
Antoine Pitrou <solipsis@pitrou.net>
parents: 47882
diff changeset
376 def zip_test_writestr_permissions(self, f, compression):
972f489ae356 #3394: zipfile.writestr doesn't set external attributes, so files are extracted mode 000 on Unix
Antoine Pitrou <solipsis@pitrou.net>
parents: 47882
diff changeset
377 # Make sure that writestr creates files with mode 0600,
972f489ae356 #3394: zipfile.writestr doesn't set external attributes, so files are extracted mode 000 on Unix
Antoine Pitrou <solipsis@pitrou.net>
parents: 47882
diff changeset
378 # when it is passed a name rather than a ZipInfo instance.
972f489ae356 #3394: zipfile.writestr doesn't set external attributes, so files are extracted mode 000 on Unix
Antoine Pitrou <solipsis@pitrou.net>
parents: 47882
diff changeset
379
972f489ae356 #3394: zipfile.writestr doesn't set external attributes, so files are extracted mode 000 on Unix
Antoine Pitrou <solipsis@pitrou.net>
parents: 47882
diff changeset
380 self.makeTestArchive(f, compression)
972f489ae356 #3394: zipfile.writestr doesn't set external attributes, so files are extracted mode 000 on Unix
Antoine Pitrou <solipsis@pitrou.net>
parents: 47882
diff changeset
381 zipfp = zipfile.ZipFile(f, "r")
972f489ae356 #3394: zipfile.writestr doesn't set external attributes, so files are extracted mode 000 on Unix
Antoine Pitrou <solipsis@pitrou.net>
parents: 47882
diff changeset
382 zinfo = zipfp.getinfo('strfile')
972f489ae356 #3394: zipfile.writestr doesn't set external attributes, so files are extracted mode 000 on Unix
Antoine Pitrou <solipsis@pitrou.net>
parents: 47882
diff changeset
383 self.assertEqual(zinfo.external_attr, 0600 << 16)
972f489ae356 #3394: zipfile.writestr doesn't set external attributes, so files are extracted mode 000 on Unix
Antoine Pitrou <solipsis@pitrou.net>
parents: 47882
diff changeset
384
972f489ae356 #3394: zipfile.writestr doesn't set external attributes, so files are extracted mode 000 on Unix
Antoine Pitrou <solipsis@pitrou.net>
parents: 47882
diff changeset
385 def test_writestr_permissions(self):
972f489ae356 #3394: zipfile.writestr doesn't set external attributes, so files are extracted mode 000 on Unix
Antoine Pitrou <solipsis@pitrou.net>
parents: 47882
diff changeset
386 for f in (TESTFN2, TemporaryFile(), StringIO()):
972f489ae356 #3394: zipfile.writestr doesn't set external attributes, so files are extracted mode 000 on Unix
Antoine Pitrou <solipsis@pitrou.net>
parents: 47882
diff changeset
387 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
972f489ae356 #3394: zipfile.writestr doesn't set external attributes, so files are extracted mode 000 on Unix
Antoine Pitrou <solipsis@pitrou.net>
parents: 47882
diff changeset
388
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
389 def tearDown(self):
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
390 os.remove(TESTFN)
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
391 os.remove(TESTFN2)
17312
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
392
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
393 class TestZip64InSmallFiles(unittest.TestCase):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
394 # These tests test the ZIP64 functionality without using large files,
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
395 # see test_zipfile64 for proper tests.
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
396
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
397 def setUp(self):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
398 self._limit = zipfile.ZIP64_LIMIT
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
399 zipfile.ZIP64_LIMIT = 5
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
400
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
401 line_gen = ("Test of zipfile line %d." % i for i in range(0, FIXEDTEST_SIZE))
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
402 self.data = '\n'.join(line_gen)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
403
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
404 # Make a source file with some lines
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
405 fp = open(TESTFN, "wb")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
406 fp.write(self.data)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
407 fp.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
408
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
409 def largeFileExceptionTest(self, f, compression):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
410 zipfp = zipfile.ZipFile(f, "w", compression)
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
411 self.assertRaises(zipfile.LargeZipFile,
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
412 zipfp.write, TESTFN, "another"+os.extsep+"name")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
413 zipfp.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
414
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
415 def largeFileExceptionTest2(self, f, compression):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
416 zipfp = zipfile.ZipFile(f, "w", compression)
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
417 self.assertRaises(zipfile.LargeZipFile,
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
418 zipfp.writestr, "another"+os.extsep+"name", self.data)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
419 zipfp.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
420
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
421 def testLargeFileException(self):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
422 for f in (TESTFN2, TemporaryFile(), StringIO()):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
423 self.largeFileExceptionTest(f, zipfile.ZIP_STORED)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
424 self.largeFileExceptionTest2(f, zipfile.ZIP_STORED)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
425
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
426 def zipTest(self, f, compression):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
427 # Create the ZIP archive
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
428 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
429 zipfp.write(TESTFN, "another"+os.extsep+"name")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
430 zipfp.write(TESTFN, TESTFN)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
431 zipfp.writestr("strfile", self.data)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
432 zipfp.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
433
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
434 # Read the ZIP archive
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
435 zipfp = zipfile.ZipFile(f, "r", compression)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
436 self.assertEqual(zipfp.read(TESTFN), self.data)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
437 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
438 self.assertEqual(zipfp.read("strfile"), self.data)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
439
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
440 # Print the ZIP directory
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
441 fp = StringIO()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
442 stdout = sys.stdout
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
443 try:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
444 sys.stdout = fp
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
445
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
446 zipfp.printdir()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
447 finally:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
448 sys.stdout = stdout
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
449
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
450 directory = fp.getvalue()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
451 lines = directory.splitlines()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
452 self.assertEquals(len(lines), 4) # Number of files + header
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
453
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
454 self.assert_('File Name' in lines[0])
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
455 self.assert_('Modified' in lines[0])
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
456 self.assert_('Size' in lines[0])
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
457
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
458 fn, date, time, size = lines[1].split()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
459 self.assertEquals(fn, 'another.name')
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
460 # XXX: timestamp is not tested
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
461 self.assertEquals(size, str(len(self.data)))
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
462
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
463 # Check the namelist
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
464 names = zipfp.namelist()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
465 self.assertEquals(len(names), 3)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
466 self.assert_(TESTFN in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
467 self.assert_("another"+os.extsep+"name" in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
468 self.assert_("strfile" in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
469
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
470 # Check infolist
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
471 infos = zipfp.infolist()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
472 names = [ i.filename for i in infos ]
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
473 self.assertEquals(len(names), 3)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
474 self.assert_(TESTFN in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
475 self.assert_("another"+os.extsep+"name" in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
476 self.assert_("strfile" in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
477 for i in infos:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
478 self.assertEquals(i.file_size, len(self.data))
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
479
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
480 # check getinfo
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
481 for nm in (TESTFN, "another"+os.extsep+"name", "strfile"):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
482 info = zipfp.getinfo(nm)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
483 self.assertEquals(info.filename, nm)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
484 self.assertEquals(info.file_size, len(self.data))
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
485
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
486 # Check that testzip doesn't raise an exception
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
487 zipfp.testzip()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
488
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
489
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
490 zipfp.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
491
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
492 def testStored(self):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
493 for f in (TESTFN2, TemporaryFile(), StringIO()):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
494 self.zipTest(f, zipfile.ZIP_STORED)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
495
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
496
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
497 if zlib:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
498 def testDeflated(self):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
499 for f in (TESTFN2, TemporaryFile(), StringIO()):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
500 self.zipTest(f, zipfile.ZIP_DEFLATED)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
501
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
502 def testAbsoluteArcnames(self):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
503 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
504 zipfp.write(TESTFN, "/absolute")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
505 zipfp.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
506
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
507 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
508 self.assertEqual(zipfp.namelist(), ["absolute"])
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
509 zipfp.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
510
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
511 def tearDown(self):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
512 zipfile.ZIP64_LIMIT = self._limit
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
513 os.remove(TESTFN)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
514 os.remove(TESTFN2)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
515
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
516 class PyZipFileTests(unittest.TestCase):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
517 def testWritePyfile(self):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
518 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
519 fn = __file__
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
520 if fn.endswith('.pyc') or fn.endswith('.pyo'):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
521 fn = fn[:-1]
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
522
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
523 zipfp.writepy(fn)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
524
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
525 bn = os.path.basename(fn)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
526 self.assert_(bn not in zipfp.namelist())
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
527 self.assert_(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
528 zipfp.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
529
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
530
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
531 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
532 fn = __file__
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
533 if fn.endswith('.pyc') or fn.endswith('.pyo'):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
534 fn = fn[:-1]
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
535
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
536 zipfp.writepy(fn, "testpackage")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
537
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
538 bn = "%s/%s"%("testpackage", os.path.basename(fn))
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
539 self.assert_(bn not in zipfp.namelist())
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
540 self.assert_(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist())
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
541 zipfp.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
542
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
543 def testWritePythonPackage(self):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
544 import email
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
545 packagedir = os.path.dirname(email.__file__)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
546
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
547 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
548 zipfp.writepy(packagedir)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
549
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
550 # Check for a couple of modules at different levels of the hieararchy
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
551 names = zipfp.namelist()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
552 self.assert_('email/__init__.pyo' in names or 'email/__init__.pyc' in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
553 self.assert_('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
554
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
555 def testWritePythonDirectory(self):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
556 os.mkdir(TESTFN2)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
557 try:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
558 fp = open(os.path.join(TESTFN2, "mod1.py"), "w")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
559 fp.write("print 42\n")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
560 fp.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
561
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
562 fp = open(os.path.join(TESTFN2, "mod2.py"), "w")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
563 fp.write("print 42 * 42\n")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
564 fp.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
565
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
566 fp = open(os.path.join(TESTFN2, "mod2.txt"), "w")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
567 fp.write("bla bla bla\n")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
568 fp.close()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
569
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
570 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
571 zipfp.writepy(TESTFN2)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
572
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
573 names = zipfp.namelist()
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
574 self.assert_('mod1.pyc' in names or 'mod1.pyo' in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
575 self.assert_('mod2.pyc' in names or 'mod2.pyo' in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
576 self.assert_('mod2.txt' not in names)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
577
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
578 finally:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
579 shutil.rmtree(TESTFN2)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
580
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
581 def testWriteNonPyfile(self):
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
582 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
583 file(TESTFN, 'w').write('most definitely not a python file')
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
584 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
585 os.remove(TESTFN)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
586
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
587
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
588 class OtherTests(unittest.TestCase):
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 44978
diff changeset
589 def testUnicodeFilenames(self):
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 44978
diff changeset
590 zf = zipfile.ZipFile(TESTFN, "w")
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 44978
diff changeset
591 zf.writestr(u"foo.txt", "Test for unicode filename")
46673
10e8ef95b11f Fix Unicode filename test.
Martin v. Löwis <martin@v.loewis.de>
parents: 46671
diff changeset
592 zf.writestr(u"\xf6.txt", "Test for unicode filename")
10e8ef95b11f Fix Unicode filename test.
Martin v. Löwis <martin@v.loewis.de>
parents: 46671
diff changeset
593 self.assertTrue(isinstance(zf.infolist()[0].filename, unicode))
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 44978
diff changeset
594 zf.close()
46673
10e8ef95b11f Fix Unicode filename test.
Martin v. Löwis <martin@v.loewis.de>
parents: 46671
diff changeset
595 zf = zipfile.ZipFile(TESTFN, "r")
10e8ef95b11f Fix Unicode filename test.
Martin v. Löwis <martin@v.loewis.de>
parents: 46671
diff changeset
596 self.assertEqual(zf.filelist[0].filename, "foo.txt")
10e8ef95b11f Fix Unicode filename test.
Martin v. Löwis <martin@v.loewis.de>
parents: 46671
diff changeset
597 self.assertEqual(zf.filelist[1].filename, u"\xf6.txt")
10e8ef95b11f Fix Unicode filename test.
Martin v. Löwis <martin@v.loewis.de>
parents: 46671
diff changeset
598 zf.close()
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 44978
diff changeset
599
40834
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
600 def testCreateNonExistentFileForAppend(self):
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
601 if os.path.exists(TESTFN):
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
602 os.unlink(TESTFN)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
603
40834
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
604 filename = 'testfile.txt'
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
605 content = 'hello, world. this is some content.'
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
606
40834
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
607 try:
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
608 zf = zipfile.ZipFile(TESTFN, 'a')
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
609 zf.writestr(filename, content)
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
610 zf.close()
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
611 except IOError, (errno, errmsg):
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
612 self.fail('Could not append data to a non-existent zip file.')
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
613
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
614 self.assert_(os.path.exists(TESTFN))
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
615
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
616 zf = zipfile.ZipFile(TESTFN, 'r')
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
617 self.assertEqual(zf.read(filename), content)
d949933cd6b4 Patch #1517891: Make 'a' create the file if it doesn't exist.
Martin v. Löwis <martin@v.loewis.de>
parents: 40833
diff changeset
618 zf.close()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
619
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
620 def testCloseErroneousFile(self):
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
621 # This test checks that the ZipFile constructor closes the file object
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
622 # it opens if there's an error in the file. If it doesn't, the traceback
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
623 # holds a reference to the ZipFile object and, indirectly, the file object.
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
624 # On Windows, this causes the os.unlink() call to fail because the
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
625 # underlying file is still open. This is SF bug #412214.
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
626 #
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
627 fp = open(TESTFN, "w")
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
628 fp.write("this is not a legal zip file\n")
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
629 fp.close()
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
630 try:
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
631 zf = zipfile.ZipFile(TESTFN)
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
632 except zipfile.BadZipfile:
41319
f57e8e325475 Make test_zipfile clean up its temporary files properly.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
633 pass
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
634
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
635 def testIsZipErroneousFile(self):
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
636 # This test checks that the is_zipfile function correctly identifies
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
637 # a file that is not a zip file
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
638
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
639 # - passing a filename
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
640 with open(TESTFN, "w") as fp:
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
641 fp.write("this is not a legal zip file\n")
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
642 chk = zipfile.is_zipfile(TESTFN)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
643 self.assert_(not chk)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
644 # - passing a file object
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
645 with open(TESTFN, "rb") as fp:
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
646 chk = zipfile.is_zipfile(fp)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
647 self.assert_(not chk)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
648 # - passing a file-like object
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
649 fp = StringIO()
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
650 fp.write("this is not a legal zip file\n")
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
651 chk = zipfile.is_zipfile(fp)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
652 self.assert_(not chk)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
653 fp.seek(0,0)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
654 chk = zipfile.is_zipfile(fp)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
655 self.assert_(not chk)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
656
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
657 def testIsZipValidFile(self):
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
658 # This test checks that the is_zipfile function correctly identifies
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
659 # a file that is a zip file
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
660
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
661 # - passing a filename
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
662 zipf = zipfile.ZipFile(TESTFN, mode="w")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
663 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
664 zipf.close()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
665 chk = zipfile.is_zipfile(TESTFN)
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
666 self.assert_(chk)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
667 # - passing a file object
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
668 with open(TESTFN, "rb") as fp:
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
669 chk = zipfile.is_zipfile(fp)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
670 self.assert_(chk)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
671 fp.seek(0,0)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
672 zip_contents = fp.read()
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
673 # - passing a file-like object
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
674 fp = StringIO()
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
675 fp.write(zip_contents)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
676 chk = zipfile.is_zipfile(fp)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
677 self.assert_(chk)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
678 fp.seek(0,0)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
679 chk = zipfile.is_zipfile(fp)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
680 self.assert_(chk)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
681
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
682 def testNonExistentFileRaisesIOError(self):
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
683 # make sure we don't raise an AttributeError when a partially-constructed
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
684 # ZipFile instance is finalized; this tests for regression on SF tracker
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
685 # bug #403871.
17312
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
686
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
687 # The bug we're testing for caused an AttributeError to be raised
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
688 # when a ZipFile instance was created for a file that did not
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
689 # exist; the .fp member was not initialized but was needed by the
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
690 # __del__() method. Since the AttributeError is in the __del__(),
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
691 # it is ignored, but the user should be sufficiently annoyed by
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
692 # the message on the output that regression will be noticed
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
693 # quickly.
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
694 self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
29625
7d41785fb60c SF patch #756996: Bare except in ZipFile.testzip()
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
695
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
696 def testClosedZipRaisesRuntimeError(self):
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
697 # Verify that testzip() doesn't swallow inappropriate exceptions.
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
698 data = StringIO()
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
699 zipf = zipfile.ZipFile(data, mode="w")
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
700 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
701 zipf.close()
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
702
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
703 # This is correct; calling .read on a closed ZipFile should throw
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
704 # a RuntimeError, and so should calling .testzip. An earlier
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
705 # version of .testzip would swallow this exception (and any other)
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
706 # and report that the first file in the archive was corrupt.
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
707 self.assertRaises(RuntimeError, zipf.read, "foo.txt")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
708 self.assertRaises(RuntimeError, zipf.open, "foo.txt")
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
709 self.assertRaises(RuntimeError, zipf.testzip)
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
710 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
711 file(TESTFN, 'w').write('zipfile test data')
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
712 self.assertRaises(RuntimeError, zipf.write, TESTFN)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
713
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
714 def test_BadConstructorMode(self):
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
715 # Check that bad modes passed to ZipFile constructor are caught
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
716 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
717
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
718 def test_BadOpenMode(self):
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
719 # Check that bad modes passed to ZipFile.open are caught
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
720 zipf = zipfile.ZipFile(TESTFN, mode="w")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
721 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
722 zipf.close()
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
723 zipf = zipfile.ZipFile(TESTFN, mode="r")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
724 # read the data to make sure the file is there
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
725 zipf.read("foo.txt")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
726 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
727 zipf.close()
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
728
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
729 def test_Read0(self):
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
730 # Check that calling read(0) on a ZipExtFile object returns an empty
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
731 # string and doesn't advance file pointer
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
732 zipf = zipfile.ZipFile(TESTFN, mode="w")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
733 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
734 # read the data to make sure the file is there
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
735 f = zipf.open("foo.txt")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
736 for i in xrange(FIXEDTEST_SIZE):
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
737 self.assertEqual(f.read(0), '')
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
738
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
739 self.assertEqual(f.read(), "O, for a Muse of Fire!")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
740 zipf.close()
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
741
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
742 def test_OpenNonexistentItem(self):
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
743 # Check that attempting to call open() for an item that doesn't
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
744 # exist in the archive raises a RuntimeError
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
745 zipf = zipfile.ZipFile(TESTFN, mode="w")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
746 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
747
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
748 def test_BadCompressionMode(self):
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
749 # Check that bad compression methods passed to ZipFile.open are caught
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
750 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
751
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
752 def test_NullByteInFilename(self):
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
753 # Check that a filename containing a null byte is properly terminated
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
754 zipf = zipfile.ZipFile(TESTFN, mode="w")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
755 zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!")
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
756 self.assertEqual(zipf.namelist(), ['foo.txt'])
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41319
diff changeset
757
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
758 def test_StructSizes(self):
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
759 # check that ZIP internal structure sizes are calculated correctly
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
760 self.assertEqual(zipfile.sizeEndCentDir, 22)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
761 self.assertEqual(zipfile.sizeCentralDir, 46)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
762 self.assertEqual(zipfile.sizeEndCentDir64, 56)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
763 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
764
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
765 def testComments(self):
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
766 # This test checks that comments on the archive are handled properly
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
767
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
768 # check default comment is empty
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
769 zipf = zipfile.ZipFile(TESTFN, mode="w")
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
770 self.assertEqual(zipf.comment, '')
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
771 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
772 zipf.close()
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
773 zipfr = zipfile.ZipFile(TESTFN, mode="r")
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
774 self.assertEqual(zipfr.comment, '')
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
775 zipfr.close()
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
776
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
777 # check a simple short comment
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
778 comment = 'Bravely taking to his feet, he beat a very brave retreat.'
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
779 zipf = zipfile.ZipFile(TESTFN, mode="w")
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
780 zipf.comment = comment
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
781 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
782 zipf.close()
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
783 zipfr = zipfile.ZipFile(TESTFN, mode="r")
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
784 self.assertEqual(zipfr.comment, comment)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
785 zipfr.close()
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
786
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
787 # check a comment of max length
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
788 comment2 = ''.join(['%d' % (i**3 % 10) for i in xrange((1 << 16)-1)])
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
789 zipf = zipfile.ZipFile(TESTFN, mode="w")
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
790 zipf.comment = comment2
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
791 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
792 zipf.close()
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
793 zipfr = zipfile.ZipFile(TESTFN, mode="r")
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
794 self.assertEqual(zipfr.comment, comment2)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
795 zipfr.close()
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
796
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
797 # check a comment that is too long is truncated
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
798 zipf = zipfile.ZipFile(TESTFN, mode="w")
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
799 zipf.comment = comment2 + 'oops'
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
800 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
801 zipf.close()
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
802 zipfr = zipfile.ZipFile(TESTFN, mode="r")
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
803 self.assertEqual(zipfr.comment, comment2)
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
804 zipfr.close()
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
805
41319
f57e8e325475 Make test_zipfile clean up its temporary files properly.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
806 def tearDown(self):
f57e8e325475 Make test_zipfile clean up its temporary files properly.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
807 support.unlink(TESTFN)
f57e8e325475 Make test_zipfile clean up its temporary files properly.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
808 support.unlink(TESTFN2)
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
809
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
810 class DecryptionTests(unittest.TestCase):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
811 # This test checks that ZIP decryption works. Since the library does not
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
812 # support encryption at the moment, we use a pre-generated encrypted
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
813 # ZIP file
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
814
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
815 data = (
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
816 'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
817 '\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
818 '\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
819 'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
820 '\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
821 '\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
822 '\x00\x00L\x00\x00\x00\x00\x00' )
44807
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
823 data2 = (
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
824 'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
825 '\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
826 '\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
827 'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
828 '\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
829 '\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
830 'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
831 '\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
832
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
833 plain = 'zipfile.py encryption test'
44807
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
834 plain2 = '\x00'*512
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
835
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
836 def setUp(self):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
837 fp = open(TESTFN, "wb")
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
838 fp.write(self.data)
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
839 fp.close()
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
840 self.zip = zipfile.ZipFile(TESTFN, "r")
44807
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
841 fp = open(TESTFN2, "wb")
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
842 fp.write(self.data2)
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
843 fp.close()
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
844 self.zip2 = zipfile.ZipFile(TESTFN2, "r")
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
845
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
846 def tearDown(self):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
847 self.zip.close()
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
848 os.unlink(TESTFN)
44807
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
849 self.zip2.close()
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
850 os.unlink(TESTFN2)
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
851
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
852 def testNoPassword(self):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
853 # Reading the encrypted file without password
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
854 # must generate a RunTime exception
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
855 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
44807
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
856 self.assertRaises(RuntimeError, self.zip2.read, "zero")
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
857
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
858 def testBadPassword(self):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
859 self.zip.setpassword("perl")
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
860 self.assertRaises(RuntimeError, self.zip.read, "test.txt")
44807
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
861 self.zip2.setpassword("perl")
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
862 self.assertRaises(RuntimeError, self.zip2.read, "zero")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
863
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
864 def testGoodPassword(self):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
865 self.zip.setpassword("python")
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
866 self.assertEquals(self.zip.read("test.txt"), self.plain)
44807
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
867 self.zip2.setpassword("12345")
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
868 self.assertEquals(self.zip2.read("zero"), self.plain2)
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
869
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
870
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
871 class TestsWithRandomBinaryFiles(unittest.TestCase):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
872 def setUp(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
873 datacount = randint(16, 64)*1024 + randint(1, 1024)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
874 self.data = ''.join((struct.pack('<f', random()*randint(-1000, 1000)) for i in xrange(datacount)))
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
875
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
876 # Make a source file with some lines
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
877 fp = open(TESTFN, "wb")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
878 fp.write(self.data)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
879 fp.close()
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41319
diff changeset
880
41319
f57e8e325475 Make test_zipfile clean up its temporary files properly.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
881 def tearDown(self):
f57e8e325475 Make test_zipfile clean up its temporary files properly.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
882 support.unlink(TESTFN)
f57e8e325475 Make test_zipfile clean up its temporary files properly.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
883 support.unlink(TESTFN2)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
884
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
885 def makeTestArchive(self, f, compression):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
886 # Create the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
887 zipfp = zipfile.ZipFile(f, "w", compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
888 zipfp.write(TESTFN, "another"+os.extsep+"name")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
889 zipfp.write(TESTFN, TESTFN)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
890 zipfp.close()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
891
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
892 def zipTest(self, f, compression):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
893 self.makeTestArchive(f, compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
894
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
895 # Read the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
896 zipfp = zipfile.ZipFile(f, "r", compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
897 testdata = zipfp.read(TESTFN)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
898 self.assertEqual(len(testdata), len(self.data))
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
899 self.assertEqual(testdata, self.data)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
900 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
901 zipfp.close()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
902
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
903 def testStored(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
904 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
905 self.zipTest(f, zipfile.ZIP_STORED)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
906
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
907 def zipOpenTest(self, f, compression):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
908 self.makeTestArchive(f, compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
909
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
910 # Read the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
911 zipfp = zipfile.ZipFile(f, "r", compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
912 zipdata1 = []
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
913 zipopen1 = zipfp.open(TESTFN)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
914 while 1:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
915 read_data = zipopen1.read(256)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
916 if not read_data:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
917 break
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
918 zipdata1.append(read_data)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
919
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
920 zipdata2 = []
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
921 zipopen2 = zipfp.open("another"+os.extsep+"name")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
922 while 1:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
923 read_data = zipopen2.read(256)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
924 if not read_data:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
925 break
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
926 zipdata2.append(read_data)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
927
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
928 testdata1 = ''.join(zipdata1)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
929 self.assertEqual(len(testdata1), len(self.data))
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
930 self.assertEqual(testdata1, self.data)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
931
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
932 testdata2 = ''.join(zipdata2)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
933 self.assertEqual(len(testdata1), len(self.data))
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
934 self.assertEqual(testdata1, self.data)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
935 zipfp.close()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
936
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
937 def testOpenStored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
938 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
939 self.zipOpenTest(f, zipfile.ZIP_STORED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
940
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
941 def zipRandomOpenTest(self, f, compression):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
942 self.makeTestArchive(f, compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
943
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
944 # Read the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
945 zipfp = zipfile.ZipFile(f, "r", compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
946 zipdata1 = []
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
947 zipopen1 = zipfp.open(TESTFN)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
948 while 1:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
949 read_data = zipopen1.read(randint(1, 1024))
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
950 if not read_data:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
951 break
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
952 zipdata1.append(read_data)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
953
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
954 testdata = ''.join(zipdata1)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
955 self.assertEqual(len(testdata), len(self.data))
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
956 self.assertEqual(testdata, self.data)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
957 zipfp.close()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
958
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
959 def testRandomOpenStored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
960 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
961 self.zipRandomOpenTest(f, zipfile.ZIP_STORED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
962
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
963 class TestsWithMultipleOpens(unittest.TestCase):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
964 def setUp(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
965 # Create the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
966 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
967 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
968 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
969 zipfp.close()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
970
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
971 def testSameFile(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
972 # Verify that (when the ZipFile is in control of creating file objects)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
973 # multiple open() calls can be made without interfering with each other.
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
974 zipf = zipfile.ZipFile(TESTFN2, mode="r")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
975 zopen1 = zipf.open('ones')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
976 zopen2 = zipf.open('ones')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
977 data1 = zopen1.read(500)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
978 data2 = zopen2.read(500)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
979 data1 += zopen1.read(500)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
980 data2 += zopen2.read(500)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
981 self.assertEqual(data1, data2)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
982 zipf.close()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
983
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
984 def testDifferentFile(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
985 # Verify that (when the ZipFile is in control of creating file objects)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
986 # multiple open() calls can be made without interfering with each other.
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
987 zipf = zipfile.ZipFile(TESTFN2, mode="r")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
988 zopen1 = zipf.open('ones')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
989 zopen2 = zipf.open('twos')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
990 data1 = zopen1.read(500)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
991 data2 = zopen2.read(500)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
992 data1 += zopen1.read(500)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
993 data2 += zopen2.read(500)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
994 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
995 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
996 zipf.close()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
997
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
998 def testInterleaved(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
999 # Verify that (when the ZipFile is in control of creating file objects)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1000 # multiple open() calls can be made without interfering with each other.
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1001 zipf = zipfile.ZipFile(TESTFN2, mode="r")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1002 zopen1 = zipf.open('ones')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1003 data1 = zopen1.read(500)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1004 zopen2 = zipf.open('twos')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1005 data2 = zopen2.read(500)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1006 data1 += zopen1.read(500)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1007 data2 += zopen2.read(500)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1008 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1009 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1010 zipf.close()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1011
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1012 def tearDown(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1013 os.remove(TESTFN2)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1014
50999
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1015 class TestWithDirectory(unittest.TestCase):
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1016 def setUp(self):
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1017 os.mkdir(TESTFN2)
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1018
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1019 def testExtractDir(self):
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1020 zipf = zipfile.ZipFile(findfile("zipdir.zip"))
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1021 zipf.extractall(TESTFN2)
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1022 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1023 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1024 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1025
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1026 def testStoreDir(self):
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1027 os.mkdir(os.path.join(TESTFN2, "x"))
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1028 zipf = zipfile.ZipFile(TESTFN, "w")
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1029 zipf.write(os.path.join(TESTFN2, "x"), "x")
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1030 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1031
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1032 def tearDown(self):
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1033 shutil.rmtree(TESTFN2)
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1034 if os.path.exists(TESTFN):
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1035 os.remove(TESTFN)
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1036
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1037
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1038 class UniversalNewlineTests(unittest.TestCase):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1039 def setUp(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1040 self.line_gen = ["Test of zipfile line %d." % i for i in xrange(FIXEDTEST_SIZE)]
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1041 self.seps = ('\r', '\r\n', '\n')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1042 self.arcdata, self.arcfiles = {}, {}
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1043 for n, s in enumerate(self.seps):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1044 self.arcdata[s] = s.join(self.line_gen) + s
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1045 self.arcfiles[s] = '%s-%d' % (TESTFN, n)
41881
3877f37c9861 Remove direct call's to file's constructor and replace them with calls to
Brett Cannon <bcannon@gmail.com>
parents: 41532
diff changeset
1046 open(self.arcfiles[s], "wb").write(self.arcdata[s])
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1047
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1048 def makeTestArchive(self, f, compression):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1049 # Create the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1050 zipfp = zipfile.ZipFile(f, "w", compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1051 for fn in self.arcfiles.values():
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1052 zipfp.write(fn, fn)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1053 zipfp.close()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1054
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1055 def readTest(self, f, compression):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1056 self.makeTestArchive(f, compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1057
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1058 # Read the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1059 zipfp = zipfile.ZipFile(f, "r")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1060 for sep, fn in self.arcfiles.items():
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1061 zipdata = zipfp.open(fn, "rU").read()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1062 self.assertEqual(self.arcdata[sep], zipdata)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1063
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1064 zipfp.close()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1065
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1066 def readlineTest(self, f, compression):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1067 self.makeTestArchive(f, compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1068
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1069 # Read the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1070 zipfp = zipfile.ZipFile(f, "r")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1071 for sep, fn in self.arcfiles.items():
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1072 zipopen = zipfp.open(fn, "rU")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1073 for line in self.line_gen:
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1074 linedata = zipopen.readline()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1075 self.assertEqual(linedata, line + '\n')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1076
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1077 zipfp.close()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1078
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1079 def readlinesTest(self, f, compression):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1080 self.makeTestArchive(f, compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1081
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1082 # Read the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1083 zipfp = zipfile.ZipFile(f, "r")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1084 for sep, fn in self.arcfiles.items():
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1085 ziplines = zipfp.open(fn, "rU").readlines()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1086 for line, zipline in zip(self.line_gen, ziplines):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1087 self.assertEqual(zipline, line + '\n')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1088
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1089 zipfp.close()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1090
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1091 def iterlinesTest(self, f, compression):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1092 self.makeTestArchive(f, compression)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1093
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1094 # Read the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1095 zipfp = zipfile.ZipFile(f, "r")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1096 for sep, fn in self.arcfiles.items():
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1097 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1098 self.assertEqual(zipline, line + '\n')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1099
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1100 zipfp.close()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1101
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1102 def testReadStored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1103 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1104 self.readTest(f, zipfile.ZIP_STORED)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1105
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1106 def testReadlineStored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1107 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1108 self.readlineTest(f, zipfile.ZIP_STORED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1109
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1110 def testReadlinesStored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1111 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1112 self.readlinesTest(f, zipfile.ZIP_STORED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1113
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1114 def testIterlinesStored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1115 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1116 self.iterlinesTest(f, zipfile.ZIP_STORED)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1117
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1118 if zlib:
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1119 def testReadDeflated(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1120 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1121 self.readTest(f, zipfile.ZIP_DEFLATED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1122
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1123 def testReadlineDeflated(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1124 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1125 self.readlineTest(f, zipfile.ZIP_DEFLATED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1126
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1127 def testReadlinesDeflated(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1128 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1129 self.readlinesTest(f, zipfile.ZIP_DEFLATED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1130
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1131 def testIterlinesDeflated(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1132 for f in (TESTFN2, TemporaryFile(), StringIO()):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1133 self.iterlinesTest(f, zipfile.ZIP_DEFLATED)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1134
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1135 def tearDown(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1136 for sep, fn in self.arcfiles.items():
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1137 os.remove(fn)
41319
f57e8e325475 Make test_zipfile clean up its temporary files properly.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
1138 support.unlink(TESTFN)
f57e8e325475 Make test_zipfile clean up its temporary files properly.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
1139 support.unlink(TESTFN2)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1140
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1141
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
1142 def test_main():
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1143 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1144 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
50999
fc38f2ecd2ac Issue #4710: Extract directories properly in the zipfile module;
Martin v. Löwis <martin@v.loewis.de>
parents: 50300
diff changeset
1145 TestWithDirectory,
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1146 UniversalNewlineTests, TestsWithRandomBinaryFiles)
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1147
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
1148 if __name__ == "__main__":
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
1149 test_main()