summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2016-12-07 13:01:29 +1300
committerThomi Richards <thomir@gmail.com>2016-12-07 13:01:29 +1300
commit0f1d622d25a96bf36cb5b635db74f047fe9b834c (patch)
treeed327ada88c613107ec5dbe5c52e192212d233a2
parent7b12bac90daad5b136b180303ac4f998f6a4df7d (diff)
downloadtestrepository-trunk.tar.gz
Fix a ResourceWarning warningsHEADtrunk
Use context managers to ensure that file are always closed.
-rw-r--r--testrepository/repository/file.py16
1 files 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.")