Skip to content

Commit 4853c0b

Browse files
authored
Merge pull request #25 from pycompression/asan
Add address sanitizer tests.
2 parents 9f76885 + 1c0c601 commit 4853c0b

File tree

4 files changed

+49
-49
lines changed

4 files changed

+49
-49
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Changelog
77
.. This document is user facing. Please word the changes in such a way
88
.. that users understand how the changes affect the new version.
99
10+
version 0.4.0-dev
11+
-----------------
12+
+ Fix some unclosed buffer errors in the gzip_ng CLI.
13+
1014
version 0.3.0
1115
-----------------
1216
+ Source distributions on Linux now default to building with configure and

src/zlib_ng/gzip_ng.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ def main():
431431
if yes_or_no not in {"y", "Y", "yes"}:
432432
sys.exit("not overwritten")
433433

434+
out_buffer = None
434435
if args.compress:
435436
if args.file is None:
436437
in_file = sys.stdin.buffer
@@ -470,6 +471,8 @@ def main():
470471
in_file.close()
471472
if out_file is not sys.stdout.buffer:
472473
out_file.close()
474+
if out_buffer is not None and out_buffer is not sys.stdout.buffer:
475+
out_buffer.close()
473476

474477

475478
if __name__ == "__main__": # pragma: no cover

tests/test_gzip_ng.py

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import os
1515
import re
1616
import shutil
17-
import subprocess
1817
import sys
1918
import tempfile
2019
import zlib
@@ -28,21 +27,6 @@
2827
DATA = b'This is a simple test with gzip_ng'
2928
COMPRESSED_DATA = gzip.compress(DATA)
3029
TEST_FILE = str((Path(__file__).parent / "data" / "test.fastq.gz"))
31-
PYPY = sys.implementation.name == "pypy"
32-
33-
34-
def run_gzip_ng(*args, stdin=None):
35-
"""Calling gzip_ng externally seems to solve some issues on PyPy where
36-
files would not be written properly when gzip_ng.main() was called. This is
37-
probably due to some out of order execution that PyPy tries to pull.
38-
Running the process externally is detrimental to the coverage report,
39-
so this is only done for PyPy."""
40-
process = subprocess.Popen(["python", "-m", "zlib_ng.gzip_ng", *args],
41-
stdout=subprocess.PIPE,
42-
stderr=subprocess.PIPE,
43-
stdin=subprocess.PIPE)
44-
45-
return process.communicate(stdin)
4630

4731

4832
def test_repr():
@@ -121,12 +105,9 @@ def test_decompress_infile_outfile(tmp_path, capsysbinary):
121105
def test_compress_infile_outfile(tmp_path, capsysbinary):
122106
test_file = tmp_path / "test"
123107
test_file.write_bytes(DATA)
124-
if PYPY:
125-
out, err = run_gzip_ng(str(test_file))
126-
else:
127-
sys.argv = ['', str(test_file)]
128-
gzip_ng.main()
129-
out, err = capsysbinary.readouterr()
108+
sys.argv = ['', str(test_file)]
109+
gzip_ng.main()
110+
out, err = capsysbinary.readouterr()
130111
out_file = test_file.with_suffix(".gz")
131112
assert err == b''
132113
assert out == b''
@@ -189,12 +170,9 @@ def test_compress_infile_out_file(tmp_path, capsysbinary):
189170
test.write_bytes(DATA)
190171
out_file = tmp_path / "compressed.gz"
191172
args = ['-o', str(out_file), str(test)]
192-
if PYPY:
193-
out, err = run_gzip_ng(*args)
194-
else:
195-
sys.argv = ['', *args]
196-
gzip_ng.main()
197-
out, err = capsysbinary.readouterr()
173+
sys.argv = ['', *args]
174+
gzip_ng.main()
175+
out, err = capsysbinary.readouterr()
198176
assert gzip.decompress(out_file.read_bytes()) == DATA
199177
assert err == b''
200178
assert out == b''
@@ -206,12 +184,9 @@ def test_compress_infile_out_file_force(tmp_path, capsysbinary):
206184
out_file = tmp_path / "compressed.gz"
207185
out_file.touch()
208186
args = ['-f', '-o', str(out_file), str(test)]
209-
if PYPY:
210-
out, err = run_gzip_ng(*args)
211-
else:
212-
sys.argv = ['', *args]
213-
gzip_ng.main()
214-
out, err = capsysbinary.readouterr()
187+
sys.argv = ['', *args]
188+
gzip_ng.main()
189+
out, err = capsysbinary.readouterr()
215190
assert gzip.decompress(out_file.read_bytes()) == DATA
216191
assert err == b''
217192
assert out == b''
@@ -254,14 +229,11 @@ def test_compress_infile_out_file_inmplicit_name_prompt_accept(
254229
test.write_bytes(DATA)
255230
out_file = tmp_path / "test.gz"
256231
out_file.touch()
257-
if PYPY:
258-
out, err = run_gzip_ng(str(test), stdin=b"y\n")
259-
else:
260-
sys.argv = ['', str(test)]
261-
mock_stdin = io.BytesIO(b"y")
262-
sys.stdin = io.TextIOWrapper(mock_stdin)
263-
gzip_ng.main()
264-
out, err = capsysbinary.readouterr()
232+
sys.argv = ['', str(test)]
233+
mock_stdin = io.BytesIO(b"y")
234+
sys.stdin = io.TextIOWrapper(mock_stdin)
235+
gzip_ng.main()
236+
out, err = capsysbinary.readouterr()
265237
assert b"already exists; do you wish to overwrite" in out
266238
assert err == b""
267239
assert gzip.decompress(out_file.read_bytes()) == DATA
@@ -271,13 +243,9 @@ def test_compress_infile_out_file_no_name(tmp_path, capsysbinary):
271243
test = tmp_path / "test"
272244
test.write_bytes(DATA)
273245
out_file = tmp_path / "compressed.gz"
274-
args = ['-n', '-o', str(out_file), str(test)]
275-
if PYPY:
276-
out, err = run_gzip_ng(*args)
277-
else:
278-
sys.argv = ['', '-n', '-o', str(out_file), str(test)]
279-
gzip_ng.main()
280-
out, err = capsysbinary.readouterr()
246+
sys.argv = ['', '-n', '-o', str(out_file), str(test)]
247+
gzip_ng.main()
248+
out, err = capsysbinary.readouterr()
281249
output = out_file.read_bytes()
282250
assert gzip.decompress(output) == DATA
283251
assert err == b''

tox.ini

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,34 @@ deps=pytest
1010
coverage
1111
passenv=
1212
PYTHON_ZLIB_NG_LINK_DYNAMIC
13+
setenv=
14+
PYTHONDEVMODE=1
1315
commands =
1416
# Create HTML coverage report for humans and xml coverage report for external services.
1517
coverage run --branch --source=zlib_ng -m pytest tests
1618
# Ignore errors during report generation. Pypy does not generate proper coverage reports.
1719
coverage html -i
1820
coverage xml -i
1921

22+
[testenv:asan]
23+
setenv=
24+
PYTHONDEVMODE=1
25+
PYTHONMALLOC=malloc
26+
CFLAGS=-lasan -fsanitize=address -fno-omit-frame-pointer
27+
allowlist_externals=bash
28+
commands=
29+
bash -c 'export LD_PRELOAD=$(gcc -print-file-name=libasan.so) && printenv LD_PRELOAD && python -c "from zlib_ng import zlib_ng" && pytest tests'
30+
31+
[testenv:compliance]
32+
deps=pytest
33+
commands=
34+
pytest -v tests/test_zlib_compliance.py tests/test_gzip_compliance.py
35+
36+
[testenv:compatibility]
37+
deps=pytest
38+
commands=
39+
pytest tests/test_isal.py
40+
2041
[testenv:lint]
2142
deps=flake8
2243
flake8-import-order
@@ -56,21 +77,25 @@ commands=
5677

5778
[testenv:benchmark-all]
5879
deps=
80+
setenv =
5981
commands=
6082
python ./benchmark_scripts/benchmark.py --all
6183

6284
[testenv:benchmark-functions]
6385
deps=
86+
setenv =
6487
commands=
6588
python ./benchmark_scripts/benchmark.py --functions
6689

6790
[testenv:benchmark-gzip]
6891
deps=
92+
setenv =
6993
commands=
7094
python ./benchmark_scripts/benchmark.py --gzip
7195

7296
[testenv:benchmark-checksums]
7397
deps=
98+
setenv =
7499
commands=
75100
python ./benchmark_scripts/benchmark.py --checksums
76101

0 commit comments

Comments
 (0)