Commit 7423903e authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #17606: Fixed support of encoded byte strings in the XMLGenerator

characters() and ignorableWhitespace() methods.  Original patch by Sebastian
Ortiz Vasquez.
parent 988aba3b
...@@ -284,6 +284,26 @@ class XmlgenTest: ...@@ -284,6 +284,26 @@ class XmlgenTest:
self.assertEqual(result.getvalue(), start + "<doc> </doc>") self.assertEqual(result.getvalue(), start + "<doc> </doc>")
def test_xmlgen_encoding_bytes(self):
encodings = ('iso-8859-15', 'utf-8',
'utf-16be', 'utf-16le',
'utf-32be', 'utf-32le')
for encoding in encodings:
result = self.ioclass()
gen = XMLGenerator(result, encoding=encoding)
gen.startDocument()
gen.startElement("doc", {"a": u'\u20ac'})
gen.characters(u"\u20ac".encode(encoding))
gen.ignorableWhitespace(" ".encode(encoding))
gen.endElement("doc")
gen.endDocument()
self.assertEqual(result.getvalue(), (
u'<?xml version="1.0" encoding="%s"?>\n'
u'<doc a="\u20ac">\u20ac </doc>' % encoding
).encode(encoding, 'xmlcharrefreplace'))
def test_xmlgen_ns(self): def test_xmlgen_ns(self):
result = self.ioclass() result = self.ioclass()
gen = XMLGenerator(result) gen = XMLGenerator(result)
......
...@@ -180,10 +180,14 @@ class XMLGenerator(handler.ContentHandler): ...@@ -180,10 +180,14 @@ class XMLGenerator(handler.ContentHandler):
self._write(u'</%s>' % self._qname(name)) self._write(u'</%s>' % self._qname(name))
def characters(self, content): def characters(self, content):
self._write(escape(unicode(content))) if not isinstance(content, unicode):
content = unicode(content, self._encoding)
self._write(escape(content))
def ignorableWhitespace(self, content): def ignorableWhitespace(self, content):
self._write(unicode(content)) if not isinstance(content, unicode):
content = unicode(content, self._encoding)
self._write(content)
def processingInstruction(self, target, data): def processingInstruction(self, target, data):
self._write(u'<?%s %s?>' % (target, data)) self._write(u'<?%s %s?>' % (target, data))
......
...@@ -1045,6 +1045,7 @@ Case Van Horsen ...@@ -1045,6 +1045,7 @@ Case Van Horsen
Kyle VanderBeek Kyle VanderBeek
Atul Varma Atul Varma
Dmitry Vasiliev Dmitry Vasiliev
Sebastian Ortiz Vasquez
Alexandre Vassalotti Alexandre Vassalotti
Frank Vercruesse Frank Vercruesse
Mike Verdone Mike Verdone
......
...@@ -38,6 +38,10 @@ Core and Builtins ...@@ -38,6 +38,10 @@ Core and Builtins
Library Library
------- -------
- Issue #17606: Fixed support of encoded byte strings in the XMLGenerator
.characters() and ignorableWhitespace() methods. Original patch by Sebastian
Ortiz Vasquez.
- Issue #16601: Restarting iteration over tarfile no more continues from where - Issue #16601: Restarting iteration over tarfile no more continues from where
it left off. Patch by Michael Birtwell. it left off. Patch by Michael Birtwell.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment