From 5a0c84a8ff1fccc7bade833ab86286953f4856ae Mon Sep 17 00:00:00 2001 From: Hugbot Date: Wed, 6 Jun 2018 14:45:36 -0700 Subject: [PATCH 1/4] Fix XML responses to not throw errors for unicode characters Changed `splunklib/data.py` `load` function to convert text provided into a utf-8 string so that Python XML parser will not throw errors when processing utf-8 encoded responses. --- splunklib/data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/splunklib/data.py b/splunklib/data.py index 41a0449a6..05298b6e8 100644 --- a/splunklib/data.py +++ b/splunklib/data.py @@ -76,7 +76,7 @@ def load(text, match=None): 'namespaces': [], 'names': {} } - root = XML(text) + root = XML(text.encode('utf-8')) items = [root] if match is None else root.findall(match) count = len(items) if count == 0: From 887440430c5b42c82ba1f55bbaef06fbe9f32b24 Mon Sep 17 00:00:00 2001 From: Hugbot Date: Wed, 6 Jun 2018 16:06:39 -0700 Subject: [PATCH 2/4] Changed `splunklib/data.py` `load` function to convert string utf-8 encoded only for utf strings, so that Python XML parser will not throw errors when processing utf-8 encoded responses. --- splunklib/data.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/splunklib/data.py b/splunklib/data.py index 05298b6e8..730fdb4c7 100644 --- a/splunklib/data.py +++ b/splunklib/data.py @@ -76,7 +76,11 @@ def load(text, match=None): 'namespaces': [], 'names': {} } - root = XML(text.encode('utf-8')) + + if isinstance(text, unicode): + text = text.encode('utf-8') + + root = XML(text) items = [root] if match is None else root.findall(match) count = len(items) if count == 0: From 9613e438441696dc9347c34eefd9b19cec184d0a Mon Sep 17 00:00:00 2001 From: Hugbot Date: Thu, 7 Jun 2018 10:55:44 -0700 Subject: [PATCH 3/4] Fix XML responses to not throw errors for unicode characters --- splunklib/data.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/splunklib/data.py b/splunklib/data.py index 730fdb4c7..0c89dcae3 100644 --- a/splunklib/data.py +++ b/splunklib/data.py @@ -17,6 +17,7 @@ """ from __future__ import absolute_import +import sys from xml.etree.ElementTree import XML from splunklib import six @@ -77,8 +78,10 @@ def load(text, match=None): 'names': {} } - if isinstance(text, unicode): - text = text.encode('utf-8') + # Convert to unicode encoding in only python 2 for xml parser + if sys.version_info < (3, 0, 0): + if isinstance(text, unicode): + text = text.encode('utf-8') root = XML(text) items = [root] if match is None else root.findall(match) From e10e50dfe7b16d09ab02709fd2f456e5f46d322e Mon Sep 17 00:00:00 2001 From: Hugbot Date: Thu, 7 Jun 2018 14:43:36 -0700 Subject: [PATCH 4/4] Collapse conditional logic for function --- splunklib/data.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/splunklib/data.py b/splunklib/data.py index 0c89dcae3..dedbb3310 100644 --- a/splunklib/data.py +++ b/splunklib/data.py @@ -79,9 +79,8 @@ def load(text, match=None): } # Convert to unicode encoding in only python 2 for xml parser - if sys.version_info < (3, 0, 0): - if isinstance(text, unicode): - text = text.encode('utf-8') + if(sys.version_info < (3, 0, 0) and isinstance(text, unicode)): + text = text.encode('utf-8') root = XML(text) items = [root] if match is None else root.findall(match)