@@ -185,12 +185,24 @@ def tearDown(self):
185185 def make_byte_stream (self ):
186186 return BytesIO (b"This is a byte stream." )
187187
188+ def make_character_stream (self ):
189+ return StringIO ("This is a character stream." )
190+
188191 def checkContent (self , stream , content ):
189192 self .assertIsNotNone (stream )
190193 self .assertEqual (stream .read (), content )
191194 stream .close ()
192195
193196
197+ def test_character_stream (self ):
198+ # If the source is an InputSource with a character stream, use it.
199+ src = InputSource (self .file )
200+ src .setCharacterStream (self .make_character_stream ())
201+ prep = prepare_input_source (src )
202+ self .assertIsNone (prep .getByteStream ())
203+ self .checkContent (prep .getCharacterStream (),
204+ "This is a character stream." )
205+
194206 def test_byte_stream (self ):
195207 # If the source is an InputSource that does not have a character
196208 # stream but does have a byte stream, use the byte stream.
@@ -225,6 +237,14 @@ def test_binary_file(self):
225237 self .checkContent (prep .getByteStream (),
226238 b"This is a byte stream." )
227239
240+ def test_text_file (self ):
241+ # If the source is a text file-like object, use it as a character
242+ # stream.
243+ prep = prepare_input_source (self .make_character_stream ())
244+ self .assertIsNone (prep .getByteStream ())
245+ self .checkContent (prep .getCharacterStream (),
246+ "This is a character stream." )
247+
228248
229249# ===== XMLGenerator
230250
@@ -904,6 +924,19 @@ def test_expat_inpsource_byte_stream(self):
904924
905925 self .assertEqual (result .getvalue (), xml_test_out )
906926
927+ def test_expat_inpsource_character_stream (self ):
928+ parser = create_parser ()
929+ result = BytesIO ()
930+ xmlgen = XMLGenerator (result )
931+
932+ parser .setContentHandler (xmlgen )
933+ inpsrc = InputSource ()
934+ with open (TEST_XMLFILE , 'rt' , encoding = 'iso-8859-1' ) as f :
935+ inpsrc .setCharacterStream (f )
936+ parser .parse (inpsrc )
937+
938+ self .assertEqual (result .getvalue (), xml_test_out )
939+
907940 # ===== IncrementalParser support
908941
909942 def test_expat_incremental (self ):
0 commit comments