@@ -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'"\\%c" is an invalid escape sequence' % (i - 32 )):
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'"\\\xfa" is an 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'"\\%o" is an invalid octal escape sequence' % i ):
12141223 check (rb'\%o' % i , bytes ([i & 0o377 ]))
12151224
1225+ with self .assertWarnsRegex (DeprecationWarning ,
1226+ r'"\\z" is an invalid escape sequence' ):
1227+ self .assertEqual (decode (br'\x\z' , 'ignore' ), (b'\\ z' , 4 ))
1228+ with self .assertWarnsRegex (DeprecationWarning ,
1229+ r'"\\501" is an 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" )
@@ -2661,24 +2677,40 @@ def test_escape_decode(self):
26612677 check (br"[\x410]" , "[A0]" )
26622678 check (br"\u20ac" , "\u20ac " )
26632679 check (br"\U0001d120" , "\U0001d120 " )
2680+
2681+ def test_decode_warnings (self ):
2682+ decode = codecs .unicode_escape_decode
2683+ check = coding_checker (self , decode )
26642684 for i in range (97 , 123 ):
26652685 b = bytes ([i ])
26662686 if b not in b'abfnrtuvx' :
2667- with self .assertWarns (DeprecationWarning ):
2687+ with self .assertWarnsRegex (DeprecationWarning ,
2688+ r'"\\%c" is an invalid escape sequence' % i ):
26682689 check (b"\\ " + b , "\\ " + chr (i ))
26692690 if b .upper () not in b'UN' :
2670- with self .assertWarns (DeprecationWarning ):
2691+ with self .assertWarnsRegex (DeprecationWarning ,
2692+ r'"\\%c" is an invalid escape sequence' % (i - 32 )):
26712693 check (b"\\ " + b .upper (), "\\ " + chr (i - 32 ))
2672- with self .assertWarns (DeprecationWarning ):
2694+ with self .assertWarnsRegex (DeprecationWarning ,
2695+ r'"\\8" is an invalid escape sequence' ):
26732696 check (br"\8" , "\\ 8" )
26742697 with self .assertWarns (DeprecationWarning ):
26752698 check (br"\9" , "\\ 9" )
2676- with self .assertWarns (DeprecationWarning ):
2699+ with self .assertWarnsRegex (DeprecationWarning ,
2700+ r'"\\\xfa" is an invalid escape sequence' ) as cm :
26772701 check (b"\\ \xfa " , "\\ \xfa " )
26782702 for i in range (0o400 , 0o1000 ):
2679- with self .assertWarns (DeprecationWarning ):
2703+ with self .assertWarnsRegex (DeprecationWarning ,
2704+ r'"\\%o" is an invalid octal escape sequence' % i ):
26802705 check (rb'\%o' % i , chr (i ))
26812706
2707+ with self .assertWarnsRegex (DeprecationWarning ,
2708+ r'"\\z" is an invalid escape sequence' ):
2709+ self .assertEqual (decode (br'\x\z' , 'ignore' ), ('\\ z' , 4 ))
2710+ with self .assertWarnsRegex (DeprecationWarning ,
2711+ r'"\\501" is an invalid octal escape sequence' ):
2712+ self .assertEqual (decode (br'\x\501' , 'ignore' ), ('\u0141 ' , 6 ))
2713+
26822714 def test_decode_errors (self ):
26832715 decode = codecs .unicode_escape_decode
26842716 for c , d in (b'x' , 2 ), (b'u' , 4 ), (b'U' , 4 ):
0 commit comments