@@ -81,6 +81,10 @@ def _default_chunk_size():
81
81
)
82
82
83
83
84
+ class BadIndex :
85
+ def __index__ (self ):
86
+ 1 / 0
87
+
84
88
class MockRawIOWithoutRead :
85
89
"""A RawIO implementation without read(), so as to exercise the default
86
90
RawIO.read() which calls readinto()."""
@@ -2716,8 +2720,31 @@ def test_constructor(self):
2716
2720
self .assertEqual (t .encoding , "utf-8" )
2717
2721
self .assertEqual (t .line_buffering , True )
2718
2722
self .assertEqual ("\xe9 \n " , t .readline ())
2719
- self .assertRaises (TypeError , t .__init__ , b , encoding = "utf-8" , newline = 42 )
2720
- self .assertRaises (ValueError , t .__init__ , b , encoding = "utf-8" , newline = 'xyzzy' )
2723
+ invalid_type = TypeError if self .is_C else ValueError
2724
+ with self .assertRaises (invalid_type ):
2725
+ t .__init__ (b , encoding = 42 )
2726
+ with self .assertRaises (UnicodeEncodeError ):
2727
+ t .__init__ (b , encoding = '\udcfe ' )
2728
+ with self .assertRaises (ValueError ):
2729
+ t .__init__ (b , encoding = 'utf-8\0 ' )
2730
+ with self .assertRaises (invalid_type ):
2731
+ t .__init__ (b , encoding = "utf-8" , errors = 42 )
2732
+ if support .Py_DEBUG or sys .flags .dev_mode or self .is_C :
2733
+ with self .assertRaises (UnicodeEncodeError ):
2734
+ t .__init__ (b , encoding = "utf-8" , errors = '\udcfe ' )
2735
+ if support .Py_DEBUG or sys .flags .dev_mode :
2736
+ # TODO: If encoded to UTF-8, should also be checked for
2737
+ # embedded null characters.
2738
+ with self .assertRaises (ValueError ):
2739
+ t .__init__ (b , encoding = "utf-8" , errors = 'replace\0 ' )
2740
+ with self .assertRaises (TypeError ):
2741
+ t .__init__ (b , encoding = "utf-8" , newline = 42 )
2742
+ with self .assertRaises (ValueError ):
2743
+ t .__init__ (b , encoding = "utf-8" , newline = '\udcfe ' )
2744
+ with self .assertRaises (ValueError ):
2745
+ t .__init__ (b , encoding = "utf-8" , newline = '\n \0 ' )
2746
+ with self .assertRaises (ValueError ):
2747
+ t .__init__ (b , encoding = "utf-8" , newline = 'xyzzy' )
2721
2748
2722
2749
def test_uninitialized (self ):
2723
2750
t = self .TextIOWrapper .__new__ (self .TextIOWrapper )
@@ -3766,6 +3793,59 @@ def test_reconfigure_defaults(self):
3766
3793
3767
3794
self .assertEqual (txt .detach ().getvalue (), b'LF\n CRLF\r \n ' )
3768
3795
3796
+ def test_reconfigure_errors (self ):
3797
+ txt = self .TextIOWrapper (self .BytesIO (), 'ascii' , 'replace' , '\r ' )
3798
+ with self .assertRaises (TypeError ): # there was a crash
3799
+ txt .reconfigure (encoding = 42 )
3800
+ if self .is_C :
3801
+ with self .assertRaises (UnicodeEncodeError ):
3802
+ txt .reconfigure (encoding = '\udcfe ' )
3803
+ with self .assertRaises (LookupError ):
3804
+ txt .reconfigure (encoding = 'locale\0 ' )
3805
+ # TODO: txt.reconfigure(encoding='utf-8\0')
3806
+ # TODO: txt.reconfigure(encoding='nonexisting')
3807
+ with self .assertRaises (TypeError ):
3808
+ txt .reconfigure (errors = 42 )
3809
+ if self .is_C :
3810
+ with self .assertRaises (UnicodeEncodeError ):
3811
+ txt .reconfigure (errors = '\udcfe ' )
3812
+ # TODO: txt.reconfigure(errors='ignore\0')
3813
+ # TODO: txt.reconfigure(errors='nonexisting')
3814
+ with self .assertRaises (TypeError ):
3815
+ txt .reconfigure (newline = 42 )
3816
+ with self .assertRaises (ValueError ):
3817
+ txt .reconfigure (newline = '\udcfe ' )
3818
+ with self .assertRaises (ValueError ):
3819
+ txt .reconfigure (newline = 'xyz' )
3820
+ if not self .is_C :
3821
+ # TODO: Should fail in C too.
3822
+ with self .assertRaises (ValueError ):
3823
+ txt .reconfigure (newline = '\n \0 ' )
3824
+ if self .is_C :
3825
+ # TODO: Use __bool__(), not __index__().
3826
+ with self .assertRaises (ZeroDivisionError ):
3827
+ txt .reconfigure (line_buffering = BadIndex ())
3828
+ with self .assertRaises (OverflowError ):
3829
+ txt .reconfigure (line_buffering = 2 ** 1000 )
3830
+ with self .assertRaises (ZeroDivisionError ):
3831
+ txt .reconfigure (write_through = BadIndex ())
3832
+ with self .assertRaises (OverflowError ):
3833
+ txt .reconfigure (write_through = 2 ** 1000 )
3834
+ with self .assertRaises (ZeroDivisionError ): # there was a crash
3835
+ txt .reconfigure (line_buffering = BadIndex (),
3836
+ write_through = BadIndex ())
3837
+ self .assertEqual (txt .encoding , 'ascii' )
3838
+ self .assertEqual (txt .errors , 'replace' )
3839
+ self .assertIs (txt .line_buffering , False )
3840
+ self .assertIs (txt .write_through , False )
3841
+
3842
+ txt .reconfigure (encoding = 'latin1' , errors = 'ignore' , newline = '\r \n ' ,
3843
+ line_buffering = True , write_through = True )
3844
+ self .assertEqual (txt .encoding , 'latin1' )
3845
+ self .assertEqual (txt .errors , 'ignore' )
3846
+ self .assertIs (txt .line_buffering , True )
3847
+ self .assertIs (txt .write_through , True )
3848
+
3769
3849
def test_reconfigure_newline (self ):
3770
3850
raw = self .BytesIO (b'CR\r EOF' )
3771
3851
txt = self .TextIOWrapper (raw , 'ascii' , newline = '\n ' )
@@ -4791,9 +4871,11 @@ def load_tests(loader, tests, pattern):
4791
4871
if test .__name__ .startswith ("C" ):
4792
4872
for name , obj in c_io_ns .items ():
4793
4873
setattr (test , name , obj )
4874
+ test .is_C = True
4794
4875
elif test .__name__ .startswith ("Py" ):
4795
4876
for name , obj in py_io_ns .items ():
4796
4877
setattr (test , name , obj )
4878
+ test .is_C = False
4797
4879
4798
4880
suite = loader .suiteClass ()
4799
4881
for test in tests :
0 commit comments