[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
parent
bde1c721e0
commit
f9e4bc85e9
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- xml - ensure file descriptor is closed (https://github.com/ansible-collections/community.general/pull/9695).
|
|
@ -923,29 +923,34 @@ def main():
|
||||||
elif LooseVersion('.'.join(to_native(f) for f in etree.LXML_VERSION)) < LooseVersion('3.0.0'):
|
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.')
|
module.warn('Using lxml version lower than 3.0.0 does not guarantee predictable element attribute order.')
|
||||||
|
|
||||||
# Check if the file exists
|
infile = None
|
||||||
if xml_string:
|
|
||||||
infile = BytesIO(to_bytes(xml_string, errors='surrogate_or_strict'))
|
|
||||||
elif os.path.isfile(xml_file):
|
|
||||||
infile = open(xml_file, 'rb')
|
|
||||||
else:
|
|
||||||
module.fail_json(msg="The target XML source '%s' does not exist." % xml_file)
|
|
||||||
|
|
||||||
# Parse and evaluate xpath expression
|
|
||||||
if xpath is not None:
|
|
||||||
try:
|
|
||||||
etree.XPath(xpath)
|
|
||||||
except etree.XPathSyntaxError as e:
|
|
||||||
module.fail_json(msg="Syntax error in xpath expression: %s (%s)" % (xpath, e))
|
|
||||||
except etree.XPathEvalError as e:
|
|
||||||
module.fail_json(msg="Evaluation error in xpath expression: %s (%s)" % (xpath, e))
|
|
||||||
|
|
||||||
# Try to parse in the target XML file
|
|
||||||
try:
|
try:
|
||||||
parser = etree.XMLParser(remove_blank_text=pretty_print, strip_cdata=strip_cdata_tags)
|
# Check if the file exists
|
||||||
doc = etree.parse(infile, parser)
|
if xml_string:
|
||||||
except etree.XMLSyntaxError as e:
|
infile = BytesIO(to_bytes(xml_string, errors='surrogate_or_strict'))
|
||||||
module.fail_json(msg="Error while parsing document: %s (%s)" % (xml_file or 'xml_string', e))
|
elif os.path.isfile(xml_file):
|
||||||
|
infile = open(xml_file, 'rb')
|
||||||
|
else:
|
||||||
|
module.fail_json(msg="The target XML source '%s' does not exist." % xml_file)
|
||||||
|
|
||||||
|
# Parse and evaluate xpath expression
|
||||||
|
if xpath is not None:
|
||||||
|
try:
|
||||||
|
etree.XPath(xpath)
|
||||||
|
except etree.XPathSyntaxError as e:
|
||||||
|
module.fail_json(msg="Syntax error in xpath expression: %s (%s)" % (xpath, e))
|
||||||
|
except etree.XPathEvalError as e:
|
||||||
|
module.fail_json(msg="Evaluation error in xpath expression: %s (%s)" % (xpath, e))
|
||||||
|
|
||||||
|
# Try to parse in the target XML file
|
||||||
|
try:
|
||||||
|
parser = etree.XMLParser(remove_blank_text=pretty_print, strip_cdata=strip_cdata_tags)
|
||||||
|
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
|
# Ensure we have the original copy to compare
|
||||||
global orig_doc
|
global orig_doc
|
||||||
|
|
Loading…
Reference in New Issue