]> granicus.if.org Git - python/commitdiff
Issue #17915: Fix interoperability of xml.sax with file objects returned by
authorGeorg Brandl <georg@python.org>
Sun, 12 May 2013 09:41:12 +0000 (11:41 +0200)
committerGeorg Brandl <georg@python.org>
Sun, 12 May 2013 09:41:12 +0000 (11:41 +0200)
codecs.open().

Lib/test/test_sax.py
Lib/xml/sax/saxutils.py
Misc/NEWS

index 3efd97245829e135de88a5054ded8077163b2cf6..32020b92c44da6cfe5ebd0c32bb178747778bcd0 100644 (file)
@@ -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)
index a62183a848329d1df6eca1fad7d2bca70c99001f..0798ecd70d10508d54a03a72eb0be7edd6d8ccfb 100644 (file)
@@ -5,6 +5,7 @@ convenience of application and driver writers.
 
 import os, urllib.parse, urllib.request
 import io
+import codecs
 from . import handler
 from . import xmlreader
 
@@ -77,6 +78,10 @@ def _gettextwriter(out, encoding):
         # 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
index 7662a43ae4e77599294545e170f804c72f136e79..7308e72a8351f0ec8abc9a5daf2587044071b575 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -20,6 +20,9 @@ Library
 - 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
 -----