@@ -77,6 +77,10 @@ def _default_chunk_size():
77
77
)
78
78
79
79
80
+ class BadIndex :
81
+ def __index__ (self ):
82
+ 1 / 0
83
+
80
84
class MockRawIOWithoutRead :
81
85
"""A RawIO implementation without read(), so as to exercise the default
82
86
RawIO.read() which calls readinto()."""
@@ -2709,8 +2713,31 @@ def test_constructor(self):
2709
2713
self .assertEqual (t .encoding , "utf-8" )
2710
2714
self .assertEqual (t .line_buffering , True )
2711
2715
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
+ if support .Py_DEBUG or sys .flags .dev_mode or self .is_C :
2726
+ with self .assertRaises (UnicodeEncodeError ):
2727
+ t .__init__ (b , encoding = "utf-8" , errors = '\udcfe ' )
2728
+ if support .Py_DEBUG or sys .flags .dev_mode :
2729
+ # TODO: If encoded to UTF-8, should also be checked for
2730
+ # embedded null characters.
2731
+ with self .assertRaises (ValueError ):
2732
+ t .__init__ (b , encoding = "utf-8" , errors = 'replace\0 ' )
2733
+ with self .assertRaises (TypeError ):
2734
+ t .__init__ (b , encoding = "utf-8" , newline = 42 )
2735
+ with self .assertRaises (ValueError ):
2736
+ t .__init__ (b , encoding = "utf-8" , newline = '\udcfe ' )
2737
+ with self .assertRaises (ValueError ):
2738
+ t .__init__ (b , encoding = "utf-8" , newline = '\n \0 ' )
2739
+ with self .assertRaises (ValueError ):
2740
+ t .__init__ (b , encoding = "utf-8" , newline = 'xyzzy' )
2714
2741
2715
2742
def test_uninitialized (self ):
2716
2743
t = self .TextIOWrapper .__new__ (self .TextIOWrapper )
@@ -3756,6 +3783,59 @@ def test_reconfigure_defaults(self):
3756
3783
3757
3784
self .assertEqual (txt .detach ().getvalue (), b'LF\n CRLF\r \n ' )
3758
3785
3786
+ def test_reconfigure_errors (self ):
3787
+ txt = self .TextIOWrapper (self .BytesIO (), 'ascii' , 'replace' , '\r ' )
3788
+ with self .assertRaises (TypeError ): # there was a crash
3789
+ txt .reconfigure (encoding = 42 )
3790
+ if self .is_C :
3791
+ with self .assertRaises (UnicodeEncodeError ):
3792
+ txt .reconfigure (encoding = '\udcfe ' )
3793
+ with self .assertRaises (LookupError ):
3794
+ txt .reconfigure (encoding = 'locale\0 ' )
3795
+ # TODO: txt.reconfigure(encoding='utf-8\0')
3796
+ # TODO: txt.reconfigure(encoding='nonexisting')
3797
+ with self .assertRaises (TypeError ):
3798
+ txt .reconfigure (errors = 42 )
3799
+ if self .is_C :
3800
+ with self .assertRaises (UnicodeEncodeError ):
3801
+ txt .reconfigure (errors = '\udcfe ' )
3802
+ # TODO: txt.reconfigure(errors='ignore\0')
3803
+ # TODO: txt.reconfigure(errors='nonexisting')
3804
+ with self .assertRaises (TypeError ):
3805
+ txt .reconfigure (newline = 42 )
3806
+ with self .assertRaises (ValueError ):
3807
+ txt .reconfigure (newline = '\udcfe ' )
3808
+ with self .assertRaises (ValueError ):
3809
+ txt .reconfigure (newline = 'xyz' )
3810
+ if not self .is_C :
3811
+ # TODO: Should fail in C too.
3812
+ with self .assertRaises (ValueError ):
3813
+ txt .reconfigure (newline = '\n \0 ' )
3814
+ if self .is_C :
3815
+ # TODO: Use __bool__(), not __index__().
3816
+ with self .assertRaises (ZeroDivisionError ):
3817
+ txt .reconfigure (line_buffering = BadIndex ())
3818
+ with self .assertRaises (OverflowError ):
3819
+ txt .reconfigure (line_buffering = 2 ** 1000 )
3820
+ with self .assertRaises (ZeroDivisionError ):
3821
+ txt .reconfigure (write_through = BadIndex ())
3822
+ with self .assertRaises (OverflowError ):
3823
+ txt .reconfigure (write_through = 2 ** 1000 )
3824
+ with self .assertRaises (ZeroDivisionError ): # there was a crash
3825
+ txt .reconfigure (line_buffering = BadIndex (),
3826
+ write_through = BadIndex ())
3827
+ self .assertEqual (txt .encoding , 'ascii' )
3828
+ self .assertEqual (txt .errors , 'replace' )
3829
+ self .assertIs (txt .line_buffering , False )
3830
+ self .assertIs (txt .write_through , False )
3831
+
3832
+ txt .reconfigure (encoding = 'latin1' , errors = 'ignore' , newline = '\r \n ' ,
3833
+ line_buffering = True , write_through = True )
3834
+ self .assertEqual (txt .encoding , 'latin1' )
3835
+ self .assertEqual (txt .errors , 'ignore' )
3836
+ self .assertIs (txt .line_buffering , True )
3837
+ self .assertIs (txt .write_through , True )
3838
+
3759
3839
def test_reconfigure_newline (self ):
3760
3840
raw = self .BytesIO (b'CR\r EOF' )
3761
3841
txt = self .TextIOWrapper (raw , 'ascii' , newline = '\n ' )
@@ -4781,9 +4861,11 @@ def load_tests(loader, tests, pattern):
4781
4861
if test .__name__ .startswith ("C" ):
4782
4862
for name , obj in c_io_ns .items ():
4783
4863
setattr (test , name , obj )
4864
+ test .is_C = True
4784
4865
elif test .__name__ .startswith ("Py" ):
4785
4866
for name , obj in py_io_ns .items ():
4786
4867
setattr (test , name , obj )
4868
+ test .is_C = False
4787
4869
4788
4870
suite = loader .suiteClass ()
4789
4871
for test in tests :
0 commit comments