@@ -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" )
@@ -2479,24 +2495,40 @@ def test_escape_decode(self):
2479
2495
check (br"[\x410]" , "[A0]" )
2480
2496
check (br"\u20ac" , "\u20ac " )
2481
2497
check (br"\U0001d120" , "\U0001d120 " )
2498
+
2499
+ def test_decode_warnings (self ):
2500
+ decode = codecs .unicode_escape_decode
2501
+ check = coding_checker (self , decode )
2482
2502
for i in range (97 , 123 ):
2483
2503
b = bytes ([i ])
2484
2504
if b not in b'abfnrtuvx' :
2485
- with self .assertWarns (DeprecationWarning ):
2505
+ with self .assertWarnsRegex (DeprecationWarning ,
2506
+ r"invalid escape sequence '\\%c'" % i ):
2486
2507
check (b"\\ " + b , "\\ " + chr (i ))
2487
2508
if b .upper () not in b'UN' :
2488
- with self .assertWarns (DeprecationWarning ):
2509
+ with self .assertWarnsRegex (DeprecationWarning ,
2510
+ r"invalid escape sequence '\\%c'" % (i - 32 )):
2489
2511
check (b"\\ " + b .upper (), "\\ " + chr (i - 32 ))
2490
- with self .assertWarns (DeprecationWarning ):
2512
+ with self .assertWarnsRegex (DeprecationWarning ,
2513
+ r"invalid escape sequence '\\8'" ):
2491
2514
check (br"\8" , "\\ 8" )
2492
2515
with self .assertWarns (DeprecationWarning ):
2493
2516
check (br"\9" , "\\ 9" )
2494
- with self .assertWarns (DeprecationWarning ):
2517
+ with self .assertWarnsRegex (DeprecationWarning ,
2518
+ r"invalid escape sequence '\\\xfa'" ) as cm :
2495
2519
check (b"\\ \xfa " , "\\ \xfa " )
2496
2520
for i in range (0o400 , 0o1000 ):
2497
- with self .assertWarns (DeprecationWarning ):
2521
+ with self .assertWarnsRegex (DeprecationWarning ,
2522
+ r"invalid octal escape sequence '\\%o'" % i ):
2498
2523
check (rb'\%o' % i , chr (i ))
2499
2524
2525
+ with self .assertWarnsRegex (DeprecationWarning ,
2526
+ r"invalid escape sequence '\\z'" ):
2527
+ self .assertEqual (decode (br'\x\z' , 'ignore' ), ('\\ z' , 4 ))
2528
+ with self .assertWarnsRegex (DeprecationWarning ,
2529
+ r"invalid octal escape sequence '\\501'" ):
2530
+ self .assertEqual (decode (br'\x\501' , 'ignore' ), ('\u0141 ' , 6 ))
2531
+
2500
2532
def test_decode_errors (self ):
2501
2533
decode = codecs .unicode_escape_decode
2502
2534
for c , d in (b'x' , 2 ), (b'u' , 4 ), (b'U' , 4 ):
0 commit comments