From 12662c974d4016642a20e2a145c8e6ca48df7faa Mon Sep 17 00:00:00 2001 From: Max Linke Date: Sat, 16 Dec 2017 23:57:05 +0100 Subject: [PATCH 1/2] use contextmanager to open/close files This helps cleaning up files access and ensures that they are closed again. This will stop the leaks happening. --- mmtf/api/default_api.py | 3 ++- mmtf/api/mmtf_writer.py | 4 ++-- mmtf/tests/codec_tests.py | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/mmtf/api/default_api.py b/mmtf/api/default_api.py index 66cb6c5..5593c97 100644 --- a/mmtf/api/default_api.py +++ b/mmtf/api/default_api.py @@ -87,7 +87,8 @@ def parse(file_path): :param file_path: the input file path. Data is not entropy compressed (e.g. gzip) :return an API to decoded data """ newDecoder = MMTFDecoder() - newDecoder.decode_data(msgpack.unpackb(open(file_path, "rb").read())) + with open(file_path, "rb") as fh: + newDecoder.decode_data(msgpack.unpackb(fh.read())) return newDecoder diff --git a/mmtf/api/mmtf_writer.py b/mmtf/api/mmtf_writer.py index a481c1f..f80ad53 100644 --- a/mmtf/api/mmtf_writer.py +++ b/mmtf/api/mmtf_writer.py @@ -256,8 +256,8 @@ def get_msgpack(self): def write_file(self, file_path): - out_f = open(file_path, "wb") - out_f.write(self.get_msgpack()) + with open(file_path, "wb") as out_f: + out_f.write(self.get_msgpack()) def init_structure(self, total_num_bonds, total_num_atoms, diff --git a/mmtf/tests/codec_tests.py b/mmtf/tests/codec_tests.py index 3012acc..09b03df 100644 --- a/mmtf/tests/codec_tests.py +++ b/mmtf/tests/codec_tests.py @@ -190,7 +190,8 @@ def test_round_trip(self): decoded.decode_data(msgpack.unpackb(packed)) def test_gzip_open(self): - ungzip_data(open("mmtf/tests/testdatastore/4CUP.mmtf.gz","rb").read()) + with open("mmtf/tests/testdatastore/4CUP.mmtf.gz","rb") as fh: + ungzip_data(fh.read()) def test_fetch(self): if _internet_on(BASE_URL): From 25a375418fe8b93a374d8a1022f75d44c5cd15b6 Mon Sep 17 00:00:00 2001 From: Max Linke Date: Sun, 17 Dec 2017 00:00:11 +0100 Subject: [PATCH 2/2] update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c61999b..113bfd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,3 +61,7 @@ All notable changes to this project will be documented in this file, following t ## v1.0.8 - 2017-08-17 ### Fixed - Changing ascii to utf8 #29 + +## v1.0.x - 20XX-XX-XX +### Fixed +- Don't leak open file handles #32