cpython: 1c01571ce0f4 (original) (raw)
Mercurial > cpython
changeset 83737:1c01571ce0f4 3.2
Issue #17915: Fix interoperability of xml.sax with file objects returned by codecs.open(). [#17915]
Georg Brandl georg@python.org | |
---|---|
date | Sun, 12 May 2013 11:41:12 +0200 |
parents | 854ba6f414a8 |
children | cef745775b65 19a5fbb924b1 |
files | Lib/test/test_sax.py Lib/xml/sax/saxutils.py Misc/NEWS |
diffstat | 3 files changed, 39 insertions(+), 0 deletions(-)[+] [-] Lib/test/test_sax.py 31 Lib/xml/sax/saxutils.py 5 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/test/test_sax.py +++ b/Lib/test/test_sax.py @@ -15,6 +15,7 @@ from xml.sax.expatreader import create_p 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()[](#l1.17)
writer = codecs.getwriter('ascii')(raw, 'xmlcharrefreplace')[](#l1.18)
writer.getvalue = raw.getvalue[](#l1.19)
return writer[](#l1.20)
- def xml(self, doc, encoding='iso-8859-1'):
return ('<?xml version="1.0" encoding="%s"?>\n%s' %[](#l1.23)
(encoding, doc)).encode('ascii', 'xmlcharrefreplace')[](#l1.24)
+ +class StreamReaderWriterXmlgenTest(XmlgenTest, unittest.TestCase):
- def ioclass(self):
writer = codecs.open(self.fname, 'w', encoding='ascii',[](#l1.30)
errors='xmlcharrefreplace', buffering=0)[](#l1.31)
self.addCleanup(support.unlink, self.fname)[](#l1.32)
writer.getvalue = self.getvalue[](#l1.33)
return writer[](#l1.34)
- def xml(self, doc, encoding='iso-8859-1'):
return ('<?xml version="1.0" encoding="%s"?>\n%s' %[](#l1.41)
(encoding, doc)).encode('ascii', 'xmlcharrefreplace')[](#l1.42)
start = b'\n' @@ -946,6 +975,8 @@ def test_main(): StringXmlgenTest, BytesXmlgenTest, WriterXmlgenTest,
StreamWriterXmlgenTest,[](#l1.50)
StreamReaderWriterXmlgenTest,[](#l1.51) ExpatReaderTest,[](#l1.52) ErrorReportingTest,[](#l1.53) XmlReaderTest)[](#l1.54)
--- a/Lib/xml/sax/saxutils.py +++ b/Lib/xml/sax/saxutils.py @@ -5,6 +5,7 @@ convenience of application and driver wr 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[](#l2.16)
return out[](#l2.17)
+ # wrap a binary writer with TextIOWrapper if isinstance(out, io.RawIOBase): # Keep the original file open when the TextIOWrapper is