File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -2666,6 +2666,22 @@ class NonbytesStream(self.StringIO):
2666
2666
t = self .TextIOWrapper (NonbytesStream ('a' ))
2667
2667
self .assertEqual (t .read (), u'a' )
2668
2668
2669
+ def test_illegal_encoder (self ):
2670
+ # bpo-31271: A TypeError should be raised in case the return value of
2671
+ # encoder's encode() is invalid.
2672
+ class BadEncoder :
2673
+ def encode (self , dummy ):
2674
+ return u'spam'
2675
+ def get_bad_encoder (dummy ):
2676
+ return BadEncoder ()
2677
+ rot13 = codecs .lookup ("rot13" )
2678
+ with support .swap_attr (rot13 , '_is_text_encoding' , True ), \
2679
+ support .swap_attr (rot13 , 'incrementalencoder' , get_bad_encoder ):
2680
+ t = io .TextIOWrapper (io .BytesIO (b'foo' ), encoding = "rot13" )
2681
+ with self .assertRaises (TypeError ):
2682
+ t .write ('bar' )
2683
+ t .flush ()
2684
+
2669
2685
def test_illegal_decoder (self ):
2670
2686
# Issue #17106
2671
2687
# Bypass the early encoding check added in issue 20404
You can’t perform that action at this time.
0 commit comments