@@ -159,18 +159,27 @@ def flush(self, zlib_mode=isal_zlib.Z_SYNC_FLUSH):
159
159
super ().flush (zlib_mode )
160
160
161
161
def _write_gzip_header (self , compresslevel = _COMPRESS_LEVEL_TRADEOFF ):
162
- # Determine what xfl flag is written for the compression level.
163
- # Equate the fast level to gzip level 1. All the other levels are
164
- # medium.
165
- if sys .version_info [0 ] == 3 and sys .version_info [1 ] < 7 :
166
- # Correct header introduced in 3.7
167
- super ()._write_gzip_header ()
168
- else :
162
+ # Python 3.9 added a `compresslevel` parameter to write gzip header.
163
+ # This only determines the value of one extra flag. Because this change
164
+ # was backported to 3.7 and 3.8 in later point versions, the attributes
165
+ # of the function should be checked before trying to use the
166
+ # compresslevel parameter.
167
+ # The gzip header has an extra flag that can be set depending on the
168
+ # compression level used. This should be set when either the fastest or
169
+ # best method is used. ISAL level 0 is larger than gzip level 1 and
170
+ # much faster, so setting the flag for fastest level is appropriate.
171
+ # ISAL level 1,2 and 3 (best)are similar in size and fall around the
172
+ # gzip level 3 size. So setting no extra flag
173
+ # (by using COMPRESS_LEVEL_TRADEOFF) is appropriate here.
174
+ if ("compresslevel" in super ()._write_gzip_header .__code__ .co_varnames
175
+ and hasattr (gzip , "_COMPRESS_LEVEL_FAST" )
176
+ and hasattr (gzip , "_COMPRESS_LEVEL_TRADEOFF" )):
169
177
if compresslevel == _COMPRESS_LEVEL_FAST :
170
- compresslevel = gzip ._COMPRESS_LEVEL_FAST
178
+ super (). _write_gzip_header ( gzip ._COMPRESS_LEVEL_FAST )
171
179
else :
172
- compresslevel = gzip ._COMPRESS_LEVEL_TRADEOFF
173
- super ()._write_gzip_header (compresslevel )
180
+ super ()._write_gzip_header (gzip ._COMPRESS_LEVEL_TRADEOFF )
181
+ else :
182
+ super ()._write_gzip_header ()
174
183
175
184
def write (self , data ):
176
185
self ._check_not_closed ()
0 commit comments