@@ -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()."""
@@ -2613,8 +2617,29 @@ def test_constructor(self):
2613
2617
self .assertEqual (t .encoding , "utf-8" )
2614
2618
self .assertEqual (t .line_buffering , True )
2615
2619
self .assertEqual ("\xe9 \n " , t .readline ())
2616
- self .assertRaises (TypeError , t .__init__ , b , encoding = "utf-8" , newline = 42 )
2617
- self .assertRaises (ValueError , t .__init__ , b , encoding = "utf-8" , newline = 'xyzzy' )
2620
+ invalid_type = TypeError if self .is_C else ValueError
2621
+ with self .assertRaises (invalid_type ):
2622
+ t .__init__ (b , encoding = 42 )
2623
+ with self .assertRaises (UnicodeEncodeError ):
2624
+ t .__init__ (b , encoding = '\udcfe ' )
2625
+ with self .assertRaises (ValueError ):
2626
+ t .__init__ (b , encoding = 'utf-8\0 ' )
2627
+ with self .assertRaises (invalid_type ):
2628
+ t .__init__ (b , encoding = "utf-8" , errors = 42 )
2629
+ if support .Py_DEBUG or sys .flags .dev_mode or self .is_C :
2630
+ with self .assertRaises (UnicodeEncodeError ):
2631
+ t .__init__ (b , encoding = "utf-8" , errors = '\udcfe ' )
2632
+ if support .Py_DEBUG or sys .flags .dev_mode or self .is_C :
2633
+ with self .assertRaises (ValueError ):
2634
+ t .__init__ (b , encoding = "utf-8" , errors = 'replace\0 ' )
2635
+ with self .assertRaises (TypeError ):
2636
+ t .__init__ (b , encoding = "utf-8" , newline = 42 )
2637
+ with self .assertRaises (ValueError ):
2638
+ t .__init__ (b , encoding = "utf-8" , newline = '\udcfe ' )
2639
+ with self .assertRaises (ValueError ):
2640
+ t .__init__ (b , encoding = "utf-8" , newline = '\n \0 ' )
2641
+ with self .assertRaises (ValueError ):
2642
+ t .__init__ (b , encoding = "utf-8" , newline = 'xyzzy' )
2618
2643
2619
2644
def test_uninitialized (self ):
2620
2645
t = self .TextIOWrapper .__new__ (self .TextIOWrapper )
@@ -3663,6 +3688,59 @@ def test_reconfigure_defaults(self):
3663
3688
3664
3689
self .assertEqual (txt .detach ().getvalue (), b'LF\n CRLF\r \n ' )
3665
3690
3691
+ def test_reconfigure_errors (self ):
3692
+ txt = self .TextIOWrapper (self .BytesIO (), 'ascii' , 'replace' , '\r ' )
3693
+ with self .assertRaises (TypeError ): # there was a crash
3694
+ txt .reconfigure (encoding = 42 )
3695
+ if self .is_C :
3696
+ with self .assertRaises (UnicodeEncodeError ):
3697
+ txt .reconfigure (encoding = '\udcfe ' )
3698
+ with self .assertRaises (LookupError ):
3699
+ txt .reconfigure (encoding = 'locale\0 ' )
3700
+ # TODO: txt.reconfigure(encoding='utf-8\0')
3701
+ # TODO: txt.reconfigure(encoding='nonexisting')
3702
+ with self .assertRaises (TypeError ):
3703
+ txt .reconfigure (errors = 42 )
3704
+ if self .is_C :
3705
+ with self .assertRaises (UnicodeEncodeError ):
3706
+ txt .reconfigure (errors = '\udcfe ' )
3707
+ # TODO: txt.reconfigure(errors='ignore\0')
3708
+ # TODO: txt.reconfigure(errors='nonexisting')
3709
+ with self .assertRaises (TypeError ):
3710
+ txt .reconfigure (newline = 42 )
3711
+ with self .assertRaises (ValueError ):
3712
+ txt .reconfigure (newline = '\udcfe ' )
3713
+ with self .assertRaises (ValueError ):
3714
+ txt .reconfigure (newline = 'xyz' )
3715
+ if not self .is_C :
3716
+ # TODO: Should fail in C too.
3717
+ with self .assertRaises (ValueError ):
3718
+ txt .reconfigure (newline = '\n \0 ' )
3719
+ if self .is_C :
3720
+ # TODO: Use __bool__(), not __index__().
3721
+ with self .assertRaises (ZeroDivisionError ):
3722
+ txt .reconfigure (line_buffering = BadIndex ())
3723
+ with self .assertRaises (OverflowError ):
3724
+ txt .reconfigure (line_buffering = 2 ** 1000 )
3725
+ with self .assertRaises (ZeroDivisionError ):
3726
+ txt .reconfigure (write_through = BadIndex ())
3727
+ with self .assertRaises (OverflowError ):
3728
+ txt .reconfigure (write_through = 2 ** 1000 )
3729
+ with self .assertRaises (ZeroDivisionError ): # there was a crash
3730
+ txt .reconfigure (line_buffering = BadIndex (),
3731
+ write_through = BadIndex ())
3732
+ self .assertEqual (txt .encoding , 'ascii' )
3733
+ self .assertEqual (txt .errors , 'replace' )
3734
+ self .assertIs (txt .line_buffering , False )
3735
+ self .assertIs (txt .write_through , False )
3736
+
3737
+ txt .reconfigure (encoding = 'latin1' , errors = 'ignore' , newline = '\r \n ' ,
3738
+ line_buffering = True , write_through = True )
3739
+ self .assertEqual (txt .encoding , 'latin1' )
3740
+ self .assertEqual (txt .errors , 'ignore' )
3741
+ self .assertIs (txt .line_buffering , True )
3742
+ self .assertIs (txt .write_through , True )
3743
+
3666
3744
def test_reconfigure_newline (self ):
3667
3745
raw = self .BytesIO (b'CR\r EOF' )
3668
3746
txt = self .TextIOWrapper (raw , 'ascii' , newline = '\n ' )
@@ -4693,9 +4771,11 @@ def load_tests(loader, tests, pattern):
4693
4771
if test .__name__ .startswith ("C" ):
4694
4772
for name , obj in c_io_ns .items ():
4695
4773
setattr (test , name , obj )
4774
+ test .is_C = True
4696
4775
elif test .__name__ .startswith ("Py" ):
4697
4776
for name , obj in py_io_ns .items ():
4698
4777
setattr (test , name , obj )
4778
+ test .is_C = False
4699
4779
4700
4780
suite = loader .suiteClass ()
4701
4781
for test in tests :
0 commit comments