3
3
verbose , run_unittest , import_module ,
4
4
precisionbigmemtest , _2G , cpython_only ,
5
5
captured_stdout , have_unicode , requires_unicode , u ,
6
- check_warnings )
6
+ check_warnings , check_py3k_warnings )
7
7
import locale
8
8
import re
9
9
from re import Scanner
@@ -66,11 +66,13 @@ def test_basic_re_sub(self):
66
66
self .assertEqual (re .sub ('(?P<unk>x)' , '\g<unk>\g<unk>' , 'xx' ), 'xxxx' )
67
67
self .assertEqual (re .sub ('(?P<unk>x)' , '\g<1>\g<1>' , 'xx' ), 'xxxx' )
68
68
69
- self .assertEqual (re .sub ('a' ,r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D' ,'a' ),
70
- '\t \n \v \r \f \a \b \\ B\\ Z\a \\ A\\ w\\ W\\ s\\ S\\ d\\ D' )
71
- self .assertEqual (re .sub ('a' , '\t \n \v \r \f \a ' , 'a' ), '\t \n \v \r \f \a ' )
72
- self .assertEqual (re .sub ('a' , '\t \n \v \r \f \a ' , 'a' ),
73
- (chr (9 )+ chr (10 )+ chr (11 )+ chr (13 )+ chr (12 )+ chr (7 )))
69
+ self .assertEqual (re .sub ('a' , r'\t\n\v\r\f\a\b' , 'a' ), '\t \n \v \r \f \a \b ' )
70
+ self .assertEqual (re .sub ('a' , '\t \n \v \r \f \a \b ' , 'a' ), '\t \n \v \r \f \a \b ' )
71
+ self .assertEqual (re .sub ('a' , '\t \n \v \r \f \a \b ' , 'a' ),
72
+ (chr (9 )+ chr (10 )+ chr (11 )+ chr (13 )+ chr (12 )+ chr (7 )+ chr (8 )))
73
+ for c in 'cdehijklmopqsuwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' :
74
+ with check_py3k_warnings ():
75
+ self .assertEqual (re .sub ('a' , '\\ ' + c , 'a' ), '\\ ' + c )
74
76
75
77
self .assertEqual (re .sub ('^\s*' , 'X' , 'test' ), 'Xtest' )
76
78
@@ -223,11 +225,11 @@ def test_re_subn(self):
223
225
224
226
def test_re_split (self ):
225
227
self .assertEqual (re .split (":" , ":a:b::c" ), ['' , 'a' , 'b' , '' , 'c' ])
226
- self .assertEqual (re .split (":* " , ":a:b::c" ), ['' , 'a' , 'b' , 'c' ])
227
- self .assertEqual (re .split ("(:* )" , ":a:b::c" ),
228
+ self .assertEqual (re .split (":+ " , ":a:b::c" ), ['' , 'a' , 'b' , 'c' ])
229
+ self .assertEqual (re .split ("(:+ )" , ":a:b::c" ),
228
230
['' , ':' , 'a' , ':' , 'b' , '::' , 'c' ])
229
- self .assertEqual (re .split ("(?::* )" , ":a:b::c" ), ['' , 'a' , 'b' , 'c' ])
230
- self .assertEqual (re .split ("(:)* " , ":a:b::c" ),
231
+ self .assertEqual (re .split ("(?::+ )" , ":a:b::c" ), ['' , 'a' , 'b' , 'c' ])
232
+ self .assertEqual (re .split ("(:)+ " , ":a:b::c" ),
231
233
['' , ':' , 'a' , ':' , 'b' , ':' , 'c' ])
232
234
self .assertEqual (re .split ("([b:]+)" , ":a:b::c" ),
233
235
['' , ':' , 'a' , ':b::' , 'c' ])
@@ -237,13 +239,34 @@ def test_re_split(self):
237
239
self .assertEqual (re .split ("(?:b)|(?::+)" , ":a:b::c" ),
238
240
['' , 'a' , '' , '' , 'c' ])
239
241
242
+ for sep , expected in [
243
+ (':*' , ['' , 'a' , 'b' , 'c' ]),
244
+ ('(?::*)' , ['' , 'a' , 'b' , 'c' ]),
245
+ ('(:*)' , ['' , ':' , 'a' , ':' , 'b' , '::' , 'c' ]),
246
+ ('(:)*' , ['' , ':' , 'a' , ':' , 'b' , ':' , 'c' ]),
247
+ ]:
248
+ with check_py3k_warnings (('' , FutureWarning )):
249
+ self .assertEqual (re .split (sep , ':a:b::c' ), expected )
250
+
251
+ for sep , expected in [
252
+ ('' , [':a:b::c' ]),
253
+ (r'\b' , [':a:b::c' ]),
254
+ (r'(?=:)' , [':a:b::c' ]),
255
+ (r'(?<=:)' , [':a:b::c' ]),
256
+ ]:
257
+ with check_py3k_warnings ():
258
+ self .assertEqual (re .split (sep , ':a:b::c' ), expected )
259
+
240
260
def test_qualified_re_split (self ):
241
261
self .assertEqual (re .split (":" , ":a:b::c" , 2 ), ['' , 'a' , 'b::c' ])
242
262
self .assertEqual (re .split (':' , 'a:b:c:d' , 2 ), ['a' , 'b' , 'c:d' ])
243
263
self .assertEqual (re .split ("(:)" , ":a:b::c" , 2 ),
244
264
['' , ':' , 'a' , ':' , 'b::c' ])
245
- self .assertEqual (re .split ("(:* )" , ":a:b::c" , 2 ),
265
+ self .assertEqual (re .split ("(:+ )" , ":a:b::c" , 2 ),
246
266
['' , ':' , 'a' , ':' , 'b::c' ])
267
+ with check_py3k_warnings (('' , FutureWarning )):
268
+ self .assertEqual (re .split ("(:*)" , ":a:b::c" , maxsplit = 2 ),
269
+ ['' , ':' , 'a' , ':' , 'b::c' ])
247
270
248
271
def test_re_findall (self ):
249
272
self .assertEqual (re .findall (":+" , "abc" ), [])
@@ -404,6 +427,29 @@ def test_special_escapes(self):
404
427
self .assertEqual (re .search (r"\d\D\w\W\s\S" ,
405
428
"1aa! a" , re .UNICODE ).group (0 ), "1aa! a" )
406
429
430
+ def test_other_escapes (self ):
431
+ self .assertRaises (re .error , re .compile , "\\ " )
432
+ self .assertEqual (re .match (r"\(" , '(' ).group (), '(' )
433
+ self .assertIsNone (re .match (r"\(" , ')' ))
434
+ self .assertEqual (re .match (r"\\" , '\\ ' ).group (), '\\ ' )
435
+ self .assertEqual (re .match (r"[\]]" , ']' ).group (), ']' )
436
+ self .assertIsNone (re .match (r"[\]]" , '[' ))
437
+ self .assertEqual (re .match (r"[a\-c]" , '-' ).group (), '-' )
438
+ self .assertIsNone (re .match (r"[a\-c]" , 'b' ))
439
+ self .assertEqual (re .match (r"[\^a]+" , 'a^' ).group (), 'a^' )
440
+ self .assertIsNone (re .match (r"[\^a]+" , 'b' ))
441
+ re .purge () # for warnings
442
+ for c in 'ceghijklmopquyzCEFGHIJKLMNOPQRTUVXY' :
443
+ warn = FutureWarning if c in 'Uu' else DeprecationWarning
444
+ with check_py3k_warnings (('' , warn )):
445
+ self .assertEqual (re .match ('\\ %c$' % c , c ).group (), c )
446
+ self .assertIsNone (re .match ('\\ %c' % c , 'a' ))
447
+ for c in 'ceghijklmopquyzABCEFGHIJKLMNOPQRTUVXYZ' :
448
+ warn = FutureWarning if c in 'Uu' else DeprecationWarning
449
+ with check_py3k_warnings (('' , warn )):
450
+ self .assertEqual (re .match ('[\\ %c]$' % c , c ).group (), c )
451
+ self .assertIsNone (re .match ('[\\ %c]' % c , 'a' ))
452
+
407
453
def test_string_boundaries (self ):
408
454
# See http://bugs.python.org/issue10713
409
455
self .assertEqual (re .search (r"\b(abc)\b" , "abc" ).group (1 ),
@@ -931,6 +977,19 @@ def test_inline_flags(self):
931
977
self .assertTrue (re .match ('(?ixu) ' + upper_char , lower_char ))
932
978
self .assertTrue (re .match ('(?ixu) ' + lower_char , upper_char ))
933
979
980
+ # Incompatibilities
981
+ re .purge ()
982
+ with check_py3k_warnings ():
983
+ re .compile ('' , re .LOCALE | re .UNICODE )
984
+ with check_py3k_warnings ():
985
+ re .compile ('(?L)' , re .UNICODE )
986
+ with check_py3k_warnings ():
987
+ re .compile ('(?u)' , re .LOCALE )
988
+ with check_py3k_warnings ():
989
+ re .compile ('(?Lu)' )
990
+ with check_py3k_warnings ():
991
+ re .compile ('(?uL)' )
992
+
934
993
def test_dollar_matches_twice (self ):
935
994
"$ matches the end of string, and just before the terminating \n "
936
995
pattern = re .compile ('$' )
@@ -967,8 +1026,9 @@ def test_compile(self):
967
1026
def test_bug_13899 (self ):
968
1027
# Issue #13899: re pattern r"[\A]" should work like "A" but matches
969
1028
# nothing. Ditto B and Z.
970
- self .assertEqual (re .findall (r'[\A\B\b\C\Z]' , 'AB\b CZ' ),
971
- ['A' , 'B' , '\b ' , 'C' , 'Z' ])
1029
+ with check_py3k_warnings ():
1030
+ self .assertEqual (re .findall (r'[\A\B\b\C\Z]' , 'AB\b CZ' ),
1031
+ ['A' , 'B' , '\b ' , 'C' , 'Z' ])
972
1032
973
1033
@precisionbigmemtest (size = _2G , memuse = 1 )
974
1034
def test_large_search (self , size ):
@@ -1261,7 +1321,11 @@ def run_re_tests():
1261
1321
1262
1322
def test_main ():
1263
1323
run_unittest (ReTests )
1264
- run_re_tests ()
1324
+ deprecations = [
1325
+ ('bad escape' , DeprecationWarning ),
1326
+ ]
1327
+ with check_py3k_warnings (* deprecations ):
1328
+ run_re_tests ()
1265
1329
1266
1330
if __name__ == "__main__" :
1267
1331
test_main ()
0 commit comments