summaryrefslogtreecommitdiff
path: root/Lib/test/test_sax.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-04-02 21:00:13 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2015-04-02 21:00:13 +0300
commitdd50ab27a592f151dea1d0a4f9abdef4e5c02ab4 (patch)
tree24e4db67475d92ad7a4547a3991191e0fc898bdf /Lib/test/test_sax.py
parentfc65ee530a51ec43a31fdcde3e26334d29f49637 (diff)
downloadcpython-dd50ab27a592f151dea1d0a4f9abdef4e5c02ab4.tar.gz
Issue #2175: SAX parsers now support a character stream of InputSource object.
Diffstat (limited to 'Lib/test/test_sax.py')
-rw-r--r--Lib/test/test_sax.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py
index c8d5b21c85..813dc2ebf1 100644
--- a/Lib/test/test_sax.py
+++ b/Lib/test/test_sax.py
@@ -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):