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