summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_xml_etree.py20
-rw-r--r--Lib/xml/etree/ElementTree.py4
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS5
4 files changed, 28 insertions, 2 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index a7ad48b2e8..3df18961ba 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -210,6 +210,26 @@ def check_encoding(ET, encoding):
"""
ET.XML("<?xml version='1.0' encoding='%s'?><xml />" % encoding)
+def processinginstruction():
+ r"""
+ Test ProcessingInstruction directly
+
+ >>> from xml.etree import ElementTree as ET
+
+ >>> ET.tostring(ET.ProcessingInstruction('test', 'instruction'))
+ '<?test instruction?>'
+ >>> ET.tostring(ET.PI('test', 'instruction'))
+ '<?test instruction?>'
+
+ Issue #2746
+
+ >>> ET.tostring(ET.PI('test', '<testing&>'))
+ '<?test <testing&>?>'
+ >>> ET.tostring(ET.PI('test', '<testing&>\xe3'), 'latin1')
+ b"<?xml version='1.0' encoding='latin1'?>\n<?test <testing&>\xe3?>"
+
+ """
+
def check_issue6233():
"""
>>> from xml.etree import ElementTree as ET
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
index c47573e313..2663b336e7 100644
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -662,9 +662,9 @@ class ElementTree:
# write XML to file
tag = node.tag
if tag is Comment:
- file.write(b"<!-- " + _encode_cdata(node.text, encoding) + b" -->")
+ file.write(_encode("<!-- %s -->" % node.text, encoding))
elif tag is ProcessingInstruction:
- file.write(b"<?" + _encode_cdata(node.text, encoding) + b"?>")
+ file.write(_encode("<?%s?>" % node.text, encoding))
else:
items = list(node.items())
xmlns_items = [] # new namespaces in this scope
diff --git a/Misc/ACKS b/Misc/ACKS
index b5dccde4fd..847d1d1208 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -529,6 +529,7 @@ Pablo Mouzo
Sjoerd Mullender
Sape Mullender
Michael Muller
+Neil Muller
R. David Murray
Piotr Meyer
John Nagle
diff --git a/Misc/NEWS b/Misc/NEWS
index 9d2b7cf083..e544868c22 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -242,6 +242,11 @@ C-API
Library
-------
+- Issue #2746: Don't escape ampersands and angle brackets ("&", "<", ">")
+ in XML processing instructions and comments. These raw characters are
+ allowed by the XML specification, and are necessary when outputting e.g.
+ PHP code in a processing instruction. Patch by Neil Muller.
+
- Issue #6233: ElementTree failed converting unicode characters to XML
entities when they could't be represented in the requested output
encoding. Patch by Jerry Chen.