summaryrefslogtreecommitdiffstats
path: root/Modules/_sre.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-02-25 15:59:46 (GMT)
committerGitHub <noreply@github.com>2019-02-25 15:59:46 (GMT)
commita24107b04c1277e3c1105f98aff5bfa3a98b33a0 (patch)
tree55aa5a700e08e3ba27b0361df2b1043be5c4701a /Modules/_sre.c
parenta180b007d96fe68b32f11dec720fbd0cd5b6758a (diff)
downloadcpython-a24107b04c1277e3c1105f98aff5bfa3a98b33a0.zip
cpython-a24107b04c1277e3c1105f98aff5bfa3a98b33a0.tar.gz
cpython-a24107b04c1277e3c1105f98aff5bfa3a98b33a0.tar.bz2
bpo-35459: Use PyDict_GetItemWithError() instead of PyDict_GetItem(). (GH-11112)
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;
}