From 42fb6ab49128f14cd353ec86a177ddda6763812e Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Tue, 9 Feb 2010 17:08:05 +0000 Subject: 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 | 17 +++++++++++++++++ Lib/xml/etree/ElementTree.py | 4 ++-- Misc/ACKS | 1 + Misc/NEWS | 5 +++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 2ccb77a..8cdd4ee 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -213,6 +213,23 @@ def check_encoding(ET, encoding): """ ET.XML("" % encoding) +def processinginstruction(): + """ + 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', '')) + '?>' + + """ # # xinclude tests (samples from appendix C of the xinclude specification) diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index 7dbc72e..e5afcbc 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -666,9 +666,9 @@ class ElementTree: # write XML to file tag = node.tag if tag is Comment: - file.write("" % _escape_cdata(node.text, encoding)) + file.write("" % _encode(node.text, encoding)) elif tag is ProcessingInstruction: - file.write("" % _escape_cdata(node.text, encoding)) + file.write("" % _encode(node.text, encoding)) else: items = node.items() xmlns_items = [] # new namespaces in this scope diff --git a/Misc/ACKS b/Misc/ACKS index 089e463..0ce586e 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -525,6 +525,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 1a752fc..0300b4f 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -78,6 +78,11 @@ Core and Builtins 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 #7869: logging: improved diagnostic for format-time errors. - Issue #7868: logging: added loggerClass attribute to Manager. -- cgit v0.12