]> granicus.if.org Git - python/commitdiff
Issue #2746: Don't escape ampersands and angle brackets ("&", "<", ">")
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 9 Feb 2010 17:08:05 +0000 (17:08 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 9 Feb 2010 17:08:05 +0000 (17:08 +0000)
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
Lib/xml/etree/ElementTree.py
Misc/ACKS
Misc/NEWS

index 2ccb77a593f2eb8916fa6e9f66e09792cde23bef..8cdd4ee3d3c8f8d78b32f5a050eadec69f67b518 100644 (file)
@@ -213,6 +213,23 @@ def check_encoding(ET, encoding):
     """
     ET.XML("<?xml version='1.0' encoding='%s'?><xml />" % encoding)
 
+def processinginstruction():
+    """
+    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&>?>'
+
+    """
 
 #
 # xinclude tests (samples from appendix C of the xinclude specification)
index 7dbc72e78f958971268f4eec3ddbbbced2ee0070..e5afcbca00a770bfabc64592f22bb41e63028cab 100644 (file)
@@ -666,9 +666,9 @@ class ElementTree:
         # write XML to file
         tag = node.tag
         if tag is Comment:
-            file.write("<!-- %s -->" % _escape_cdata(node.text, encoding))
+            file.write("<!-- %s -->" % _encode(node.text, encoding))
         elif tag is ProcessingInstruction:
-            file.write("<?%s?>" % _escape_cdata(node.text, encoding))
+            file.write("<?%s?>" % _encode(node.text, encoding))
         else:
             items = node.items()
             xmlns_items = [] # new namespaces in this scope
index 089e4634a7389e5ab83a885dad2d165d63940136..0ce586eb4e920dd5aed90cdd3d99c17aa4991579 100644 (file)
--- 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
index 1a752fc7d8cfb4baaec512632cdc4ffcb2df2cca..0300b4f5e4b2268a8a748807b954f0e01d200907 100644 (file)
--- 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.