diff -r 5d99042bd40f Lib/test/test_sax.py
--- a/Lib/test/test_sax.py Tue Apr 02 12:15:07 2013 -0400
+++ b/Lib/test/test_sax.py Tue Apr 02 13:28:26 2013 -0500
@@ -258,6 +258,25 @@
u'\u20ac' % encoding
).encode(encoding, 'xmlcharrefreplace'))
+ def test_xmlgen_encoding_str(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,'replace'))
+ gen.endElement("doc")
+ gen.endDocument()
+
+ self.assertEqual(result.getvalue(), (
+ u'\n'
+ u'\u20ac' % encoding
+ ).encode(encoding, 'xmlcharrefreplace'))
+
def test_xmlgen_unencodable(self):
result = self.ioclass()
gen = XMLGenerator(result, encoding='ascii')
diff -r 5d99042bd40f Lib/xml/sax/saxutils.py
--- a/Lib/xml/sax/saxutils.py Tue Apr 02 12:15:07 2013 -0400
+++ b/Lib/xml/sax/saxutils.py Tue Apr 02 13:28:26 2013 -0500
@@ -180,7 +180,10 @@
self._write(u'%s>' % self._qname(name))
def characters(self, content):
- self._write(escape(unicode(content)))
+ if isinstance(content, unicode):
+ self._write(escape(content))
+ else:
+ self._write(escape(unicode(content,self._encoding)))
def ignorableWhitespace(self, content):
self._write(unicode(content))