From a372532fd92211e9f14e2372ef2b740b5bf2f06c Mon Sep 17 00:00:00 2001 From: Federico Ressi Date: Thu, 23 Aug 2018 07:51:26 +0200 Subject: [PATCH] Close descriptor after reading file. This change has been motivated by below ResourceWarning Python3 is emitting when using this library: ResourceWarning: unclosed file <_io.TextIOWrapper name='.../site-packages/os_service_types/data/service-types.json' mode='r' encoding='UTF-8'> return json.load(open(os.path.join(DATA_DIR, filename), 'r')) Change-Id: Id86cdedab44bf4096185ced2ed6b9379a71bc3e1 --- os_service_types/data/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/os_service_types/data/__init__.py b/os_service_types/data/__init__.py index bc3c704..68991b0 100644 --- a/os_service_types/data/__init__.py +++ b/os_service_types/data/__init__.py @@ -21,5 +21,10 @@ DATA_DIR = os.path.dirname(__file__) def read_data(filename): - """Return data that is shipped inside the python package.""" - return json.load(open(os.path.join(DATA_DIR, filename), 'r')) + """Return data that is shipped inside the Python package. + + """ + + filepath = os.path.join(DATA_DIR, filename) + with open(filepath, 'r') as fd: + return json.load(fd)