From f957c812ac3221773058ba2fa8cd38017537da8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Wed, 27 Jul 2016 09:34:09 +0300 Subject: [PATCH] Handle more file open/close with "with" --- gitdb/db/ref.py | 3 ++- gitdb/test/db/test_ref.py | 7 +++---- gitdb/test/test_pack.py | 5 ++--- gitdb/test/test_util.py | 10 +++------- 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 @@ def _update_dbs_from_ref_file(self): # 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 @@ def rewind_streams(): 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 @@ def rewind_streams(): # 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 @@ def test_basics(self): 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)