@@ -1899,15 +1899,7 @@ match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def)
1899
1899
void * ptr ;
1900
1900
Py_ssize_t i , j ;
1901
1901
1902
- if (index < 0 || index >= self -> groups ) {
1903
- /* raise IndexError if we were given a bad group number */
1904
- PyErr_SetString (
1905
- PyExc_IndexError ,
1906
- "no such group"
1907
- );
1908
- return NULL ;
1909
- }
1910
-
1902
+ assert (0 <= index && index < self -> groups );
1911
1903
index *= 2 ;
1912
1904
1913
1905
if (self -> string == Py_None || self -> mark [index ] < 0 ) {
@@ -1940,25 +1932,39 @@ match_getindex(MatchObject* self, PyObject* index)
1940
1932
return 0 ;
1941
1933
1942
1934
if (PyIndex_Check (index )) {
1943
- return PyNumber_AsSsize_t (index , NULL );
1935
+ i = PyNumber_AsSsize_t (index , NULL );
1944
1936
}
1937
+ else {
1938
+ i = -1 ;
1945
1939
1946
- i = -1 ;
1947
-
1948
- if (self -> pattern -> groupindex ) {
1949
- index = PyDict_GetItem (self -> pattern -> groupindex , index );
1950
- if (index && PyLong_Check (index )) {
1951
- i = PyLong_AsSsize_t (index );
1940
+ if (self -> pattern -> groupindex ) {
1941
+ index = PyDict_GetItemWithError (self -> pattern -> groupindex , index );
1942
+ if (index && PyLong_Check (index )) {
1943
+ i = PyLong_AsSsize_t (index );
1944
+ }
1952
1945
}
1953
1946
}
1947
+ if (i < 0 || i >= self -> groups ) {
1948
+ /* raise IndexError if we were given a bad group number */
1949
+ if (!PyErr_Occurred ()) {
1950
+ PyErr_SetString (PyExc_IndexError , "no such group" );
1951
+ }
1952
+ return -1 ;
1953
+ }
1954
1954
1955
1955
return i ;
1956
1956
}
1957
1957
1958
1958
static PyObject *
1959
1959
match_getslice (MatchObject * self , PyObject * index , PyObject * def )
1960
1960
{
1961
- return match_getslice_by_index (self , match_getindex (self , index ), def );
1961
+ Py_ssize_t i = match_getindex (self , index );
1962
+
1963
+ if (i < 0 ) {
1964
+ return NULL ;
1965
+ }
1966
+
1967
+ return match_getslice_by_index (self , i , def );
1962
1968
}
1963
1969
1964
1970
/*[clinic input]
@@ -2114,11 +2120,7 @@ _sre_SRE_Match_start_impl(MatchObject *self, PyObject *group)
2114
2120
{
2115
2121
Py_ssize_t index = match_getindex (self , group );
2116
2122
2117
- if (index < 0 || index >= self -> groups ) {
2118
- PyErr_SetString (
2119
- PyExc_IndexError ,
2120
- "no such group"
2121
- );
2123
+ if (index < 0 ) {
2122
2124
return -1 ;
2123
2125
}
2124
2126
@@ -2141,11 +2143,7 @@ _sre_SRE_Match_end_impl(MatchObject *self, PyObject *group)
2141
2143
{
2142
2144
Py_ssize_t index = match_getindex (self , group );
2143
2145
2144
- if (index < 0 || index >= self -> groups ) {
2145
- PyErr_SetString (
2146
- PyExc_IndexError ,
2147
- "no such group"
2148
- );
2146
+ if (index < 0 ) {
2149
2147
return -1 ;
2150
2148
}
2151
2149
@@ -2195,11 +2193,7 @@ _sre_SRE_Match_span_impl(MatchObject *self, PyObject *group)
2195
2193
{
2196
2194
Py_ssize_t index = match_getindex (self , group );
2197
2195
2198
- if (index < 0 || index >= self -> groups ) {
2199
- PyErr_SetString (
2200
- PyExc_IndexError ,
2201
- "no such group"
2202
- );
2196
+ if (index < 0 ) {
2203
2197
return NULL ;
2204
2198
}
2205
2199
0 commit comments