@@ -1196,23 +1196,39 @@ def test_escape(self):
11961196 check (br"[\1010]" , b"[A0]" )
11971197 check (br"[\x41]" , b"[A]" )
11981198 check (br"[\x410]" , b"[A0]" )
1199+
1200+ def test_warnings (self ):
1201+ decode = codecs .escape_decode
1202+ check = coding_checker (self , decode )
11991203 for i in range (97 , 123 ):
12001204 b = bytes ([i ])
12011205 if b not in b'abfnrtvx' :
1202- with self .assertWarns (DeprecationWarning ):
1206+ with self .assertWarnsRegex (DeprecationWarning ,
1207+ r"'\\%c' is an invalid escape sequence" % i ):
12031208 check (b"\\ " + b , b"\\ " + b )
1204- with self .assertWarns (DeprecationWarning ):
1209+ with self .assertWarnsRegex (DeprecationWarning ,
1210+ r"invalid escape sequence" ):
12051211 check (b"\\ " + b .upper (), b"\\ " + b .upper ())
1206- with self .assertWarns (DeprecationWarning ):
1212+ with self .assertWarnsRegex (DeprecationWarning ,
1213+ r"'\\8' is an invalid escape sequence" ):
12071214 check (br"\8" , b"\\ 8" )
12081215 with self .assertWarns (DeprecationWarning ):
12091216 check (br"\9" , b"\\ 9" )
1210- with self .assertWarns (DeprecationWarning ):
1217+ with self .assertWarnsRegex (DeprecationWarning ,
1218+ r'invalid escape sequence' ) as cm :
12111219 check (b"\\ \xfa " , b"\\ \xfa " )
12121220 for i in range (0o400 , 0o1000 ):
1213- with self .assertWarns (DeprecationWarning ):
1221+ with self .assertWarnsRegex (DeprecationWarning ,
1222+ r'invalid octal escape sequence' ):
12141223 check (rb'\%o' % i , bytes ([i & 0o377 ]))
12151224
1225+ with self .assertWarnsRegex (DeprecationWarning ,
1226+ r'invalid escape sequence' ):
1227+ self .assertEqual (decode (br'\x\z' , 'ignore' ), (b'\\ z' , 4 ))
1228+ with self .assertWarnsRegex (DeprecationWarning ,
1229+ r'invalid octal escape sequence' ):
1230+ self .assertEqual (decode (br'\x\501' , 'ignore' ), (b'A' , 6 ))
1231+
12161232 def test_errors (self ):
12171233 decode = codecs .escape_decode
12181234 self .assertRaises (ValueError , decode , br"\x" )
@@ -2479,24 +2495,40 @@ def test_escape_decode(self):
24792495 check (br"[\x410]" , "[A0]" )
24802496 check (br"\u20ac" , "\u20ac " )
24812497 check (br"\U0001d120" , "\U0001d120 " )
2498+
2499+ def test_decode_warnings (self ):
2500+ decode = codecs .unicode_escape_decode
2501+ check = coding_checker (self , decode )
24822502 for i in range (97 , 123 ):
24832503 b = bytes ([i ])
24842504 if b not in b'abfnrtuvx' :
2485- with self .assertWarns (DeprecationWarning ):
2505+ with self .assertWarnsRegex (DeprecationWarning ,
2506+ r'invalid escape sequence' ):
24862507 check (b"\\ " + b , "\\ " + chr (i ))
24872508 if b .upper () not in b'UN' :
2488- with self .assertWarns (DeprecationWarning ):
2509+ with self .assertWarnsRegex (DeprecationWarning ,
2510+ 'invalid escape sequence' ):
24892511 check (b"\\ " + b .upper (), "\\ " + chr (i - 32 ))
2490- with self .assertWarns (DeprecationWarning ):
2512+ with self .assertWarnsRegex (DeprecationWarning ,
2513+ r'invalid escape sequence' ):
24912514 check (br"\8" , "\\ 8" )
24922515 with self .assertWarns (DeprecationWarning ):
24932516 check (br"\9" , "\\ 9" )
2494- with self .assertWarns (DeprecationWarning ):
2517+ with self .assertWarnsRegex (DeprecationWarning ,
2518+ r'invalid escape sequence' ) as cm :
24952519 check (b"\\ \xfa " , "\\ \xfa " )
24962520 for i in range (0o400 , 0o1000 ):
2497- with self .assertWarns (DeprecationWarning ):
2521+ with self .assertWarnsRegex (DeprecationWarning ,
2522+ r'invalid octal escape sequence' ):
24982523 check (rb'\%o' % i , chr (i ))
24992524
2525+ with self .assertWarnsRegex (DeprecationWarning ,
2526+ r'invalid escape sequence' ):
2527+ self .assertEqual (decode (br'\x\z' , 'ignore' ), ('\\ z' , 4 ))
2528+ with self .assertWarnsRegex (DeprecationWarning ,
2529+ r'invalid octal escape sequence' ):
2530+ self .assertEqual (decode (br'\x\501' , 'ignore' ), ('\u0141 ' , 6 ))
2531+
25002532 def test_decode_errors (self ):
25012533 decode = codecs .unicode_escape_decode
25022534 for c , d in (b'x' , 2 ), (b'u' , 4 ), (b'U' , 4 ):
0 commit comments