summaryrefslogtreecommitdiff
path: root/gitdb
diff options
context:
space:
mode:
authorVille Skyttä <ville.skytta@iki.fi>2016-07-27 09:34:09 +0300
committerVille Skyttä <ville.skytta@iki.fi>2016-07-27 09:34:09 +0300
commitf957c812ac3221773058ba2fa8cd38017537da8a (patch)
tree25e1d8b81197ee3d2a3267ead992e5dbd86b335e /gitdb
parentd1996e04dbf4841b853b60c1365f0f5fd28d170c (diff)
downloadgitdb-f957c812ac3221773058ba2fa8cd38017537da8a.tar.gz
Handle more file open/close with "with"
Diffstat (limited to 'gitdb')
-rw-r--r--gitdb/db/ref.py3
-rw-r--r--gitdb/test/db/test_ref.py7
-rw-r--r--gitdb/test/test_pack.py5
-rw-r--r--gitdb/test/test_util.py10
4 files changed, 10 insertions, 15 deletions
diff --git a/gitdb/db/ref.py b/gitdb/db/ref.py
index 83a9f61..2e3db86 100644
--- a/gitdb/db/ref.py
+++ b/gitdb/db/ref.py
@@ -41,7 +41,8 @@ class ReferenceDB(CompoundDB):
# try to get as many as possible, don't fail if some are unavailable
ref_paths = list()
try:
- ref_paths = [l.strip() for l in open(self._ref_file, 'r').readlines()]
+ with open(self._ref_file, 'r') as f:
+ ref_paths = [l.strip() for l in f]
except (OSError, IOError):
pass
# END handle alternates
diff --git a/gitdb/test/db/test_ref.py b/gitdb/test/db/test_ref.py
index 6bac245..2049698 100644
--- a/gitdb/test/db/test_ref.py
+++ b/gitdb/test/db/test_ref.py
@@ -21,10 +21,9 @@ class TestReferenceDB(TestDBBase):
def make_alt_file(self, alt_path, alt_list):
"""Create an alternates file which contains the given alternates.
The list can be empty"""
- alt_file = open(alt_path, "wb")
- for alt in alt_list:
- alt_file.write(alt.encode("utf-8") + "\n".encode("ascii"))
- alt_file.close()
+ with open(alt_path, "wb") as alt_file:
+ for alt in alt_list:
+ alt_file.write(alt.encode("utf-8") + "\n".encode("ascii"))
@with_rw_directory
def test_writing(self, path):
diff --git a/gitdb/test/test_pack.py b/gitdb/test/test_pack.py
index ff10572..8ecfcd9 100644
--- a/gitdb/test/test_pack.py
+++ b/gitdb/test/test_pack.py
@@ -197,7 +197,6 @@ class TestPack(TestBase):
obj.stream.seek(0)
# END utility
for ppath, ipath, num_obj in zip((pack_path, ) * 2, (index_path, None), (len(pack_objs), None)):
- pfile = open(ppath, 'wb')
iwrite = None
if ipath:
ifile = open(ipath, 'wb')
@@ -210,8 +209,8 @@ class TestPack(TestBase):
# END rewind streams
iteration += 1
- pack_sha, index_sha = PackEntity.write_pack(pack_objs, pfile.write, iwrite, object_count=num_obj)
- pfile.close()
+ with open(ppath, 'wb') as pfile:
+ pack_sha, index_sha = PackEntity.write_pack(pack_objs, pfile.write, iwrite, object_count=num_obj)
assert os.path.getsize(ppath) > 100
# verify pack
diff --git a/gitdb/test/test_util.py b/gitdb/test/test_util.py
index 1dee544..c298920 100644
--- a/gitdb/test/test_util.py
+++ b/gitdb/test/test_util.py
@@ -25,19 +25,15 @@ class TestUtils(TestBase):
def _cmp_contents(self, file_path, data):
# raise if data from file at file_path
# does not match data string
- fp = open(file_path, "rb")
- try:
+ with open(file_path, "rb") as fp:
assert fp.read() == data.encode("ascii")
- finally:
- fp.close()
def test_lockedfd(self):
my_file = tempfile.mktemp()
orig_data = "hello"
new_data = "world"
- my_file_fp = open(my_file, "wb")
- my_file_fp.write(orig_data.encode("ascii"))
- my_file_fp.close()
+ with open(my_file, "wb") as my_file_fp:
+ my_file_fp.write(orig_data.encode("ascii"))
try:
lfd = LockedFD(my_file)