From 09d56cab42e956145610f629919dcfa6e64e191f Mon Sep 17 00:00:00 2001 From: rawat-he Date: Thu, 20 Aug 2020 10:33:07 +1000 Subject: [PATCH] When using lookup plugin yang_json2xml with python3 getting below error msg: 'An unhandled exception occurred while running the lookup plugin ''yang_json2xml''. Error was a , original message: Error while reading xml document: cannot use a string pattern on a bytes-like object' Make this plugin python3 compatible. Signed-off-by: rawat-he --- lookup_plugins/yang_json2xml.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lookup_plugins/yang_json2xml.py b/lookup_plugins/yang_json2xml.py index 80dab37..bf11883 100644 --- a/lookup_plugins/yang_json2xml.py +++ b/lookup_plugins/yang_json2xml.py @@ -201,8 +201,10 @@ def run(self, terms, variables, **kwargs): try: json2xml_exec.main() with open(xml_file_path, 'r+') as fp: - content = fp.read() - + b_content = fp.read() + content = to_text(b_content, errors='surrogate_or_strict') + except UnicodeError as uni_error: + raise AnsibleError('Error while translating to text: %s' % str(uni_error)) except SystemExit: pass finally: @@ -225,7 +227,7 @@ def run(self, terms, variables, **kwargs): if not keep_tmp_files: temp_dir = os.path.join(JSON2XML_DIR_PATH, plugin_instance) shutil.rmtree(os.path.realpath(os.path.expanduser(temp_dir)), ignore_errors=True) - res.append(etree.tostring(root)) + res.append(etree.tostring(root).decode('utf-8')) return res