Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e1f85e

Browse files
committed
Fix the minidom test.
In order to do this, I added an optional encoding argument to io.StringIO. The toprettyxml() function returns bytes when you specify an encoding now.
1 parent 3992db8 commit 3e1f85e

3 files changed

Lines changed: 21 additions & 24 deletions

File tree

Lib/io.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,11 +1262,13 @@ class StringIO(TextIOWrapper):
12621262

12631263
# XXX This is really slow, but fully functional
12641264

1265-
def __init__(self, initial_value=""):
1266-
super(StringIO, self).__init__(BytesIO(), "utf-8")
1265+
def __init__(self, initial_value="", encoding="utf-8", newline=None):
1266+
super(StringIO, self).__init__(BytesIO(),
1267+
encoding=encoding,
1268+
newline=newline)
12671269
if initial_value:
12681270
self.write(initial_value)
12691271
self.seek(0)
12701272

12711273
def getvalue(self):
1272-
return self.buffer.getvalue().decode("utf-8")
1274+
return self.buffer.getvalue().decode(self._encoding)

Lib/test/test_minidom.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -869,17 +869,17 @@ def testSAX2DOM(self):
869869

870870
def testEncodings(self):
871871
doc = parseString('<foo>&#x20ac;</foo>')
872-
self.confirm(doc.toxml() == '<?xml version="1.0" ?><foo>\u20ac</foo>'
873-
and doc.toxml('utf-8') ==
874-
'<?xml version="1.0" encoding="utf-8"?><foo>\xe2\x82\xac</foo>'
875-
and doc.toxml('iso-8859-15') ==
876-
'<?xml version="1.0" encoding="iso-8859-15"?><foo>\xa4</foo>',
877-
"testEncodings - encoding EURO SIGN")
872+
self.assertEqual(doc.toxml(),
873+
'<?xml version="1.0" ?><foo>\u20ac</foo>')
874+
self.assertEqual(doc.toxml('utf-8'),
875+
b'<?xml version="1.0" encoding="utf-8"?><foo>\xe2\x82\xac</foo>')
876+
self.assertEqual(doc.toxml('iso-8859-15'),
877+
b'<?xml version="1.0" encoding="iso-8859-15"?><foo>\xa4</foo>')
878878

879879
# Verify that character decoding errors throw exceptions instead
880880
# of crashing
881881
self.assertRaises(UnicodeDecodeError, parseString,
882-
'<fran\xe7ais>Comment \xe7a va ? Tr\xe8s bien ?</fran\xe7ais>')
882+
b'<fran\xe7ais>Comment \xe7a va ? Tr\xe8s bien ?</fran\xe7ais>')
883883

884884
doc.unlink()
885885

Lib/xml/dom/minidom.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* SAX 2 namespaces
1515
"""
1616

17+
import io
1718
import xml.dom
1819

1920
from xml.dom import EMPTY_NAMESPACE, EMPTY_PREFIX, XMLNS_NAMESPACE, domreg
@@ -44,20 +45,19 @@ def __bool__(self):
4445
def toxml(self, encoding = None):
4546
return self.toprettyxml("", "", encoding)
4647

47-
def toprettyxml(self, indent="\t", newl="\n", encoding = None):
48+
def toprettyxml(self, indent="\t", newl="\n", encoding=None):
4849
# indent = the indentation string to prepend, per level
4950
# newl = the newline string to append
50-
writer = _get_StringIO()
51-
if encoding is not None:
52-
import codecs
53-
# Can't use codecs.getwriter to preserve 2.0 compatibility
54-
writer = codecs.lookup(encoding)[3](writer)
51+
writer = io.StringIO(encoding=encoding)
5552
if self.nodeType == Node.DOCUMENT_NODE:
5653
# Can pass encoding only to document, to put it into XML header
5754
self.writexml(writer, "", indent, newl, encoding)
5855
else:
5956
self.writexml(writer, "", indent, newl)
60-
return writer.getvalue()
57+
if encoding is None:
58+
return writer.getvalue()
59+
else:
60+
return writer.buffer.getvalue()
6161

6262
def hasChildNodes(self):
6363
if self.childNodes:
@@ -360,7 +360,7 @@ def __init__(self, qName, namespaceURI=EMPTY_NAMESPACE, localName=None,
360360

361361
def _get_localName(self):
362362
if 'localName' in self.__dict__:
363-
return self.__dict__['localName']
363+
return self.__dict__['localName']
364364
return self.nodeName.split(":", 1)[-1]
365365

366366
def _get_name(self):
@@ -665,7 +665,7 @@ def __init__(self, tagName, namespaceURI=EMPTY_NAMESPACE, prefix=None,
665665

666666
def _get_localName(self):
667667
if 'localName' in self.__dict__:
668-
return self.__dict__['localName']
668+
return self.__dict__['localName']
669669
return self.tagName.split(":", 1)[-1]
670670

671671
def _get_tagName(self):
@@ -1897,11 +1897,6 @@ def _nssplit(qualifiedName):
18971897
return (None, fields[0])
18981898

18991899

1900-
def _get_StringIO():
1901-
# we can't use cStringIO since it doesn't support Unicode strings
1902-
from StringIO import StringIO
1903-
return StringIO()
1904-
19051900
def _do_pulldom_parse(func, args, kwargs):
19061901
events = func(*args, **kwargs)
19071902
toktype, rootNode = events.getEvent()

0 commit comments

Comments
 (0)