@@ -1196,23 +1196,39 @@ def test_escape(self):
1196
1196
check (br"[\1010]" , b"[A0]" )
1197
1197
check (br"[\x41]" , b"[A]" )
1198
1198
check (br"[\x410]" , b"[A0]" )
1199
+
1200
+ def test_warnings (self ):
1201
+ decode = codecs .escape_decode
1202
+ check = coding_checker (self , decode )
1199
1203
for i in range (97 , 123 ):
1200
1204
b = bytes ([i ])
1201
1205
if b not in b'abfnrtvx' :
1202
- with self .assertWarns (DeprecationWarning ):
1206
+ with self .assertWarnsRegex (DeprecationWarning ,
1207
+ r"invalid escape sequence '\\%c'" % i ):
1203
1208
check (b"\\ " + b , b"\\ " + b )
1204
- with self .assertWarns (DeprecationWarning ):
1209
+ with self .assertWarnsRegex (DeprecationWarning ,
1210
+ r"invalid escape sequence '\\%c'" % (i - 32 )):
1205
1211
check (b"\\ " + b .upper (), b"\\ " + b .upper ())
1206
- with self .assertWarns (DeprecationWarning ):
1212
+ with self .assertWarnsRegex (DeprecationWarning ,
1213
+ r"invalid escape sequence '\\8'" ):
1207
1214
check (br"\8" , b"\\ 8" )
1208
1215
with self .assertWarns (DeprecationWarning ):
1209
1216
check (br"\9" , b"\\ 9" )
1210
- with self .assertWarns (DeprecationWarning ):
1217
+ with self .assertWarnsRegex (DeprecationWarning ,
1218
+ r"invalid escape sequence '\\\xfa'" ) as cm :
1211
1219
check (b"\\ \xfa " , b"\\ \xfa " )
1212
1220
for i in range (0o400 , 0o1000 ):
1213
- with self .assertWarns (DeprecationWarning ):
1221
+ with self .assertWarnsRegex (DeprecationWarning ,
1222
+ r"invalid octal escape sequence '\\%o'" % i ):
1214
1223
check (rb'\%o' % i , bytes ([i & 0o377 ]))
1215
1224
1225
+ with self .assertWarnsRegex (DeprecationWarning ,
1226
+ r"invalid escape sequence '\\z'" ):
1227
+ self .assertEqual (decode (br'\x\z' , 'ignore' ), (b'\\ z' , 4 ))
1228
+ with self .assertWarnsRegex (DeprecationWarning ,
1229
+ r"invalid octal escape sequence '\\501'" ):
1230
+ self .assertEqual (decode (br'\x\501' , 'ignore' ), (b'A' , 6 ))
1231
+
1216
1232
def test_errors (self ):
1217
1233
decode = codecs .escape_decode
1218
1234
self .assertRaises (ValueError , decode , br"\x" )
@@ -2661,24 +2677,40 @@ def test_escape_decode(self):
2661
2677
check (br"[\x410]" , "[A0]" )
2662
2678
check (br"\u20ac" , "\u20ac " )
2663
2679
check (br"\U0001d120" , "\U0001d120 " )
2680
+
2681
+ def test_decode_warnings (self ):
2682
+ decode = codecs .unicode_escape_decode
2683
+ check = coding_checker (self , decode )
2664
2684
for i in range (97 , 123 ):
2665
2685
b = bytes ([i ])
2666
2686
if b not in b'abfnrtuvx' :
2667
- with self .assertWarns (DeprecationWarning ):
2687
+ with self .assertWarnsRegex (DeprecationWarning ,
2688
+ r"invalid escape sequence '\\%c'" % i ):
2668
2689
check (b"\\ " + b , "\\ " + chr (i ))
2669
2690
if b .upper () not in b'UN' :
2670
- with self .assertWarns (DeprecationWarning ):
2691
+ with self .assertWarnsRegex (DeprecationWarning ,
2692
+ r"invalid escape sequence '\\%c'" % (i - 32 )):
2671
2693
check (b"\\ " + b .upper (), "\\ " + chr (i - 32 ))
2672
- with self .assertWarns (DeprecationWarning ):
2694
+ with self .assertWarnsRegex (DeprecationWarning ,
2695
+ r"invalid escape sequence '\\8'" ):
2673
2696
check (br"\8" , "\\ 8" )
2674
2697
with self .assertWarns (DeprecationWarning ):
2675
2698
check (br"\9" , "\\ 9" )
2676
- with self .assertWarns (DeprecationWarning ):
2699
+ with self .assertWarnsRegex (DeprecationWarning ,
2700
+ r"invalid escape sequence '\\\xfa'" ) as cm :
2677
2701
check (b"\\ \xfa " , "\\ \xfa " )
2678
2702
for i in range (0o400 , 0o1000 ):
2679
- with self .assertWarns (DeprecationWarning ):
2703
+ with self .assertWarnsRegex (DeprecationWarning ,
2704
+ r"invalid octal escape sequence '\\%o'" % i ):
2680
2705
check (rb'\%o' % i , chr (i ))
2681
2706
2707
+ with self .assertWarnsRegex (DeprecationWarning ,
2708
+ r"invalid escape sequence '\\z'" ):
2709
+ self .assertEqual (decode (br'\x\z' , 'ignore' ), ('\\ z' , 4 ))
2710
+ with self .assertWarnsRegex (DeprecationWarning ,
2711
+ r"invalid octal escape sequence '\\501'" ):
2712
+ self .assertEqual (decode (br'\x\501' , 'ignore' ), ('\u0141 ' , 6 ))
2713
+
2682
2714
def test_decode_errors (self ):
2683
2715
decode = codecs .unicode_escape_decode
2684
2716
for c , d in (b'x' , 2 ), (b'u' , 4 ), (b'U' , 4 ):
0 commit comments