annotate zipfile266.diff @ 124:19c42a3cd962

Very fast testees no longer break Popen on some UNIX systems on Python 2.6+
author Oleg Oshmyan <chortos@inbox.lv>
date Sun, 24 Apr 2011 20:05:20 +0100
parents 4ea7133ac25c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1 --- /usr/lib/python2.6/zipfile.py 2010-07-05 14:48:38.000000000 +0300
30
f17f19d9eb0a hasattr -> try in the newly added 2.6 zipfile
Oleg Oshmyan <chortos@inbox.lv>
parents: 29
diff changeset
2 +++ zipfile.py 2010-11-25 01:39:22.749743303 +0200
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
3 @@ -1,6 +1,7 @@
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
4 """
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
5 Read and write ZIP files.
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
6 """
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
7 +# Improved by Chortos-2 in 2009 and 2010 (added bzip2 support)
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
8 import struct, os, time, sys, shutil
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
9 import binascii, cStringIO, stat
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
10
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
11 @@ -11,8 +12,13 @@
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
12 zlib = None
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
13 crc32 = binascii.crc32
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
14
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
15 +try:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
16 + import bz2 # We may need its compression method
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
17 +except ImportError:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
18 + bz2 = None
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
19 +
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
20 __all__ = ["BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", "is_zipfile",
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
21 - "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile" ]
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
22 + "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile", "ZIP_BZIP2" ]
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
23
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
24 class BadZipfile(Exception):
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
25 pass
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
26 @@ -33,6 +39,7 @@
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
27 # constants for Zip file compression methods
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
28 ZIP_STORED = 0
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
29 ZIP_DEFLATED = 8
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
30 +ZIP_BZIP2 = 12
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
31 # Other ZIP compression methods not supported
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
32
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
33 # Below are some formats and associated data for reading/writing headers using
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
34 @@ -467,6 +474,9 @@
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
35 self.compreadsize = 64*1024
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
36 if self.compress_type == ZIP_DEFLATED:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
37 self.dc = zlib.decompressobj(-15)
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
38 + elif self.compress_type == ZIP_BZIP2:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
39 + self.dc = bz2.BZ2Decompressor()
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
40 + self.compreadsize = 900000
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
41
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
42 def set_univ_newlines(self, univ_newlines):
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
43 self.univ_newlines = univ_newlines
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
44 @@ -578,7 +588,7 @@
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
45 if self.compress_type == ZIP_STORED:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
46 lr = len(self.readbuffer)
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
47 bytesToRead = min(bytesToRead, size - lr)
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
48 - elif self.compress_type == ZIP_DEFLATED:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
49 + else:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
50 if len(self.readbuffer) > size:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
51 # the user has requested fewer bytes than we've already
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
52 # pulled through the decompressor; don't read any more
30
f17f19d9eb0a hasattr -> try in the newly added 2.6 zipfile
Oleg Oshmyan <chortos@inbox.lv>
parents: 29
diff changeset
53 @@ -608,14 +618,17 @@
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
54 newdata = ''.join(map(self.decrypter, newdata))
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
55
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
56 # decompress newly read data if necessary
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
57 - if newdata and self.compress_type == ZIP_DEFLATED:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
58 + if newdata and self.compress_type != ZIP_STORED:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
59 newdata = self.dc.decompress(newdata)
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
60 - self.rawbuffer = self.dc.unconsumed_tail
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
61 + self.rawbuffer = self.dc.unconsumed_tail if self.compress_type == ZIP_DEFLATED else ''
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
62 if self.eof and len(self.rawbuffer) == 0:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
63 # we're out of raw bytes (both from the file and
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
64 # the local buffer); flush just to make sure the
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
65 # decompressor is done
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
66 - newdata += self.dc.flush()
30
f17f19d9eb0a hasattr -> try in the newly added 2.6 zipfile
Oleg Oshmyan <chortos@inbox.lv>
parents: 29
diff changeset
67 + try:
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
68 + newdata += self.dc.flush()
30
f17f19d9eb0a hasattr -> try in the newly added 2.6 zipfile
Oleg Oshmyan <chortos@inbox.lv>
parents: 29
diff changeset
69 + except AttributeError:
f17f19d9eb0a hasattr -> try in the newly added 2.6 zipfile
Oleg Oshmyan <chortos@inbox.lv>
parents: 29
diff changeset
70 + pass
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
71 # prevent decompressor from being used again
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
72 self.dc = None
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
73
30
f17f19d9eb0a hasattr -> try in the newly added 2.6 zipfile
Oleg Oshmyan <chortos@inbox.lv>
parents: 29
diff changeset
74 @@ -641,7 +654,8 @@
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
75 file: Either the path to the file, or a file-like object.
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
76 If it is a path, the file will be opened and closed by ZipFile.
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
77 mode: The mode can be either read "r", write "w" or append "a".
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
78 - compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib).
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
79 + compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib),
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
80 + or ZIP_BZIP2 (requires bz2).
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
81 allowZip64: if True ZipFile will create files with ZIP64 extensions when
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
82 needed, otherwise it will raise an exception when this would
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
83 be necessary.
30
f17f19d9eb0a hasattr -> try in the newly added 2.6 zipfile
Oleg Oshmyan <chortos@inbox.lv>
parents: 29
diff changeset
84 @@ -661,6 +675,10 @@
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
85 if not zlib:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
86 raise RuntimeError,\
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
87 "Compression requires the (missing) zlib module"
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
88 + elif compression == ZIP_BZIP2:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
89 + if not bz2:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
90 + raise RuntimeError,\
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
91 + "Compression requires the (missing) bz2 module"
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
92 else:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
93 raise RuntimeError, "That compression method is not supported"
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
94
30
f17f19d9eb0a hasattr -> try in the newly added 2.6 zipfile
Oleg Oshmyan <chortos@inbox.lv>
parents: 29
diff changeset
95 @@ -987,7 +1005,10 @@
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
96 if zinfo.compress_type == ZIP_DEFLATED and not zlib:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
97 raise RuntimeError, \
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
98 "Compression requires the (missing) zlib module"
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
99 - if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED):
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
100 + if zinfo.compress_type == ZIP_BZIP2 and not bz2:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
101 + raise RuntimeError, \
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
102 + "Compression requires the (missing) bz2 module"
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
103 + if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2):
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
104 raise RuntimeError, \
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
105 "That compression method is not supported"
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
106 if zinfo.file_size > ZIP64_LIMIT:
30
f17f19d9eb0a hasattr -> try in the newly added 2.6 zipfile
Oleg Oshmyan <chortos@inbox.lv>
parents: 29
diff changeset
107 @@ -1048,6 +1069,8 @@
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
108 if zinfo.compress_type == ZIP_DEFLATED:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
109 cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
110 zlib.DEFLATED, -15)
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
111 + elif zinfo.compress_type == ZIP_BZIP2:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
112 + cmpr = bz2.BZ2Compressor()
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
113 else:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
114 cmpr = None
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
115 while 1:
30
f17f19d9eb0a hasattr -> try in the newly added 2.6 zipfile
Oleg Oshmyan <chortos@inbox.lv>
parents: 29
diff changeset
116 @@ -1105,6 +1128,10 @@
29
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
117 zlib.DEFLATED, -15)
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
118 bytes = co.compress(bytes) + co.flush()
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
119 zinfo.compress_size = len(bytes) # Compressed size
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
120 + elif zinfo.compress_type == ZIP_BZIP2:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
121 + co = bz2.BZ2Compressor()
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
122 + bytes = co.compress(bytes) + co.flush()
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
123 + zinfo.compress_size = len(bytes) # Compressed size
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
124 else:
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
125 zinfo.compress_size = zinfo.file_size
a8cc383b787c Clean up zipfiles and diff them to stock ones
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
126 zinfo.header_offset = self.fp.tell() # Start of header bytes