diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index 65c99f1..422600a 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -214,6 +214,21 @@ 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("%s?>" % _escape_cdata(node.text, encoding))
+ file.write("%s?>" % _encode(node.text, encoding))
else:
items = node.items()
xmlns_items = [] # new namespaces in this scope