diff options
author | animalize <animalize@users.noreply.github.com> | 2019-02-18 13:26:37 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2019-02-18 13:26:37 (GMT) |
commit | 4a7f44a2ed49ff1e87db062e7177a56c6e4bbdb0 (patch) | |
tree | 6daa91592d1d09f5b8ed34a1121e600a8bb03d74 /Lib/test/test_re.py | |
parent | 02c04f26dfa637db7091b7e16036a980bbf40893 (diff) | |
download | cpython-4a7f44a2ed49ff1e87db062e7177a56c6e4bbdb0.zip cpython-4a7f44a2ed49ff1e87db062e7177a56c6e4bbdb0.tar.gz cpython-4a7f44a2ed49ff1e87db062e7177a56c6e4bbdb0.tar.bz2 |
bpo-34294: re module, fix wrong capturing groups in rare cases. (GH-11546)
Need to reset capturing groups between two SRE(match) callings in loops, this fixes wrong capturing groups in rare cases.
Also add a missing index in re.rst.
Diffstat (limited to 'Lib/test/test_re.py')
-rw-r--r-- | Lib/test/test_re.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index ab1d985..797d85d 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -2067,6 +2067,40 @@ ELSE self.assertEqual(m.group(), b'xyz') self.assertEqual(m2.group(), b'') + def test_bug_34294(self): + # Issue 34294: wrong capturing groups + + # exists since Python 2 + s = "a\tx" + p = r"\b(?=(\t)|(x))x" + self.assertEqual(re.search(p, s).groups(), (None, 'x')) + + # introduced in Python 3.7.0 + s = "ab" + p = r"(?=(.)(.)?)" + self.assertEqual(re.findall(p, s), + [('a', 'b'), ('b', '')]) + self.assertEqual([m.groups() for m in re.finditer(p, s)], + [('a', 'b'), ('b', None)]) + + # test-cases provided by issue34294, introduced in Python 3.7.0 + p = r"(?=<(?P<tag>\w+)/?>(?:(?P<text>.+?)</(?P=tag)>)?)" + s = "<test><foo2/></test>" + self.assertEqual(re.findall(p, s), + [('test', '<foo2/>'), ('foo2', '')]) + self.assertEqual([m.groupdict() for m in re.finditer(p, s)], + [{'tag': 'test', 'text': '<foo2/>'}, + {'tag': 'foo2', 'text': None}]) + s = "<test>Hello</test><foo/>" + self.assertEqual([m.groupdict() for m in re.finditer(p, s)], + [{'tag': 'test', 'text': 'Hello'}, + {'tag': 'foo', 'text': None}]) + s = "<test>Hello</test><foo/><foo/>" + self.assertEqual([m.groupdict() for m in re.finditer(p, s)], + [{'tag': 'test', 'text': 'Hello'}, + {'tag': 'foo', 'text': None}, + {'tag': 'foo', 'text': None}]) + class PatternReprTests(unittest.TestCase): def check(self, pattern, expected): |