Skip to content

Commit b7d21ed

Browse files
authored
Merge pull request #20 from pycompression/headerbug
Fix bug that occurred when writing the header.
2 parents 44fa4ef + c88a0cc commit b7d21ed

File tree

6 files changed

+93
-52
lines changed

6 files changed

+93
-52
lines changed

.github/workflows/ci.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Continous integration
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- develop
8+
- master
9+
10+
11+
jobs:
12+
lint:
13+
runs-on: ubuntu-20.04
14+
strategy:
15+
matrix:
16+
python-version:
17+
- 3.6
18+
steps:
19+
- uses: actions/[email protected]
20+
- name: Set up Python ${{ matrix.python-version }}
21+
uses: actions/setup-python@v2
22+
- name: Install tox
23+
run: pip install tox
24+
- name: Lint
25+
run: tox -e lint
26+
docs:
27+
needs: lint
28+
runs-on: ubuntu-20.04
29+
steps:
30+
- uses: actions/[email protected]
31+
- name: Set up Python ${{ matrix.python-version }}
32+
uses: actions/setup-python@v2
33+
- name: Install tox
34+
run: pip install tox
35+
- name: Install isal
36+
run: sudo apt-get install libisal-dev
37+
- name: Build docs
38+
run: tox -e docs
39+
test:
40+
runs-on: ubuntu-20.04
41+
strategy:
42+
matrix:
43+
python-version:
44+
- 3.6
45+
- 3.7
46+
- 3.8
47+
- 3.9
48+
needs: lint
49+
steps:
50+
- uses: actions/[email protected]
51+
- name: Set up Python ${{ matrix.python-version }}
52+
uses: actions/setup-python@v2
53+
- name: Install tox
54+
run: pip install tox
55+
- name: Install isal
56+
run: sudo apt-get install libisal-dev
57+
- name: Run tests
58+
run: tox -e py3
59+
- name: Upload coverage report
60+
if: ${{ matrix.python-version == 3.6 }} # Only upload coverage once
61+
uses: codecov/codecov-action@v1

.travis.yml

Lines changed: 0 additions & 37 deletions
This file was deleted.

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Changelog
99
1010
version 0.2.0-dev
1111
-----------------
12+
+ Fixed a bug where writing of the gzip header would crash if an older version
13+
of Python 3.7 was used such as on Debian or Ubuntu. This is due to
14+
differences between point releases because of a backported feature. The code
15+
now checks if the backported feature is present.
1216
+ Added Python 3.9 to the testing.
1317
+ Fixed ``setup.py`` to list setuptools as a requirement.
1418
+ Changed homepage to reflect move to pycompression organization.

src/isal/igzip.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,27 @@ def flush(self, zlib_mode=isal_zlib.Z_SYNC_FLUSH):
159159
super().flush(zlib_mode)
160160

161161
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")):
169177
if compresslevel == _COMPRESS_LEVEL_FAST:
170-
compresslevel = gzip._COMPRESS_LEVEL_FAST
178+
super()._write_gzip_header(gzip._COMPRESS_LEVEL_FAST)
171179
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()
174183

175184
def write(self, data):
176185
self._check_not_closed()

tests/test_gzip_compliance.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,11 @@ def test_metadata(self):
365365
struct.pack('<i', mtime)) # little-endian
366366

367367
xflByte = fRead.read(1)
368-
if sys.version_info[0] == 3 and sys.version_info[1] < 7:
369-
self.assertEqual(xflByte, b'\x02') # maximum compression
368+
if ("compresslevel" in
369+
gzip.GzipFile._write_gzip_header.__code__.co_varnames):
370+
self.assertEqual(xflByte, b'\x00') # fast compression
370371
else:
371-
self.assertEqual(xflByte, b'\x00') # medium compression
372+
self.assertEqual(xflByte, b'\x02') # maximum compression
372373
osByte = fRead.read(1)
373374
self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent)
374375

@@ -399,8 +400,9 @@ def test_compresslevel_metadata(self):
399400
# specifically, discussion of XFL in section 2.3.1
400401
cases = [
401402
('fast', 0, b'\x04'),
402-
('best', 3, b'\x00'), # Comparable to medium gzip level.
403-
('tradeoff', 2, b'\x00'), # Dito
403+
('best', 3, b'\x00'), # Smaller than fast, bigger than best gzip.
404+
('tradeoff', 2, b'\x00'), # therefore medium is appropriate.
405+
('1', 1, b'\x00')
404406
]
405407
xflOffset = 8
406408

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ commands=
2525
deps=flake8
2626
flake8-import-order
2727
mypy
28+
# Linting does not need the installed package.
29+
skip_install=true
2830
commands =
2931
flake8 src tests setup.py benchmark.py
3032

0 commit comments

Comments
 (0)