@@ -77,6 +77,10 @@ def _default_chunk_size():
7777)
7878
7979
80+ class BadIndex :
81+ def __index__ (self ):
82+ 1 / 0
83+
8084class MockRawIOWithoutRead :
8185 """A RawIO implementation without read(), so as to exercise the default
8286 RawIO.read() which calls readinto()."""
@@ -2709,8 +2713,27 @@ def test_constructor(self):
27092713 self .assertEqual (t .encoding , "utf-8" )
27102714 self .assertEqual (t .line_buffering , True )
27112715 self .assertEqual ("\xe9 \n " , t .readline ())
2712- self .assertRaises (TypeError , t .__init__ , b , encoding = "utf-8" , newline = 42 )
2713- self .assertRaises (ValueError , t .__init__ , b , encoding = "utf-8" , newline = 'xyzzy' )
2716+ invalid_type = TypeError if self .is_C else ValueError
2717+ with self .assertRaises (invalid_type ):
2718+ t .__init__ (b , encoding = 42 )
2719+ with self .assertRaises (UnicodeEncodeError ):
2720+ t .__init__ (b , encoding = '\udcfe ' )
2721+ with self .assertRaises (ValueError ):
2722+ t .__init__ (b , encoding = 'utf-8\0 ' )
2723+ with self .assertRaises (invalid_type ):
2724+ t .__init__ (b , encoding = "utf-8" , errors = 42 )
2725+ with self .assertRaises (UnicodeEncodeError ):
2726+ t .__init__ (b , encoding = "utf-8" , errors = '\udcfe ' )
2727+ with self .assertRaises (ValueError ):
2728+ t .__init__ (b , encoding = "utf-8" , errors = 'replace\0 ' )
2729+ with self .assertRaises (TypeError ):
2730+ t .__init__ (b , encoding = "utf-8" , newline = 42 )
2731+ with self .assertRaises (ValueError ):
2732+ t .__init__ (b , encoding = "utf-8" , newline = '\udcfe ' )
2733+ with self .assertRaises (ValueError ):
2734+ t .__init__ (b , encoding = "utf-8" , newline = '\n \0 ' )
2735+ with self .assertRaises (ValueError ):
2736+ t .__init__ (b , encoding = "utf-8" , newline = 'xyzzy' )
27142737
27152738 def test_uninitialized (self ):
27162739 t = self .TextIOWrapper .__new__ (self .TextIOWrapper )
@@ -3756,6 +3779,59 @@ def test_reconfigure_defaults(self):
37563779
37573780 self .assertEqual (txt .detach ().getvalue (), b'LF\n CRLF\r \n ' )
37583781
3782+ def test_reconfigure_errors (self ):
3783+ txt = self .TextIOWrapper (self .BytesIO (), 'ascii' , 'replace' , '\r ' )
3784+ with self .assertRaises (TypeError ): # there was a crash
3785+ txt .reconfigure (encoding = 42 )
3786+ if self .is_C :
3787+ with self .assertRaises (UnicodeEncodeError ):
3788+ txt .reconfigure (encoding = '\udcfe ' )
3789+ with self .assertRaises (LookupError ):
3790+ txt .reconfigure (encoding = 'locale\0 ' )
3791+ # TODO: txt.reconfigure(encoding='utf-8\0')
3792+ # TODO: txt.reconfigure(encoding='nonexisting')
3793+ with self .assertRaises (TypeError ):
3794+ txt .reconfigure (errors = 42 )
3795+ if self .is_C :
3796+ with self .assertRaises (UnicodeEncodeError ):
3797+ txt .reconfigure (errors = '\udcfe ' )
3798+ # TODO: txt.reconfigure(errors='ignore\0')
3799+ # TODO: txt.reconfigure(errors='nonexisting')
3800+ with self .assertRaises (TypeError ):
3801+ txt .reconfigure (newline = 42 )
3802+ with self .assertRaises (ValueError ):
3803+ txt .reconfigure (newline = '\udcfe ' )
3804+ with self .assertRaises (ValueError ):
3805+ txt .reconfigure (newline = 'xyz' )
3806+ if not self .is_C :
3807+ # TODO: Should fail in C too.
3808+ with self .assertRaises (ValueError ):
3809+ txt .reconfigure (newline = '\n \0 ' )
3810+ if self .is_C :
3811+ # TODO: Use __bool__(), not __index__().
3812+ with self .assertRaises (ZeroDivisionError ):
3813+ txt .reconfigure (line_buffering = BadIndex ())
3814+ with self .assertRaises (OverflowError ):
3815+ txt .reconfigure (line_buffering = 2 ** 1000 )
3816+ with self .assertRaises (ZeroDivisionError ):
3817+ txt .reconfigure (write_through = BadIndex ())
3818+ with self .assertRaises (OverflowError ):
3819+ txt .reconfigure (write_through = 2 ** 1000 )
3820+ with self .assertRaises (ZeroDivisionError ): # there was a crash
3821+ txt .reconfigure (line_buffering = BadIndex (),
3822+ write_through = BadIndex ())
3823+ self .assertEqual (txt .encoding , 'ascii' )
3824+ self .assertEqual (txt .errors , 'replace' )
3825+ self .assertIs (txt .line_buffering , False )
3826+ self .assertIs (txt .write_through , False )
3827+
3828+ txt .reconfigure (encoding = 'latin1' , errors = 'ignore' , newline = '\r \n ' ,
3829+ line_buffering = True , write_through = True )
3830+ self .assertEqual (txt .encoding , 'latin1' )
3831+ self .assertEqual (txt .errors , 'ignore' )
3832+ self .assertIs (txt .line_buffering , True )
3833+ self .assertIs (txt .write_through , True )
3834+
37593835 def test_reconfigure_newline (self ):
37603836 raw = self .BytesIO (b'CR\r EOF' )
37613837 txt = self .TextIOWrapper (raw , 'ascii' , newline = '\n ' )
@@ -4781,9 +4857,11 @@ def load_tests(loader, tests, pattern):
47814857 if test .__name__ .startswith ("C" ):
47824858 for name , obj in c_io_ns .items ():
47834859 setattr (test , name , obj )
4860+ test .is_C = True
47844861 elif test .__name__ .startswith ("Py" ):
47854862 for name , obj in py_io_ns .items ():
47864863 setattr (test , name , obj )
4864+ test .is_C = False
47874865
47884866 suite = loader .suiteClass ()
47894867 for test in tests :
0 commit comments