summaryrefslogtreecommitdiffstats
path: root/Modules/_sre.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/_sre.c')
-rw-r--r--Modules/_sre.c58
1 files changed, 26 insertions, 32 deletions
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 21c41b5..5cea756 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -1899,15 +1899,7 @@ match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def)
void* ptr;
Py_ssize_t i, j;
- if (index < 0 || index >= self->groups) {
- /* raise IndexError if we were given a bad group number */
- PyErr_SetString(
- PyExc_IndexError,
- "no such group"
- );
- return NULL;
- }
-
+ assert(0 <= index && index < self->groups);
index *= 2;
if (self->string == Py_None || self->mark[index] < 0) {
@@ -1940,17 +1932,25 @@ match_getindex(MatchObject* self, PyObject* index)
return 0;
if (PyIndex_Check(index)) {
- return PyNumber_AsSsize_t(index, NULL);
+ i = PyNumber_AsSsize_t(index, NULL);
}
+ else {
+ i = -1;
- i = -1;
-
- if (self->pattern->groupindex) {
- index = PyDict_GetItem(self->pattern->groupindex, index);
- if (index && PyLong_Check(index)) {
- i = PyLong_AsSsize_t(index);
+ if (self->pattern->groupindex) {
+ index = PyDict_GetItemWithError(self->pattern->groupindex, index);
+ if (index && PyLong_Check(index)) {
+ i = PyLong_AsSsize_t(index);
+ }
}
}
+ if (i < 0 || i >= self->groups) {
+ /* raise IndexError if we were given a bad group number */
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_IndexError, "no such group");
+ }
+ return -1;
+ }
return i;
}
@@ -1958,7 +1958,13 @@ match_getindex(MatchObject* self, PyObject* index)
static PyObject*
match_getslice(MatchObject* self, PyObject* index, PyObject* def)
{
- return match_getslice_by_index(self, match_getindex(self, index), def);
+ Py_ssize_t i = match_getindex(self, index);
+
+ if (i < 0) {
+ return NULL;
+ }
+
+ return match_getslice_by_index(self, i, def);
}
/*[clinic input]
@@ -2114,11 +2120,7 @@ _sre_SRE_Match_start_impl(MatchObject *self, PyObject *group)
{
Py_ssize_t index = match_getindex(self, group);
- if (index < 0 || index >= self->groups) {
- PyErr_SetString(
- PyExc_IndexError,
- "no such group"
- );
+ if (index < 0) {
return -1;
}
@@ -2141,11 +2143,7 @@ _sre_SRE_Match_end_impl(MatchObject *self, PyObject *group)
{
Py_ssize_t index = match_getindex(self, group);
- if (index < 0 || index >= self->groups) {
- PyErr_SetString(
- PyExc_IndexError,
- "no such group"
- );
+ if (index < 0) {
return -1;
}
@@ -2195,11 +2193,7 @@ _sre_SRE_Match_span_impl(MatchObject *self, PyObject *group)
{
Py_ssize_t index = match_getindex(self, group);
- if (index < 0 || index >= self->groups) {
- PyErr_SetString(
- PyExc_IndexError,
- "no such group"
- );
+ if (index < 0) {
return NULL;
}