Skip to content

Commit

Permalink
Make sure that configuration files are closed when loading them (#1423)
Browse files Browse the repository at this point in the history
  • Loading branch information
saulbein authored Jul 24, 2024
1 parent ada5fce commit 8dc46e2
Showing 1 changed file with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,25 +316,24 @@ def get(

if conf_file:
try:
config_from_file = yaml.safe_load(open(conf_file))
with open(conf_file) as file:
config_from_file = yaml.safe_load(file)

except OSError:
logger.warning(
f"configuration file {conf_file} not found. "
f"Using default config."
)
config_from_file = yaml.safe_load(
open(RecognizerConfigurationLoader._get_full_conf_path())
)
with open(RecognizerConfigurationLoader._get_full_conf_path()) as file:
config_from_file = yaml.safe_load(file)

except Exception as e:
raise ValueError(
f"Failed to parse file {conf_file}." f"Error: {str(e)}"
)
else:
config_from_file = yaml.safe_load(
open(RecognizerConfigurationLoader._get_full_conf_path())
)
with open(RecognizerConfigurationLoader._get_full_conf_path()) as file:
config_from_file = yaml.safe_load(file)

if config_from_file and not isinstance(config_from_file, dict):
raise TypeError(
Expand Down

0 comments on commit 8dc46e2

Please sign in to comment.