From 49fb3d7cb8811089ec5b8d22f79e29c8a55c1e44 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sat, 30 Mar 2024 22:48:39 +0100 Subject: [PATCH] [PR #8169/b444e873 backport][stable-8] xml: make module work with lxml 5.1.1 (#8172) xml: make module work with lxml 5.1.1 (#8169) Make module work with lxml 5.1.1. (cherry picked from commit b444e8739c4a445311709420b47026a9ebe76ac2) Co-authored-by: Felix Fontein --- changelogs/fragments/8169-lxml.yml | 2 ++ plugins/modules/xml.py | 9 +++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/8169-lxml.yml diff --git a/changelogs/fragments/8169-lxml.yml b/changelogs/fragments/8169-lxml.yml new file mode 100644 index 0000000000..e2c1b8b952 --- /dev/null +++ b/changelogs/fragments/8169-lxml.yml @@ -0,0 +1,2 @@ +bugfixes: + - "xml - make module work with lxml 5.1.1, which removed some internals that the module was relying on (https://github.com/ansible-collections/community.general/pull/8169)." diff --git a/plugins/modules/xml.py b/plugins/modules/xml.py index a3c12b8eec..f5cdbeac38 100644 --- a/plugins/modules/xml.py +++ b/plugins/modules/xml.py @@ -436,11 +436,16 @@ def is_attribute(tree, xpath, namespaces): """ Test if a given xpath matches and that match is an attribute An xpath attribute search will only match one item""" + + # lxml 5.1.1 removed etree._ElementStringResult, so we can no longer simply assume it's there + # (https://github.com/lxml/lxml/commit/eba79343d0e7ad1ce40169f60460cdd4caa29eb3) + ElementStringResult = getattr(etree, '_ElementStringResult', None) + if xpath_matches(tree, xpath, namespaces): match = tree.xpath(xpath, namespaces=namespaces) - if isinstance(match[0], etree._ElementStringResult): + if isinstance(match[0], etree._ElementUnicodeResult): return True - elif isinstance(match[0], etree._ElementUnicodeResult): + elif ElementStringResult is not None and isinstance(match[0], ElementStringResult): return True return False