From 3e740112a079b2a2d42de5ba34ee87d773e96e34 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 22 Mar 2023 13:15:55 +0100 Subject: [PATCH] [PR #6180/f66cc7c9 backport][stable-5] Replace deprecated error with BadZipFile (#6215) Replace deprecated error with BadZipFile (#6180) * Replace deprecated error with BadZipFile * Use imported BadZipFile Co-authored-by: Felix Fontein * Add news fragment * Update new fragment Co-authored-by: Felix Fontein --------- Co-authored-by: Felix Fontein (cherry picked from commit f66cc7c933316c9c0094564d4b143df58375c2f8) Co-authored-by: Hugo van Kemenade --- changelogs/fragments/6180-replace-deprecated-badzipfile.yml | 2 ++ plugins/modules/files/archive.py | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/6180-replace-deprecated-badzipfile.yml diff --git a/changelogs/fragments/6180-replace-deprecated-badzipfile.yml b/changelogs/fragments/6180-replace-deprecated-badzipfile.yml new file mode 100644 index 0000000000..7e0916a8d9 --- /dev/null +++ b/changelogs/fragments/6180-replace-deprecated-badzipfile.yml @@ -0,0 +1,2 @@ +bugfixes: + - "archive - avoid deprecated exception class on Python 3 (https://github.com/ansible-collections/community.general/pull/6180)." diff --git a/plugins/modules/files/archive.py b/plugins/modules/files/archive.py index 83eae34f56..5aaab4e0d0 100644 --- a/plugins/modules/files/archive.py +++ b/plugins/modules/files/archive.py @@ -191,6 +191,10 @@ from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.common.text.converters import to_bytes, to_native from ansible.module_utils import six +try: # python 3.2+ + from zipfile import BadZipFile # type: ignore[attr-defined] +except ImportError: # older python + from zipfile import BadZipfile as BadZipFile LZMA_IMP_ERR = None if six.PY3: @@ -527,7 +531,7 @@ class ZipArchive(Archive): archive = zipfile.ZipFile(_to_native_ascii(path), 'r') checksums = set((info.filename, info.CRC) for info in archive.infolist()) archive.close() - except zipfile.BadZipfile: + except BadZipFile: checksums = set() return checksums