Commits

Georg Brandl committed 1c01571

Issue #17915: Fix interoperability of xml.sax with file objects returned by
codecs.open().

Comments (0)

Files changed (3)

Lib/test/test_sax.py

 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
         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'
 
                  StringXmlgenTest,
                  BytesXmlgenTest,
                  WriterXmlgenTest,
+                 StreamWriterXmlgenTest,
+                 StreamReaderWriterXmlgenTest,
                  ExpatReaderTest,
                  ErrorReportingTest,
                  XmlReaderTest)

Lib/xml/sax/saxutils.py

 
 import os, urllib.parse, urllib.request
 import io
+import codecs
 from . import handler
 from . import xmlreader
 
         # use a text writer as is
         return out
 
+    if isinstance(out, (codecs.StreamWriter, codecs.StreamReaderWriter)):
+        # use a codecs stream writer as is
+        return out
+
     # wrap a binary writer with TextIOWrapper
     if isinstance(out, io.RawIOBase):
         # Keep the original file open when the TextIOWrapper is
 - Issue #1159051: Back out a fix for handling corrupted gzip files that
   broke backwards compatibility.
 
+- Issue #17915: Fix interoperability of xml.sax with file objects returned by
+  codecs.open().
+
 Build
 -----