diff -r e046ae3e9628 Lib/test/test_re.py --- a/Lib/test/test_re.py Thu Oct 17 12:48:32 2013 +0300 +++ b/Lib/test/test_re.py Thu Oct 17 22:51:49 2013 +0300 @@ -1104,6 +1104,19 @@ self.assertEqual(re.compile(pattern, re.S).findall(b'xyz'), [b'xyz'], msg=pattern) + def test_match_repr(self): + for string in '[abracadabra]', S('[abracadabra]'): + m = re.search(r'(.+)(.*?)\1', string) + self.assertEqual(repr(m), "<%s.%s object: " + "groups=2, span=(1, 12), group0='abracadabra'>" % + (type(m).__module__, type(m).__qualname__)) + for string in (b'[abracadabra]', B(b'[abracadabra]'), + bytearray(b'[abracadabra]'), + memoryview(b'[abracadabra]')): + m = re.search(rb'(.+)(.*?)\1', string) + self.assertEqual(repr(m), "<%s.%s object: " + "groups=2, span=(1, 12), group0=b'abracadabra'>" % + (type(m).__module__, type(m).__qualname__)) def test_bug_2537(self): # issue 2537: empty submatches diff -r e046ae3e9628 Modules/_sre.c --- a/Modules/_sre.c Thu Oct 17 12:48:32 2013 +0300 +++ b/Modules/_sre.c Thu Oct 17 22:51:49 2013 +0300 @@ -3636,6 +3636,22 @@ return match_regs(self); } +static PyObject * +match_repr(MatchObject *self) +{ + PyObject *result; + PyObject *group0 = match_getslice_by_index(self, 0, Py_None); + if (group0 == NULL) + return NULL; + result = PyUnicode_FromFormat( + "<%s object: groups=%d, span=(%d, %d), group0=%.50R>", + Py_TYPE(self)->tp_name, + self->groups - 1, self->mark[0], self->mark[1], group0); + Py_DECREF(group0); + return result; +} + + static PyGetSetDef match_getset[] = { {"lastindex", (getter)match_lastindex_get, (setter)NULL}, {"lastgroup", (getter)match_lastgroup_get, (setter)NULL}, @@ -3664,7 +3680,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 */ @@ -3965,3 +3981,4 @@ /* vim:ts=4:sw=4:et */ +