From c7860d4d4bd57451c1ebec029e452ffbc2ae7975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20Bergstr=C3=B6m?= Date: Fri, 10 Mar 2017 16:16:45 +0100 Subject: [PATCH] Open unarchive file in binary mode 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. --- lib/ansible/modules/files/unarchive.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/ansible/modules/files/unarchive.py b/lib/ansible/modules/files/unarchive.py index 8db9f0dd2ff..195e8716984 100644 --- a/lib/ansible/modules/files/unarchive.py +++ b/lib/ansible/modules/files/unarchive.py @@ -172,7 +172,7 @@ BUFSIZE = 65536 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 ''' @@ -221,7 +221,7 @@ class ZipArchive(object): 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: @@ -240,7 +240,7 @@ class ZipArchive(object): 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') @@ -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