Skip to content

Commit 3a1cd20

Browse files
committed
Fix issue with unclosed file in igzip.main
1 parent e75a63f commit 3a1cd20

File tree

2 files changed

+20
-47
lines changed

2 files changed

+20
-47
lines changed

src/isal/igzip.py

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

340+
out_buffer = None
340341
if args.compress:
341342
if args.file is None:
342343
in_file = sys.stdin.buffer
@@ -374,6 +375,8 @@ def main():
374375
in_file.close()
375376
if out_file is not sys.stdout.buffer:
376377
out_file.close()
378+
if out_buffer is not None and out_buffer is not sys.stdout.buffer:
379+
out_buffer.close()
377380

378381

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

tests/test_igzip.py

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,6 @@
2929
DATA = b'This is a simple test with igzip'
3030
COMPRESSED_DATA = gzip.compress(DATA)
3131
TEST_FILE = str((Path(__file__).parent / "data" / "test.fastq.gz"))
32-
PYPY = sys.implementation.name == "pypy"
33-
34-
35-
def run_isal_igzip(*args, stdin=None):
36-
"""Calling isal.igzip externally seems to solve some issues on PyPy where
37-
files would not be written properly when igzip.main() was called. This is
38-
probably due to some out of order execution that PyPy tries to pull.
39-
Running the process externally is detrimental to the coverage report,
40-
so this is only done for PyPy."""
41-
process = subprocess.Popen(["python", "-m", "isal.igzip", *args],
42-
stdout=subprocess.PIPE,
43-
stderr=subprocess.PIPE,
44-
stdin=subprocess.PIPE)
45-
46-
return process.communicate(stdin)
4732

4833

4934
def test_wrong_compresslevel_igzipfile():
@@ -128,12 +113,9 @@ def test_decompress_infile_outfile(tmp_path, capsysbinary):
128113
def test_compress_infile_outfile(tmp_path, capsysbinary):
129114
test_file = tmp_path / "test"
130115
test_file.write_bytes(DATA)
131-
if PYPY:
132-
out, err = run_isal_igzip(str(test_file))
133-
else:
134-
sys.argv = ['', str(test_file)]
135-
igzip.main()
136-
out, err = capsysbinary.readouterr()
116+
sys.argv = ['', str(test_file)]
117+
igzip.main()
118+
out, err = capsysbinary.readouterr()
137119
out_file = test_file.with_suffix(".gz")
138120
assert err == b''
139121
assert out == b''
@@ -196,12 +178,9 @@ def test_compress_infile_out_file(tmp_path, capsysbinary):
196178
test.write_bytes(DATA)
197179
out_file = tmp_path / "compressed.gz"
198180
args = ['-o', str(out_file), str(test)]
199-
if PYPY:
200-
out, err = run_isal_igzip(*args)
201-
else:
202-
sys.argv = ['', *args]
203-
igzip.main()
204-
out, err = capsysbinary.readouterr()
181+
sys.argv = ['', *args]
182+
igzip.main()
183+
out, err = capsysbinary.readouterr()
205184
assert gzip.decompress(out_file.read_bytes()) == DATA
206185
assert err == b''
207186
assert out == b''
@@ -213,12 +192,9 @@ def test_compress_infile_out_file_force(tmp_path, capsysbinary):
213192
out_file = tmp_path / "compressed.gz"
214193
out_file.touch()
215194
args = ['-f', '-o', str(out_file), str(test)]
216-
if PYPY:
217-
out, err = run_isal_igzip(*args)
218-
else:
219-
sys.argv = ['', *args]
220-
igzip.main()
221-
out, err = capsysbinary.readouterr()
195+
sys.argv = ['', *args]
196+
igzip.main()
197+
out, err = capsysbinary.readouterr()
222198
assert gzip.decompress(out_file.read_bytes()) == DATA
223199
assert err == b''
224200
assert out == b''
@@ -261,14 +237,11 @@ def test_compress_infile_out_file_inmplicit_name_prompt_accept(
261237
test.write_bytes(DATA)
262238
out_file = tmp_path / "test.gz"
263239
out_file.touch()
264-
if PYPY:
265-
out, err = run_isal_igzip(str(test), stdin=b"y\n")
266-
else:
267-
sys.argv = ['', str(test)]
268-
mock_stdin = io.BytesIO(b"y")
269-
sys.stdin = io.TextIOWrapper(mock_stdin)
270-
igzip.main()
271-
out, err = capsysbinary.readouterr()
240+
sys.argv = ['', str(test)]
241+
mock_stdin = io.BytesIO(b"y")
242+
sys.stdin = io.TextIOWrapper(mock_stdin)
243+
igzip.main()
244+
out, err = capsysbinary.readouterr()
272245
assert b"already exists; do you wish to overwrite" in out
273246
assert err == b""
274247
assert gzip.decompress(out_file.read_bytes()) == DATA
@@ -279,12 +252,9 @@ def test_compress_infile_out_file_no_name(tmp_path, capsysbinary):
279252
test.write_bytes(DATA)
280253
out_file = tmp_path / "compressed.gz"
281254
args = ['-n', '-o', str(out_file), str(test)]
282-
if PYPY:
283-
out, err = run_isal_igzip(*args)
284-
else:
285-
sys.argv = ['', '-n', '-o', str(out_file), str(test)]
286-
igzip.main()
287-
out, err = capsysbinary.readouterr()
255+
sys.argv = ['', '-n', '-o', str(out_file), str(test)]
256+
igzip.main()
257+
out, err = capsysbinary.readouterr()
288258
output = out_file.read_bytes()
289259
assert gzip.decompress(output) == DATA
290260
assert err == b''

0 commit comments

Comments
 (0)