Commit a5c6e8f3 authored by Fredrik Lundh's avatar Fredrik Lundh

- fixed default value handling in group/groupdict

- added test suite
parent 336c87c0
...@@ -20,7 +20,7 @@ M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE ...@@ -20,7 +20,7 @@ M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE
S = DOTALL = sre_compile.SRE_FLAG_DOTALL S = DOTALL = sre_compile.SRE_FLAG_DOTALL
X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE
# sre extensions (may or may not be in 1.6 final) # sre extensions (may or may not be in 2.0 final)
T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE
U = UNICODE = sre_compile.SRE_FLAG_UNICODE U = UNICODE = sre_compile.SRE_FLAG_UNICODE
......
test_sre
test_support -- test failed re module pickle
test_support -- test failed re module cPickle
=== Syntax error: ('(?P<foo_123>a)(?P=foo_123)', 'aa', 0, 'g1', 'a')
=== Failed incorrectly ('^(.+)?B', 'AB', 0, 'g1', 'A')
=== Failed incorrectly ('(a+)+\\1', 'aa', 0, 'found+"-"+g1', 'aa-a')
=== grouping error ('([^/]*/)*sub1/', 'd:msgs/tdir/sub1/trial/away.cpp', 0, 'found+"-"+g1', 'd:msgs/tdir/sub1/-tdir/') 'd:msgs/tdir/sub1/-trial/' should be 'd:msgs/tdir/sub1/-tdir/'
=== Syntax error: ('(?P<id>aa)(?P=id)', 'aaaa', 0, 'found+"-"+id', 'aaaa-aa')
=== grouping error ('([abc])*bcd', 'abcd', 0, 'found+"-"+g1', 'abcd-a') 'abcd-c' should be 'abcd-a'
=== grouping error ('(?i)([abc])*bcd', 'ABCD', 0, 'found+"-"+g1', 'ABCD-A') 'ABCD-C' should be 'ABCD-A'
=== Syntax error: ('a(?!b).', 'abad', 0, 'found', 'ad')
=== Syntax error: ('a(?=d).', 'abad', 0, 'found', 'ad')
=== Syntax error: ('a(?=c|d).', 'abad', 0, 'found', 'ad')
=== Failed incorrectly ('^(.+)?B', 'AB', 0, 'g1', 'A')
This diff is collapsed.
...@@ -1566,7 +1566,7 @@ match_dealloc(MatchObject* self) ...@@ -1566,7 +1566,7 @@ match_dealloc(MatchObject* self)
} }
static PyObject* 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) { if (index < 0 || index >= self->groups) {
/* raise IndexError if we were given a bad group number */ /* raise IndexError if we were given a bad group number */
...@@ -1578,9 +1578,9 @@ match_getslice_by_index(MatchObject* self, int index) ...@@ -1578,9 +1578,9 @@ match_getslice_by_index(MatchObject* self, int index)
} }
if (self->string == Py_None || self->mark[index+index] < 0) { if (self->string == Py_None || self->mark[index+index] < 0) {
/* return None if the string or group is undefined */ /* return default value if the string or group is undefined */
Py_INCREF(Py_None); Py_INCREF(def);
return Py_None; return def;
} }
return PySequence_GetSlice( return PySequence_GetSlice(
...@@ -1605,9 +1605,9 @@ match_getindex(MatchObject* self, PyObject* index) ...@@ -1605,9 +1605,9 @@ match_getindex(MatchObject* self, PyObject* index)
} }
static PyObject* 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* static PyObject*
...@@ -1620,10 +1620,10 @@ match_group(MatchObject* self, PyObject* args) ...@@ -1620,10 +1620,10 @@ match_group(MatchObject* self, PyObject* args)
switch (size) { switch (size) {
case 0: case 0:
result = match_getslice(self, Py_False); result = match_getslice(self, Py_False, Py_None);
break; break;
case 1: case 1:
result = match_getslice(self, PyTuple_GET_ITEM(args, 0)); result = match_getslice(self, PyTuple_GET_ITEM(args, 0), Py_None);
break; break;
default: default:
/* fetch multiple items */ /* fetch multiple items */
...@@ -1631,7 +1631,9 @@ match_group(MatchObject* self, PyObject* args) ...@@ -1631,7 +1631,9 @@ match_group(MatchObject* self, PyObject* args)
if (!result) if (!result)
return NULL; return NULL;
for (i = 0; i < size; i++) { 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) { if (!item) {
Py_DECREF(result); Py_DECREF(result);
return NULL; return NULL;
...@@ -1649,7 +1651,9 @@ match_groups(MatchObject* self, PyObject* args) ...@@ -1649,7 +1651,9 @@ match_groups(MatchObject* self, PyObject* args)
PyObject* result; PyObject* result;
int index; 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); result = PyTuple_New(self->groups-1);
if (!result) if (!result)
...@@ -1657,8 +1661,7 @@ match_groups(MatchObject* self, PyObject* args) ...@@ -1657,8 +1661,7 @@ match_groups(MatchObject* self, PyObject* args)
for (index = 1; index < self->groups; index++) { for (index = 1; index < self->groups; index++) {
PyObject* item; PyObject* item;
/* FIXME: <fl> handle default! */ item = match_getslice_by_index(self, index, def);
item = match_getslice_by_index(self, index);
if (!item) { if (!item) {
Py_DECREF(result); Py_DECREF(result);
return NULL; return NULL;
...@@ -1676,17 +1679,19 @@ match_groupdict(MatchObject* self, PyObject* args) ...@@ -1676,17 +1679,19 @@ match_groupdict(MatchObject* self, PyObject* args)
PyObject* keys; PyObject* keys;
int index; int index;
/* FIXME: <fl> handle default value! */ PyObject* def = Py_None;
if (!PyArg_ParseTuple(args, "|O", &def))
return NULL;
result = PyDict_New(); result = PyDict_New();
if (!result) if (!result || !self->pattern->groupindex)
return NULL;
if (!self->pattern->groupindex)
return result; return result;
keys = PyMapping_Keys(self->pattern->groupindex); keys = PyMapping_Keys(self->pattern->groupindex);
if (!keys) if (!keys) {
Py_DECREF(result);
return NULL; return NULL;
}
for (index = 0; index < PyList_GET_SIZE(keys); index++) { for (index = 0; index < PyList_GET_SIZE(keys); index++) {
PyObject* key; PyObject* key;
...@@ -1697,7 +1702,7 @@ match_groupdict(MatchObject* self, PyObject* args) ...@@ -1697,7 +1702,7 @@ match_groupdict(MatchObject* self, PyObject* args)
Py_DECREF(result); Py_DECREF(result);
return NULL; return NULL;
} }
item = match_getslice(self, key); item = match_getslice(self, key, def);
if (!item) { if (!item) {
Py_DECREF(key); Py_DECREF(key);
Py_DECREF(keys); Py_DECREF(keys);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment