Skip to content

Commit 4da3bc5

Browse files
committed
Fix resource warning when using the CLI
1 parent 18e3c7c commit 4da3bc5

File tree

2 files changed

+20
-49
lines changed

2 files changed

+20
-49
lines changed

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''

0 commit comments

Comments
 (0)