Skip to content

Commit cd85d0b

Browse files
bpo-28765: Use concrete types API in _sre.c. (#1009)
1 parent baf9f29 commit cd85d0b

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

Modules/_sre.c

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,9 @@ deepcopy(PyObject** object, PyObject* memo)
701701
{
702702
PyObject* copy;
703703

704+
if (!*object)
705+
return 1;
706+
704707
copy = call(
705708
"copy", "deepcopy",
706709
PyTuple_Pack(2, *object, memo)
@@ -1368,6 +1371,8 @@ PyDoc_STRVAR(pattern_doc, "Compiled regular expression objects");
13681371
static PyObject *
13691372
pattern_groupindex(PatternObject *self)
13701373
{
1374+
if (self->groupindex == NULL)
1375+
return PyDict_New();
13711376
return PyDictProxy_New(self->groupindex);
13721377
}
13731378

@@ -1448,11 +1453,14 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
14481453

14491454
self->groups = groups;
14501455

1451-
Py_INCREF(groupindex);
1452-
self->groupindex = groupindex;
1453-
1454-
Py_INCREF(indexgroup);
1455-
self->indexgroup = indexgroup;
1456+
if (PyDict_GET_SIZE(groupindex) > 0) {
1457+
Py_INCREF(groupindex);
1458+
self->groupindex = groupindex;
1459+
if (PyTuple_GET_SIZE(indexgroup) > 0) {
1460+
Py_INCREF(indexgroup);
1461+
self->indexgroup = indexgroup;
1462+
}
1463+
}
14561464

14571465
if (!_validate(self)) {
14581466
Py_DECREF(self);
@@ -1994,13 +2002,10 @@ match_getindex(MatchObject* self, PyObject* index)
19942002
i = -1;
19952003

19962004
if (self->pattern->groupindex) {
1997-
index = PyObject_GetItem(self->pattern->groupindex, index);
1998-
if (index) {
1999-
if (PyLong_Check(index))
2000-
i = PyLong_AsSsize_t(index);
2001-
Py_DECREF(index);
2002-
} else
2003-
PyErr_Clear();
2005+
index = PyDict_GetItem(self->pattern->groupindex, index);
2006+
if (index && PyLong_Check(index)) {
2007+
i = PyLong_AsSsize_t(index);
2008+
}
20042009
}
20052010

20062011
return i;
@@ -2118,40 +2123,34 @@ static PyObject *
21182123
_sre_SRE_Match_groupdict_impl(MatchObject *self, PyObject *default_value)
21192124
/*[clinic end generated code: output=29917c9073e41757 input=0ded7960b23780aa]*/
21202125
{
2121-
PyObject* result;
2122-
PyObject* keys;
2123-
Py_ssize_t index;
2126+
PyObject *result;
2127+
PyObject *key;
2128+
PyObject *value;
2129+
Py_ssize_t pos = 0;
2130+
Py_hash_t hash;
21242131

21252132
result = PyDict_New();
21262133
if (!result || !self->pattern->groupindex)
21272134
return result;
21282135

2129-
keys = PyMapping_Keys(self->pattern->groupindex);
2130-
if (!keys)
2131-
goto failed;
2132-
2133-
for (index = 0; index < PyList_GET_SIZE(keys); index++) {
2136+
while (_PyDict_Next(self->pattern->groupindex, &pos, &key, &value, &hash)) {
21342137
int status;
2135-
PyObject* key;
2136-
PyObject* value;
2137-
key = PyList_GET_ITEM(keys, index);
2138-
if (!key)
2139-
goto failed;
2138+
Py_INCREF(key);
21402139
value = match_getslice(self, key, default_value);
2141-
if (!value)
2140+
if (!value) {
2141+
Py_DECREF(key);
21422142
goto failed;
2143-
status = PyDict_SetItem(result, key, value);
2143+
}
2144+
status = _PyDict_SetItem_KnownHash(result, key, value, hash);
21442145
Py_DECREF(value);
2146+
Py_DECREF(key);
21452147
if (status < 0)
21462148
goto failed;
21472149
}
21482150

2149-
Py_DECREF(keys);
2150-
21512151
return result;
21522152

21532153
failed:
2154-
Py_XDECREF(keys);
21552154
Py_DECREF(result);
21562155
return NULL;
21572156
}
@@ -2378,13 +2377,14 @@ match_lastindex_get(MatchObject *self)
23782377
static PyObject *
23792378
match_lastgroup_get(MatchObject *self)
23802379
{
2381-
if (self->pattern->indexgroup && self->lastindex >= 0) {
2382-
PyObject* result = PySequence_GetItem(
2383-
self->pattern->indexgroup, self->lastindex
2384-
);
2385-
if (result)
2386-
return result;
2387-
PyErr_Clear();
2380+
if (self->pattern->indexgroup &&
2381+
self->lastindex >= 0 &&
2382+
self->lastindex < PyTuple_GET_SIZE(self->pattern->indexgroup))
2383+
{
2384+
PyObject *result = PyTuple_GET_ITEM(self->pattern->indexgroup,
2385+
self->lastindex);
2386+
Py_INCREF(result);
2387+
return result;
23882388
}
23892389
Py_RETURN_NONE;
23902390
}

0 commit comments

Comments
 (0)