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):
return ret; /* should never get here */
}
+/* need to reset capturing groups between two SRE(match) callings in loops */
+#define RESET_CAPTURE_GROUP() \
+ do { state->lastmark = state->lastindex = -1; } while (0)
+
LOCAL(Py_ssize_t)
SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
{
if (status != 0)
return status;
++ptr;
+ RESET_CAPTURE_GROUP();
}
return 0;
}
/* close but no cigar -- try again */
if (++ptr >= end)
return 0;
+ RESET_CAPTURE_GROUP();
}
i = overlap[i];
} while (i != 0);
if (status != 0)
break;
ptr++;
+ RESET_CAPTURE_GROUP();
}
} else {
/* general case */
state->must_advance = 0;
while (status == 0 && ptr < end) {
ptr++;
+ RESET_CAPTURE_GROUP();
TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
state->start = state->ptr = ptr;
status = SRE(match)(state, pattern, 0);