summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-05-12 14:31:59 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-05-12 14:31:59 (GMT)
commit3068aed92bb7557cdef7e18a4a02ddff2dc1ad0b (patch)
treec2458b5ff8986edc481aa40300c93cf24030e437
parent7025349aa8c09f6910ca985d72a059019129d1f8 (diff)
parent3eab6b363a968e907605fe749d12941c3be29761 (diff)
downloadcpython-3068aed92bb7557cdef7e18a4a02ddff2dc1ad0b.zip
cpython-3068aed92bb7557cdef7e18a4a02ddff2dc1ad0b.tar.gz
cpython-3068aed92bb7557cdef7e18a4a02ddff2dc1ad0b.tar.bz2
Issue #17606: Fixed support of encoded byte strings in the XMLGenerator
characters() and ignorableWhitespace() methods. Original patch by Sebastian Ortiz Vasquez.
-rw-r--r--Lib/test/test_sax.py18
-rw-r--r--Lib/xml/sax/saxutils.py4
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS4
4 files changed, 27 insertions, 0 deletions
diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py
index 0261c6a..502c962 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("<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
index 0798ecd..74de9b0 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 5f5b84b..6d08530 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1281,6 +1281,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 5008cb3..a7fd8e8 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -91,6 +91,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.