Skip to content

Commit 5298b1b

Browse files
committed
Fix several test issues
1 parent 52949ea commit 5298b1b

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/isal/igzip.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626
import gzip
2727
import io
2828
import os
29-
3029
import _compression
3130
import sys
32-
31+
from gzip import READ, WRITE
3332
from . import isal_zlib
3433

3534
__all__ = ["IGzipFile", "open", "compress", "decompress", "BadGzipFile"]
@@ -41,10 +40,12 @@
4140

4241
BUFFER_SIZE = _compression.BUFFER_SIZE
4342

44-
class BadGzipFile(OSError):
43+
try:
44+
class BadGzipFile(gzip.BadGzipFile):
45+
pass
46+
except AttributeError: # Versions lower than 3.8 do not have BadGzipFile
4547
pass
4648

47-
4849
# The open method was copied from the python source with minor adjustments.
4950
def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_TRADEOFF,
5051
encoding=None, errors=None, newline=None):
@@ -104,7 +105,7 @@ def __init__(self, filename=None, mode=None,
104105
isal_zlib.ISAL_BEST_SPEED, isal_zlib.ISAL_BEST_COMPRESSION
105106
))
106107
super().__init__(filename, mode, compresslevel, fileobj, mtime)
107-
if hasattr(self, "compress"):
108+
if self.mode == gzip.WRITE:
108109
self.compress = isal_zlib.compressobj(compresslevel,
109110
isal_zlib.DEFLATED,
110111
-isal_zlib.MAX_WBITS,
@@ -169,6 +170,12 @@ def _read_eof(self):
169170
# Gzip files can be padded with zeroes and still have archives.
170171
# Consume all zero bytes and set the file position to the first
171172
# non-zero byte. See http://www.gzip.org/#faq8
173+
# Also the isal_zlib.decompressobj does not consume the last two bytes
174+
# when using ISAL_GZIP_NO_HDR.
175+
for i in range(2):
176+
c = self._fp.read(1)
177+
if c == b"\x00":
178+
break
172179
c = b"\x00"
173180
while c == b"\x00":
174181
c = self._fp.read(1)
@@ -187,12 +194,12 @@ def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None):
187194
return buf.getvalue()
188195

189196

190-
# Unlike stdlib, do not use the roundabout way of doing this via a file.
191197
def decompress(data):
192198
"""Decompress a gzip compressed string in one shot.
193199
Return the decompressed string.
194200
"""
195-
return isal_zlib.decompress(data, wbits=16 + isal_zlib.MAX_WBITS)
201+
with IGzipFile(fileobj=io.BytesIO(data)) as f:
202+
return f.read()
196203

197204

198205
def main():

tests/test_gzip_compliance.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,17 @@ def test_compresslevel_metadata(self):
391391
# see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
392392
# specifically, discussion of XFL in section 2.3.1
393393
cases = [
394-
('fast', 1, b'\x04'),
395-
('best', 9, b'\x02'),
396-
('tradeoff', 6, b'\x00'),
394+
('fast', 0, b'\x04'),
395+
('best', 3, b'\x02'),
396+
('tradeoff', 2, b'\x00'),
397397
]
398398
xflOffset = 8
399399

400400
for (name, level, expectedXflByte) in cases:
401+
major, minor, _, _, _ = sys.version_info
402+
if major == 3 and minor <=7 or major < 3:
403+
# Specific xfl bytes introduced in 3.7
404+
expectedXflByte = b'\x02'
401405
with self.subTest(name):
402406
fWrite = igzip.IGzipFile(self.filename, 'w', compresslevel=level)
403407
with fWrite:
@@ -440,14 +444,24 @@ def test_zero_padded_file(self):
440444
d = f.read()
441445
self.assertEqual(d, data1 * 50, "Incorrect data in file")
442446

447+
@unittest.skipIf(sys.version_info[0] == 3 and sys.version_info[1] < 8
448+
or sys.version_info[0] < 3,
449+
reason="BadGzipFile exception only in version 3.8 or "
450+
"higher")
443451
def test_igzip_BadGzipFile_exception(self):
444452
self.assertTrue(issubclass(igzip.BadGzipFile, OSError))
445453

446454
def test_bad_gzip_file(self):
455+
major, minor, _, _, _ = sys.version_info
456+
if major == 3 and minor >= 8 or major > 3:
457+
error = igzip.BadGzipFile
458+
else:
459+
error = OSError
460+
447461
with open(self.filename, 'wb') as file:
448462
file.write(data1 * 50)
449463
with igzip.IGzipFile(self.filename, 'r') as file:
450-
self.assertRaises(igzip.BadGzipFile, file.readlines)
464+
self.assertRaises(error, file.readlines)
451465

452466
def test_non_seekable_file(self):
453467
uncompressed = data1 * 50
@@ -518,7 +532,11 @@ def test_fileobj_mode(self):
518532
if "x" in mode:
519533
os.unlink(self.filename)
520534
with open(self.filename, mode) as f:
521-
with self.assertWarns(FutureWarning):
535+
major, minor, _, _, _ = sys.version_info
536+
if major == 3 and minor >= 9 or major > 3:
537+
with self.assertWarns(FutureWarning):
538+
g = igzip.IGzipFile(fileobj=f)
539+
else:
522540
g = igzip.IGzipFile(fileobj=f)
523541
with g:
524542
self.assertEqual(g.mode, igzip.WRITE)
@@ -562,7 +580,7 @@ def test_compress(self):
562580
def test_compress_mtime(self):
563581
mtime = 123456789
564582
for data in [data1, data2]:
565-
for args in [(), (1,), (6,), (9,)]:
583+
for args in [(), (0,), (1,), (2,), (3,)]:
566584
with self.subTest(data=data, args=args):
567585
datac = igzip.compress(data, *args, mtime=mtime)
568586
self.assertEqual(type(datac), bytes)

0 commit comments

Comments
 (0)