bpo-35459: Use PyDict_GetItemWithError() instead of PyDict_GetItem() by serhiy-storchaka · Pull Request #11112 · python/cpython
Expand Up
@@ -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) { Expand Down Expand Up @@ -1940,25 +1932,39 @@ 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; }
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] Expand Down Expand Up @@ -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; }
Expand All @@ -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; }
Expand Down Expand Up @@ -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; }
Expand Down
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) { Expand Down Expand Up @@ -1940,25 +1932,39 @@ 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; }
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] Expand Down Expand Up @@ -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; }
Expand All @@ -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; }
Expand Down Expand Up @@ -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; }
Expand Down