diff options
author | Eric V. Smith <eric@trueblade.com> | 2016-09-11 12:55:43 (GMT) |
---|---|---|
committer | Eric V. Smith <eric@trueblade.com> | 2016-09-11 12:55:43 (GMT) |
commit | 605bdae0780a1ff8a88b80e6e67f3f355fb2ddb4 (patch) | |
tree | 2bb1f4625ba02d82fec908d179535a2d8147f73a /Modules/_sre.c | |
parent | a3c1728bb6641208a9e253af4ffeda23bc464bbe (diff) | |
download | cpython-605bdae0780a1ff8a88b80e6e67f3f355fb2ddb4.zip cpython-605bdae0780a1ff8a88b80e6e67f3f355fb2ddb4.tar.gz cpython-605bdae0780a1ff8a88b80e6e67f3f355fb2ddb4.tar.bz2 |
Issue 24454: Improve the usability of the re match object named group API
Diffstat (limited to 'Modules/_sre.c')
-rw-r--r-- | Modules/_sre.c | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/Modules/_sre.c b/Modules/_sre.c index afa9099..e4372be 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -2121,6 +2121,12 @@ match_group(MatchObject* self, PyObject* args) return result; } +static PyObject* +match_getitem(MatchObject* self, PyObject* name) +{ + return match_getslice(self, name, Py_None); +} + /*[clinic input] _sre.SRE_Match.groups @@ -2416,6 +2422,9 @@ PyDoc_STRVAR(match_group_doc, Return subgroup(s) of the match by indices or names.\n\ For 0 returns the entire match."); +PyDoc_STRVAR(match_getitem_doc, +"__getitem__(name) <==> group(name).\n"); + static PyObject * match_lastindex_get(MatchObject *self) { @@ -2706,6 +2715,13 @@ static PyTypeObject Pattern_Type = { pattern_getset, /* tp_getset */ }; +/* Match objects do not support length or assignment, but do support + __getitem__. */ +static PyMappingMethods match_as_mapping = { + NULL, + (binaryfunc)match_getitem, + NULL +}; static PyMethodDef match_methods[] = { {"group", (PyCFunction) match_group, METH_VARARGS, match_group_doc}, @@ -2717,6 +2733,7 @@ static PyMethodDef match_methods[] = { _SRE_SRE_MATCH_EXPAND_METHODDEF _SRE_SRE_MATCH___COPY___METHODDEF _SRE_SRE_MATCH___DEEPCOPY___METHODDEF + {"__getitem__", (PyCFunction)match_getitem, METH_O|METH_COEXIST, match_getitem_doc}, {NULL, NULL} }; @@ -2751,7 +2768,7 @@ static PyTypeObject Match_Type = { (reprfunc)match_repr, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ + &match_as_mapping, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ |