always use binary file write mode on windows

At the event of this commit there are, including tests, 626 uses of
salt.utils.fopen in the salt codebase, 477 of which do not set binary
mode.  Files should always be written in binary mode on windows, so
ensure it always there for windows.  It most likely would not be a
problem for other systems, but to be safe, only set it for windows.  As
it is required on windows, we can solve all occurrences and potential
occurrences of this problem in one place.
This commit is contained in:
Justin Findlay 2015-05-14 14:40:40 -06:00
parent cdf421b9ed
commit a978f5c091

View file

@ -1028,6 +1028,19 @@ def fopen(*args, **kwargs):
)
return flopen(*args, **kwargs)
# ensure 'binary' mode is always used on windows
if is_windows():
if len(args) > 1:
args = list(args)
if 'b' not in args[1]:
args[1] += 'b'
elif kwargs.get('mode', None):
if 'b' not in kwargs['mode']:
kwargs['mode'] += 'b'
else:
# the default is to read
kwargs['mode'] = 'rb'
fhandle = open(*args, **kwargs)
if is_fcntl_available():
# modify the file descriptor on systems with fcntl