@@ -135,6 +135,7 @@ def test_basic_re_sub(self):
135135 self .assertEqual (re .sub ('(?P<a>x)' , r'\g<a>\g<1>' , 'xx' ), 'xxxx' )
136136 self .assertEqual (re .sub ('(?P<unk>x)' , r'\g<unk>\g<unk>' , 'xx' ), 'xxxx' )
137137 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' )
138139
139140 self .assertEqual (re .sub ('a' , r'\t\n\v\r\f\a\b' , 'a' ), '\t \n \v \r \f \a \b ' )
140141 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):
274275 self .checkPatternError ('(?P<©>x)' , "bad character in group name '©'" , 4 )
275276 self .checkPatternError ('(?P=©)' , "bad character in group name '©'" , 4 )
276277 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 )
277293
278294 def test_symbolic_refs (self ):
279295 self .assertEqual (re .sub ('(?P<a>x)|(?P<b>y)' , r'\g<b>' , 'xx' ), '' )
@@ -306,12 +322,35 @@ def test_symbolic_refs_errors(self):
306322 re .sub ('(?P<a>x)' , r'\g<ab>' , 'xx' )
307323 self .checkTemplateError ('(?P<a>x)' , r'\g<-1>' , 'xx' ,
308324 "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' )
309338 self .checkTemplateError ('(?P<a>x)' , r'\g<©>' , 'xx' ,
310339 "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__ )
311346 self .checkTemplateError ('(?P<a>x)' , r'\g<㊀>' , 'xx' ,
312347 "bad character in group name '㊀'" , 3 )
313348 self .checkTemplateError ('(?P<a>x)' , r'\g<¹>' , 'xx' ,
314349 "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' )
315354
316355 def test_re_subn (self ):
317356 self .assertEqual (re .subn ("(?i)b+" , "x" , "bbbb BBBB" ), ('x x' , 2 ))
@@ -577,10 +616,27 @@ def test_re_groupref_exists_errors(self):
577616 self .checkPatternError (r'(?P<a>)(?(0)a|b)' , 'bad group number' , 10 )
578617 self .checkPatternError (r'()(?(-1)a|b)' ,
579618 "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)' )
580632 self .checkPatternError (r'()(?(㊀)a|b)' ,
581633 "bad character in group name '㊀'" , 5 )
582634 self .checkPatternError (r'()(?(¹)a|b)' ,
583635 "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)' )
584640 self .checkPatternError (r'()(?(1' ,
585641 "missing ), unterminated name" , 5 )
586642 self .checkPatternError (r'()(?(1)a' ,
0 commit comments