[PR #9695/191a4d8f backport][stable-9] xml: ensure the stream object is closed in main() (#9711)

xml: ensure the stream object is closed in main() (#9695)

* ensure the stream object is closed in main()

* add changelog frag

* Update plugins/modules/xml.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/xml.py

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 191a4d8f63)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
patchback/backports/stable-9/d5add1ed9f3addb4c99f6c3aae9002f033a8e9e7/pr-9694
patchback[bot] 2025-02-09 14:26:42 +01:00 committed by GitHub
parent bde1c721e0
commit f9e4bc85e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 22 deletions

View File

@ -0,0 +1,2 @@
bugfixes:
- xml - ensure file descriptor is closed (https://github.com/ansible-collections/community.general/pull/9695).

View File

@ -923,6 +923,8 @@ def main():
elif LooseVersion('.'.join(to_native(f) for f in etree.LXML_VERSION)) < LooseVersion('3.0.0'):
module.warn('Using lxml version lower than 3.0.0 does not guarantee predictable element attribute order.')
infile = None
try:
# Check if the file exists
if xml_string:
infile = BytesIO(to_bytes(xml_string, errors='surrogate_or_strict'))
@ -946,6 +948,9 @@ def main():
doc = etree.parse(infile, parser)
except etree.XMLSyntaxError as e:
module.fail_json(msg="Error while parsing document: %s (%s)" % (xml_file or 'xml_string', e))
finally:
if infile:
infile.close()
# Ensure we have the original copy to compare
global orig_doc