Index: Lib/test/test_xml_etree.py
===================================================================
--- Lib/test/test_xml_etree.py (revision 73515)
+++ Lib/test/test_xml_etree.py (working copy)
@@ -210,7 +210,18 @@
"""
ET.XML("" % encoding)
+def check_issue6233():
+ """
+>>> from xml.etree import ElementTree as ET
+>>> e = ET.XML("
t\xe3g
")
+>>> ET.tostring(e, 'ascii')
+ b"\\ntãg"
+>>> e = ET.XML("t\xe3g".encode('iso-8859-1')) # create byte string with the right encoding
+>>> ET.tostring(e, 'ascii')
+ b"\\ntãg"
+ """
+
#
# xinclude tests (samples from appendix C of the xinclude specification)
Index: Lib/xml/etree/ElementTree.py
===================================================================
--- Lib/xml/etree/ElementTree.py (revision 73515)
+++ Lib/xml/etree/ElementTree.py (working copy)
@@ -662,9 +662,9 @@
# write XML to file
tag = node.tag
if tag is Comment:
- file.write(_encode("" % _escape_cdata(node.text), encoding))
+ file.write(b"")
elif tag is ProcessingInstruction:
- file.write(_encode("%s?>" % _escape_cdata(node.text), encoding))
+ file.write(b"" + _encode_cdata(node.text, encoding) + b"?>")
else:
items = list(node.items())
xmlns_items = [] # new namespaces in this scope
@@ -696,7 +696,7 @@
if node.text or len(node):
file.write(_encode(">", encoding))
if node.text:
- file.write(_encode(_escape_cdata(node.text), encoding))
+ file.write(_encode_cdata(node.text, encoding))
for n in node:
self._write(file, n, encoding, namespaces)
file.write(_encode("" + tag + ">", encoding))
@@ -705,7 +705,7 @@
for k, v in xmlns_items:
del namespaces[v]
if node.tail:
- file.write(_encode(_escape_cdata(node.tail), encoding))
+ file.write(_encode_cdata(node.tail, encoding))
# --------------------------------------------------------------------
# helpers
@@ -788,6 +788,19 @@
# the following functions assume an ascii-compatible encoding
# (or "utf-16")
+def _encode_cdata(text, encoding):
+ # escape character data
+ try:
+ text = text.replace("&", "&")
+ text = text.replace("<", "<") + text = text.replace(">", ">")
+ if encoding:
+ return text.encode(encoding, "xmlcharrefreplace")
+ else:
+ return text
+ except (TypeError, AttributeError):
+ _raise_serialization_error(text)
+
def _escape_cdata(text):
# escape character data
try: