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
This commit is contained in:
Federico Ressi 2018-08-23 07:51:26 +02:00
parent 537e5a8d69
commit a372532fd9
1 changed files with 7 additions and 2 deletions

View File

@ -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)