Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c502df4

Browse files
committed
Issue #17915: Fix interoperability of xml.sax with file objects returned by
codecs.open().
1 parent 93b061b commit c502df4

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

Lib/test/test_sax.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from xml.sax.handler import feature_namespaces
1616
from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
1717
from io import BytesIO, StringIO
18+
import codecs
1819
import os.path
1920
import shutil
2021
from test import support
@@ -538,6 +539,34 @@ def tell(self):
538539
def getvalue(self):
539540
return b''.join(self)
540541

542+
class StreamWriterXmlgenTest(XmlgenTest, unittest.TestCase):
543+
def ioclass(self):
544+
raw = BytesIO()
545+
writer = codecs.getwriter('ascii')(raw, 'xmlcharrefreplace')
546+
writer.getvalue = raw.getvalue
547+
return writer
548+
549+
def xml(self, doc, encoding='iso-8859-1'):
550+
return ('<?xml version="1.0" encoding="%s"?>\n%s' %
551+
(encoding, doc)).encode('ascii', 'xmlcharrefreplace')
552+
553+
class StreamReaderWriterXmlgenTest(XmlgenTest, unittest.TestCase):
554+
fname = support.TESTFN + '-codecs'
555+
556+
def ioclass(self):
557+
writer = codecs.open(self.fname, 'w', encoding='ascii',
558+
errors='xmlcharrefreplace', buffering=0)
559+
self.addCleanup(support.unlink, self.fname)
560+
writer.getvalue = self.getvalue
561+
return writer
562+
563+
def getvalue(self):
564+
with open(self.fname, 'rb') as f:
565+
return f.read()
566+
567+
def xml(self, doc, encoding='iso-8859-1'):
568+
return ('<?xml version="1.0" encoding="%s"?>\n%s' %
569+
(encoding, doc)).encode('ascii', 'xmlcharrefreplace')
541570

542571
start = b'<?xml version="1.0" encoding="iso-8859-1"?>\n'
543572

@@ -946,6 +975,8 @@ def test_main():
946975
StringXmlgenTest,
947976
BytesXmlgenTest,
948977
WriterXmlgenTest,
978+
StreamWriterXmlgenTest,
979+
StreamReaderWriterXmlgenTest,
949980
ExpatReaderTest,
950981
ErrorReportingTest,
951982
XmlReaderTest)

Lib/xml/sax/saxutils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import os, urllib.parse, urllib.request
77
import io
8+
import codecs
89
from . import handler
910
from . import xmlreader
1011

@@ -77,6 +78,10 @@ def _gettextwriter(out, encoding):
7778
# use a text writer as is
7879
return out
7980

81+
if isinstance(out, (codecs.StreamWriter, codecs.StreamReaderWriter)):
82+
# use a codecs stream writer as is
83+
return out
84+
8085
# wrap a binary writer with TextIOWrapper
8186
if isinstance(out, io.RawIOBase):
8287
# Keep the original file open when the TextIOWrapper is

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Library
2020
- Issue #1159051: Back out a fix for handling corrupted gzip files that
2121
broke backwards compatibility.
2222

23+
- Issue #17915: Fix interoperability of xml.sax with file objects returned by
24+
codecs.open().
25+
2326
Build
2427
-----
2528

0 commit comments

Comments
 (0)