diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-05-03 12:54:23 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-05-03 12:54:23 (GMT) |
commit | a860aeaf983d7df14534cd09947b4b721da8f525 (patch) | |
tree | 69a35b8619d2fe51396c86835dbcf4af4448ca21 /Modules/_sre.c | |
parent | 5abdf48430138b46d850a226a4a6b6ee3a651090 (diff) | |
download | cpython-a860aeaf983d7df14534cd09947b4b721da8f525.zip cpython-a860aeaf983d7df14534cd09947b4b721da8f525.tar.gz cpython-a860aeaf983d7df14534cd09947b4b721da8f525.tar.bz2 |
Issue #20148: Converted the _sre module to Argument Clinic.
Diffstat (limited to 'Modules/_sre.c')
-rw-r--r-- | Modules/_sre.c | 702 |
1 files changed, 391 insertions, 311 deletions
diff --git a/Modules/_sre.c b/Modules/_sre.c index 8cad1ac..e3245a0 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -258,25 +258,46 @@ data_stack_grow(SRE_STATE* state, Py_ssize_t size) /* see sre.h for object declarations */ static PyObject*pattern_new_match(PatternObject*, SRE_STATE*, Py_ssize_t); -static PyObject*pattern_scanner(PatternObject*, PyObject*, PyObject* kw); +static PyObject *pattern_scanner(PatternObject *, PyObject *, Py_ssize_t, Py_ssize_t); -static PyObject * -sre_codesize(PyObject* self, PyObject *unused) + +/*[clinic input] +module _sre +class _sre.SRE_Pattern "PatternObject *" "&Pattern_Type" +class _sre.SRE_Match "MatchObject *" "&Match_Type" +class _sre.SRE_Scanner "ScannerObject *" "&Scanner_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0230ec19a0deac8]*/ + +/*[clinic input] +_sre.getcodesize -> int +[clinic start generated code]*/ + +static int +_sre_getcodesize_impl(PyModuleDef *module) +/*[clinic end generated code: output=794f1f98ef4883e5 input=bd6f6ecf4916bb2b]*/ { - return PyLong_FromSize_t(sizeof(SRE_CODE)); + return sizeof(SRE_CODE); } -static PyObject * -sre_getlower(PyObject* self, PyObject* args) +/*[clinic input] +_sre.getlower -> int + + character: int + flags: int + / + +[clinic start generated code]*/ + +static int +_sre_getlower_impl(PyModuleDef *module, int character, int flags) +/*[clinic end generated code: output=5fc3616ae2a4c306 input=087d2f1c44bbca6f]*/ { - int character, flags; - if (!PyArg_ParseTuple(args, "ii", &character, &flags)) - return NULL; if (flags & SRE_FLAG_LOCALE) - return Py_BuildValue("i", sre_lower_locale(character)); + return sre_lower_locale(character); if (flags & SRE_FLAG_UNICODE) - return Py_BuildValue("i", sre_lower_unicode(character)); - return Py_BuildValue("i", sre_lower(character)); + return sre_lower_unicode(character); + return sre_lower(character); } LOCAL(void) @@ -552,27 +573,32 @@ fix_string_param(PyObject *string, PyObject *string2, const char *oldname) return string; } +/*[clinic input] +_sre.SRE_Pattern.match + + string: object = NULL + pos: Py_ssize_t = 0 + endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize + * + pattern: object = NULL + +Matches zero or more characters at the beginning of the string. +[clinic start generated code]*/ + static PyObject * -pattern_match(PatternObject *self, PyObject *args, PyObject *kwargs) +_sre_SRE_Pattern_match_impl(PatternObject *self, PyObject *string, + Py_ssize_t pos, Py_ssize_t endpos, + PyObject *pattern) +/*[clinic end generated code: output=74b4b1da3bb2d84e input=3d079aa99979b81d]*/ { - static char *_keywords[] = {"string", "pos", "endpos", "pattern", NULL}; - PyObject *string = NULL; - Py_ssize_t pos = 0; - Py_ssize_t endpos = PY_SSIZE_T_MAX; - PyObject *pattern = NULL; SRE_STATE state; Py_ssize_t status; PyObject *match; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, - "|Onn$O:match", _keywords, - &string, &pos, &endpos, &pattern)) - return NULL; string = fix_string_param(string, pattern, "pattern"); if (!string) return NULL; - string = state_init(&state, (PatternObject *)self, string, pos, endpos); - if (!string) + if (!state_init(&state, (PatternObject *)self, string, pos, endpos)) return NULL; state.ptr = state.start; @@ -592,27 +618,33 @@ pattern_match(PatternObject *self, PyObject *args, PyObject *kwargs) return match; } -static PyObject* -pattern_fullmatch(PatternObject* self, PyObject* args, PyObject* kw) +/*[clinic input] +_sre.SRE_Pattern.fullmatch + + string: object = NULL + pos: Py_ssize_t = 0 + endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize + * + pattern: object = NULL + +Matches against all of the string +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Pattern_fullmatch_impl(PatternObject *self, PyObject *string, + Py_ssize_t pos, Py_ssize_t endpos, + PyObject *pattern) +/*[clinic end generated code: output=1c98bc5da744ea94 input=d4228606cc12580f]*/ { SRE_STATE state; Py_ssize_t status; PyObject *match; - PyObject *string = NULL, *string2 = NULL; - Py_ssize_t start = 0; - Py_ssize_t end = PY_SSIZE_T_MAX; - static char* kwlist[] = { "string", "pos", "endpos", "pattern", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:fullmatch", kwlist, - &string, &start, &end, &string2)) - return NULL; - - string = fix_string_param(string, string2, "pattern"); + string = fix_string_param(string, pattern, "pattern"); if (!string) return NULL; - string = state_init(&state, self, string, start, end); - if (!string) + if (!state_init(&state, self, string, pos, endpos)) return NULL; state.ptr = state.start; @@ -632,27 +664,35 @@ pattern_fullmatch(PatternObject* self, PyObject* args, PyObject* kw) return match; } -static PyObject* -pattern_search(PatternObject* self, PyObject* args, PyObject* kw) +/*[clinic input] +_sre.SRE_Pattern.search + + string: object = NULL + pos: Py_ssize_t = 0 + endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize + * + pattern: object = NULL + +Scan through string looking for a match, and return a corresponding match object instance. + +Return None if no position in the string matches. +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Pattern_search_impl(PatternObject *self, PyObject *string, + Py_ssize_t pos, Py_ssize_t endpos, + PyObject *pattern) +/*[clinic end generated code: output=3839394a18e5ea4f input=dab42720f4be3a4b]*/ { SRE_STATE state; Py_ssize_t status; PyObject *match; - PyObject *string = NULL, *string2 = NULL; - Py_ssize_t start = 0; - Py_ssize_t end = PY_SSIZE_T_MAX; - static char* kwlist[] = { "string", "pos", "endpos", "pattern", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:search", kwlist, - &string, &start, &end, &string2)) - return NULL; - - string = fix_string_param(string, string2, "pattern"); + string = fix_string_param(string, pattern, "pattern"); if (!string) return NULL; - string = state_init(&state, self, string, start, end); - if (!string) + if (!state_init(&state, self, string, pos, endpos)) return NULL; TRACE(("|%p|%p|SEARCH\n", PatternObject_GetCode(self), state.ptr)); @@ -718,28 +758,34 @@ deepcopy(PyObject** object, PyObject* memo) } #endif -static PyObject* -pattern_findall(PatternObject* self, PyObject* args, PyObject* kw) +/*[clinic input] +_sre.SRE_Pattern.findall + + string: object = NULL + pos: Py_ssize_t = 0 + endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize + * + source: object = NULL + +Return a list of all non-overlapping matches of pattern in string. +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Pattern_findall_impl(PatternObject *self, PyObject *string, + Py_ssize_t pos, Py_ssize_t endpos, + PyObject *source) +/*[clinic end generated code: output=51295498b300639d input=df688355c056b9de]*/ { SRE_STATE state; PyObject* list; Py_ssize_t status; Py_ssize_t i, b, e; - PyObject *string = NULL, *string2 = NULL; - Py_ssize_t start = 0; - Py_ssize_t end = PY_SSIZE_T_MAX; - static char* kwlist[] = { "string", "pos", "endpos", "source", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:findall", kwlist, - &string, &start, &end, &string2)) - return NULL; - - string = fix_string_param(string, string2, "source"); + string = fix_string_param(string, source, "source"); if (!string) return NULL; - string = state_init(&state, self, string, start, end); - if (!string) + if (!state_init(&state, self, string, pos, endpos)) return NULL; list = PyList_New(0); @@ -819,14 +865,28 @@ error: } -static PyObject* -pattern_finditer(PatternObject* pattern, PyObject* args, PyObject* kw) +/*[clinic input] +_sre.SRE_Pattern.finditer + + string: object + pos: Py_ssize_t = 0 + endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize + +Return an iterator over all non-overlapping matches for the RE pattern in string. + +For each match, the iterator returns a match object. +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Pattern_finditer_impl(PatternObject *self, PyObject *string, + Py_ssize_t pos, Py_ssize_t endpos) +/*[clinic end generated code: output=0bbb1a0aeb38bb14 input=612aab69e9fe08e4]*/ { PyObject* scanner; PyObject* search; PyObject* iterator; - scanner = pattern_scanner(pattern, args, kw); + scanner = pattern_scanner(self, string, pos, endpos); if (!scanner) return NULL; @@ -841,8 +901,38 @@ pattern_finditer(PatternObject* pattern, PyObject* args, PyObject* kw) return iterator; } -static PyObject* -pattern_split(PatternObject* self, PyObject* args, PyObject* kw) +/*[clinic input] +_sre.SRE_Pattern.scanner + + string: object + pos: Py_ssize_t = 0 + endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize + +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Pattern_scanner_impl(PatternObject *self, PyObject *string, + Py_ssize_t pos, Py_ssize_t endpos) +/*[clinic end generated code: output=54ea548aed33890b input=3aacdbde77a3a637]*/ +{ + return pattern_scanner(self, string, pos, endpos); +} + +/*[clinic input] +_sre.SRE_Pattern.split + + string: object = NULL + maxsplit: Py_ssize_t = 0 + * + source: object = NULL + +Split string by the occurrences of pattern. +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Pattern_split_impl(PatternObject *self, PyObject *string, + Py_ssize_t maxsplit, PyObject *source) +/*[clinic end generated code: output=20bac2ff55b9f84c input=41e0b2e35e599d7b]*/ { SRE_STATE state; PyObject* list; @@ -852,14 +942,7 @@ pattern_split(PatternObject* self, PyObject* args, PyObject* kw) Py_ssize_t i; void* last; - PyObject *string = NULL, *string2 = NULL; - Py_ssize_t maxsplit = 0; - static char* kwlist[] = { "string", "maxsplit", "source", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kw, "|On$O:split", kwlist, - &string, &maxsplit, &string2)) - return NULL; - - string = fix_string_param(string, string2, "source"); + string = fix_string_param(string, source, "source"); if (!string) return NULL; @@ -876,8 +959,7 @@ pattern_split(PatternObject* self, PyObject* args, PyObject* kw) return NULL; } - string = state_init(&state, self, string, 0, PY_SSIZE_T_MAX); - if (!string) + if (!state_init(&state, self, string, 0, PY_SSIZE_T_MAX)) return NULL; list = PyList_New(0); @@ -1021,8 +1103,7 @@ pattern_subx(PatternObject* self, PyObject* ptemplate, PyObject* string, } } - string = state_init(&state, self, string, 0, PY_SSIZE_T_MAX); - if (!string) { + if (!state_init(&state, self, string, 0, PY_SSIZE_T_MAX)) { Py_DECREF(filter); return NULL; } @@ -1162,36 +1243,50 @@ error: } -static PyObject* -pattern_sub(PatternObject* self, PyObject* args, PyObject* kw) -{ - PyObject* ptemplate; - PyObject* string; - Py_ssize_t count = 0; - static char* kwlist[] = { "repl", "string", "count", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|n:sub", kwlist, - &ptemplate, &string, &count)) - return NULL; +/*[clinic input] +_sre.SRE_Pattern.sub + + repl: object + string: object + count: Py_ssize_t = 0 + +Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. +[clinic start generated code]*/ - return pattern_subx(self, ptemplate, string, count, 0); +static PyObject * +_sre_SRE_Pattern_sub_impl(PatternObject *self, PyObject *repl, + PyObject *string, Py_ssize_t count) +/*[clinic end generated code: output=1dbf2ec3479cba00 input=c53d70be0b3caf86]*/ +{ + return pattern_subx(self, repl, string, count, 0); } -static PyObject* -pattern_subn(PatternObject* self, PyObject* args, PyObject* kw) -{ - PyObject* ptemplate; - PyObject* string; - Py_ssize_t count = 0; - static char* kwlist[] = { "repl", "string", "count", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|n:subn", kwlist, - &ptemplate, &string, &count)) - return NULL; +/*[clinic input] +_sre.SRE_Pattern.subn + + repl: object + string: object + count: Py_ssize_t = 0 + +Return the tuple (new_string, number_of_subs_made) found by replacing the leftmost non-overlapping occurrences of pattern with the replacement repl. +[clinic start generated code]*/ - return pattern_subx(self, ptemplate, string, count, 1); +static PyObject * +_sre_SRE_Pattern_subn_impl(PatternObject *self, PyObject *repl, + PyObject *string, Py_ssize_t count) +/*[clinic end generated code: output=0d9522cd529e9728 input=e7342d7ce6083577]*/ +{ + return pattern_subx(self, repl, string, count, 1); } -static PyObject* -pattern_copy(PatternObject* self, PyObject *unused) +/*[clinic input] +_sre.SRE_Pattern.__copy__ + +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Pattern___copy___impl(PatternObject *self) +/*[clinic end generated code: output=85dedc2db1bd8694 input=a730a59d863bc9f5]*/ { #ifdef USE_BUILTIN_COPY PatternObject* copy; @@ -1218,8 +1313,16 @@ pattern_copy(PatternObject* self, PyObject *unused) #endif } -static PyObject* -pattern_deepcopy(PatternObject* self, PyObject* memo) +/*[clinic input] +_sre.SRE_Pattern.__deepcopy__ + + memo: object + +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Pattern___deepcopy___impl(PatternObject *self, PyObject *memo) +/*[clinic end generated code: output=75efe69bd12c5d7d input=3959719482c07f70]*/ { #ifdef USE_BUILTIN_COPY PatternObject* copy; @@ -1321,68 +1424,9 @@ done: return result; } -PyDoc_STRVAR(pattern_match_doc, -"match(string[, pos[, endpos]]) -> match object or None.\n\ - Matches zero or more characters at the beginning of the string"); - -PyDoc_STRVAR(pattern_fullmatch_doc, -"fullmatch(string[, pos[, endpos]]) -> match object or None.\n\ - Matches against all of the string"); - -PyDoc_STRVAR(pattern_search_doc, -"search(string[, pos[, endpos]]) -> match object or None.\n\ - Scan through string looking for a match, and return a corresponding\n\ - match object instance. Return None if no position in the string matches."); - -PyDoc_STRVAR(pattern_split_doc, -"split(string[, maxsplit = 0]) -> list.\n\ - Split string by the occurrences of pattern."); - -PyDoc_STRVAR(pattern_findall_doc, -"findall(string[, pos[, endpos]]) -> list.\n\ - Return a list of all non-overlapping matches of pattern in string."); - -PyDoc_STRVAR(pattern_finditer_doc, -"finditer(string[, pos[, endpos]]) -> iterator.\n\ - Return an iterator over all non-overlapping matches for the \n\ - RE pattern in string. For each match, the iterator returns a\n\ - match object."); - -PyDoc_STRVAR(pattern_sub_doc, -"sub(repl, string[, count = 0]) -> newstring.\n\ - Return the string obtained by replacing the leftmost non-overlapping\n\ - occurrences of pattern in string by the replacement repl."); - -PyDoc_STRVAR(pattern_subn_doc, -"subn(repl, string[, count = 0]) -> (newstring, number of subs)\n\ - Return the tuple (new_string, number_of_subs_made) found by replacing\n\ - the leftmost non-overlapping occurrences of pattern with the\n\ - replacement repl."); - PyDoc_STRVAR(pattern_doc, "Compiled regular expression objects"); -static PyMethodDef pattern_methods[] = { - {"match", (PyCFunction) pattern_match, METH_VARARGS|METH_KEYWORDS, - pattern_match_doc}, - {"fullmatch", (PyCFunction) pattern_fullmatch, METH_VARARGS|METH_KEYWORDS, - pattern_fullmatch_doc}, - {"search", (PyCFunction) pattern_search, METH_VARARGS|METH_KEYWORDS, - pattern_search_doc}, - {"sub", (PyCFunction) pattern_sub, METH_VARARGS|METH_KEYWORDS, - pattern_sub_doc}, - {"subn", (PyCFunction) pattern_subn, METH_VARARGS|METH_KEYWORDS, - pattern_subn_doc}, - {"split", (PyCFunction) pattern_split, METH_VARARGS|METH_KEYWORDS, - pattern_split_doc}, - {"findall", (PyCFunction) pattern_findall, METH_VARARGS|METH_KEYWORDS, - pattern_findall_doc}, - {"finditer", (PyCFunction) pattern_finditer, METH_VARARGS|METH_KEYWORDS, - pattern_finditer_doc}, - {"scanner", (PyCFunction) pattern_scanner, METH_VARARGS|METH_KEYWORDS}, - {"__copy__", (PyCFunction) pattern_copy, METH_NOARGS}, - {"__deepcopy__", (PyCFunction) pattern_deepcopy, METH_O}, - {NULL, NULL} -}; +static PyMethodDef pattern_methods[]; /* PatternObject's 'groupindex' method. */ static PyObject * @@ -1439,26 +1483,29 @@ static PyTypeObject Pattern_Type = { static int _validate(PatternObject *self); /* Forward */ +/*[clinic input] +_sre.compile + + pattern: object + flags: int + code: object(subclass_of='&PyList_Type') + groups: Py_ssize_t + groupindex: object + indexgroup: object + +[clinic start generated code]*/ + static PyObject * -_compile(PyObject* self_, PyObject* args) +_sre_compile_impl(PyModuleDef *module, PyObject *pattern, int flags, + PyObject *code, Py_ssize_t groups, PyObject *groupindex, + PyObject *indexgroup) +/*[clinic end generated code: output=3004b293730bf309 input=7d059ec8ae1edb85]*/ { /* "compile" pattern descriptor to pattern object */ PatternObject* self; Py_ssize_t i, n; - PyObject* pattern; - int flags = 0; - PyObject* code; - Py_ssize_t groups = 0; - PyObject* groupindex = NULL; - PyObject* indexgroup = NULL; - - if (!PyArg_ParseTuple(args, "OiO!nOO", &pattern, &flags, - &PyList_Type, &code, &groups, - &groupindex, &indexgroup)) - return NULL; - n = PyList_GET_SIZE(code); /* coverity[ampersand_in_size] */ self = PyObject_NEW_VAR(PatternObject, &Pattern_Type, n); @@ -2071,13 +2118,22 @@ match_getslice(MatchObject* self, PyObject* index, PyObject* def) return match_getslice_by_index(self, match_getindex(self, index), def); } -static PyObject* -match_expand(MatchObject* self, PyObject* ptemplate) +/*[clinic input] +_sre.SRE_Match.expand + + template: object + +Return the string obtained by doing backslash substitution on the string template, as done by the sub() method. +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Match_expand_impl(MatchObject *self, PyObject *template) +/*[clinic end generated code: output=931b58ccc323c3a1 input=4bfdb22c2f8b146a]*/ { /* delegate to Python code */ return call( SRE_PY_MODULE, "_expand", - PyTuple_Pack(3, self->pattern, self, ptemplate) + PyTuple_Pack(3, self->pattern, self, template) ); } @@ -2116,24 +2172,29 @@ match_group(MatchObject* self, PyObject* args) return result; } -static PyObject* -match_groups(MatchObject* self, PyObject* args, PyObject* kw) +/*[clinic input] +_sre.SRE_Match.groups + + default: object = None + Is used for groups that did not participate in the match. + +Return a tuple containing all the subgroups of the match, from 1. +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Match_groups_impl(MatchObject *self, PyObject *default_value) +/*[clinic end generated code: output=daf8e2641537238a input=bb069ef55dabca91]*/ { PyObject* result; Py_ssize_t index; - PyObject* def = Py_None; - static char* kwlist[] = { "default", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groups", kwlist, &def)) - return NULL; - result = PyTuple_New(self->groups-1); if (!result) return NULL; for (index = 1; index < self->groups; index++) { PyObject* item; - item = match_getslice_by_index(self, index, def); + item = match_getslice_by_index(self, index, default_value); if (!item) { Py_DECREF(result); return NULL; @@ -2144,18 +2205,23 @@ match_groups(MatchObject* self, PyObject* args, PyObject* kw) return result; } -static PyObject* -match_groupdict(MatchObject* self, PyObject* args, PyObject* kw) +/*[clinic input] +_sre.SRE_Match.groupdict + + default: object = None + Is used for groups that did not participate in the match. + +Return a dictionary containing all the named subgroups of the match, keyed by the subgroup name. +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Match_groupdict_impl(MatchObject *self, PyObject *default_value) +/*[clinic end generated code: output=29917c9073e41757 input=0ded7960b23780aa]*/ { PyObject* result; PyObject* keys; Py_ssize_t index; - PyObject* def = Py_None; - static char* kwlist[] = { "default", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groupdict", kwlist, &def)) - return NULL; - result = PyDict_New(); if (!result || !self->pattern->groupindex) return result; @@ -2171,7 +2237,7 @@ match_groupdict(MatchObject* self, PyObject* args, PyObject* kw) key = PyList_GET_ITEM(keys, index); if (!key) goto failed; - value = match_getslice(self, key, def); + value = match_getslice(self, key, default_value); if (!value) { Py_DECREF(key); goto failed; @@ -2192,50 +2258,58 @@ failed: return NULL; } -static PyObject* -match_start(MatchObject* self, PyObject* args) -{ - Py_ssize_t index; +/*[clinic input] +_sre.SRE_Match.start -> Py_ssize_t - PyObject* index_ = NULL; - if (!PyArg_UnpackTuple(args, "start", 0, 1, &index_)) - return NULL; + group: object(c_default="NULL") = 0 + / - index = match_getindex(self, index_); +Return index of the start of the substring matched by group. +[clinic start generated code]*/ + +static Py_ssize_t +_sre_SRE_Match_start_impl(MatchObject *self, PyObject *group) +/*[clinic end generated code: output=3f6e7f9df2fb5201 input=ced8e4ed4b33ee6c]*/ +{ + Py_ssize_t index = match_getindex(self, group); if (index < 0 || index >= self->groups) { PyErr_SetString( PyExc_IndexError, "no such group" ); - return NULL; + return -1; } /* mark is -1 if group is undefined */ - return PyLong_FromSsize_t(self->mark[index*2]); + return self->mark[index*2]; } -static PyObject* -match_end(MatchObject* self, PyObject* args) -{ - Py_ssize_t index; +/*[clinic input] +_sre.SRE_Match.end -> Py_ssize_t - PyObject* index_ = NULL; - if (!PyArg_UnpackTuple(args, "end", 0, 1, &index_)) - return NULL; + group: object(c_default="NULL") = 0 + / - index = match_getindex(self, index_); +Return index of the end of the substring matched by group. +[clinic start generated code]*/ + +static Py_ssize_t +_sre_SRE_Match_end_impl(MatchObject *self, PyObject *group) +/*[clinic end generated code: output=f4240b09911f7692 input=1b799560c7f3d7e6]*/ +{ + Py_ssize_t index = match_getindex(self, group); if (index < 0 || index >= self->groups) { PyErr_SetString( PyExc_IndexError, "no such group" ); - return NULL; + return -1; } /* mark is -1 if group is undefined */ - return PyLong_FromSsize_t(self->mark[index*2+1]); + return self->mark[index*2+1]; } LOCAL(PyObject*) @@ -2265,16 +2339,20 @@ _pair(Py_ssize_t i1, Py_ssize_t i2) return NULL; } -static PyObject* -match_span(MatchObject* self, PyObject* args) -{ - Py_ssize_t index; +/*[clinic input] +_sre.SRE_Match.span - PyObject* index_ = NULL; - if (!PyArg_UnpackTuple(args, "span", 0, 1, &index_)) - return NULL; + group: object(c_default="NULL") = 0 + / - index = match_getindex(self, index_); +For MatchObject m, return the 2-tuple (m.start(group), m.end(group)). +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Match_span_impl(MatchObject *self, PyObject *group) +/*[clinic end generated code: output=f02ae40594d14fe6 input=49092b6008d176d3]*/ +{ + Py_ssize_t index = match_getindex(self, group); if (index < 0 || index >= self->groups) { PyErr_SetString( @@ -2314,8 +2392,14 @@ match_regs(MatchObject* self) return regs; } -static PyObject* -match_copy(MatchObject* self, PyObject *unused) +/*[clinic input] +_sre.SRE_Match.__copy__ + +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Match___copy___impl(MatchObject *self) +/*[clinic end generated code: output=a779c5fc8b5b4eb4 input=3bb4d30b6baddb5b]*/ { #ifdef USE_BUILTIN_COPY MatchObject* copy; @@ -2345,8 +2429,16 @@ match_copy(MatchObject* self, PyObject *unused) #endif } -static PyObject* -match_deepcopy(MatchObject* self, PyObject* memo) +/*[clinic input] +_sre.SRE_Match.__deepcopy__ + + memo: object + +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Match___deepcopy___impl(MatchObject *self, PyObject *memo) +/*[clinic end generated code: output=2b657578eb03f4a3 input=b65b72489eac64cc]*/ { #ifdef USE_BUILTIN_COPY MatchObject* copy; @@ -2377,49 +2469,7 @@ 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_start_doc, -"start([group=0]) -> int.\n\ - Return index of the start of the substring matched by group."); - -PyDoc_STRVAR(match_end_doc, -"end([group=0]) -> int.\n\ - Return index of the end of the substring matched by group."); - -PyDoc_STRVAR(match_span_doc, -"span([group]) -> tuple.\n\ - For MatchObject m, return the 2-tuple (m.start(group), m.end(group))."); - -PyDoc_STRVAR(match_groups_doc, -"groups([default=None]) -> tuple.\n\ - Return a tuple containing all the subgroups of the match, from 1.\n\ - The default argument is used for groups\n\ - that did not participate in the match"); - -PyDoc_STRVAR(match_groupdict_doc, -"groupdict([default=None]) -> dict.\n\ - Return a dictionary containing all the named subgroups of the match,\n\ - keyed by the subgroup name. The default argument is used for groups\n\ - that did not participate in the match"); - -PyDoc_STRVAR(match_expand_doc, -"expand(template) -> str.\n\ - Return the string obtained by doing backslash substitution\n\ - on the string template, as done by the sub() method."); - -static PyMethodDef match_methods[] = { - {"group", (PyCFunction) match_group, METH_VARARGS, match_group_doc}, - {"start", (PyCFunction) match_start, METH_VARARGS, match_start_doc}, - {"end", (PyCFunction) match_end, METH_VARARGS, match_end_doc}, - {"span", (PyCFunction) match_span, METH_VARARGS, match_span_doc}, - {"groups", (PyCFunction) match_groups, METH_VARARGS|METH_KEYWORDS, - match_groups_doc}, - {"groupdict", (PyCFunction) match_groupdict, METH_VARARGS|METH_KEYWORDS, - match_groupdict_doc}, - {"expand", (PyCFunction) match_expand, METH_O, match_expand_doc}, - {"__copy__", (PyCFunction) match_copy, METH_NOARGS}, - {"__deepcopy__", (PyCFunction) match_deepcopy, METH_O}, - {NULL, NULL} -}; +static PyMethodDef match_methods[]; static PyObject * match_lastindex_get(MatchObject *self) @@ -2597,8 +2647,14 @@ scanner_dealloc(ScannerObject* self) PyObject_DEL(self); } -static PyObject* -scanner_match(ScannerObject* self, PyObject *unused) +/*[clinic input] +_sre.SRE_Scanner.match + +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Scanner_match_impl(ScannerObject *self) +/*[clinic end generated code: output=936b30c63d4b81eb input=881a0154f8c13d9a]*/ { SRE_STATE* state = &self->state; PyObject* match; @@ -2624,8 +2680,14 @@ scanner_match(ScannerObject* self, PyObject *unused) } -static PyObject* -scanner_search(ScannerObject* self, PyObject *unused) +/*[clinic input] +_sre.SRE_Scanner.search + +[clinic start generated code]*/ + +static PyObject * +_sre_SRE_Scanner_search_impl(ScannerObject *self) +/*[clinic end generated code: output=7dc211986088f025 input=161223ee92ef9270]*/ { SRE_STATE* state = &self->state; PyObject* match; @@ -2650,11 +2712,7 @@ scanner_search(ScannerObject* self, PyObject *unused) return match; } -static PyMethodDef scanner_methods[] = { - {"match", (PyCFunction) scanner_match, METH_NOARGS}, - {"search", (PyCFunction) scanner_search, METH_NOARGS}, - {NULL, NULL} -}; +static PyMethodDef scanner_methods[]; #define SCAN_OFF(x) offsetof(ScannerObject, x) static PyMemberDef scanner_members[] = { @@ -2694,47 +2752,69 @@ static PyTypeObject Scanner_Type = { 0, /* tp_getset */ }; -static PyObject* -pattern_scanner(PatternObject* pattern, PyObject* args, PyObject* kw) +static PyObject * +pattern_scanner(PatternObject *self, PyObject *string, Py_ssize_t pos, Py_ssize_t endpos) { - /* create search state object */ - - ScannerObject* self; - - PyObject *string = NULL, *string2 = NULL; - Py_ssize_t start = 0; - Py_ssize_t end = PY_SSIZE_T_MAX; - static char* kwlist[] = { "string", "pos", "endpos", "source", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:scanner", kwlist, - &string, &start, &end, &string2)) - return NULL; - - string = fix_string_param(string, string2, "source"); - if (!string) - return NULL; + ScannerObject* scanner; /* create scanner object */ - self = PyObject_NEW(ScannerObject, &Scanner_Type); - if (!self) + scanner = PyObject_NEW(ScannerObject, &Scanner_Type); + if (!scanner) return NULL; - self->pattern = NULL; + scanner->pattern = NULL; - string = state_init(&self->state, pattern, string, start, end); - if (!string) { - Py_DECREF(self); + /* create search state object */ + if (!state_init(&scanner->state, self, string, pos, endpos)) { + Py_DECREF(scanner); return NULL; } - Py_INCREF(pattern); - self->pattern = (PyObject*) pattern; + Py_INCREF(self); + scanner->pattern = (PyObject*) self; - return (PyObject*) self; + return (PyObject*) scanner; } +#include "clinic/_sre.c.h" + +static PyMethodDef pattern_methods[] = { + _SRE_SRE_PATTERN_MATCH_METHODDEF + _SRE_SRE_PATTERN_FULLMATCH_METHODDEF + _SRE_SRE_PATTERN_SEARCH_METHODDEF + _SRE_SRE_PATTERN_SUB_METHODDEF + _SRE_SRE_PATTERN_SUBN_METHODDEF + _SRE_SRE_PATTERN_FINDALL_METHODDEF + _SRE_SRE_PATTERN_SPLIT_METHODDEF + _SRE_SRE_PATTERN_FINDITER_METHODDEF + _SRE_SRE_PATTERN_SCANNER_METHODDEF + _SRE_SRE_PATTERN___COPY___METHODDEF + _SRE_SRE_PATTERN___DEEPCOPY___METHODDEF + {NULL, NULL} +}; + +static PyMethodDef match_methods[] = { + {"group", (PyCFunction) match_group, METH_VARARGS, match_group_doc}, + _SRE_SRE_MATCH_START_METHODDEF + _SRE_SRE_MATCH_END_METHODDEF + _SRE_SRE_MATCH_SPAN_METHODDEF + _SRE_SRE_MATCH_GROUPS_METHODDEF + _SRE_SRE_MATCH_GROUPDICT_METHODDEF + _SRE_SRE_MATCH_EXPAND_METHODDEF + _SRE_SRE_MATCH___COPY___METHODDEF + _SRE_SRE_MATCH___DEEPCOPY___METHODDEF + {NULL, NULL} +}; + +static PyMethodDef scanner_methods[] = { + _SRE_SRE_SCANNER_MATCH_METHODDEF + _SRE_SRE_SCANNER_SEARCH_METHODDEF + {NULL, NULL} +}; + static PyMethodDef _functions[] = { - {"compile", _compile, METH_VARARGS}, - {"getcodesize", sre_codesize, METH_NOARGS}, - {"getlower", sre_getlower, METH_VARARGS}, + _SRE_COMPILE_METHODDEF + _SRE_GETCODESIZE_METHODDEF + _SRE_GETLOWER_METHODDEF {NULL, NULL} }; |