diff options
author | Georg Brandl <georg@python.org> | 2013-05-12 09:41:12 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2013-05-12 09:41:12 (GMT) |
commit | c502df4e3e00ec2481f1f0a80af37c9d822787b3 (patch) | |
tree | 71315fc75d9fa55815137fcbf93fd408bdec89ce /Lib/test/test_sax.py | |
parent | 93b061bc3e1c9285ec1ce6405b85d3a1e072833f (diff) | |
download | cpython-c502df4e3e00ec2481f1f0a80af37c9d822787b3.zip cpython-c502df4e3e00ec2481f1f0a80af37c9d822787b3.tar.gz cpython-c502df4e3e00ec2481f1f0a80af37c9d822787b3.tar.bz2 |
Issue #17915: Fix interoperability of xml.sax with file objects returned by
codecs.open().
Diffstat (limited to 'Lib/test/test_sax.py')
-rw-r--r-- | Lib/test/test_sax.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py index 3efd972..32020b9 100644 --- a/Lib/test/test_sax.py +++ b/Lib/test/test_sax.py @@ -15,6 +15,7 @@ from xml.sax.expatreader import create_parser from xml.sax.handler import feature_namespaces from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl from io import BytesIO, StringIO +import codecs import os.path import shutil from test import support @@ -538,6 +539,34 @@ class WriterXmlgenTest(BytesXmlgenTest): def getvalue(self): return b''.join(self) +class StreamWriterXmlgenTest(XmlgenTest, unittest.TestCase): + def ioclass(self): + raw = BytesIO() + writer = codecs.getwriter('ascii')(raw, 'xmlcharrefreplace') + writer.getvalue = raw.getvalue + return writer + + def xml(self, doc, encoding='iso-8859-1'): + return ('<?xml version="1.0" encoding="%s"?>\n%s' % + (encoding, doc)).encode('ascii', 'xmlcharrefreplace') + +class StreamReaderWriterXmlgenTest(XmlgenTest, unittest.TestCase): + fname = support.TESTFN + '-codecs' + + def ioclass(self): + writer = codecs.open(self.fname, 'w', encoding='ascii', + errors='xmlcharrefreplace', buffering=0) + self.addCleanup(support.unlink, self.fname) + writer.getvalue = self.getvalue + return writer + + def getvalue(self): + with open(self.fname, 'rb') as f: + return f.read() + + def xml(self, doc, encoding='iso-8859-1'): + return ('<?xml version="1.0" encoding="%s"?>\n%s' % + (encoding, doc)).encode('ascii', 'xmlcharrefreplace') start = b'<?xml version="1.0" encoding="iso-8859-1"?>\n' @@ -946,6 +975,8 @@ def test_main(): StringXmlgenTest, BytesXmlgenTest, WriterXmlgenTest, + StreamWriterXmlgenTest, + StreamReaderWriterXmlgenTest, ExpatReaderTest, ErrorReportingTest, XmlReaderTest) |