@@ -135,6 +135,7 @@ def test_basic_re_sub(self):
135
135
self .assertEqual (re .sub ('(?P<a>x)' , r'\g<a>\g<1>' , 'xx' ), 'xxxx' )
136
136
self .assertEqual (re .sub ('(?P<unk>x)' , r'\g<unk>\g<unk>' , 'xx' ), 'xxxx' )
137
137
self .assertEqual (re .sub ('(?P<unk>x)' , r'\g<1>\g<1>' , 'xx' ), 'xxxx' )
138
+ self .assertEqual (re .sub ('()x' , r'\g<0>\g<0>' , 'xx' ), 'xxxx' )
138
139
139
140
self .assertEqual (re .sub ('a' , r'\t\n\v\r\f\a\b' , 'a' ), '\t \n \v \r \f \a \b ' )
140
141
self .assertEqual (re .sub ('a' , '\t \n \v \r \f \a \b ' , 'a' ), '\t \n \v \r \f \a \b ' )
@@ -274,6 +275,21 @@ def test_symbolic_groups_errors(self):
274
275
self .checkPatternError ('(?P<©>x)' , "bad character in group name '©'" , 4 )
275
276
self .checkPatternError ('(?P=©)' , "bad character in group name '©'" , 4 )
276
277
self .checkPatternError ('(?(©)y)' , "bad character in group name '©'" , 3 )
278
+ with self .assertWarnsRegex (DeprecationWarning ,
279
+ r"bad character in group name '\\xc2\\xb5' "
280
+ r"at position 4" ) as w :
281
+ re .compile (b'(?P<\xc2 \xb5 >x)' )
282
+ self .assertEqual (w .filename , __file__ )
283
+ with self .assertWarnsRegex (DeprecationWarning ,
284
+ r"bad character in group name '\\xc2\\xb5' "
285
+ r"at position 4" ):
286
+ self .checkPatternError (b'(?P=\xc2 \xb5 )' ,
287
+ r"unknown group name '\xc2\xb5'" , 4 )
288
+ with self .assertWarnsRegex (DeprecationWarning ,
289
+ r"bad character in group name '\\xc2\\xb5' "
290
+ r"at position 3" ):
291
+ self .checkPatternError (b'(?(\xc2 \xb5 )y)' ,
292
+ r"unknown group name '\xc2\xb5'" , 3 )
277
293
278
294
def test_symbolic_refs (self ):
279
295
self .assertEqual (re .sub ('(?P<a>x)|(?P<b>y)' , r'\g<b>' , 'xx' ), '' )
@@ -306,12 +322,35 @@ def test_symbolic_refs_errors(self):
306
322
re .sub ('(?P<a>x)' , r'\g<ab>' , 'xx' )
307
323
self .checkTemplateError ('(?P<a>x)' , r'\g<-1>' , 'xx' ,
308
324
"bad character in group name '-1'" , 3 )
325
+ with self .assertWarnsRegex (DeprecationWarning ,
326
+ r"bad character in group name '\+1' "
327
+ r"at position 3" ) as w :
328
+ re .sub ('(?P<a>x)' , r'\g<+1>' , 'xx' )
329
+ self .assertEqual (w .filename , __file__ )
330
+ with self .assertWarnsRegex (DeprecationWarning ,
331
+ r"bad character in group name '1_0' "
332
+ r"at position 3" ):
333
+ re .sub ('()' * 10 , r'\g<1_0>' , 'xx' )
334
+ with self .assertWarnsRegex (DeprecationWarning ,
335
+ r"bad character in group name ' 1 ' "
336
+ r"at position 3" ):
337
+ re .sub ('(?P<a>x)' , r'\g< 1 >' , 'xx' )
309
338
self .checkTemplateError ('(?P<a>x)' , r'\g<©>' , 'xx' ,
310
339
"bad character in group name '©'" , 3 )
340
+ with self .assertWarnsRegex (DeprecationWarning ,
341
+ r"bad character in group name '\\xc2\\xb5' "
342
+ r"at position 3" ) as w :
343
+ with self .assertRaisesRegex (IndexError , "unknown group name '\xc2 \xb5 '" ):
344
+ re .sub (b'(?P<a>x)' , b'\\ g<\xc2 \xb5 >' , b'xx' )
345
+ self .assertEqual (w .filename , __file__ )
311
346
self .checkTemplateError ('(?P<a>x)' , r'\g<㊀>' , 'xx' ,
312
347
"bad character in group name '㊀'" , 3 )
313
348
self .checkTemplateError ('(?P<a>x)' , r'\g<¹>' , 'xx' ,
314
349
"bad character in group name '¹'" , 3 )
350
+ with self .assertWarnsRegex (DeprecationWarning ,
351
+ r"bad character in group name '१' "
352
+ r"at position 3" ):
353
+ re .sub ('(?P<a>x)' , r'\g<१>' , 'xx' )
315
354
316
355
def test_re_subn (self ):
317
356
self .assertEqual (re .subn ("(?i)b+" , "x" , "bbbb BBBB" ), ('x x' , 2 ))
@@ -577,10 +616,27 @@ def test_re_groupref_exists_errors(self):
577
616
self .checkPatternError (r'(?P<a>)(?(0)a|b)' , 'bad group number' , 10 )
578
617
self .checkPatternError (r'()(?(-1)a|b)' ,
579
618
"bad character in group name '-1'" , 5 )
619
+ with self .assertWarnsRegex (DeprecationWarning ,
620
+ r"bad character in group name '\+1' "
621
+ r"at position 5" ) as w :
622
+ re .compile (r'()(?(+1)a|b)' )
623
+ self .assertEqual (w .filename , __file__ )
624
+ with self .assertWarnsRegex (DeprecationWarning ,
625
+ r"bad character in group name '1_0' "
626
+ r"at position 23" ):
627
+ re .compile (r'()' * 10 + r'(?(1_0)a|b)' )
628
+ with self .assertWarnsRegex (DeprecationWarning ,
629
+ r"bad character in group name ' 1 ' "
630
+ r"at position 5" ):
631
+ re .compile (r'()(?( 1 )a|b)' )
580
632
self .checkPatternError (r'()(?(㊀)a|b)' ,
581
633
"bad character in group name '㊀'" , 5 )
582
634
self .checkPatternError (r'()(?(¹)a|b)' ,
583
635
"bad character in group name '¹'" , 5 )
636
+ with self .assertWarnsRegex (DeprecationWarning ,
637
+ r"bad character in group name '१' "
638
+ r"at position 5" ):
639
+ re .compile (r'()(?(१)a|b)' )
584
640
self .checkPatternError (r'()(?(1' ,
585
641
"missing ), unterminated name" , 5 )
586
642
self .checkPatternError (r'()(?(1)a' ,
0 commit comments