bugfix: f.close() only required if open succeeded

when open fails, instead of raising the original error, the "f.close()" was erroring in the final clause.
This commit is contained in:
yonatanp 2018-01-05 11:33:47 +02:00 committed by GitHub
parent 5707a39df6
commit a7ad636dab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -309,9 +309,10 @@ def parse_requirements(file_):
logging.error("Failed on file: {}".format(file_)) logging.error("Failed on file: {}".format(file_))
raise raise
else: else:
data = [x.strip() for x in f.readlines() if x != "\n"] try:
finally: data = [x.strip() for x in f.readlines() if x != "\n"]
f.close() finally:
f.close()
data = [x for x in data if x[0].isalpha()] data = [x for x in data if x[0].isalpha()]
@ -375,16 +376,17 @@ def clean(file_, imports):
logging.error("Failed on file: {}".format(file_)) logging.error("Failed on file: {}".format(file_))
raise raise
else: else:
for i in f.readlines(): try:
if re_remove.match(i) is None: for i in f.readlines():
to_write.append(i) if re_remove.match(i) is None:
f.seek(0) to_write.append(i)
f.truncate() f.seek(0)
f.truncate()
for i in to_write: for i in to_write:
f.write(i) f.write(i)
finally: finally:
f.close() f.close()
logging.info("Successfully cleaned up requirements in " + file_) logging.info("Successfully cleaned up requirements in " + file_)