Skip to content

Commit 63af7b3

Browse files
Add more tests for group names and refs in RE (GH-91695)
(cherry picked from commit 7407008) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 1c27a1a commit 63af7b3

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

Lib/test/test_re.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ def test_symbolic_groups(self):
217217
re.compile(r'(?P<a>x)(?P=a)(?(a)y)')
218218
re.compile(r'(?P<a1>x)(?P=a1)(?(a1)y)')
219219
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):
220230
self.checkPatternError(r'(?P<a>)(?P<a>)',
221231
"redefinition of group name 'a' as group 2; "
222232
"was group 1")
@@ -242,16 +252,22 @@ def test_symbolic_groups(self):
242252
self.checkPatternError(r'(?(-1))', "bad character in group name '-1'", 3)
243253
self.checkPatternError(r'(?(1a))', "bad character in group name '1a'", 3)
244254
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)')
248255
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')
249266
# Support > 100 groups.
250267
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')
253269

254-
def test_symbolic_refs(self):
270+
def test_symbolic_refs_errors(self):
255271
self.checkTemplateError('(?P<a>x)', r'\g<a', 'xx',
256272
'missing >, unterminated name', 3)
257273
self.checkTemplateError('(?P<a>x)', r'\g<', 'xx',
@@ -269,18 +285,14 @@ def test_symbolic_refs(self):
269285
'invalid group reference 2', 1)
270286
with self.assertRaisesRegex(IndexError, "unknown group name 'ab'"):
271287
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'), '')
274288
self.checkTemplateError('(?P<a>x)', r'\g<-1>', 'xx',
275289
"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')
279290
self.checkTemplateError('(?P<a>x)', r'\g<©>', 'xx',
280291
"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)
284296

285297
def test_re_subn(self):
286298
self.assertEqual(re.subn("(?i)b+", "x", "bbbb BBBB"), ('x x', 2))
@@ -542,9 +554,23 @@ def test_re_groupref_exists(self):
542554
pat = '(?:%s)(?(200)z)' % pat
543555
self.assertEqual(re.match(pat, 'xc8yz').span(), (0, 5))
544556

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)
546569
self.checkPatternError(r'()(?(1)a|b',
547570
'missing ), unterminated subpattern', 2)
571+
self.checkPatternError(r'()(?(1)a|b|c',
572+
'conditional backref with more than '
573+
'two branches', 10)
548574
self.checkPatternError(r'()(?(1)a|b|c)',
549575
'conditional backref with more than '
550576
'two branches', 10)

0 commit comments

Comments
 (0)