comparison 2.00/zipfile266.diff @ 30:f17f19d9eb0a

hasattr -> try in the newly added 2.6 zipfile
author Oleg Oshmyan <chortos@inbox.lv>
date Wed, 24 Nov 2010 23:42:06 +0000
parents a8cc383b787c
children
comparison
equal deleted inserted replaced
29:a8cc383b787c 30:f17f19d9eb0a
1 --- /usr/lib/python2.6/zipfile.py 2010-07-05 14:48:38.000000000 +0300 1 --- /usr/lib/python2.6/zipfile.py 2010-07-05 14:48:38.000000000 +0300
2 +++ zipfile.py 2010-08-05 03:51:16.062274000 +0300 2 +++ zipfile.py 2010-11-25 01:39:22.749743303 +0200
3 @@ -1,6 +1,7 @@ 3 @@ -1,6 +1,7 @@
4 """ 4 """
5 Read and write ZIP files. 5 Read and write ZIP files.
6 """ 6 """
7 +# Improved by Chortos-2 in 2009 and 2010 (added bzip2 support) 7 +# Improved by Chortos-2 in 2009 and 2010 (added bzip2 support)
48 - elif self.compress_type == ZIP_DEFLATED: 48 - elif self.compress_type == ZIP_DEFLATED:
49 + else: 49 + else:
50 if len(self.readbuffer) > size: 50 if len(self.readbuffer) > size:
51 # the user has requested fewer bytes than we've already 51 # the user has requested fewer bytes than we've already
52 # pulled through the decompressor; don't read any more 52 # pulled through the decompressor; don't read any more
53 @@ -608,14 +618,15 @@ 53 @@ -608,14 +618,17 @@
54 newdata = ''.join(map(self.decrypter, newdata)) 54 newdata = ''.join(map(self.decrypter, newdata))
55 55
56 # decompress newly read data if necessary 56 # decompress newly read data if necessary
57 - if newdata and self.compress_type == ZIP_DEFLATED: 57 - if newdata and self.compress_type == ZIP_DEFLATED:
58 + if newdata and self.compress_type != ZIP_STORED: 58 + if newdata and self.compress_type != ZIP_STORED:
62 if self.eof and len(self.rawbuffer) == 0: 62 if self.eof and len(self.rawbuffer) == 0:
63 # we're out of raw bytes (both from the file and 63 # we're out of raw bytes (both from the file and
64 # the local buffer); flush just to make sure the 64 # the local buffer); flush just to make sure the
65 # decompressor is done 65 # decompressor is done
66 - newdata += self.dc.flush() 66 - newdata += self.dc.flush()
67 + if hasattr(self.dc, 'flush'): 67 + try:
68 + newdata += self.dc.flush() 68 + newdata += self.dc.flush()
69 + except AttributeError:
70 + pass
69 # prevent decompressor from being used again 71 # prevent decompressor from being used again
70 self.dc = None 72 self.dc = None
71 73
72 @@ -641,7 +652,8 @@ 74 @@ -641,7 +654,8 @@
73 file: Either the path to the file, or a file-like object. 75 file: Either the path to the file, or a file-like object.
74 If it is a path, the file will be opened and closed by ZipFile. 76 If it is a path, the file will be opened and closed by ZipFile.
75 mode: The mode can be either read "r", write "w" or append "a". 77 mode: The mode can be either read "r", write "w" or append "a".
76 - compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib). 78 - compression: ZIP_STORED (no compression) or ZIP_DEFLATED (requires zlib).
77 + compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib), 79 + compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib),
78 + or ZIP_BZIP2 (requires bz2). 80 + or ZIP_BZIP2 (requires bz2).
79 allowZip64: if True ZipFile will create files with ZIP64 extensions when 81 allowZip64: if True ZipFile will create files with ZIP64 extensions when
80 needed, otherwise it will raise an exception when this would 82 needed, otherwise it will raise an exception when this would
81 be necessary. 83 be necessary.
82 @@ -661,6 +673,10 @@ 84 @@ -661,6 +675,10 @@
83 if not zlib: 85 if not zlib:
84 raise RuntimeError,\ 86 raise RuntimeError,\
85 "Compression requires the (missing) zlib module" 87 "Compression requires the (missing) zlib module"
86 + elif compression == ZIP_BZIP2: 88 + elif compression == ZIP_BZIP2:
87 + if not bz2: 89 + if not bz2:
88 + raise RuntimeError,\ 90 + raise RuntimeError,\
89 + "Compression requires the (missing) bz2 module" 91 + "Compression requires the (missing) bz2 module"
90 else: 92 else:
91 raise RuntimeError, "That compression method is not supported" 93 raise RuntimeError, "That compression method is not supported"
92 94
93 @@ -987,7 +1003,10 @@ 95 @@ -987,7 +1005,10 @@
94 if zinfo.compress_type == ZIP_DEFLATED and not zlib: 96 if zinfo.compress_type == ZIP_DEFLATED and not zlib:
95 raise RuntimeError, \ 97 raise RuntimeError, \
96 "Compression requires the (missing) zlib module" 98 "Compression requires the (missing) zlib module"
97 - if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED): 99 - if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED):
98 + if zinfo.compress_type == ZIP_BZIP2 and not bz2: 100 + if zinfo.compress_type == ZIP_BZIP2 and not bz2:
100 + "Compression requires the (missing) bz2 module" 102 + "Compression requires the (missing) bz2 module"
101 + if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2): 103 + if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2):
102 raise RuntimeError, \ 104 raise RuntimeError, \
103 "That compression method is not supported" 105 "That compression method is not supported"
104 if zinfo.file_size > ZIP64_LIMIT: 106 if zinfo.file_size > ZIP64_LIMIT:
105 @@ -1048,6 +1067,8 @@ 107 @@ -1048,6 +1069,8 @@
106 if zinfo.compress_type == ZIP_DEFLATED: 108 if zinfo.compress_type == ZIP_DEFLATED:
107 cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, 109 cmpr = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION,
108 zlib.DEFLATED, -15) 110 zlib.DEFLATED, -15)
109 + elif zinfo.compress_type == ZIP_BZIP2: 111 + elif zinfo.compress_type == ZIP_BZIP2:
110 + cmpr = bz2.BZ2Compressor() 112 + cmpr = bz2.BZ2Compressor()
111 else: 113 else:
112 cmpr = None 114 cmpr = None
113 while 1: 115 while 1:
114 @@ -1105,6 +1126,10 @@ 116 @@ -1105,6 +1128,10 @@
115 zlib.DEFLATED, -15) 117 zlib.DEFLATED, -15)
116 bytes = co.compress(bytes) + co.flush() 118 bytes = co.compress(bytes) + co.flush()
117 zinfo.compress_size = len(bytes) # Compressed size 119 zinfo.compress_size = len(bytes) # Compressed size
118 + elif zinfo.compress_type == ZIP_BZIP2: 120 + elif zinfo.compress_type == ZIP_BZIP2:
119 + co = bz2.BZ2Compressor() 121 + co = bz2.BZ2Compressor()