diff -r 038dd5e1f01b Lib/test/test_re.py --- a/Lib/test/test_re.py Sat Feb 04 14:59:11 2017 +0100 +++ b/Lib/test/test_re.py Sat Feb 04 19:30:27 2017 +0300 @@ -1821,6 +1821,13 @@ warnings.simplefilter('error', BytesWarning) self.assertNotEqual(pattern3, pattern1) + def test_bug_29444(self): + pattern = b'A*' + s = bytearray(b'A' * 100) + m = re.search(pattern, s) + del s[:] + self.assertEqual(m.group(), b'') + class PatternReprTests(unittest.TestCase): def check(self, pattern, expected): diff -r 038dd5e1f01b Modules/_sre.c --- a/Modules/_sre.c Sat Feb 04 14:59:11 2017 +0100 +++ b/Modules/_sre.c Sat Feb 04 19:30:27 2017 +0300 @@ -1945,6 +1945,7 @@ Py_buffer view; PyObject *result; void* ptr; + Py_ssize_t i, j; if (index < 0 || index >= self->groups) { /* raise IndexError if we were given a bad group number */ @@ -1966,8 +1967,14 @@ ptr = getstring(self->string, &length, &isbytes, &charsize, &view); if (ptr == NULL) return NULL; + + i = self->mark[index]; + j = self->mark[index+1]; + + PySlice_AdjustIndices(length, &i, &j, 1); + result = getslice(isbytes, ptr, - self->string, self->mark[index], self->mark[index+1]); + self->string, i, j); if (isbytes && view.buf != NULL) PyBuffer_Release(&view); return result;