Skip to content

Commit 6f47a13

Browse files
[3.13] Revert "[3.13] gh-140797: Forbid capturing groups in re.Scanner lexicon patterns (GH-140944) (GH-140983)" (GH-142231)
Revert "[3.13] gh-140797: Forbid capturing groups in re.Scanner lexicon patterns (GH-140944) (GH-140983)" This reverts commit ee894d2.
1 parent 29100c8 commit 6f47a13

File tree

3 files changed

+4
-22
lines changed

3 files changed

+4
-22
lines changed

Lib/re/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,9 @@ def __init__(self, lexicon, flags=0):
399399
s = _parser.State()
400400
s.flags = flags
401401
for phrase, action in lexicon:
402-
sub_pattern = _parser.parse(phrase, flags)
403-
if sub_pattern.state.groups != 1:
404-
raise ValueError("Cannot use capturing groups in re.Scanner")
405402
gid = s.opengroup()
406403
p.append(_parser.SubPattern(s, [
407-
(SUBPATTERN, (gid, 0, 0, sub_pattern)),
404+
(SUBPATTERN, (gid, 0, 0, _parser.parse(phrase, flags))),
408405
]))
409406
s.closegroup(gid, p[-1])
410407
p = _parser.SubPattern(s, [(BRANCH, (None, p))])

Lib/test/test_re.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,24 +1638,6 @@ def s_int(scanner, token): return int(token)
16381638
(['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5,
16391639
'op+', 'bar'], ''))
16401640

1641-
def test_bug_gh140797(self):
1642-
# gh140797: Capturing groups are not allowed in re.Scanner
1643-
1644-
msg = r"Cannot use capturing groups in re\.Scanner"
1645-
# Capturing group throws an error
1646-
with self.assertRaisesRegex(ValueError, msg):
1647-
Scanner([("(a)b", None)])
1648-
1649-
# Named Group
1650-
with self.assertRaisesRegex(ValueError, msg):
1651-
Scanner([("(?P<name>a)", None)])
1652-
1653-
# Non-capturing groups should pass normally
1654-
s = Scanner([("(?:a)b", lambda scanner, token: token)])
1655-
result, rem = s.scan("ab")
1656-
self.assertEqual(result,['ab'])
1657-
self.assertEqual(rem,'')
1658-
16591641
def test_bug_448951(self):
16601642
# bug 448951 (similar to 429357, but with single char match)
16611643
# (Also test greedy matches.)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Revert changes to the undocumented :class:`!re.Scanner` class. Capturing
2+
groups are still allowed for backward compatibility, although using them can
3+
lead to incorrect result. They will be forbidden in future Python versions.

0 commit comments

Comments
 (0)