[Python-checkins] cpython (3.3): Issue #17606: Fixed support of encoded byte strings in the XMLGenerator

serhiy.storchaka python-checkins at python.org
Sun May 12 16:32:51 CEST 2013


http://hg.python.org/cpython/rev/e730447caf20
changeset:   83748:e730447caf20
branch:      3.3
parent:      83745:6c5a3d194a10
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun May 12 17:31:16 2013 +0300
summary:
  Issue #17606: Fixed support of encoded byte strings in the XMLGenerator
characters() and ignorableWhitespace() methods.  Original patch by Sebastian
Ortiz Vasquez.

files:
  Lib/test/test_sax.py    |  18 ++++++++++++++++++
  Lib/xml/sax/saxutils.py |   4 ++++
  Misc/ACKS               |   1 +
  Misc/NEWS               |   4 ++++
  4 files changed, 27 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py
--- a/Lib/test/test_sax.py
+++ b/Lib/test/test_sax.py
@@ -319,6 +319,24 @@
 
         self.assertEqual(result.getvalue(), self.xml("<doc> </doc>"))
 
+    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('<doc a="\u20ac">\u20ac </doc>', 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
--- a/Lib/xml/sax/saxutils.py
+++ b/Lib/xml/sax/saxutils.py
@@ -209,11 +209,15 @@
     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
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1254,6 +1254,7 @@
 Andrew Vant
 Atul Varma
 Dmitry Vasiliev
+Sebastian Ortiz Vasquez
 Alexandre Vassalotti
 Nadeem Vawda
 Frank Vercruesse
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -49,6 +49,10 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list