@@ -1736,8 +1736,6 @@ def __str__(self):
1736
1736
'character buffers are decoded to unicode'
1737
1737
)
1738
1738
1739
- self .assertRaises (TypeError , str , 42 , 42 , 42 )
1740
-
1741
1739
def test_constructor_keyword_args (self ):
1742
1740
"""Pass various keyword argument combinations to the constructor."""
1743
1741
# The object argument can be passed as a keyword.
@@ -2652,22 +2650,45 @@ def test_check_encoding_errors(self):
2652
2650
self .assertEqual (proc .rc , 10 , proc )
2653
2651
2654
2652
def test_str_invalid_call (self ):
2655
- check = lambda * a , ** kw : self .assertRaises (TypeError , str , * a , ** kw )
2656
-
2657
2653
# too many args
2658
- check (1 , "" , "" , 1 )
2654
+ with self .assertRaisesRegex (TypeError , r"str expected at most 3 arguments, got 4" ):
2655
+ str ("too" , "many" , "argu" , "ments" )
2656
+ with self .assertRaisesRegex (TypeError , r"str expected at most 3 arguments, got 4" ):
2657
+ str (1 , "" , "" , 1 )
2659
2658
2660
2659
# no such kw arg
2661
- check (test = 1 )
2660
+ with self .assertRaisesRegex (TypeError , r"str\(\) got an unexpected keyword argument 'test'" ):
2661
+ str (test = 1 )
2662
2662
2663
2663
# 'encoding' must be str
2664
- check (1 , encoding = 1 )
2665
- check (1 , 1 )
2664
+ with self .assertRaisesRegex (TypeError , r"str\(\) argument 'encoding' must be str, not int" ):
2665
+ str (1 , 1 )
2666
+ with self .assertRaisesRegex (TypeError , r"str\(\) argument 'encoding' must be str, not int" ):
2667
+ str (1 , encoding = 1 )
2668
+ with self .assertRaisesRegex (TypeError , r"str\(\) argument 'encoding' must be str, not bytes" ):
2669
+ str (b"x" , b"ascii" )
2670
+ with self .assertRaisesRegex (TypeError , r"str\(\) argument 'encoding' must be str, not bytes" ):
2671
+ str (b"x" , encoding = b"ascii" )
2666
2672
2667
2673
# 'errors' must be str
2668
- check (1 , errors = 1 )
2669
- check (1 , "" , errors = 1 )
2670
- check (1 , 1 , 1 )
2674
+ with self .assertRaisesRegex (TypeError , r"str\(\) argument 'encoding' must be str, not int" ):
2675
+ str (1 , 1 , 1 )
2676
+ with self .assertRaisesRegex (TypeError , r"str\(\) argument 'errors' must be str, not int" ):
2677
+ str (1 , errors = 1 )
2678
+ with self .assertRaisesRegex (TypeError , r"str\(\) argument 'errors' must be str, not int" ):
2679
+ str (1 , "" , errors = 1 )
2680
+ with self .assertRaisesRegex (TypeError , r"str\(\) argument 'errors' must be str, not bytes" ):
2681
+ str (b"x" , "ascii" , b"strict" )
2682
+ with self .assertRaisesRegex (TypeError , r"str\(\) argument 'errors' must be str, not bytes" ):
2683
+ str (b"x" , "ascii" , errors = b"strict" )
2684
+
2685
+ # both positional and kwarg
2686
+ with self .assertRaisesRegex (TypeError , r"argument for str\(\) given by name \('encoding'\) and position \(2\)" ):
2687
+ str (b"x" , "utf-8" , encoding = "ascii" )
2688
+ with self .assertRaisesRegex (TypeError , r"str\(\) takes at most 3 arguments \(4 given\)" ):
2689
+ str (b"x" , "utf-8" , "ignore" , encoding = "ascii" )
2690
+ with self .assertRaisesRegex (TypeError , r"str\(\) takes at most 3 arguments \(4 given\)" ):
2691
+ str (b"x" , "utf-8" , "strict" , errors = "ignore" )
2671
2692
2672
2693
2673
2694
class StringModuleTest (unittest .TestCase ):
0 commit comments