diff -r 1043cc2cb0ff Lib/test/test_re.py --- a/Lib/test/test_re.py Sat Sep 07 15:24:01 2013 +0300 +++ b/Lib/test/test_re.py Fri Sep 13 19:52:26 2013 +0300 @@ -1050,6 +1050,20 @@ self.assertEqual(re.compile(pattern, re.S).findall(b'xyz'), [b'xyz'], msg=pattern) + def test_match_repr(self): + match1 = re.search("test", "testrepr") + match2 = re.search("testrepr", "testtestreprtest") + match3 = re.search("a" * (sre_constants.SRE_MATCH_REPR_SIZE + 1), + "a" * (sre_constants.SRE_MATCH_REPR_SIZE + 2)) + + self.assertEqual(repr(match1), + '') + self.assertEqual(repr(match2), + '') + self.assertEqual(repr(match3), + '') + def test_bug_2537(self): # issue 2537: empty submatches diff -r 1043cc2cb0ff Modules/_sre.c --- a/Modules/_sre.c Sat Sep 07 15:24:01 2013 +0300 +++ b/Modules/_sre.c Fri Sep 13 19:52:26 2013 +0300 @@ -3628,6 +3628,31 @@ return match_regs(self); } +static PyObject * +match_repr(MatchObject *self) +{ + int start = self->mark[0]; + int end = self->mark[1]; + PyObject* seq; + PyObject* defrepr; + + if ((end - start) > SRE_MATCH_REPR_SIZE) { + seq = PySequence_GetSlice( + self->string, start, SRE_MATCH_REPR_SIZE); + defrepr = PyUnicode_FromString("..."); + seq = PySequence_Concat(seq, defrepr); + } else + seq = PySequence_GetSlice( + self->string, start, end); + + return PyUnicode_FromFormat("", + self->groups, + start, + end, + seq); +} + + static PyGetSetDef match_getset[] = { {"lastindex", (getter)match_lastindex_get, (setter)NULL}, {"lastgroup", (getter)match_lastgroup_get, (setter)NULL}, @@ -3656,7 +3681,7 @@ 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_reserved */ - 0, /* tp_repr */ + (reprfunc)match_repr, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ @@ -3957,3 +3982,4 @@ /* vim:ts=4:sw=4:et */ + diff -r 1043cc2cb0ff Modules/sre_constants.h --- a/Modules/sre_constants.h Sat Sep 07 15:24:01 2013 +0300 +++ b/Modules/sre_constants.h Fri Sep 13 19:52:26 2013 +0300 @@ -84,3 +84,4 @@ #define SRE_INFO_PREFIX 1 #define SRE_INFO_LITERAL 2 #define SRE_INFO_CHARSET 4 +#define SRE_MATCH_REPR_SIZE 50 \ No newline at end of file