Skip to content

Commit

Permalink
Making sure files are closed
Browse files Browse the repository at this point in the history
Fixing `ResourceWarning` (file handle leak) in `read_documents`
  • Loading branch information
Kirill888 committed May 31, 2017
1 parent a9119f6 commit 7d95ae8
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions datacube/utils/__init__.py
Expand Up @@ -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':
Expand Down

0 comments on commit 7d95ae8

Please sign in to comment.