bpo-35459: Use PyDict_GetItemWithError() instead of PyDict_GetItem(). (GH-11112)

This commit is contained in:
Serhiy Storchaka 2019-02-25 17:59:46 +02:00 committed by GitHub
parent a180b007d9
commit a24107b04c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 538 additions and 242 deletions

View file

@ -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;
}