annotate Lib/test/test_zipfile.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents ac0ec1f31b0a
children 8feaa8d04c56
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
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
7 import os
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
8 import io
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
9 import sys
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
10 import time
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
11 import shutil
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
12 import struct
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
13 import zipfile
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
14 import unittest
17224
f9395f7627c4 Itamar Shtull-Trauring <itamar@maxnm.com>:
Fred Drake <fdrake@acm.org>
parents: 16827
diff changeset
15
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
16 from StringIO import StringIO
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
17 from tempfile import TemporaryFile
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
18 from random import randint, random
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
19 from unittest import skipUnless
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
20
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
21 from test.test_support import TESTFN, run_unittest, findfile, unlink
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
22
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
23 TESTFN2 = TESTFN + "2"
51000
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
24 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
25 FIXEDTEST_SIZE = 1000
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
26
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
27 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
28 ('ziptest2dir/_ziptest2', 'qawsedrftg'),
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
29 ('/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
30 ('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
31
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
32
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
33 class TestsWithSourceFile(unittest.TestCase):
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
34 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
35 self.line_gen = ["Zipfile test line %d. random float: %f" % (i, random())
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
36 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
37 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
38
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
39 # Make a source file with some lines
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
40 with open(TESTFN, "wb") as fp:
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
41 fp.write(self.data)
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
42
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
43 def make_test_archive(self, f, compression):
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
44 # Create the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
45 with zipfile.ZipFile(f, "w", compression) as zipfp:
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
46 zipfp.write(TESTFN, "another.name")
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
47 zipfp.write(TESTFN, TESTFN)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
48 zipfp.writestr("strfile", self.data)
17245
0377f927cafa Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17224
diff changeset
49
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
50 def zip_test(self, f, compression):
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
51 self.make_test_archive(f, compression)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
52
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
53 # Read the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
54 with zipfile.ZipFile(f, "r", compression) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
55 self.assertEqual(zipfp.read(TESTFN), self.data)
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
56 self.assertEqual(zipfp.read("another.name"), self.data)
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
57 self.assertEqual(zipfp.read("strfile"), self.data)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
58
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
59 # Print the ZIP directory
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
60 fp = StringIO()
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
61 stdout = sys.stdout
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
62 try:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
63 sys.stdout = fp
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
64 zipfp.printdir()
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
65 finally:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
66 sys.stdout = stdout
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
67
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
68 directory = fp.getvalue()
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
69 lines = directory.splitlines()
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
70 self.assertEqual(len(lines), 4) # Number of files + header
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
71
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
72 self.assertIn('File Name', lines[0])
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
73 self.assertIn('Modified', lines[0])
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
74 self.assertIn('Size', lines[0])
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
75
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
76 fn, date, time_, size = lines[1].split()
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
77 self.assertEqual(fn, 'another.name')
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
78 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
79 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
80 self.assertEqual(size, str(len(self.data)))
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
81
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
82 # Check the namelist
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
83 names = zipfp.namelist()
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
84 self.assertEqual(len(names), 3)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
85 self.assertIn(TESTFN, names)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
86 self.assertIn("another.name", names)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
87 self.assertIn("strfile", names)
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
88
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
89 # Check infolist
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
90 infos = zipfp.infolist()
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
91 names = [i.filename for i in infos]
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
92 self.assertEqual(len(names), 3)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
93 self.assertIn(TESTFN, names)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
94 self.assertIn("another.name", names)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
95 self.assertIn("strfile", names)
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
96 for i in infos:
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
97 self.assertEqual(i.file_size, len(self.data))
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
98
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
99 # check getinfo
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
100 for nm in (TESTFN, "another.name", "strfile"):
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
101 info = zipfp.getinfo(nm)
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
102 self.assertEqual(info.filename, nm)
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
103 self.assertEqual(info.file_size, len(self.data))
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
104
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
105 # Check that testzip doesn't raise an exception
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
106 zipfp.testzip()
12323
1a207c68e8b4 Test for zipfile.py, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
108 def test_stored(self):
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
109 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
110 self.zip_test(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
111
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
112 def zip_open_test(self, f, compression):
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
113 self.make_test_archive(f, compression)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
114
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
115 # Read the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
116 with zipfile.ZipFile(f, "r", compression) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
117 zipdata1 = []
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
118 with zipfp.open(TESTFN) as zipopen1:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
119 while True:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
120 read_data = zipopen1.read(256)
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
121 if not read_data:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
122 break
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
123 zipdata1.append(read_data)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
124
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
125 zipdata2 = []
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
126 with zipfp.open("another.name") as zipopen2:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
127 while True:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
128 read_data = zipopen2.read(256)
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
129 if not read_data:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
130 break
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
131 zipdata2.append(read_data)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
132
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
133 self.assertEqual(''.join(zipdata1), self.data)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
134 self.assertEqual(''.join(zipdata2), self.data)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
135
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
136 def test_open_stored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
137 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
138 self.zip_open_test(f, zipfile.ZIP_STORED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
139
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
140 def test_open_via_zip_info(self):
47195
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
141 # Create the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
142 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
143 zipfp.writestr("name", "foo")
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
144 zipfp.writestr("name", "bar")
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
145
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
146 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
147 infos = zipfp.infolist()
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
148 data = ""
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
149 for info in infos:
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
150 with zipfp.open(info) as f:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
151 data += f.read()
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
152 self.assertTrue(data == "foobar" or data == "barfoo")
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
153 data = ""
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
154 for info in infos:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
155 data += zipfp.read(info)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
156 self.assertTrue(data == "foobar" or data == "barfoo")
47195
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
157
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
158 def zip_random_open_test(self, f, compression):
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
159 self.make_test_archive(f, compression)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
160
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
161 # Read the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
162 with zipfile.ZipFile(f, "r", compression) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
163 zipdata1 = []
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
164 with zipfp.open(TESTFN) as zipopen1:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
165 while True:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
166 read_data = zipopen1.read(randint(1, 1024))
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
167 if not read_data:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
168 break
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
169 zipdata1.append(read_data)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
170
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
171 self.assertEqual(''.join(zipdata1), self.data)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
172
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
173 def test_random_open_stored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
174 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
175 self.zip_random_open_test(f, zipfile.ZIP_STORED)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
176
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
177 def test_univeral_readaheads(self):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
178 f = StringIO()
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
179
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
180 data = 'a\r\n' * 16 * 1024
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
181 with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as zipfp:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
182 zipfp.writestr(TESTFN, data)
12323
1a207c68e8b4 Test for zipfile.py, by Jim Ahlstrom.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
184 data2 = ''
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
185 with zipfile.ZipFile(f, 'r') as zipfp:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
186 with zipfp.open(TESTFN, 'rU') as zipopen:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
187 for line in zipopen:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
188 data2 += line
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
189
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
190 self.assertEqual(data, data2.replace('\n', '\r\n'))
47195
52a66373437d Patch #1775025: allow opening zipfile members via ZipInfo instances.
Georg Brandl <georg@python.org>
parents: 46673
diff changeset
191
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
192 def zip_readline_read_test(self, f, compression):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
193 self.make_test_archive(f, compression)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
194
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
195 # Read the ZIP archive
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
196 with zipfile.ZipFile(f, "r") as zipfp:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
197 with zipfp.open(TESTFN) as zipopen:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
198 data = ''
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
199 while True:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
200 read = zipopen.readline()
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
201 if not read:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
202 break
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
203 data += read
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
204
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
205 read = zipopen.read(100)
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
206 if not read:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
207 break
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
208 data += read
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
209
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
210 self.assertEqual(data, self.data)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
211
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
212 def zip_readline_test(self, f, compression):
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
213 self.make_test_archive(f, compression)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
214
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
215 # Read the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
216 with zipfile.ZipFile(f, "r") as zipfp:
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
217 with zipfp.open(TESTFN) as zipopen:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
218 for line in self.line_gen:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
219 linedata = zipopen.readline()
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
220 self.assertEqual(linedata, line + '\n')
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
221
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
222 def zip_readlines_test(self, f, compression):
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
223 self.make_test_archive(f, compression)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
224
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
225 # Read the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
226 with zipfile.ZipFile(f, "r") as zipfp:
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
227 with zipfp.open(TESTFN) as zo:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
228 ziplines = zo.readlines()
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
229 for line, zipline in zip(self.line_gen, ziplines):
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
230 self.assertEqual(zipline, line + '\n')
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
231
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
232 def zip_iterlines_test(self, f, compression):
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
233 self.make_test_archive(f, compression)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
234
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
235 # Read the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
236 with zipfile.ZipFile(f, "r") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
237 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
238 self.assertEqual(zipline, line + '\n')
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
239
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
240 def test_readline_read_stored(self):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
241 # Issue #7610: calls to readline() interleaved with calls to read().
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()):
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
243 self.zip_readline_read_test(f, zipfile.ZIP_STORED)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
244
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
245 def test_readline_stored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
246 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
247 self.zip_readline_test(f, zipfile.ZIP_STORED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
248
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
249 def test_readlines_stored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
250 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
251 self.zip_readlines_test(f, zipfile.ZIP_STORED)
17312
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
252
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
253 def test_iterlines_stored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
254 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
255 self.zip_iterlines_test(f, zipfile.ZIP_STORED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
256
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
257 @skipUnless(zlib, "requires zlib")
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
258 def test_deflated(self):
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
259 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
260 self.zip_test(f, zipfile.ZIP_DEFLATED)
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
261
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
262 @skipUnless(zlib, "requires zlib")
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
263 def test_open_deflated(self):
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
264 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
265 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
266
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
267 @skipUnless(zlib, "requires zlib")
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
268 def test_random_open_deflated(self):
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
269 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
270 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
271
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
272 @skipUnless(zlib, "requires zlib")
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
273 def test_readline_read_deflated(self):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
274 # Issue #7610: calls to readline() interleaved with calls to read().
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
275 for f in (TESTFN2, TemporaryFile(), StringIO()):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
276 self.zip_readline_read_test(f, zipfile.ZIP_DEFLATED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
277
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
278 @skipUnless(zlib, "requires zlib")
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
279 def test_readline_deflated(self):
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
280 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
281 self.zip_readline_test(f, zipfile.ZIP_DEFLATED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
282
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
283 @skipUnless(zlib, "requires zlib")
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
284 def test_readlines_deflated(self):
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
285 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
286 self.zip_readlines_test(f, zipfile.ZIP_DEFLATED)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
287
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
288 @skipUnless(zlib, "requires zlib")
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
289 def test_iterlines_deflated(self):
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
290 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
291 self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
292
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
293 @skipUnless(zlib, "requires zlib")
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
294 def test_low_compression(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
295 """Check for cases where compressed data is larger than original."""
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
296 # Create the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
297 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
298 zipfp.writestr("strfile", '12')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
299
55107
44fcdbb62038 if zlib -> skipUnless(zlib) and minor cleanups
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
300 # Get an open object for strfile
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
301 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp:
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
302 with zipfp.open("strfile") as openobj:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
303 self.assertEqual(openobj.read(1), '1')
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
304 self.assertEqual(openobj.read(1), '2')
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
305
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
306 def test_absolute_arcnames(self):
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
307 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
308 zipfp.write(TESTFN, "/absolute")
36609
46ec948cbde8 Bug #1413790: zipfile now sanitizes absolute archive names that are
Georg Brandl <georg@python.org>
parents: 33204
diff changeset
309
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
310 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
311 self.assertEqual(zipfp.namelist(), ["absolute"])
36630
eb0977932e2d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 36609
diff changeset
312
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
313 def test_append_to_zip_file(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
314 """Test appending to an existing zipfile."""
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
315 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
316 zipfp.write(TESTFN, TESTFN)
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
317
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
318 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
319 zipfp.writestr("strfile", self.data)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
320 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
321
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
322 def test_append_to_non_zip_file(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
323 """Test appending to an existing file that is not a zipfile."""
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
324 # 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
325 # line "fpin.seek(-22, 2)" in _EndRecData
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
326 data = 'I am not a ZipFile!'*10
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
327 with open(TESTFN2, 'wb') as f:
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
328 f.write(data)
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
329
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
330 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
331 zipfp.write(TESTFN, TESTFN)
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
332
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
333 with open(TESTFN2, 'rb') as f:
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
334 f.seek(len(data))
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
335 with zipfile.ZipFile(f, "r") as zipfp:
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
336 self.assertEqual(zipfp.namelist(), [TESTFN])
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
337
70748
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
338 def test_ignores_newline_at_end(self):
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
339 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
340 zipfp.write(TESTFN, TESTFN)
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
341 with open(TESTFN2, 'a') as f:
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
342 f.write("\r\n\00\00\00")
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
343 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
344 self.assertIsInstance(zipfp, zipfile.ZipFile)
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
345
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
346 def test_ignores_stuff_appended_past_comments(self):
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
347 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
348 zipfp.comment = b"this is a comment"
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
349 zipfp.write(TESTFN, TESTFN)
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
350 with open(TESTFN2, 'a') as f:
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
351 f.write("abcdef\r\n")
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
352 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
353 self.assertIsInstance(zipfp, zipfile.ZipFile)
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
354 self.assertEqual(zipfp.comment, b"this is a comment")
cc3255a707c7 #10694: zipfile now ignores garbage at the end of a zipfile.
R David Murray <rdmurray@bitdance.com>
parents: 69464
diff changeset
355
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
356 def test_write_default_name(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
357 """Check that calling ZipFile.write without arcname specified
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
358 produces the expected result."""
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
359 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
360 zipfp.write(TESTFN)
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
361 self.assertEqual(zipfp.read(TESTFN), open(TESTFN).read())
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
362
55920
39be3ef82d23 #6026 - fix tests that failed without zlib
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55485
diff changeset
363 @skipUnless(zlib, "requires zlib")
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
364 def test_per_file_compression(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
365 """Check that files within a Zip archive can have different
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
366 compression options."""
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
367 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
368 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
369 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
370 sinfo = zipfp.getinfo('storeme')
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
371 dinfo = zipfp.getinfo('deflateme')
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
372 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
373 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
374
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
375 def test_write_to_readonly(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
376 """Check that trying to call write() on a readonly ZipFile object
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
377 raises a RuntimeError."""
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
378 with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
379 zipfp.writestr("somefile.txt", "bogus")
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
380
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
381 with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
382 self.assertRaises(RuntimeError, zipfp.write, TESTFN)
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
383
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
384 def test_extract(self):
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
385 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
386 for fpath, fdata in SMALL_TEST_DATA:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
387 zipfp.writestr(fpath, fdata)
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
388
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
389 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
390 for fpath, fdata in SMALL_TEST_DATA:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
391 writtenfile = zipfp.extract(fpath)
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
392
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
393 # make sure it was written to the right place
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
394 if os.path.isabs(fpath):
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
395 correctfile = os.path.join(os.getcwd(), fpath[1:])
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
396 else:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
397 correctfile = os.path.join(os.getcwd(), fpath)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
398 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
399
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
400 self.assertEqual(writtenfile, correctfile)
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
401
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
402 # make sure correct data is in correct file
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
403 self.assertEqual(fdata, open(writtenfile, "rb").read())
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
404 os.remove(writtenfile)
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
405
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
406 # 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
407 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
408
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
409 def test_extract_all(self):
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
410 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
411 for fpath, fdata in SMALL_TEST_DATA:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
412 zipfp.writestr(fpath, fdata)
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
413
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
414 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
415 zipfp.extractall()
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
416 for fpath, fdata in SMALL_TEST_DATA:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
417 if os.path.isabs(fpath):
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
418 outfile = os.path.join(os.getcwd(), fpath[1:])
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
419 else:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
420 outfile = os.path.join(os.getcwd(), fpath)
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
421
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
422 self.assertEqual(fdata, open(outfile, "rb").read())
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
423 os.remove(outfile)
44576
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
424
addc18596a15 #467924, patch by Alan McIntyre: Add ZipFile.extract and ZipFile.extractall.
Georg Brandl <georg@python.org>
parents: 42225
diff changeset
425 # 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
426 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
427
58821
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58540
diff changeset
428 def test_writestr_compression(self):
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58540
diff changeset
429 zipfp = zipfile.ZipFile(TESTFN2, "w")
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58540
diff changeset
430 zipfp.writestr("a.txt", "hello world", compress_type=zipfile.ZIP_STORED)
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58540
diff changeset
431 if zlib:
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58540
diff changeset
432 zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_DEFLATED)
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58540
diff changeset
433
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58540
diff changeset
434 info = zipfp.getinfo('a.txt')
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58540
diff changeset
435 self.assertEqual(info.compress_type, zipfile.ZIP_STORED)
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58540
diff changeset
436
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58540
diff changeset
437 if zlib:
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58540
diff changeset
438 info = zipfp.getinfo('b.txt')
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58540
diff changeset
439 self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED)
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58540
diff changeset
440
a280844b68f4 Issue 6003: ZipFile.writestr "compression_type" argument
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58540
diff changeset
441
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
442 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
443 # 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
444 # 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
445
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
446 self.make_test_archive(f, compression)
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
447 with zipfile.ZipFile(f, "r") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
448 zinfo = zipfp.getinfo('strfile')
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
449 self.assertEqual(zinfo.external_attr, 0600 << 16)
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
450
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
451 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
452 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
453 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
454
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
455 def test_close(self):
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
456 """Check that the zipfile is closed after the 'with' block."""
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
457 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
458 for fpath, fdata in SMALL_TEST_DATA:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
459 zipfp.writestr(fpath, fdata)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
460 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
461 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
462
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
463 with zipfile.ZipFile(TESTFN2, "r") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
464 self.assertTrue(zipfp.fp is not None, 'zipfp is not open')
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
465 self.assertTrue(zipfp.fp is None, 'zipfp is not closed')
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
466
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
467 def test_close_on_exception(self):
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
468 """Check that the zipfile is closed if an exception is raised in the
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
469 'with' block."""
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
470 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
471 for fpath, fdata in SMALL_TEST_DATA:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
472 zipfp.writestr(fpath, fdata)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
473
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
474 try:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
475 with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
476 raise zipfile.BadZipfile()
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
477 except zipfile.BadZipfile:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
478 self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
479
72991
649ac338203f Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
Senthil Kumaran <senthil@uthcode.com>
parents: 70748
diff changeset
480 def test_add_file_before_1980(self):
649ac338203f Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
Senthil Kumaran <senthil@uthcode.com>
parents: 70748
diff changeset
481 # Set atime and mtime to 1970-01-01
649ac338203f Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
Senthil Kumaran <senthil@uthcode.com>
parents: 70748
diff changeset
482 os.utime(TESTFN, (0, 0))
649ac338203f Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
Senthil Kumaran <senthil@uthcode.com>
parents: 70748
diff changeset
483 with zipfile.ZipFile(TESTFN2, "w") as zipfp:
649ac338203f Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
Senthil Kumaran <senthil@uthcode.com>
parents: 70748
diff changeset
484 self.assertRaises(ValueError, zipfp.write, TESTFN)
649ac338203f Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
Senthil Kumaran <senthil@uthcode.com>
parents: 70748
diff changeset
485
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
486 def tearDown(self):
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
487 unlink(TESTFN)
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
488 unlink(TESTFN2)
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
489
17312
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
490
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
491 class TestZip64InSmallFiles(unittest.TestCase):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
492 # 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
493 # see test_zipfile64 for proper tests.
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
494
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
495 def setUp(self):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
496 self._limit = zipfile.ZIP64_LIMIT
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
497 zipfile.ZIP64_LIMIT = 5
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
498
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
499 line_gen = ("Test of zipfile line %d." % i
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
500 for i in range(0, FIXEDTEST_SIZE))
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
501 self.data = '\n'.join(line_gen)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
502
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
503 # Make a source file with some lines
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
504 with open(TESTFN, "wb") as fp:
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
505 fp.write(self.data)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
506
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
507 def large_file_exception_test(self, f, compression):
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
508 with zipfile.ZipFile(f, "w", compression) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
509 self.assertRaises(zipfile.LargeZipFile,
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
510 zipfp.write, TESTFN, "another.name")
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
511
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
512 def large_file_exception_test2(self, f, compression):
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
513 with zipfile.ZipFile(f, "w", compression) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
514 self.assertRaises(zipfile.LargeZipFile,
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
515 zipfp.writestr, "another.name", self.data)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
516
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
517 def test_large_file_exception(self):
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
518 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
519 self.large_file_exception_test(f, zipfile.ZIP_STORED)
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
520 self.large_file_exception_test2(f, zipfile.ZIP_STORED)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
521
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
522 def zip_test(self, f, compression):
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
523 # Create the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
524 with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp:
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
525 zipfp.write(TESTFN, "another.name")
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
526 zipfp.write(TESTFN, TESTFN)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
527 zipfp.writestr("strfile", self.data)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
528
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
529 # Read the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
530 with zipfile.ZipFile(f, "r", compression) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
531 self.assertEqual(zipfp.read(TESTFN), self.data)
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
532 self.assertEqual(zipfp.read("another.name"), self.data)
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
533 self.assertEqual(zipfp.read("strfile"), self.data)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
534
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
535 # Print the ZIP directory
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
536 fp = StringIO()
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
537 stdout = sys.stdout
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
538 try:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
539 sys.stdout = fp
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
540 zipfp.printdir()
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
541 finally:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
542 sys.stdout = stdout
38845
cacd47a61e79 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38838
diff changeset
543
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
544 directory = fp.getvalue()
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
545 lines = directory.splitlines()
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
546 self.assertEqual(len(lines), 4) # Number of files + header
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
547
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
548 self.assertIn('File Name', lines[0])
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
549 self.assertIn('Modified', lines[0])
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
550 self.assertIn('Size', lines[0])
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
551
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
552 fn, date, time_, size = lines[1].split()
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
553 self.assertEqual(fn, 'another.name')
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
554 self.assertTrue(time.strptime(date, '%Y-%m-%d'))
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
555 self.assertTrue(time.strptime(time_, '%H:%M:%S'))
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
556 self.assertEqual(size, str(len(self.data)))
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
557
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
558 # Check the namelist
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
559 names = zipfp.namelist()
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
560 self.assertEqual(len(names), 3)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
561 self.assertIn(TESTFN, names)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
562 self.assertIn("another.name", names)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
563 self.assertIn("strfile", names)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
564
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
565 # Check infolist
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
566 infos = zipfp.infolist()
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
567 names = [i.filename for i in infos]
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
568 self.assertEqual(len(names), 3)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
569 self.assertIn(TESTFN, names)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
570 self.assertIn("another.name", names)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
571 self.assertIn("strfile", names)
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
572 for i in infos:
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
573 self.assertEqual(i.file_size, len(self.data))
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
574
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
575 # check getinfo
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
576 for nm in (TESTFN, "another.name", "strfile"):
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
577 info = zipfp.getinfo(nm)
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
578 self.assertEqual(info.filename, nm)
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
579 self.assertEqual(info.file_size, len(self.data))
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
580
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
581 # Check that testzip doesn't raise an exception
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
582 zipfp.testzip()
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
583
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
584 def test_stored(self):
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
585 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
586 self.zip_test(f, zipfile.ZIP_STORED)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
587
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
588 @skipUnless(zlib, "requires zlib")
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
589 def test_deflated(self):
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
590 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
591 self.zip_test(f, zipfile.ZIP_DEFLATED)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
592
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
593 def test_absolute_arcnames(self):
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
594 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED,
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
595 allowZip64=True) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
596 zipfp.write(TESTFN, "/absolute")
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
597
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
598 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
599 self.assertEqual(zipfp.namelist(), ["absolute"])
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
600
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
601 def tearDown(self):
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
602 zipfile.ZIP64_LIMIT = self._limit
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
603 unlink(TESTFN)
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
604 unlink(TESTFN2)
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
605
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
606
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
607 class PyZipFileTests(unittest.TestCase):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
608 def test_write_pyfile(self):
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
609 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
610 fn = __file__
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
611 if fn.endswith('.pyc') or fn.endswith('.pyo'):
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
612 fn = fn[:-1]
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
613
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
614 zipfp.writepy(fn)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
615
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
616 bn = os.path.basename(fn)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
617 self.assertNotIn(bn, zipfp.namelist())
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
618 self.assertTrue(bn + 'o' in zipfp.namelist() or
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
619 bn + 'c' in zipfp.namelist())
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
620
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
621 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
622 fn = __file__
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
623 if fn.endswith(('.pyc', '.pyo')):
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
624 fn = fn[:-1]
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
625
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
626 zipfp.writepy(fn, "testpackage")
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
627
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
628 bn = "%s/%s" % ("testpackage", os.path.basename(fn))
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
629 self.assertNotIn(bn, zipfp.namelist())
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
630 self.assertTrue(bn + 'o' in zipfp.namelist() or
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
631 bn + 'c' in zipfp.namelist())
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
632
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
633 def test_write_python_package(self):
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
634 import email
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
635 packagedir = os.path.dirname(email.__file__)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
636
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
637 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
638 zipfp.writepy(packagedir)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
639
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
640 # Check for a couple of modules at different levels of the
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
641 # hierarchy
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
642 names = zipfp.namelist()
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
643 self.assertTrue('email/__init__.pyo' in names or
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
644 'email/__init__.pyc' in names)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
645 self.assertTrue('email/mime/text.pyo' in names or
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
646 'email/mime/text.pyc' in names)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
647
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
648 def test_write_python_directory(self):
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
649 os.mkdir(TESTFN2)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
650 try:
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
651 with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
58034
7c630931cc83 indentation and further alignment with py3k
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58032
diff changeset
652 fp.write("print(42)\n")
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
653
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
654 with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp:
58034
7c630931cc83 indentation and further alignment with py3k
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58032
diff changeset
655 fp.write("print(42 * 42)\n")
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
656
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
657 with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp:
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
658 fp.write("bla bla bla\n")
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
659
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
660 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
661 zipfp.writepy(TESTFN2)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
662
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
663 names = zipfp.namelist()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54311
diff changeset
664 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54311
diff changeset
665 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58034
diff changeset
666 self.assertNotIn('mod2.txt', names)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
667
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
668 finally:
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
669 shutil.rmtree(TESTFN2)
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
670
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
671 def test_write_non_pyfile(self):
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
672 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
673 open(TESTFN, 'w').write('most definitely not a python file')
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
674 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
675 os.remove(TESTFN)
38838
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
676
fd412a00a07d Patch #1446489 (zipfile: support for ZIP64)
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 36630
diff changeset
677
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
678 class OtherTests(unittest.TestCase):
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
679 zips_with_bad_crc = {
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
680 zipfile.ZIP_STORED: (
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
681 b'PK\003\004\024\0\0\0\0\0 \213\212;:r'
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
682 b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af'
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
683 b'ilehello,AworldP'
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
684 b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:'
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
685 b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0'
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
686 b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi'
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
687 b'lePK\005\006\0\0\0\0\001\0\001\0003\000'
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
688 b'\0\0/\0\0\0\0\0'),
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
689 zipfile.ZIP_DEFLATED: (
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
690 b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA'
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
691 b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
692 b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0'
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
693 b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n'
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
694 b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05'
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
695 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00'
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
696 b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00'
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
697 b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00'),
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
698 }
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 44978
diff changeset
699
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
700 def test_unicode_filenames(self):
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
701 with zipfile.ZipFile(TESTFN, "w") as zf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
702 zf.writestr(u"foo.txt", "Test for unicode filename")
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
703 zf.writestr(u"\xf6.txt", "Test for unicode filename")
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
704 self.assertIsInstance(zf.infolist()[0].filename, unicode)
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
705
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
706 with zipfile.ZipFile(TESTFN, "r") as zf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
707 self.assertEqual(zf.filelist[0].filename, "foo.txt")
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
708 self.assertEqual(zf.filelist[1].filename, u"\xf6.txt")
46671
8e33f316ce14 Issue #1734346: Support Unicode file names for zipfiles.
Martin v. Löwis <martin@v.loewis.de>
parents: 44978
diff changeset
709
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
710 def test_create_non_existent_file_for_append(self):
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
711 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
712 os.unlink(TESTFN)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
713
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
714 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
715 content = 'hello, world. this is some content.'
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
716
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
717 try:
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
718 with zipfile.ZipFile(TESTFN, 'a') as zf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
719 zf.writestr(filename, content)
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
720 except IOError:
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
721 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
722
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54311
diff changeset
723 self.assertTrue(os.path.exists(TESTFN))
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
724
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
725 with zipfile.ZipFile(TESTFN, 'r') as zf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
726 self.assertEqual(zf.read(filename), content)
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
727
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
728 def test_close_erroneous_file(self):
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
729 # This test checks that the ZipFile constructor closes the file object
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
730 # it opens if there's an error in the file. If it doesn't, the
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
731 # traceback holds a reference to the ZipFile object and, indirectly,
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
732 # the file object.
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
733 # 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
734 # 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
735 #
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
736 with open(TESTFN, "w") as fp:
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
737 fp.write("this is not a legal zip file\n")
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
738 try:
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
739 zf = zipfile.ZipFile(TESTFN)
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
740 except zipfile.BadZipfile:
41319
f57e8e325475 Make test_zipfile clean up its temporary files properly.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
741 pass
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
742
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
743 def test_is_zip_erroneous_file(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
744 """Check that is_zipfile() correctly identifies non-zip files."""
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
745 # - passing a filename
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
746 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
747 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
748 chk = zipfile.is_zipfile(TESTFN)
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
749 self.assertFalse(chk)
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
750 # - passing a file object
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
751 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
752 chk = zipfile.is_zipfile(fp)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54311
diff changeset
753 self.assertTrue(not chk)
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
754 # - 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
755 fp = StringIO()
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
756 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
757 chk = zipfile.is_zipfile(fp)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54311
diff changeset
758 self.assertTrue(not chk)
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
759 fp.seek(0, 0)
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
760 chk = zipfile.is_zipfile(fp)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54311
diff changeset
761 self.assertTrue(not chk)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
762
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
763 def test_is_zip_valid_file(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
764 """Check that is_zipfile() correctly identifies zip files."""
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
765 # - passing a filename
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
766 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
767 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
768 chk = zipfile.is_zipfile(TESTFN)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54311
diff changeset
769 self.assertTrue(chk)
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
770 # - passing a file object
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
771 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
772 chk = zipfile.is_zipfile(fp)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54311
diff changeset
773 self.assertTrue(chk)
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
774 fp.seek(0, 0)
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
775 zip_contents = fp.read()
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
776 # - 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
777 fp = StringIO()
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
778 fp.write(zip_contents)
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
779 chk = zipfile.is_zipfile(fp)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54311
diff changeset
780 self.assertTrue(chk)
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
781 fp.seek(0, 0)
50300
94f996bc64b3 Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48073
diff changeset
782 chk = zipfile.is_zipfile(fp)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54311
diff changeset
783 self.assertTrue(chk)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
784
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
785 def test_non_existent_file_raises_IOError(self):
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
786 # 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
787 # 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
788 # bug #403871.
17312
7635f8838c68 Sf bug [ #412214 ] ZipFile constructor leaves files open.
Tim Peters <tim.peters@gmail.com>
parents: 17245
diff changeset
789
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
790 # 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
791 # 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
792 # 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
793 # __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
794 # 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
795 # 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
796 # quickly.
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
797 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
798
58175
99e7c1b8b1e1 Merged revisions 74245 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54312
diff changeset
799 def test_empty_file_raises_BadZipFile(self):
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
800 with open(TESTFN, 'w') as f:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
801 pass
58175
99e7c1b8b1e1 Merged revisions 74245 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54312
diff changeset
802 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
99e7c1b8b1e1 Merged revisions 74245 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54312
diff changeset
803
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
804 with open(TESTFN, 'w') as fp:
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
805 fp.write("short file")
58175
99e7c1b8b1e1 Merged revisions 74245 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54312
diff changeset
806 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
99e7c1b8b1e1 Merged revisions 74245 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 54312
diff changeset
807
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
808 def test_closed_zip_raises_RuntimeError(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
809 """Verify that testzip() doesn't swallow inappropriate exceptions."""
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
810 data = StringIO()
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
811 with zipfile.ZipFile(data, mode="w") as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
812 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
813
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
814 # 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
815 # 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
816 # 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
817 # 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
818 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
819 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
820 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
821 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
822 open(TESTFN, 'w').write('zipfile test data')
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
823 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
824
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
825 def test_bad_constructor_mode(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
826 """Check that bad modes passed to ZipFile constructor are caught."""
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
827 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
828
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
829 def test_bad_open_mode(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
830 """Check that bad modes passed to ZipFile.open are caught."""
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
831 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
832 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
833
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
834 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
835 # read the data to make sure the file is there
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
836 zipf.read("foo.txt")
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
837 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
838
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
839 def test_read0(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
840 """Check that calling read(0) on a ZipExtFile object returns an empty
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
841 string and doesn't advance file pointer."""
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
842 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
843 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
844 # read the data to make sure the file is there
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
845 with zipf.open("foo.txt") as f:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
846 for i in xrange(FIXEDTEST_SIZE):
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
847 self.assertEqual(f.read(0), '')
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
848
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
849 self.assertEqual(f.read(), "O, for a Muse of Fire!")
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
850
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
851 def test_open_non_existent_item(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
852 """Check that attempting to call open() for an item that doesn't
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
853 exist in the archive raises a RuntimeError."""
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
854 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
855 self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
856
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
857 def test_bad_compression_mode(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
858 """Check that bad compression methods passed to ZipFile.open are
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
859 caught."""
42225
343b60b604bd Patch #1675424: Added tests for uncovered code in the zipfile module.
Georg Brandl <georg@python.org>
parents: 41881
diff changeset
860 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
861
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
862 def test_null_byte_in_filename(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
863 """Check that a filename containing a null byte is properly
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
864 terminated."""
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
865 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
866 zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!")
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
867 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
868
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
869 def test_struct_sizes(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
870 """Check that ZIP internal structure sizes are calculated correctly."""
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
871 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
872 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
873 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
874 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
875
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
876 def test_comments(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
877 """Check that comments on the archive are handled properly."""
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
878
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
879 # check default comment is empty
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
880 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
881 self.assertEqual(zipf.comment, '')
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
882 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
883
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
884 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
885 self.assertEqual(zipf.comment, '')
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
886
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
887 # 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
888 comment = 'Bravely taking to his feet, he beat a very brave retreat.'
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
889 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
890 zipf.comment = comment
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
891 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
892 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
893 self.assertEqual(zipf.comment, comment)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
894
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
895 # 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
896 comment2 = ''.join(['%d' % (i**3 % 10) for i in xrange((1 << 16)-1)])
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
897 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
898 zipf.comment = comment2
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
899 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
900
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
901 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
902 self.assertEqual(zipf.comment, comment2)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
903
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
904 # check a comment that is too long is truncated
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
905 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
906 zipf.comment = comment2 + 'oops'
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
907 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
908 with zipfile.ZipFile(TESTFN, mode="r") as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
909 self.assertEqual(zipf.comment, comment2)
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
910
76269
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
911 def test_change_comment_in_empty_archive(self):
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
912 with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
913 self.assertFalse(zipf.filelist)
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
914 zipf.comment = b"this is a comment"
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
915 with zipfile.ZipFile(TESTFN, "r") as zipf:
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
916 self.assertEqual(zipf.comment, b"this is a comment")
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
917
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
918 def test_change_comment_in_nonempty_archive(self):
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
919 with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
920 zipf.writestr("foo.txt", "O, for a Muse of Fire!")
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
921 with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
922 self.assertTrue(zipf.filelist)
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
923 zipf.comment = b"this is a comment"
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
924 with zipfile.ZipFile(TESTFN, "r") as zipf:
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
925 self.assertEqual(zipf.comment, b"this is a comment")
ac0ec1f31b0a #14399: zipfile now correctly handles comments added to empty zipfiles.
R David Murray <rdmurray@bitdance.com>
parents: 72991
diff changeset
926
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
927 def check_testzip_with_bad_crc(self, compression):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
928 """Tests that files with bad CRCs return their name from testzip."""
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
929 zipdata = self.zips_with_bad_crc[compression]
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
930
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
931 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
932 # testzip returns the name of the first corrupt file, or None
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
933 self.assertEqual('afile', zipf.testzip())
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
934
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
935 def test_testzip_with_bad_crc_stored(self):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
936 self.check_testzip_with_bad_crc(zipfile.ZIP_STORED)
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
937
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
938 @skipUnless(zlib, "requires zlib")
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
939 def test_testzip_with_bad_crc_deflated(self):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
940 self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED)
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
941
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
942 def check_read_with_bad_crc(self, compression):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
943 """Tests that files with bad CRCs raise a BadZipfile exception when read."""
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
944 zipdata = self.zips_with_bad_crc[compression]
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
945
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
946 # Using ZipFile.read()
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
947 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
948 self.assertRaises(zipfile.BadZipfile, zipf.read, 'afile')
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
949
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
950 # Using ZipExtFile.read()
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
951 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
952 with zipf.open('afile', 'r') as corrupt_file:
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
953 self.assertRaises(zipfile.BadZipfile, corrupt_file.read)
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
954
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
955 # Same with small reads (in order to exercise the buffering logic)
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
956 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
957 with zipf.open('afile', 'r') as corrupt_file:
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
958 corrupt_file.MIN_READ_SIZE = 2
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
959 with self.assertRaises(zipfile.BadZipfile):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
960 while corrupt_file.read(2):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
961 pass
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
962
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
963 def test_read_with_bad_crc_stored(self):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
964 self.check_read_with_bad_crc(zipfile.ZIP_STORED)
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
965
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
966 @skipUnless(zlib, "requires zlib")
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
967 def test_read_with_bad_crc_deflated(self):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
968 self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED)
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
969
64674
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
970 def check_read_return_size(self, compression):
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
971 # Issue #9837: ZipExtFile.read() shouldn't return more bytes
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
972 # than requested.
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
973 for test_size in (1, 4095, 4096, 4097, 16384):
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
974 file_size = test_size + 1
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
975 junk = b''.join(struct.pack('B', randint(0, 255))
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
976 for x in range(file_size))
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
977 with zipfile.ZipFile(io.BytesIO(), "w", compression) as zipf:
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
978 zipf.writestr('foo', junk)
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
979 with zipf.open('foo', 'r') as fp:
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
980 buf = fp.read(test_size)
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
981 self.assertEqual(len(buf), test_size)
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
982
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
983 def test_read_return_size_stored(self):
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
984 self.check_read_return_size(zipfile.ZIP_STORED)
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
985
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
986 @skipUnless(zlib, "requires zlib")
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
987 def test_read_return_size_deflated(self):
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
988 self.check_read_return_size(zipfile.ZIP_DEFLATED)
7ac0e607c4d6 Merged revisions 84737 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63935
diff changeset
989
66450
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
990 def test_empty_zipfile(self):
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
991 # Check that creating a file in 'w' or 'a' mode and closing without
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
992 # adding any files to the archives creates a valid empty ZIP file
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
993 with zipfile.ZipFile(TESTFN, mode="w") as zipf:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
994 pass
66450
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
995 try:
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
996 zipf = zipfile.ZipFile(TESTFN, mode="r")
67846
d33903eb3388 Fix typo: BadZipFile exists in 3.2+ only, not older versions.
Éric Araujo <merwok@netwok.org>
parents: 66450
diff changeset
997 except zipfile.BadZipfile:
66450
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
998 self.fail("Unable to create empty ZIP file in 'w' mode")
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
999
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1000 with zipfile.ZipFile(TESTFN, mode="a") as zipf:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1001 pass
66450
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
1002 try:
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
1003 zipf = zipfile.ZipFile(TESTFN, mode="r")
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
1004 except:
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
1005 self.fail("Unable to create empty ZIP file in 'a' mode")
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
1006
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
1007 def test_open_empty_file(self):
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
1008 # Issue 1710703: Check that opening a file with less than 22 bytes
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
1009 # raises a BadZipfile exception (rather than the previously unhelpful
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
1010 # IOError)
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1011 with open(TESTFN, 'w') as f:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1012 pass
66450
e48ed2ef6d5f Merged revisions 85455 via svnmerge from
Georg Brandl <georg@python.org>
parents: 64674
diff changeset
1013 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN, 'r')
47882
acfad8640e21 Patch #1622: Correct interpretation of various ZIP header fields.
Martin v. Löwis <martin@v.loewis.de>
parents: 47195
diff changeset
1014
72991
649ac338203f Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
Senthil Kumaran <senthil@uthcode.com>
parents: 70748
diff changeset
1015 def test_create_zipinfo_before_1980(self):
649ac338203f Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
Senthil Kumaran <senthil@uthcode.com>
parents: 70748
diff changeset
1016 self.assertRaises(ValueError,
649ac338203f Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
Senthil Kumaran <senthil@uthcode.com>
parents: 70748
diff changeset
1017 zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0))
649ac338203f Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
Senthil Kumaran <senthil@uthcode.com>
parents: 70748
diff changeset
1018
41319
f57e8e325475 Make test_zipfile clean up its temporary files properly.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
1019 def tearDown(self):
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1020 unlink(TESTFN)
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1021 unlink(TESTFN2)
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1022
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
1023
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1024 class DecryptionTests(unittest.TestCase):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1025 """Check that ZIP decryption works. Since the library does not
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1026 support encryption at the moment, we use a pre-generated encrypted
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1027 ZIP file."""
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1028
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1029 data = (
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1030 '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
1031 '\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
1032 '\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
1033 '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
1034 '\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
1035 '\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
1036 '\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
1037 data2 = (
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
1038 '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
1039 '\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
1040 '\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
1041 '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
1042 '\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
1043 '\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
1044 '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
1045 '\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
1046
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1047 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
1048 plain2 = '\x00'*512
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1049
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1050 def setUp(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1051 with open(TESTFN, "wb") as fp:
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1052 fp.write(self.data)
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1053 self.zip = zipfile.ZipFile(TESTFN, "r")
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1054 with open(TESTFN2, "wb") as fp:
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1055 fp.write(self.data2)
44807
1120b3d606f2 Fix zipfile decryption. The check for validity only worked on one
Gregory P. Smith <greg@mad-scientist.com>
parents: 44576
diff changeset
1056 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
1057
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1058 def tearDown(self):
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1059 self.zip.close()
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1060 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
1061 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
1062 os.unlink(TESTFN2)
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1063
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1064 def test_no_password(self):
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1065 # 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
1066 # 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
1067 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
1068 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
1069
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1070 def test_bad_password(self):
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1071 self.zip.setpassword("perl")
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1072 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
1073 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
1074 self.assertRaises(RuntimeError, self.zip2.read, "zero")
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1075
55920
39be3ef82d23 #6026 - fix tests that failed without zlib
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55485
diff changeset
1076 @skipUnless(zlib, "requires zlib")
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1077 def test_good_password(self):
40833
daf063527b97 Patch #698833: Support file decryption in zipfile.
Martin v. Löwis <martin@v.loewis.de>
parents: 38845
diff changeset
1078 self.zip.setpassword("python")
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1079 self.assertEqual(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
1080 self.zip2.setpassword("12345")
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1081 self.assertEqual(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
1082
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1083
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1084 class TestsWithRandomBinaryFiles(unittest.TestCase):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1085 def setUp(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1086 datacount = randint(16, 64)*1024 + randint(1, 1024)
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1087 self.data = ''.join(struct.pack('<f', random()*randint(-1000, 1000))
58034
7c630931cc83 indentation and further alignment with py3k
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58032
diff changeset
1088 for i in xrange(datacount))
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1089
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1090 # Make a source file with some lines
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1091 with open(TESTFN, "wb") as fp:
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1092 fp.write(self.data)
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41319
diff changeset
1093
41319
f57e8e325475 Make test_zipfile clean up its temporary files properly.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
1094 def tearDown(self):
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1095 unlink(TESTFN)
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1096 unlink(TESTFN2)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1097
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1098 def make_test_archive(self, f, compression):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1099 # Create the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1100 with zipfile.ZipFile(f, "w", compression) as zipfp:
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1101 zipfp.write(TESTFN, "another.name")
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1102 zipfp.write(TESTFN, TESTFN)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1103
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1104 def zip_test(self, f, compression):
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1105 self.make_test_archive(f, compression)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1106
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1107 # Read the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1108 with zipfile.ZipFile(f, "r", compression) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1109 testdata = zipfp.read(TESTFN)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1110 self.assertEqual(len(testdata), len(self.data))
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1111 self.assertEqual(testdata, self.data)
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1112 self.assertEqual(zipfp.read("another.name"), self.data)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1113
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1114 def test_stored(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()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1116 self.zip_test(f, zipfile.ZIP_STORED)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1117
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1118 @skipUnless(zlib, "requires zlib")
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1119 def test_deflated(self):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1120 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1121 self.zip_test(f, zipfile.ZIP_DEFLATED)
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1122
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1123 def zip_open_test(self, f, compression):
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1124 self.make_test_archive(f, compression)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1125
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1126 # Read the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1127 with zipfile.ZipFile(f, "r", compression) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1128 zipdata1 = []
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1129 with zipfp.open(TESTFN) as zipopen1:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1130 while True:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1131 read_data = zipopen1.read(256)
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1132 if not read_data:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1133 break
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1134 zipdata1.append(read_data)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1135
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1136 zipdata2 = []
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1137 with zipfp.open("another.name") as zipopen2:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1138 while True:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1139 read_data = zipopen2.read(256)
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1140 if not read_data:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1141 break
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1142 zipdata2.append(read_data)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1143
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1144 testdata1 = ''.join(zipdata1)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1145 self.assertEqual(len(testdata1), len(self.data))
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1146 self.assertEqual(testdata1, self.data)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1147
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1148 testdata2 = ''.join(zipdata2)
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1149 self.assertEqual(len(testdata2), len(self.data))
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1150 self.assertEqual(testdata2, self.data)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1151
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1152 def test_open_stored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1153 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1154 self.zip_open_test(f, zipfile.ZIP_STORED)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1155
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1156 @skipUnless(zlib, "requires zlib")
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1157 def test_open_deflated(self):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1158 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1159 self.zip_open_test(f, zipfile.ZIP_DEFLATED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1160
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1161 def zip_random_open_test(self, f, compression):
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1162 self.make_test_archive(f, compression)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1163
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1164 # Read the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1165 with zipfile.ZipFile(f, "r", compression) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1166 zipdata1 = []
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1167 with zipfp.open(TESTFN) as zipopen1:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1168 while True:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1169 read_data = zipopen1.read(randint(1, 1024))
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1170 if not read_data:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1171 break
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1172 zipdata1.append(read_data)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1173
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1174 testdata = ''.join(zipdata1)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1175 self.assertEqual(len(testdata), len(self.data))
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1176 self.assertEqual(testdata, self.data)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1177
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1178 def test_random_open_stored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1179 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1180 self.zip_random_open_test(f, zipfile.ZIP_STORED)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1181
63935
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1182 @skipUnless(zlib, "requires zlib")
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1183 def test_random_open_deflated(self):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1184 for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
ed66b24171af Merged revisions 83959-83960 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58821
diff changeset
1185 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1186
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1187
55920
39be3ef82d23 #6026 - fix tests that failed without zlib
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55485
diff changeset
1188 @skipUnless(zlib, "requires zlib")
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1189 class TestsWithMultipleOpens(unittest.TestCase):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1190 def setUp(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1191 # Create the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1192 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1193 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1194 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1195
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1196 def test_same_file(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1197 # 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
1198 # multiple open() calls can be made without interfering with each other.
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1199 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1200 zopen1 = zipf.open('ones')
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1201 zopen2 = zipf.open('ones')
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1202 data1 = zopen1.read(500)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1203 data2 = zopen2.read(500)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1204 data1 += zopen1.read(500)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1205 data2 += zopen2.read(500)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1206 self.assertEqual(data1, data2)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1207
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1208 def test_different_file(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1209 # 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
1210 # multiple open() calls can be made without interfering with each other.
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1211 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1212 with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1213 data1 = zopen1.read(500)
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1214 data2 = zopen2.read(500)
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1215 data1 += zopen1.read(500)
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1216 data2 += zopen2.read(500)
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1217 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1218 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1219
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1220 def test_interleaved(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1221 # 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
1222 # multiple open() calls can be made without interfering with each other.
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1223 with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1224 with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1225 data1 = zopen1.read(500)
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1226 data2 = zopen2.read(500)
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1227 data1 += zopen1.read(500)
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1228 data2 += zopen2.read(500)
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1229 self.assertEqual(data1, '1'*FIXEDTEST_SIZE)
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1230 self.assertEqual(data2, '2'*FIXEDTEST_SIZE)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1231
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1232 def tearDown(self):
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1233 unlink(TESTFN2)
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1234
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1235
51000
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1236 class TestWithDirectory(unittest.TestCase):
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1237 def setUp(self):
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1238 os.mkdir(TESTFN2)
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1239
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1240 def test_extract_dir(self):
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1241 with zipfile.ZipFile(findfile("zipdir.zip")) as zipf:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1242 zipf.extractall(TESTFN2)
51000
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1243 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1244 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1245 self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1246
54312
e67b1d071ed6 Merged revisions 72893 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 51000
diff changeset
1247 def test_bug_6050(self):
e67b1d071ed6 Merged revisions 72893 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 51000
diff changeset
1248 # Extraction should succeed if directories already exist
e67b1d071ed6 Merged revisions 72893 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 51000
diff changeset
1249 os.mkdir(os.path.join(TESTFN2, "a"))
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1250 self.test_extract_dir()
54312
e67b1d071ed6 Merged revisions 72893 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 51000
diff changeset
1251
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1252 def test_store_dir(self):
51000
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1253 os.mkdir(os.path.join(TESTFN2, "x"))
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1254 zipf = zipfile.ZipFile(TESTFN, "w")
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1255 zipf.write(os.path.join(TESTFN2, "x"), "x")
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1256 self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1257
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1258 def tearDown(self):
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1259 shutil.rmtree(TESTFN2)
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1260 if os.path.exists(TESTFN):
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1261 unlink(TESTFN)
51000
6f761e5714e0 Merged revisions 68885 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 48073
diff changeset
1262
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1263
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1264 class UniversalNewlineTests(unittest.TestCase):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1265 def setUp(self):
58032
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1266 self.line_gen = ["Test of zipfile line %d." % i
384a7a3709dc cleanup and refactoring
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58000
diff changeset
1267 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
1268 self.seps = ('\r', '\r\n', '\n')
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1269 self.arcdata, self.arcfiles = {}, {}
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1270 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
1271 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
1272 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
1273 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
1274
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1275 def make_test_archive(self, f, compression):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1276 # Create the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1277 with zipfile.ZipFile(f, "w", compression) as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1278 for fn in self.arcfiles.values():
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1279 zipfp.write(fn, fn)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1280
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1281 def read_test(self, f, compression):
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1282 self.make_test_archive(f, compression)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1283
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1284 # Read the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1285 with zipfile.ZipFile(f, "r") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1286 for sep, fn in self.arcfiles.items():
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1287 with zipfp.open(fn, "rU") as fp:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1288 zipdata = fp.read()
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1289 self.assertEqual(self.arcdata[sep], zipdata)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1290
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
1291 def readline_read_test(self, f, compression):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
1292 self.make_test_archive(f, compression)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1293
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1294 # Read the ZIP archive
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1295 zipfp = zipfile.ZipFile(f, "r")
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1296 for sep, fn in self.arcfiles.items():
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1297 with zipfp.open(fn, "rU") as zipopen:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1298 data = ''
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1299 while True:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1300 read = zipopen.readline()
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1301 if not read:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1302 break
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1303 data += read
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1304
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1305 read = zipopen.read(5)
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1306 if not read:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1307 break
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1308 data += read
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1309
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
1310 self.assertEqual(data, self.arcdata['\n'])
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1311
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1312 zipfp.close()
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1313
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1314 def readline_test(self, f, compression):
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1315 self.make_test_archive(f, compression)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1316
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1317 # Read the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1318 with zipfile.ZipFile(f, "r") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1319 for sep, fn in self.arcfiles.items():
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1320 with zipfp.open(fn, "rU") as zipopen:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1321 for line in self.line_gen:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1322 linedata = zipopen.readline()
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1323 self.assertEqual(linedata, line + '\n')
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1324
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1325 def readlines_test(self, f, compression):
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1326 self.make_test_archive(f, compression)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1327
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1328 # Read the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1329 with zipfile.ZipFile(f, "r") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1330 for sep, fn in self.arcfiles.items():
69464
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1331 with zipfp.open(fn, "rU") as fp:
83a426e969f5 Fix #8886. Use context managers throughout zipfile tests.
Brian Curtin <brian@python.org>
parents: 68256
diff changeset
1332 ziplines = fp.readlines()
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1333 for line, zipline in zip(self.line_gen, ziplines):
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1334 self.assertEqual(zipline, line + '\n')
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1335
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1336 def iterlines_test(self, f, compression):
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1337 self.make_test_archive(f, compression)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1338
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1339 # Read the ZIP archive
58000
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1340 with zipfile.ZipFile(f, "r") as zipfp:
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1341 for sep, fn in self.arcfiles.items():
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1342 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
4f0988e8fcb1 #5511: Added the ability to use ZipFile as a context manager. Patch by Brian Curtin.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55920
diff changeset
1343 self.assertEqual(zipline, line + '\n')
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1344
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1345 def test_read_stored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1346 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1347 self.read_test(f, zipfile.ZIP_STORED)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1348
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
1349 def test_readline_read_stored(self):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
1350 # Issue #7610: calls to readline() interleaved with calls to read().
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
1351 for f in (TESTFN2, TemporaryFile(), StringIO()):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
1352 self.readline_read_test(f, zipfile.ZIP_STORED)
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
1353
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1354 def test_readline_stored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1355 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1356 self.readline_test(f, zipfile.ZIP_STORED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1357
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1358 def test_readlines_stored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1359 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1360 self.readlines_test(f, zipfile.ZIP_STORED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1361
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1362 def test_iterlines_stored(self):
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1363 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1364 self.iterlines_test(f, zipfile.ZIP_STORED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1365
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1366 @skipUnless(zlib, "requires zlib")
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1367 def test_read_deflated(self):
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1368 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1369 self.read_test(f, zipfile.ZIP_DEFLATED)
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1370
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1371 @skipUnless(zlib, "requires zlib")
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
1372 def test_readline_read_deflated(self):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
1373 # Issue #7610: calls to readline() interleaved with calls to read().
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
1374 for f in (TESTFN2, TemporaryFile(), StringIO()):
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
1375 self.readline_read_test(f, zipfile.ZIP_DEFLATED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1376
58540
1a0081b85c04 Issue #7610: Reworked implementation of the internal
Antoine Pitrou <solipsis@pitrou.net>
parents: 58472
diff changeset
1377 @skipUnless(zlib, "requires zlib")
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1378 def test_readline_deflated(self):
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1379 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1380 self.readline_test(f, zipfile.ZIP_DEFLATED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1381
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1382 @skipUnless(zlib, "requires zlib")
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1383 def test_readlines_deflated(self):
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1384 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1385 self.readlines_test(f, zipfile.ZIP_DEFLATED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1386
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1387 @skipUnless(zlib, "requires zlib")
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1388 def test_iterlines_deflated(self):
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1389 for f in (TESTFN2, TemporaryFile(), StringIO()):
55257
93906b39ffec methods' names pep8ification
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55193
diff changeset
1390 self.iterlines_test(f, zipfile.ZIP_DEFLATED)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1391
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1392 def tearDown(self):
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1393 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
1394 os.remove(fn)
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1395 unlink(TESTFN)
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1396 unlink(TESTFN2)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1397
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1398
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
1399 def test_main():
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1400 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests,
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41012
diff changeset
1401 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens,
55193
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1402 TestWithDirectory, UniversalNewlineTests,
cddd5164db03 more cleanups and if zlib -> skipUnless(zlib)
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55107
diff changeset
1403 TestsWithRandomBinaryFiles)
41012
f1e88c3502d9 Patch #1121142: Implement ZipFile.open.
Martin v. Löwis <martin@v.loewis.de>
parents: 40834
diff changeset
1404
33191
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
1405 if __name__ == "__main__":
edb853ed6c78 Port test_zipfile to unittest (patch #736962).
Johannes Gijsbers <jlg@dds.nl>
parents: 29625
diff changeset
1406 test_main()