From: Serhiy Storchaka Date: Sun, 12 May 2013 14:31:16 +0000 (+0300) Subject: Issue #17606: Fixed support of encoded byte strings in the XMLGenerator X-Git-Tag: v3.3.2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3eab6b363a968e907605fe749d12941c3be29761;p=thirdparty%2FPython%2Fcpython.git Issue #17606: Fixed support of encoded byte strings in the XMLGenerator characters() and ignorableWhitespace() methods. Original patch by Sebastian Ortiz Vasquez. --- diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py index 0261c6a73e50..502c962d8e0d 100644 --- a/Lib/test/test_sax.py +++ b/Lib/test/test_sax.py @@ -319,6 +319,24 @@ class XmlgenTest: self.assertEqual(result.getvalue(), self.xml(" ")) + def test_xmlgen_encoding_bytes(self): + encodings = ('iso-8859-15', 'utf-8', 'utf-8-sig', + 'utf-16', 'utf-16be', 'utf-16le', + 'utf-32', 'utf-32be', 'utf-32le') + for encoding in encodings: + result = self.ioclass() + gen = XMLGenerator(result, encoding=encoding) + + gen.startDocument() + gen.startElement("doc", {"a": '\u20ac'}) + gen.characters("\u20ac".encode(encoding)) + gen.ignorableWhitespace(" ".encode(encoding)) + gen.endElement("doc") + gen.endDocument() + + self.assertEqual(result.getvalue(), + self.xml('\u20ac ', encoding=encoding)) + def test_xmlgen_ns(self): result = self.ioclass() gen = XMLGenerator(result) diff --git a/Lib/xml/sax/saxutils.py b/Lib/xml/sax/saxutils.py index 0798ecd70d10..74de9b07fc11 100644 --- a/Lib/xml/sax/saxutils.py +++ b/Lib/xml/sax/saxutils.py @@ -209,11 +209,15 @@ class XMLGenerator(handler.ContentHandler): def characters(self, content): if content: self._finish_pending_start_element() + if not isinstance(content, str): + content = str(content, self._encoding) self._write(escape(content)) def ignorableWhitespace(self, content): if content: self._finish_pending_start_element() + if not isinstance(content, str): + content = str(content, self._encoding) self._write(content) def processingInstruction(self, target, data): diff --git a/Misc/ACKS b/Misc/ACKS index 47b354d16c9b..1b4940e33376 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1254,6 +1254,7 @@ Kyle VanderBeek Andrew Vant Atul Varma Dmitry Vasiliev +Sebastian Ortiz Vasquez Alexandre Vassalotti Nadeem Vawda Frank Vercruesse diff --git a/Misc/NEWS b/Misc/NEWS index 0d7700f6324e..af14f93d416e 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -49,6 +49,10 @@ Core and Builtins Library ------- +- Issue #17606: Fixed support of encoded byte strings in the XMLGenerator + .characters() and ignorableWhitespace() methods. Original patch by Sebastian + Ortiz Vasquez. + - Issue #17732: Ignore distutils.cfg options pertaining to install paths if a virtual environment is active.