diff -r 7aa75ea4116d Doc/library/xml.dom.minidom.rst --- a/Doc/library/xml.dom.minidom.rst Sun Jul 08 23:06:45 2012 +1000 +++ b/Doc/library/xml.dom.minidom.rst Sun Jul 08 17:49:42 2012 +0300 @@ -147,12 +147,7 @@ the DOM node. With an explicit *encoding* [1]_ argument, the result is a byte - string in the specified encoding. It is recommended that you - always specify an encoding; you may use any encoding you like, but - an argument of "utf-8" is the most common choice, avoiding - :exc:`UnicodeError` exceptions in case of unrepresentable text - data. - + string in the specified encoding. With no *encoding* argument, the result is a Unicode string, and the XML declaration in the resulting string does not specify an encoding. Encoding this string in an encoding other than UTF-8 is diff -r 7aa75ea4116d Lib/test/test_minidom.py --- a/Lib/test/test_minidom.py Sun Jul 08 23:06:45 2012 +1000 +++ b/Lib/test/test_minidom.py Sun Jul 08 17:49:42 2012 +0300 @@ -1067,6 +1067,11 @@ b'\xe2\x82\xac') self.assertEqual(doc.toxml('iso-8859-15'), b'\xa4') + self.assertEqual(doc.toxml('us-ascii'), + b'') + self.assertEqual(doc.toxml('utf-16'), + '' + '\u20ac'.encode('utf-16')) # Verify that character decoding errors throw exceptions instead # of crashing diff -r 7aa75ea4116d Lib/xml/dom/minidom.py --- a/Lib/xml/dom/minidom.py Sun Jul 08 23:06:45 2012 +1000 +++ b/Lib/xml/dom/minidom.py Sun Jul 08 17:49:42 2012 +0300 @@ -14,7 +14,6 @@ * SAX 2 namespaces """ -import codecs import io import xml.dom @@ -49,17 +48,22 @@ def toprettyxml(self, indent="\t", newl="\n", encoding=None): # indent = the indentation string to prepend, per level # newl = the newline string to append - use_encoding = "utf-8" if encoding is None else encoding - writer = codecs.getwriter(use_encoding)(io.BytesIO()) + if encoding is None: + writer = io.StringIO() + else: + writer = io.TextIOWrapper(io.BytesIO(), + encoding=encoding, + errors="xmlcharrefreplace", + newline='\n') if self.nodeType == Node.DOCUMENT_NODE: # Can pass encoding only to document, to put it into XML header self.writexml(writer, "", indent, newl, encoding) else: self.writexml(writer, "", indent, newl) if encoding is None: - return writer.stream.getvalue().decode(use_encoding) + return writer.getvalue() else: - return writer.stream.getvalue() + return writer.detach().getvalue() def hasChildNodes(self): return bool(self.childNodes)