]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] Revert "gh-140797: Forbid capturing groups in re.Scanner lexicon patterns...
authorHugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Fri, 5 Dec 2025 16:45:15 +0000 (18:45 +0200)
committerGitHub <noreply@github.com>
Fri, 5 Dec 2025 16:45:15 +0000 (16:45 +0000)
Lib/re/__init__.py
Lib/test/test_re.py
Misc/NEWS.d/next/Library/2025-12-05-17-58-29.gh-issue-140797.YxB27u.rst [new file with mode: 0644]

index e0d6c844b4fa7c59ed2df75cdbfc84c516c19ee6..af2808a77da69152b32aafb5a5c941d076921bc4 100644 (file)
@@ -399,12 +399,9 @@ class Scanner:
         s = _parser.State()
         s.flags = flags
         for phrase, action in lexicon:
-            sub_pattern = _parser.parse(phrase, flags)
-            if sub_pattern.state.groups != 1:
-                raise ValueError("Cannot use capturing groups in re.Scanner")
             gid = s.opengroup()
             p.append(_parser.SubPattern(s, [
-                (SUBPATTERN, (gid, 0, 0, sub_pattern)),
+                (SUBPATTERN, (gid, 0, 0, _parser.parse(phrase, flags))),
                 ]))
             s.closegroup(gid, p[-1])
         p = _parser.SubPattern(s, [(BRANCH, (None, p))])
index b7ed86849cb1938e5120eca766548e472a550591..f6e797b3785dbeb21c2e3bd94ebffed10f043b54 100644 (file)
@@ -1639,24 +1639,6 @@ class ReTests(unittest.TestCase):
                          (['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5,
                            'op+', 'bar'], ''))
 
-    def test_bug_gh140797(self):
-        # gh140797: Capturing groups are not allowed in re.Scanner
-
-        msg = r"Cannot use capturing groups in re\.Scanner"
-        # Capturing group throws an error
-        with self.assertRaisesRegex(ValueError, msg):
-            Scanner([("(a)b", None)])
-
-        # Named Group
-        with self.assertRaisesRegex(ValueError, msg):
-            Scanner([("(?P<name>a)", None)])
-
-        # Non-capturing groups should pass normally
-        s = Scanner([("(?:a)b", lambda scanner, token: token)])
-        result, rem = s.scan("ab")
-        self.assertEqual(result,['ab'])
-        self.assertEqual(rem,'')
-
     def test_bug_448951(self):
         # bug 448951 (similar to 429357, but with single char match)
         # (Also test greedy matches.)
diff --git a/Misc/NEWS.d/next/Library/2025-12-05-17-58-29.gh-issue-140797.YxB27u.rst b/Misc/NEWS.d/next/Library/2025-12-05-17-58-29.gh-issue-140797.YxB27u.rst
new file mode 100644 (file)
index 0000000..ebbe06f
--- /dev/null
@@ -0,0 +1,3 @@
+Revert changes to the undocumented :class:`!re.Scanner` class. Capturing
+groups are still allowed for backward compatibility, although using them can
+lead to incorrect result. They will be forbidden in future Python versions.