mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
Issue #17087: Improved the repr for regular expression match objects.
This commit is contained in:
parent
d5fd8df22f
commit
36af10c1f7
3 changed files with 41 additions and 1 deletions
|
|
@ -1104,6 +1104,28 @@ class ReTests(unittest.TestCase):
|
||||||
self.assertEqual(re.compile(pattern, re.S).findall(b'xyz'),
|
self.assertEqual(re.compile(pattern, re.S).findall(b'xyz'),
|
||||||
[b'xyz'], msg=pattern)
|
[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; "
|
||||||
|
"span=(1, 12), match='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; "
|
||||||
|
"span=(1, 12), match=b'abracadabra'>" %
|
||||||
|
(type(m).__module__, type(m).__qualname__))
|
||||||
|
|
||||||
|
first, second = list(re.finditer("(aa)|(bb)", "aa bb"))
|
||||||
|
self.assertEqual(repr(first), "<%s.%s object; "
|
||||||
|
"span=(0, 2), match='aa'>" %
|
||||||
|
(type(second).__module__, type(first).__qualname__))
|
||||||
|
self.assertEqual(repr(second), "<%s.%s object; "
|
||||||
|
"span=(3, 5), match='bb'>" %
|
||||||
|
(type(second).__module__, type(second).__qualname__))
|
||||||
|
|
||||||
|
|
||||||
def test_bug_2537(self):
|
def test_bug_2537(self):
|
||||||
# issue 2537: empty submatches
|
# issue 2537: empty submatches
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,8 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #17087: Improved the repr for regular expression match objects.
|
||||||
|
|
||||||
- Issue #18235: Fix the sysconfig variables LDSHARED and BLDSHARED under AIX.
|
- Issue #18235: Fix the sysconfig variables LDSHARED and BLDSHARED under AIX.
|
||||||
Patch by David Edelsohn.
|
Patch by David Edelsohn.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3637,6 +3637,22 @@ match_regs_get(MatchObject *self)
|
||||||
return match_regs(self);
|
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; span=(%d, %d), match=%.50R>",
|
||||||
|
Py_TYPE(self)->tp_name,
|
||||||
|
self->mark[0], self->mark[1], group0);
|
||||||
|
Py_DECREF(group0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static PyGetSetDef match_getset[] = {
|
static PyGetSetDef match_getset[] = {
|
||||||
{"lastindex", (getter)match_lastindex_get, (setter)NULL},
|
{"lastindex", (getter)match_lastindex_get, (setter)NULL},
|
||||||
{"lastgroup", (getter)match_lastgroup_get, (setter)NULL},
|
{"lastgroup", (getter)match_lastgroup_get, (setter)NULL},
|
||||||
|
|
@ -3665,7 +3681,7 @@ static PyTypeObject Match_Type = {
|
||||||
0, /* tp_getattr */
|
0, /* tp_getattr */
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
0, /* tp_reserved */
|
0, /* tp_reserved */
|
||||||
0, /* tp_repr */
|
(reprfunc)match_repr, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
0, /* tp_as_number */
|
||||||
0, /* tp_as_sequence */
|
0, /* tp_as_sequence */
|
||||||
0, /* tp_as_mapping */
|
0, /* tp_as_mapping */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue