Use with keyword for opening target file

Simply reading a file is dangerous, it might happens that the file
might be open but not closed will leads to ResourceWarning: unclosed
file error, It is necessary to use with keyword which will close the
file first and then allow it to open it.

Changing r mode to rb mode as in Python3, While r mode is used then
it reads the file based on file encoding provided but with rb mode
it reads the stream as a binary which avoides the encoding issue.

Change-Id: Ia48c1cb0e436ac2c53fc6bd46c4a906c70a51325
This commit is contained in:
Chandan Kumar 2019-01-31 12:43:32 +05:30
parent 034edf0c93
commit c59178a69a
1 changed files with 2 additions and 1 deletions

View File

@ -1089,7 +1089,8 @@ class Deploy(command.Command):
raise exceptions.DeploymentError(msg)
target = override_file
data = open(target, 'r').read()
with open(target, 'rb') as fb:
data = fb.read()
if not data.strip():
# since an empty file isn't valid yaml, let's be more specific
msg = (_("hieradata override file (%s) cannot be empty") % target)