From 7d95ae87a7e48a0df3ced24b36c80ffd2b23eb01 Mon Sep 17 00:00:00 2001 From: Kirill Kouzoubov Date: Wed, 31 May 2017 14:06:09 +1000 Subject: [PATCH] Making sure files are closed Fixing `ResourceWarning` (file handle leak) in `read_documents` --- datacube/utils/__init__.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/datacube/utils/__init__.py b/datacube/utils/__init__.py index 8d405331eb..03f41be6a4 100644 --- a/datacube/utils/__init__.py +++ b/datacube/utils/__init__.py @@ -263,13 +263,15 @@ def read_documents(*paths): if suffix in ('.yaml', '.yml'): try: - for parsed_doc in yaml.load_all(opener(str(path), 'r'), Loader=NoDatesSafeLoader): - yield path, parsed_doc + with opener(str(path), 'r') as file: + for parsed_doc in yaml.load_all(file, Loader=NoDatesSafeLoader): + yield path, parsed_doc except yaml.YAMLError as e: raise InvalidDocException('Failed to load %s: %s' % (path, e)) elif suffix == '.json': try: - yield path, json.load(opener(str(path), 'r')) + with opener(str(path), 'r') as file: + yield path, json.load(file) except ValueError as e: raise InvalidDocException('Failed to load %s: %s' % (path, e)) elif suffix == '.nc':