diff -r 4f7845be9e23 Lib/sre_constants.py --- a/Lib/sre_constants.py Mon Aug 19 23:31:18 2013 +0200 +++ b/Lib/sre_constants.py Tue Aug 20 16:22:07 2013 +0300 @@ -215,6 +215,8 @@ SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix) SRE_INFO_CHARSET = 4 # pattern starts with character from given set +SRE_MATCH_REPR_SIZE = 50 # repr match maximum size + if __name__ == "__main__": def dump(f, d, prefix): items = sorted(d.items(), key=lambda a: a[1]) @@ -255,5 +257,7 @@ f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL) f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET) + f.write("#define SRE_MATCH_REPR_SIZE %d\n" % SRE_MATCH_REPR_SIZE) + f.close() print("done") diff -r 4f7845be9e23 Lib/test/test_re.py --- a/Lib/test/test_re.py Mon Aug 19 23:31:18 2013 +0200 +++ b/Lib/test/test_re.py Tue Aug 20 16:22:07 2013 +0300 @@ -1050,6 +1050,24 @@ 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.assertRegex(repr(match1), + 'groups=1, span=\({} {}\), group0=test' + .format(match1.start(), match1.end())) + self.assertRegex(repr(match2), + 'groups=1, span=\({} {}\), group0=testrepr' + .format(match2.start(), match2.end())) + self.assertRegex(repr(match3), + 'groups=1, span=\({} {}\), group0={}...' + .format(match3.start(), + match3.start() + sre_constants.SRE_MATCH_REPR_SIZE + 1, + "a" * sre_constants.SRE_MATCH_REPR_SIZE)) + def test_bug_2537(self): # issue 2537: empty submatches diff -r 4f7845be9e23 Modules/_sre.c --- a/Modules/_sre.c Mon Aug 19 23:31:18 2013 +0200 +++ b/Modules/_sre.c Tue Aug 20 16:22:07 2013 +0300 @@ -3619,6 +3619,32 @@ 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("%s(groups=%d, span=(%d %d), group0=%U)", + Py_TYPE(self)->tp_name, + self->groups, + start, + end, + seq); +} + + static PyGetSetDef match_getset[] = { {"lastindex", (getter)match_lastindex_get, (setter)NULL}, {"lastgroup", (getter)match_lastgroup_get, (setter)NULL}, @@ -3647,7 +3673,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 */ @@ -3948,3 +3974,4 @@ /* vim:ts=4:sw=4:et */ + diff -r 4f7845be9e23 Modules/sre_constants.h --- a/Modules/sre_constants.h Mon Aug 19 23:31:18 2013 +0200 +++ b/Modules/sre_constants.h Tue Aug 20 16:22:07 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