From 1c156c0d16d2d6a814c66aeb6b313df660dd866d Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Sun, 12 Mar 2023 21:39:30 +0200 Subject: [PATCH] Replace deprecated BadZipfile with BadZipFile (#80198) * Replace deprecated BadZipfile with BadZipFile * Retain support for Python 2.7 --- lib/ansible/modules/unarchive.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/ansible/modules/unarchive.py b/lib/ansible/modules/unarchive.py index 60147808b61..bb081ffbb1f 100644 --- a/lib/ansible/modules/unarchive.py +++ b/lib/ansible/modules/unarchive.py @@ -253,7 +253,7 @@ import stat import time import traceback from functools import partial -from zipfile import ZipFile, BadZipfile +from zipfile import ZipFile from ansible.module_utils._text import to_bytes, to_native, to_text from ansible.module_utils.basic import AnsibleModule @@ -266,6 +266,11 @@ try: # python 3.3+ except ImportError: # older python from pipes import quote +try: # python 3.2+ + from zipfile import BadZipFile # type: ignore[attr-defined] +except ImportError: # older python + from zipfile import BadZipfile as BadZipFile + # String from tar that shows the tar contents are different from the # filesystem OWNER_DIFF_RE = re.compile(r': Uid differs$') @@ -351,7 +356,7 @@ class ZipArchive(object): try: archive = ZipFile(self.src) - except BadZipfile as e: + except BadZipFile as e: if e.args[0].lower().startswith('bad magic number'): # Python2.4 can't handle zipfiles with > 64K files. Try using # /usr/bin/unzip instead @@ -376,7 +381,7 @@ class ZipArchive(object): self._files_in_archive = [] try: archive = ZipFile(self.src) - except BadZipfile as e: + except BadZipFile as e: if e.args[0].lower().startswith('bad magic number'): # Python2.4 can't handle zipfiles with > 64K files. Try using # /usr/bin/unzip instead