From 99f69ee7a1cf4d032d420ad69d22bd44b8cf6cc8 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Tue, 9 Feb 2010 17:25:47 +0000 Subject: [PATCH] Merged revisions 78125 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ........ r78125 | antoine.pitrou | 2010-02-09 18:08:05 +0100 (mar., 09 févr. 2010) | 7 lines 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. ........ --- Lib/test/test_xml_etree.py | 20 ++++++++++++++++++++ Lib/xml/etree/ElementTree.py | 4 ++-- Misc/ACKS | 1 + Misc/NEWS | 5 +++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index a7ad48b2e8e..3df18961ba4 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("" % encoding) +def processinginstruction(): + r""" + Test ProcessingInstruction directly + + >>> from xml.etree import ElementTree as ET + + >>> ET.tostring(ET.ProcessingInstruction('test', 'instruction')) + '' + >>> ET.tostring(ET.PI('test', 'instruction')) + '' + + Issue #2746 + + >>> ET.tostring(ET.PI('test', '')) + '?>' + >>> ET.tostring(ET.PI('test', '\xe3'), 'latin1') + b"\n\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 c47573e3136..2663b336e73 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"") + file.write(_encode("" % node.text, encoding)) elif tag is ProcessingInstruction: - file.write(b"") + file.write(_encode("" % node.text, encoding)) else: items = list(node.items()) xmlns_items = [] # new namespaces in this scope diff --git a/Misc/ACKS b/Misc/ACKS index b5dccde4fd3..847d1d1208f 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 9d2b7cf0836..e544868c226 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.