Skip to content

Commit

Permalink
Open unarchive file in binary mode
Browse files Browse the repository at this point in the history
Recent Python3 versions require open() to specify binary mode if the data is anything other than text.

Python3: Use int() instead of long() in unarchive

Changes long() to int() for CRC values in the unarchive module. Affects unarchiving of zip files. Since CRC values in zipfile are 32 bits the behaviour should be unchanged even in Python 2.
  • Loading branch information
kabergstrom authored and abadger committed Apr 7, 2017
1 parent 1963e50 commit c7860d4
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/ansible/modules/files/unarchive.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@

def crc32(path):
''' Return a CRC32 checksum of a file '''
return binascii.crc32(open(path).read()) & 0xffffffff
return binascii.crc32(open(path, 'rb').read()) & 0xffffffff

def shell_escape(string):
''' Quote meta-characters in the args for the unix shell '''
Expand Down Expand Up @@ -221,7 +221,7 @@ def _legacy_file_list(self, force_refresh=False):
for line in out.splitlines()[3:-2]:
fields = line.split(None, 7)
self._files_in_archive.append(fields[7])
self._infodict[fields[7]] = long(fields[6])
self._infodict[fields[7]] = int(fields[6])

def _crc32(self, path):
if self._infodict:
Expand All @@ -240,7 +240,7 @@ def _crc32(self, path):
else:
try:
for item in archive.infolist():
self._infodict[item.filename] = long(item.CRC)
self._infodict[item.filename] = int(item.CRC)
except:
archive.close()
raise UnarchiveError('Unable to list files in the archive')
Expand Down Expand Up @@ -803,6 +803,7 @@ def main():
# If download fails, raise a proper exception
if rsp is None:
raise Exception(info['msg'])

# open in binary mode for python3
f = open(package, 'wb')
# Read 1kb at a time to save on ram
Expand Down

0 comments on commit c7860d4

Please sign in to comment.