Merge pull request #4426 from shin-/4392-context-mgr

Close the open file handle using context manager
This commit is contained in:
Joffrey F 2017-02-06 13:30:49 -08:00 committed by GitHub
commit 1636985a7a
1 changed files with 7 additions and 5 deletions

View File

@ -2,6 +2,7 @@ from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import codecs import codecs
import contextlib
import logging import logging
import os import os
@ -31,11 +32,12 @@ def env_vars_from_file(filename):
elif not os.path.isfile(filename): elif not os.path.isfile(filename):
raise ConfigurationError("%s is not a file." % (filename)) raise ConfigurationError("%s is not a file." % (filename))
env = {} env = {}
for line in codecs.open(filename, 'r', 'utf-8'): with contextlib.closing(codecs.open(filename, 'r', 'utf-8')) as fileobj:
line = line.strip() for line in fileobj:
if line and not line.startswith('#'): line = line.strip()
k, v = split_env(line) if line and not line.startswith('#'):
env[k] = v k, v = split_env(line)
env[k] = v
return env return env