Skip to content

Commit

Permalink
Merge pull request #111 from yonatanp/master
Browse files Browse the repository at this point in the history
bugfix: f.close() only required if open succeeded
  • Loading branch information
bndr authored Nov 14, 2019
2 parents 05a28a4 + a7ad636 commit 6ca1f42
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions pipreqs/pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,10 @@ def parse_requirements(file_):
logging.error("Failed on file: {}".format(file_))
raise
else:
data = [x.strip() for x in f.readlines() if x != "\n"]
finally:
f.close()
try:
data = [x.strip() for x in f.readlines() if x != "\n"]
finally:
f.close()

data = [x for x in data if x[0].isalpha()]

Expand Down Expand Up @@ -376,16 +377,17 @@ def clean(file_, imports):
logging.error("Failed on file: {}".format(file_))
raise
else:
for i in f.readlines():
if re_remove.match(i) is None:
to_write.append(i)
f.seek(0)
f.truncate()

for i in to_write:
f.write(i)
finally:
f.close()
try:
for i in f.readlines():
if re_remove.match(i) is None:
to_write.append(i)
f.seek(0)
f.truncate()

for i in to_write:
f.write(i)
finally:
f.close()

logging.info("Successfully cleaned up requirements in " + file_)

Expand Down

0 comments on commit 6ca1f42

Please sign in to comment.