Skip to content

Commit

Permalink
Move the version test for string to xml_to_tuple
Browse files Browse the repository at this point in the history
Moves the version test and conversion to utf-8 to xml_to_tuple
  • Loading branch information
KSchopmeyer committed Nov 16, 2016
1 parent c23bd91 commit 423422c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
6 changes: 3 additions & 3 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ Enhancements
* Added a section "Prerequisite operating system packages" to the documentation
that describes the prerequisite packages by distribution.

* Modified xml parser to use sax parser in place of DOM parser for response
processing. This is a significant reduction in memory usage.
See issue # 498.
* Modified xml parser to use sax parser in place of DOM parser for operation
response processing and indication processing This is a significant reduction
in memory usage. See issue # 498.


Bug fixes
Expand Down
9 changes: 1 addition & 8 deletions pywbem/tupleparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
# This module is meant to be safe for 'import *'.

from __future__ import absolute_import
import sys
import six

from .cim_obj import CIMInstance, CIMInstanceName, CIMClass, CIMClassName, \
Expand Down Expand Up @@ -1772,13 +1771,7 @@ def parse_embeddedObject(val): # pylint: disable=invalid-name
if val is None:
return None

# the following is required because version 3.4 does not accept str
# for the sax processor
if sys.version_info >= (3, 0) and sys.version_info < (3, 5):
if isinstance(val, str):
val = val.encode("utf-8")

tuptree = xml_to_tupletree_sax(val) # performs the un-embedding
tuptree = xml_to_tupletree_sax(val) # perform the un-embedding
# TODO ks 11/14/16 Does not handle exceptions. See cim_operations l 800
if tuptree[0] == 'INSTANCE':
return parse_instance(tuptree)
Expand Down
30 changes: 28 additions & 2 deletions pywbem/tupletree.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import xml.dom.minidom
import xml.sax
import re
import sys

__all__ = []

Expand Down Expand Up @@ -118,9 +119,34 @@ def characters(self, content):

def xml_to_tupletree_sax(xml_string):
"""
Parse an XML string into tupletree with SAX.
Returns the root.
Parse an XML string into tupletree with SAX parser.
Parses the string using the class CIMContentHandler and
returns the root element. As a SAX parser it uses minimal
memory.
This is a replacement for the previous parser (xml_to_tuple)
which used the dom parser.
Parameters:
xml_string (:term:`string`) or (:term:`byte string`): A string
or bytes string containing the XML to be parsed.
Returns:
(:class: `CIMContentHandler`) : The root tuple from the
parse:
Raises:
TypeError, ParseError, SAXParseException, or ExpatError.
"""
handler = CIMContentHandler()

# The following is required because python version 3.4 does not accept
# str for the sax processor xml input.
if sys.version_info[0:2] == (3, 4):
if isinstance(xml_string, str):
xml_string = xml_string.encode("utf-8")

xml.sax.parseString(xml_string, handler, None)
return handler.root
14 changes: 0 additions & 14 deletions testsuite/test_tupleparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"""

from __future__ import absolute_import
import sys

import unittest

Expand All @@ -32,12 +31,6 @@ def _run_single(self, obj):

xml = obj.tocimxml().toxml()

# inserted here because some versions of sax processor do not
# support str input.gi
if sys.version_info >= (3, 0) and sys.version_info < (3, 5):
if isinstance(xml, str):
xml = xml.encode("utf-8")

# Parse back to an object

result = tupleparse.parse_any(tupletree.xml_to_tupletree_sax(xml))
Expand All @@ -59,16 +52,9 @@ def _run_single(self, xml, obj):
"""
# Parse raw XML to an object

# inserted here because some versions of sax processor do not
# support str input
if sys.version_info >= (3, 0) and sys.version_info < (3, 5):
if isinstance(xml, str):
xml = xml.encode("utf-8")

result = tupleparse.parse_any(tupletree.xml_to_tupletree_sax(xml))

# Assert XML parses to particular Python object
print('rawafter %s' % result)

self.assertEqual(obj, result,
'parsed XML: %s' % result)
Expand Down

0 comments on commit 423422c

Please sign in to comment.