summaryrefslogtreecommitdiff
path: root/Lib/xml/dom
diff options
context:
space:
mode:
authorEli Bendersky <eliben@gmail.com>2012-07-13 09:52:39 +0300
committerEli Bendersky <eliben@gmail.com>2012-07-13 09:52:39 +0300
commit21fa4ba7c40354061238d10c354347f6b174a5b6 (patch)
treefefaf9d71f1bfd48d4e7adb92b96da926b932b41 /Lib/xml/dom
parent7e409ddeae9a23da7b35762bde641d2084d7e774 (diff)
downloadcpython-21fa4ba7c40354061238d10c354347f6b174a5b6.tar.gz
Issue #15296: Fix minidom.toxml/toprettyxml for non-unicode encodings. Patch by Serhiy Storchaka, with some minor style adjustments by me.
Diffstat (limited to 'Lib/xml/dom')
-rw-r--r--Lib/xml/dom/minidom.py22
1 files changed, 12 insertions, 10 deletions
diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py
index 275e20c420..28e5030b86 100644
--- a/Lib/xml/dom/minidom.py
+++ b/Lib/xml/dom/minidom.py
@@ -14,7 +14,6 @@ Todo:
* SAX 2 namespaces
"""
-import codecs
import io
import xml.dom
@@ -47,19 +46,22 @@ class Node(xml.dom.Node):
return self.toprettyxml("", "", encoding)
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)
@@ -1788,12 +1790,12 @@ class Document(Node, DocumentLS):
raise xml.dom.NotSupportedErr("cannot import document type nodes")
return _clone_node(node, deep, self)
- def writexml(self, writer, indent="", addindent="", newl="",
- encoding = None):
+ def writexml(self, writer, indent="", addindent="", newl="", encoding=None):
if encoding is None:
writer.write('<?xml version="1.0" ?>'+newl)
else:
- writer.write('<?xml version="1.0" encoding="%s"?>%s' % (encoding, newl))
+ writer.write('<?xml version="1.0" encoding="%s"?>%s' % (
+ encoding, newl))
for node in self.childNodes:
node.writexml(writer, indent, addindent, newl)