Skip to content

Commit 51d918d

Browse files
committed
Address next round of comments
1 parent 4095b52 commit 51d918d

File tree

3 files changed

+38
-50
lines changed

3 files changed

+38
-50
lines changed

Doc/library/re.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,7 @@ You can also destructure match objects with python's ``match`` statement::
15901590
that if *group* did not contribute to the match, this is ``(-1, -1)``.
15911591
*group* defaults to zero, the entire match.
15921592

1593+
15931594
.. method:: Match.index(value, start=0, stop=sys.maxsize, /)
15941595

15951596
Return the index of the first occurrence of the value among the matched groups.
@@ -1598,12 +1599,14 @@ You can also destructure match objects with python's ``match`` statement::
15981599

15991600
.. versionadded:: next
16001601

1602+
16011603
.. method:: Match.count(value, /)
16021604

16031605
Return the number of occurrences of the value among the matched groups.
16041606

16051607
.. versionadded:: next
16061608

1609+
16071610
.. attribute:: Match.pos
16081611

16091612
The value of *pos* which was passed to the :meth:`~Pattern.search` or

Lib/test/test_re.py

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
cpython_only, captured_stdout,
33
check_disallow_instantiation, linked_to_musl,
44
warnings_helper, SHORT_TIMEOUT, Stopwatch, requires_resource)
5+
import itertools
56
import locale
67
import re
78
import string
@@ -610,31 +611,15 @@ def test_match_getitem(self):
610611

611612
def test_match_getitem_slice(self):
612613
m = re.match(r"(a)(b)(c)", "abc")
613-
self.assertEqual(m[:0], ())
614-
self.assertEqual(m[:1], ("abc",))
615-
self.assertEqual(m[:2], ("abc", "a"))
616-
self.assertEqual(m[:3], ("abc", "a", "b"))
617-
self.assertEqual(m[:4], ("abc", "a", "b", "c"))
618-
self.assertEqual(m[0:], ("abc", "a", "b", "c"))
619-
self.assertEqual(m[1:], ("a", "b", "c"))
620-
self.assertEqual(m[2:], ("b", "c"))
621-
self.assertEqual(m[3:], ("c",))
622-
self.assertEqual(m[4:], ())
623-
self.assertEqual(m[:-4], ())
624-
self.assertEqual(m[:-3], ("abc",))
625-
self.assertEqual(m[:-2], ("abc", "a"))
626-
self.assertEqual(m[:-1], ("abc", "a", "b"))
627-
self.assertEqual(m[-4:], ("abc", "a", "b", "c"))
628-
self.assertEqual(m[-3:], ("a", "b", "c"))
629-
self.assertEqual(m[-2:], ("b", "c"))
630-
self.assertEqual(m[-1:], ("c",))
631-
self.assertEqual(m[1:-1], ("a", "b"))
632-
self.assertEqual(m[::-1], ("c", "b", "a", "abc"))
633-
self.assertEqual(m[::4], ("abc",))
634-
self.assertEqual(m[2:2], ())
635-
self.assertEqual(m[3:1], ())
636-
self.assertEqual(m[1:3], ("a", "b"))
637-
self.assertEqual(m[-1::-2], ("c", "a"))
614+
seq = ("abc", "a", "b", "c")
615+
indices = [None, *range(-len(seq), len(seq) + 1)]
616+
for start, end, step in itertools.product(
617+
indices,
618+
indices,
619+
filter(lambda x: x != 0, indices), # slice step cannot be zero
620+
):
621+
with self.subTest(start=start, end=end, step=step):
622+
self.assertEqual(m[start:end:step], seq[start:end:step])
638623

639624
def test_match_sequence(self):
640625
m = re.match(r"(a)(b)(c)", "abc")
@@ -664,52 +649,57 @@ def test_match_sequence(self):
664649
self.assertEqual(v, "123")
665650

666651
def test_match_iter(self):
667-
m = re.match(r"(a)(b)(c)", "abc")
668-
it = iter(m)
652+
it = iter(re.match(r"(a)(b)(c)", "abc"))
669653
self.assertEqual(next(it), "abc")
670654
self.assertEqual(next(it), "a")
671655
self.assertEqual(next(it), "b")
672656
self.assertEqual(next(it), "c")
673-
with self.assertRaises(StopIteration):
674-
next(it)
657+
self.assertRaises(StopIteration, next, it)
675658

676659
def test_match_index(self):
677-
m = re.match(r"(a)(b)(c)", "abc")
678-
self.assertEqual(m.index("abc"), 0)
660+
m = re.match(r"(a)(b)(c)(b)", "abcb")
661+
self.assertEqual(m.index("abcb"), 0)
679662
self.assertEqual(m.index("a"), 1)
680663
self.assertEqual(m.index("b"), 2)
681664
self.assertEqual(m.index("c"), 3)
682665
self.assertRaises(ValueError, m.index, "123")
683666

684667
# With start index.
685-
self.assertRaises(ValueError, m.index, "abc", 1)
686668
self.assertEqual(m.index("a", 1), 1)
687669
self.assertEqual(m.index("b", 1), 2)
688670
self.assertEqual(m.index("c", 1), 3)
671+
self.assertRaises(ValueError, m.index, "abcb", 1)
689672
self.assertRaises(ValueError, m.index, "123", 1)
690673

691-
self.assertRaises(ValueError, m.index, "abc", 2)
692-
self.assertRaises(ValueError, m.index, "a", 2)
693674
self.assertEqual(m.index("b", 2), 2)
694675
self.assertEqual(m.index("c", 2), 3)
676+
self.assertRaises(ValueError, m.index, "abcb", 2)
677+
self.assertRaises(ValueError, m.index, "a", 2)
695678
self.assertRaises(ValueError, m.index, "123", 2)
696679

697-
self.assertRaises(ValueError, m.index, "abc", 3)
698-
self.assertRaises(ValueError, m.index, "a", 3)
699-
self.assertRaises(ValueError, m.index, "b", 3)
680+
self.assertEqual(m.index("b", 3), 4)
700681
self.assertEqual(m.index("c", 3), 3)
682+
self.assertRaises(ValueError, m.index, "abcb", 3)
683+
self.assertRaises(ValueError, m.index, "a", 3)
701684
self.assertRaises(ValueError, m.index, "123", 3)
702685

703-
self.assertRaises(ValueError, m.index, "abc", 4)
686+
self.assertEqual(m.index("b", 4), 4)
687+
self.assertRaises(ValueError, m.index, "abcb", 4)
704688
self.assertRaises(ValueError, m.index, "a", 4)
705-
self.assertRaises(ValueError, m.index, "b", 4)
706689
self.assertRaises(ValueError, m.index, "c", 4)
707690
self.assertRaises(ValueError, m.index, "123", 4)
708691

692+
self.assertRaises(ValueError, m.index, "abcb", 5)
693+
self.assertRaises(ValueError, m.index, "a", 5)
694+
self.assertRaises(ValueError, m.index, "b", 5)
695+
self.assertRaises(ValueError, m.index, "c", 5)
696+
self.assertRaises(ValueError, m.index, "123", 5)
697+
709698
# With start index and stop index.
710-
self.assertRaises(ValueError, m.index, "b", 0, 2)
711699
self.assertEqual(m.index("b", 1, 3), 2)
712700
self.assertEqual(m.index("b", 2, 4), 2)
701+
self.assertEqual(m.index("b", 3, 5), 4)
702+
self.assertRaises(ValueError, m.index, "b", 0, 2)
713703
self.assertRaises(ValueError, m.index, "b", 3, 4)
714704
self.assertRaises(ValueError, m.index, "b", -1, 0)
715705

@@ -749,7 +739,6 @@ def test_match_match_case(self):
749739
case _:
750740
self.fail()
751741

752-
753742
def test_re_fullmatch(self):
754743
# Issue 16203: Proposal: add re.fullmatch() method.
755744
self.assertEqual(re.fullmatch(r"a", "a").span(), (0, 1))

Modules/_sre/sre.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,18 +2442,15 @@ static PyObject*
24422442
match_item(PyObject *op, Py_ssize_t index)
24432443
{
24442444
MatchObject *self = _MatchObject_CAST(op);
2445-
24462445
if (index < 0 || index >= self->groups) {
2447-
/* raise IndexError if we were given a bad group number */
24482446
PyErr_SetString(PyExc_IndexError, "no such group");
24492447
return NULL;
24502448
}
2451-
24522449
return match_getslice_by_index(self, index, Py_None);
24532450
}
24542451

24552452
static PyObject*
2456-
match_getitem(PyObject *op, PyObject* item)
2453+
match_subscript(PyObject *op, PyObject* item)
24572454
{
24582455
MatchObject *self = _MatchObject_CAST(op);
24592456

@@ -2492,7 +2489,7 @@ match_getitem(PyObject *op, PyObject* item)
24922489
PyObject* index = PyDict_GetItemWithError(self->pattern->groupindex, item);
24932490
if (index && PyLong_Check(index)) {
24942491
Py_ssize_t i = PyLong_AsSsize_t(index);
2495-
if (!PyErr_Occurred()) {
2492+
if (i != -1 || !PyErr_Occurred()) {
24962493
return match_item(op, i);
24972494
}
24982495
}
@@ -2698,7 +2695,7 @@ _sre_SRE_Match_index_impl(MatchObject *self, PyObject *value,
26982695
Py_ssize_t start, Py_ssize_t stop)
26992696
/*[clinic end generated code: output=846597f6f96f829c input=7f41b5a99e0ad88e]*/
27002697
{
2701-
PySlice_AdjustIndices(self->groups, &start, &stop, 1);
2698+
(void)PySlice_AdjustIndices(self->groups, &start, &stop, 1);
27022699

27032700
for (Py_ssize_t i = start; i < stop; i++) {
27042701
PyObject* group = match_getslice_by_index(self, i, Py_None);
@@ -2732,9 +2729,8 @@ _sre_SRE_Match_count_impl(MatchObject *self, PyObject *value)
27322729
/*[clinic end generated code: output=c0b81bdce5872620 input=b1f3372cfb4b8c74]*/
27332730
{
27342731
Py_ssize_t count = 0;
2735-
Py_ssize_t i;
27362732

2737-
for (i = 0; i < self->groups; i++) {
2733+
for (Py_ssize_t i = 0; i < self->groups; i++) {
27382734
PyObject* group = match_getslice_by_index(self, i, Py_None);
27392735
if (group == NULL) {
27402736
return NULL;
@@ -3412,7 +3408,7 @@ static PyType_Slot match_slots[] = {
34123408
{Py_sq_item, match_item},
34133409

34143410
// Support group names provided as subscripts
3415-
{Py_mp_subscript, match_getitem},
3411+
{Py_mp_subscript, match_subscript},
34163412

34173413
{0, NULL},
34183414
};

0 commit comments

Comments
 (0)