summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorFredrik Lundh <fredrik@pythonware.com>2000-06-30 07:08:20 (GMT)
committerFredrik Lundh <fredrik@pythonware.com>2000-06-30 07:08:20 (GMT)
commitdf02d0b3f0f438e6a773528010cc360d01b8f393 (patch)
treef4d45a0bbb9f268b6da56e013738d429e71f5054 /Modules
parent47c60ec9a0dfabcccdfdeee9d3077f08423505bd (diff)
downloadcpython-df02d0b3f0f438e6a773528010cc360d01b8f393.zip
cpython-df02d0b3f0f438e6a773528010cc360d01b8f393.tar.gz
cpython-df02d0b3f0f438e6a773528010cc360d01b8f393.tar.bz2
- fixed default value handling in group/groupdict
- added test suite
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_sre.c41
1 files changed, 23 insertions, 18 deletions
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 7b1adbd..6fcd65e 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -1566,7 +1566,7 @@ match_dealloc(MatchObject* self)
}
static PyObject*
-match_getslice_by_index(MatchObject* self, int index)
+match_getslice_by_index(MatchObject* self, int index, PyObject* def)
{
if (index < 0 || index >= self->groups) {
/* raise IndexError if we were given a bad group number */
@@ -1578,9 +1578,9 @@ match_getslice_by_index(MatchObject* self, int index)
}
if (self->string == Py_None || self->mark[index+index] < 0) {
- /* return None if the string or group is undefined */
- Py_INCREF(Py_None);
- return Py_None;
+ /* return default value if the string or group is undefined */
+ Py_INCREF(def);
+ return def;
}
return PySequence_GetSlice(
@@ -1605,9 +1605,9 @@ match_getindex(MatchObject* self, PyObject* index)
}
static PyObject*
-match_getslice(MatchObject* self, PyObject* index)
+match_getslice(MatchObject* self, PyObject* index, PyObject* def)
{
- return match_getslice_by_index(self, match_getindex(self, index));
+ return match_getslice_by_index(self, match_getindex(self, index), def);
}
static PyObject*
@@ -1620,10 +1620,10 @@ match_group(MatchObject* self, PyObject* args)
switch (size) {
case 0:
- result = match_getslice(self, Py_False);
+ result = match_getslice(self, Py_False, Py_None);
break;
case 1:
- result = match_getslice(self, PyTuple_GET_ITEM(args, 0));
+ result = match_getslice(self, PyTuple_GET_ITEM(args, 0), Py_None);
break;
default:
/* fetch multiple items */
@@ -1631,7 +1631,9 @@ match_group(MatchObject* self, PyObject* args)
if (!result)
return NULL;
for (i = 0; i < size; i++) {
- PyObject* item = match_getslice(self, PyTuple_GET_ITEM(args, i));
+ PyObject* item = match_getslice(
+ self, PyTuple_GET_ITEM(args, i), Py_None
+ );
if (!item) {
Py_DECREF(result);
return NULL;
@@ -1649,7 +1651,9 @@ match_groups(MatchObject* self, PyObject* args)
PyObject* result;
int index;
- /* FIXME: <fl> handle default value! */
+ PyObject* def = Py_None;
+ if (!PyArg_ParseTuple(args, "|O", &def))
+ return NULL;
result = PyTuple_New(self->groups-1);
if (!result)
@@ -1657,8 +1661,7 @@ match_groups(MatchObject* self, PyObject* args)
for (index = 1; index < self->groups; index++) {
PyObject* item;
- /* FIXME: <fl> handle default! */
- item = match_getslice_by_index(self, index);
+ item = match_getslice_by_index(self, index, def);
if (!item) {
Py_DECREF(result);
return NULL;
@@ -1676,17 +1679,19 @@ match_groupdict(MatchObject* self, PyObject* args)
PyObject* keys;
int index;
- /* FIXME: <fl> handle default value! */
+ PyObject* def = Py_None;
+ if (!PyArg_ParseTuple(args, "|O", &def))
+ return NULL;
result = PyDict_New();
- if (!result)
- return NULL;
- if (!self->pattern->groupindex)
+ if (!result || !self->pattern->groupindex)
return result;
keys = PyMapping_Keys(self->pattern->groupindex);
- if (!keys)
+ if (!keys) {
+ Py_DECREF(result);
return NULL;
+ }
for (index = 0; index < PyList_GET_SIZE(keys); index++) {
PyObject* key;
@@ -1697,7 +1702,7 @@ match_groupdict(MatchObject* self, PyObject* args)
Py_DECREF(result);
return NULL;
}
- item = match_getslice(self, key);
+ item = match_getslice(self, key, def);
if (!item) {
Py_DECREF(key);
Py_DECREF(keys);