@@ -217,6 +217,16 @@ def test_symbolic_groups(self):
217
217
re .compile (r'(?P<a>x)(?P=a)(?(a)y)' )
218
218
re .compile (r'(?P<a1>x)(?P=a1)(?(a1)y)' )
219
219
re .compile (r'(?P<a1>x)\1(?(1)y)' )
220
+ re .compile (b'(?P<a1>x)(?P=a1)(?(a1)y)' )
221
+ # New valid identifiers in Python 3
222
+ re .compile ('(?P<µ>x)(?P=µ)(?(µ)y)' )
223
+ re .compile ('(?P<𝔘𝔫𝔦𝔠𝔬𝔡𝔢>x)(?P=𝔘𝔫𝔦𝔠𝔬𝔡𝔢)(?(𝔘𝔫𝔦𝔠𝔬𝔡𝔢)y)' )
224
+ # Support > 100 groups.
225
+ pat = '|' .join ('x(?P<a%d>%x)y' % (i , i ) for i in range (1 , 200 + 1 ))
226
+ pat = '(?:%s)(?(200)z|t)' % pat
227
+ self .assertEqual (re .match (pat , 'xc8yz' ).span (), (0 , 5 ))
228
+
229
+ def test_symbolic_groups_errors (self ):
220
230
self .checkPatternError (r'(?P<a>)(?P<a>)' ,
221
231
"redefinition of group name 'a' as group 2; "
222
232
"was group 1" )
@@ -242,16 +252,22 @@ def test_symbolic_groups(self):
242
252
self .checkPatternError (r'(?(-1))' , "bad character in group name '-1'" , 3 )
243
253
self .checkPatternError (r'(?(1a))' , "bad character in group name '1a'" , 3 )
244
254
self .checkPatternError (r'(?(a.))' , "bad character in group name 'a.'" , 3 )
245
- # New valid/invalid identifiers in Python 3
246
- re .compile ('(?P<µ>x)(?P=µ)(?(µ)y)' )
247
- re .compile ('(?P<𝔘𝔫𝔦𝔠𝔬𝔡𝔢>x)(?P=𝔘𝔫𝔦𝔠𝔬𝔡𝔢)(?(𝔘𝔫𝔦𝔠𝔬𝔡𝔢)y)' )
248
255
self .checkPatternError ('(?P<©>x)' , "bad character in group name '©'" , 4 )
256
+ self .checkPatternError ('(?P=©)' , "bad character in group name '©'" , 4 )
257
+ self .checkPatternError ('(?(©)y)' , "bad character in group name '©'" , 3 )
258
+
259
+ def test_symbolic_refs (self ):
260
+ self .assertEqual (re .sub ('(?P<a>x)|(?P<b>y)' , r'\g<b>' , 'xx' ), '' )
261
+ self .assertEqual (re .sub ('(?P<a>x)|(?P<b>y)' , r'\2' , 'xx' ), '' )
262
+ self .assertEqual (re .sub (b'(?P<a1>x)' , br'\g<a1>' , b'xx' ), b'xx' )
263
+ # New valid identifiers in Python 3
264
+ self .assertEqual (re .sub ('(?P<µ>x)' , r'\g<µ>' , 'xx' ), 'xx' )
265
+ self .assertEqual (re .sub ('(?P<𝔘𝔫𝔦𝔠𝔬𝔡𝔢>x)' , r'\g<𝔘𝔫𝔦𝔠𝔬𝔡𝔢>' , 'xx' ), 'xx' )
249
266
# Support > 100 groups.
250
267
pat = '|' .join ('x(?P<a%d>%x)y' % (i , i ) for i in range (1 , 200 + 1 ))
251
- pat = '(?:%s)(?(200)z|t)' % pat
252
- self .assertEqual (re .match (pat , 'xc8yz' ).span (), (0 , 5 ))
268
+ self .assertEqual (re .sub (pat , r'\g<200>' , 'xc8yzxc8y' ), 'c8zc8' )
253
269
254
- def test_symbolic_refs (self ):
270
+ def test_symbolic_refs_errors (self ):
255
271
self .checkTemplateError ('(?P<a>x)' , r'\g<a' , 'xx' ,
256
272
'missing >, unterminated name' , 3 )
257
273
self .checkTemplateError ('(?P<a>x)' , r'\g<' , 'xx' ,
@@ -269,18 +285,14 @@ def test_symbolic_refs(self):
269
285
'invalid group reference 2' , 1 )
270
286
with self .assertRaisesRegex (IndexError , "unknown group name 'ab'" ):
271
287
re .sub ('(?P<a>x)' , r'\g<ab>' , 'xx' )
272
- self .assertEqual (re .sub ('(?P<a>x)|(?P<b>y)' , r'\g<b>' , 'xx' ), '' )
273
- self .assertEqual (re .sub ('(?P<a>x)|(?P<b>y)' , r'\2' , 'xx' ), '' )
274
288
self .checkTemplateError ('(?P<a>x)' , r'\g<-1>' , 'xx' ,
275
289
"bad character in group name '-1'" , 3 )
276
- # New valid/invalid identifiers in Python 3
277
- self .assertEqual (re .sub ('(?P<µ>x)' , r'\g<µ>' , 'xx' ), 'xx' )
278
- self .assertEqual (re .sub ('(?P<𝔘𝔫𝔦𝔠𝔬𝔡𝔢>x)' , r'\g<𝔘𝔫𝔦𝔠𝔬𝔡𝔢>' , 'xx' ), 'xx' )
279
290
self .checkTemplateError ('(?P<a>x)' , r'\g<©>' , 'xx' ,
280
291
"bad character in group name '©'" , 3 )
281
- # Support > 100 groups.
282
- pat = '|' .join ('x(?P<a%d>%x)y' % (i , i ) for i in range (1 , 200 + 1 ))
283
- self .assertEqual (re .sub (pat , r'\g<200>' , 'xc8yzxc8y' ), 'c8zc8' )
292
+ self .checkTemplateError ('(?P<a>x)' , r'\g<㊀>' , 'xx' ,
293
+ "bad character in group name '㊀'" , 3 )
294
+ self .checkTemplateError ('(?P<a>x)' , r'\g<¹>' , 'xx' ,
295
+ "bad character in group name '¹'" , 3 )
284
296
285
297
def test_re_subn (self ):
286
298
self .assertEqual (re .subn ("(?i)b+" , "x" , "bbbb BBBB" ), ('x x' , 2 ))
@@ -542,9 +554,23 @@ def test_re_groupref_exists(self):
542
554
pat = '(?:%s)(?(200)z)' % pat
543
555
self .assertEqual (re .match (pat , 'xc8yz' ).span (), (0 , 5 ))
544
556
545
- self .checkPatternError (r'(?P<a>)(?(0))' , 'bad group number' , 10 )
557
+ def test_re_groupref_exists_errors (self ):
558
+ self .checkPatternError (r'(?P<a>)(?(0)a|b)' , 'bad group number' , 10 )
559
+ self .checkPatternError (r'()(?(-1)a|b)' ,
560
+ "bad character in group name '-1'" , 5 )
561
+ self .checkPatternError (r'()(?(㊀)a|b)' ,
562
+ "bad character in group name '㊀'" , 5 )
563
+ self .checkPatternError (r'()(?(¹)a|b)' ,
564
+ "bad character in group name '¹'" , 5 )
565
+ self .checkPatternError (r'()(?(1' ,
566
+ "missing ), unterminated name" , 5 )
567
+ self .checkPatternError (r'()(?(1)a' ,
568
+ "missing ), unterminated subpattern" , 2 )
546
569
self .checkPatternError (r'()(?(1)a|b' ,
547
570
'missing ), unterminated subpattern' , 2 )
571
+ self .checkPatternError (r'()(?(1)a|b|c' ,
572
+ 'conditional backref with more than '
573
+ 'two branches' , 10 )
548
574
self .checkPatternError (r'()(?(1)a|b|c)' ,
549
575
'conditional backref with more than '
550
576
'two branches' , 10 )
0 commit comments