Issue #2175: SAX parsers now support a character stream of InputSource object.

This commit is contained in:
Serhiy Storchaka 2015-04-02 21:00:13 +03:00
parent 278ba2690c
commit 61de087f0f
7 changed files with 64 additions and 12 deletions

View file

@ -185,12 +185,24 @@ class PrepareInputSourceTest(unittest.TestCase):
def make_byte_stream(self):
return BytesIO(b"This is a byte stream.")
def make_character_stream(self):
return StringIO("This is a character stream.")
def checkContent(self, stream, content):
self.assertIsNotNone(stream)
self.assertEqual(stream.read(), content)
stream.close()
def test_character_stream(self):
# If the source is an InputSource with a character stream, use it.
src = InputSource(self.file)
src.setCharacterStream(self.make_character_stream())
prep = prepare_input_source(src)
self.assertIsNone(prep.getByteStream())
self.checkContent(prep.getCharacterStream(),
"This is a character stream.")
def test_byte_stream(self):
# If the source is an InputSource that does not have a character
# stream but does have a byte stream, use the byte stream.
@ -225,6 +237,14 @@ class PrepareInputSourceTest(unittest.TestCase):
self.checkContent(prep.getByteStream(),
b"This is a byte stream.")
def test_text_file(self):
# If the source is a text file-like object, use it as a character
# stream.
prep = prepare_input_source(self.make_character_stream())
self.assertIsNone(prep.getByteStream())
self.checkContent(prep.getCharacterStream(),
"This is a character stream.")
# ===== XMLGenerator
@ -904,6 +924,19 @@ class ExpatReaderTest(XmlTestBase):
self.assertEqual(result.getvalue(), xml_test_out)
def test_expat_inpsource_character_stream(self):
parser = create_parser()
result = BytesIO()
xmlgen = XMLGenerator(result)
parser.setContentHandler(xmlgen)
inpsrc = InputSource()
with open(TEST_XMLFILE, 'rt', encoding='iso-8859-1') as f:
inpsrc.setCharacterStream(f)
parser.parse(inpsrc)
self.assertEqual(result.getvalue(), xml_test_out)
# ===== IncrementalParser support
def test_expat_incremental(self):