Skip to content

Handle more file open/close with "with" #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gitdb/db/ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions gitdb/test/db/test_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 2 additions & 3 deletions gitdb/test/test_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand Down
10 changes: 3 additions & 7 deletions gitdb/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down