From 4eff0dfdb58bf4a262ad1f08e0dd2921a4b5f2db Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 29 Nov 2016 11:42:57 +0100 Subject: Fix a ResourceWarning warnings Use context managers to ensure that file are always closed. --- testrepository/repository/file.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/testrepository/repository/file.py b/testrepository/repository/file.py index 02fcf05..6ac0240 100644 --- a/testrepository/repository/file.py +++ b/testrepository/repository/file.py @@ -69,8 +69,9 @@ class RepositoryFactory(AbstractRepositoryFactory): if e.errno == errno.ENOENT: raise RepositoryNotFound(url) raise - if '1\n' != stream.read(): - raise ValueError(url) + with stream: + if '1\n' != stream.read(): + raise ValueError(url) return Repository(base) @@ -99,7 +100,8 @@ class Repository(AbstractRepository): return value def _next_stream(self): - next_content = open(os.path.join(self.base, 'next-stream'), 'rt').read() + with open(os.path.join(self.base, 'next-stream'), 'rt') as fp: + next_content = fp.read() try: return int(next_content) except ValueError: @@ -116,8 +118,8 @@ class Repository(AbstractRepository): def get_failing(self): try: - run_subunit_content = open( - os.path.join(self.base, "failing"), 'rb').read() + with open( os.path.join(self.base, "failing"), 'rb') as fp: + run_subunit_content = fp.read() except IOError: err = sys.exc_info()[1] if err.errno == errno.ENOENT: @@ -128,8 +130,8 @@ class Repository(AbstractRepository): def get_test_run(self, run_id): try: - run_subunit_content = open( - os.path.join(self.base, str(run_id)), 'rb').read() + with open( os.path.join(self.base, str(run_id)), 'rb') as fp: + run_subunit_content = fp.read() except IOError as e: if e.errno == errno.ENOENT: raise KeyError("No such run.") -- cgit v1.2.1