From 3a46149de1e58ce72563c4011bfee64781bc4af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Mon, 4 Feb 2019 10:00:30 +0100 Subject: [PATCH] Fix several ResourceWarning: unclosed file (#741) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mickaƫl Schoentgen --- httpie/input.py | 4 +-- tests/test_downloads.py | 59 ++++++++++++++++++++++------------------- tests/utils.py | 2 ++ 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/httpie/input.py b/httpie/input.py index c753bcc1..1ab34c9b 100644 --- a/httpie/input.py +++ b/httpie/input.py @@ -752,7 +752,7 @@ def parse_items(items, def readable_file_arg(filename): try: - open(filename, 'rb') + with open(filename, 'rb'): + return filename except IOError as ex: raise ArgumentTypeError('%s: %s' % (filename, ex.args[1])) - return filename diff --git a/tests/test_downloads.py b/tests/test_downloads.py index a678d0d8..5356721f 100644 --- a/tests/test_downloads.py +++ b/tests/test_downloads.py @@ -131,35 +131,38 @@ class TestDownloads: assert body == r def test_download_with_Content_Length(self, httpbin_both): - devnull = open(os.devnull, 'w') - downloader = Downloader(output_file=devnull, progress_file=devnull) - downloader.start(Response( - url=httpbin_both.url + '/', - headers={'Content-Length': 10} - )) - time.sleep(1.1) - downloader.chunk_downloaded(b'12345') - time.sleep(1.1) - downloader.chunk_downloaded(b'12345') - downloader.finish() - assert not downloader.interrupted + with open(os.devnull, 'w') as devnull: + downloader = Downloader(output_file=devnull, progress_file=devnull) + downloader.start(Response( + url=httpbin_both.url + '/', + headers={'Content-Length': 10} + )) + time.sleep(1.1) + downloader.chunk_downloaded(b'12345') + time.sleep(1.1) + downloader.chunk_downloaded(b'12345') + downloader.finish() + assert not downloader.interrupted + downloader._progress_reporter.join() def test_download_no_Content_Length(self, httpbin_both): - devnull = open(os.devnull, 'w') - downloader = Downloader(output_file=devnull, progress_file=devnull) - downloader.start(Response(url=httpbin_both.url + '/')) - time.sleep(1.1) - downloader.chunk_downloaded(b'12345') - downloader.finish() - assert not downloader.interrupted + with open(os.devnull, 'w') as devnull: + downloader = Downloader(output_file=devnull, progress_file=devnull) + downloader.start(Response(url=httpbin_both.url + '/')) + time.sleep(1.1) + downloader.chunk_downloaded(b'12345') + downloader.finish() + assert not downloader.interrupted + downloader._progress_reporter.join() def test_download_interrupted(self, httpbin_both): - devnull = open(os.devnull, 'w') - downloader = Downloader(output_file=devnull, progress_file=devnull) - downloader.start(Response( - url=httpbin_both.url + '/', - headers={'Content-Length': 5} - )) - downloader.chunk_downloaded(b'1234') - downloader.finish() - assert downloader.interrupted + with open(os.devnull, 'w') as devnull: + downloader = Downloader(output_file=devnull, progress_file=devnull) + downloader.start(Response( + url=httpbin_both.url + '/', + headers={'Content-Length': 5} + )) + downloader.chunk_downloaded(b'1234') + downloader.finish() + assert downloader.interrupted + downloader._progress_reporter.join() diff --git a/tests/utils.py b/tests/utils.py index c9b3575e..23bca713 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -62,6 +62,8 @@ class MockEnvironment(Environment): return super(MockEnvironment, self).config def cleanup(self): + self.stdout.close() + self.stderr.close() if self._delete_config_dir: assert self.config_dir.startswith(tempfile.gettempdir()) from shutil import rmtree