diff -r e634b377d5cc Modules/_sre.c --- a/Modules/_sre.c Tue Jan 07 14:25:26 2014 -0800 +++ b/Modules/_sre.c Wed Jan 08 01:43:31 2014 +0200 @@ -271,20 +271,87 @@ /* 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*_sre_SRE_Pattern_scanner(PyObject *self, PyObject *args, PyObject *kwargs); +static PyObject *pattern_scanner(PatternObject *, PyObject *, Py_ssize_t, Py_ssize_t); + + +/*[clinic input] +module _sre +class _sre.SRE_Pattern +class _sre.SRE_Match +class _sre.SRE_Scanner +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + +/*[clinic input] +_sre.getcodesize +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_getcodesize__doc__, +"getcodesize()"); + +#define _SRE_GETCODESIZE_METHODDEF \ + {"getcodesize", (PyCFunction)_sre_getcodesize, METH_NOARGS, _sre_getcodesize__doc__}, static PyObject * -sre_codesize(PyObject* self, PyObject *unused) +_sre_getcodesize_impl(PyModuleDef *module); + +static PyObject * +_sre_getcodesize(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = _sre_getcodesize_impl(module); + + return return_value; +} + +static PyObject * +_sre_getcodesize_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=385e6020b92b3f9d787a14dca59b5a6da5a0f19e]*/ { return PyLong_FromSize_t(sizeof(SRE_CODE)); } +/*[clinic input] +_sre.getlower + + character: int + flags: int + / + +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_getlower__doc__, +"getlower(character, flags)"); + +#define _SRE_GETLOWER_METHODDEF \ + {"getlower", (PyCFunction)_sre_getlower, METH_VARARGS, _sre_getlower__doc__}, + static PyObject * -sre_getlower(PyObject* self, PyObject* args) +_sre_getlower_impl(PyModuleDef *module, int character, int flags); + +static PyObject * +_sre_getlower(PyModuleDef *module, PyObject *args) { - int character, flags; - if (!PyArg_ParseTuple(args, "ii", &character, &flags)) - return NULL; + PyObject *return_value = NULL; + int character; + int flags; + + if (!PyArg_ParseTuple(args, + "ii:getlower", + &character, &flags)) + goto exit; + return_value = _sre_getlower_impl(module, character, flags); + +exit: + return return_value; +} + +static PyObject * +_sre_getlower_impl(PyModuleDef *module, int character, int flags) +/*[clinic end generated code: checksum=4b05906ca71a7f4807394e5a025f51338e1de2f9]*/ +{ if (flags & SRE_FLAG_LOCALE) return Py_BuildValue("i", sre_lower_locale(character)); if (flags & SRE_FLAG_UNICODE) @@ -527,10 +594,7 @@ } /*[clinic input] -module _sre -class _sre.SRE_Pattern - -_sre.SRE_Pattern.match as pattern_match +_sre.SRE_Pattern.match self: self(type="PatternObject *") pattern: object @@ -540,18 +604,18 @@ Matches zero or more characters at the beginning of the string. [clinic start generated code]*/ -PyDoc_STRVAR(pattern_match__doc__, +PyDoc_STRVAR(_sre_SRE_Pattern_match__doc__, "match(pattern, pos=0, endpos=sys.maxsize)\n" "Matches zero or more characters at the beginning of the string."); -#define PATTERN_MATCH_METHODDEF \ - {"match", (PyCFunction)pattern_match, METH_VARARGS|METH_KEYWORDS, pattern_match__doc__}, +#define _SRE_SRE_PATTERN_MATCH_METHODDEF \ + {"match", (PyCFunction)_sre_SRE_Pattern_match, METH_VARARGS|METH_KEYWORDS, _sre_SRE_Pattern_match__doc__}, static PyObject * -pattern_match_impl(PatternObject *self, PyObject *pattern, Py_ssize_t pos, Py_ssize_t endpos); +_sre_SRE_Pattern_match_impl(PatternObject *self, PyObject *pattern, Py_ssize_t pos, Py_ssize_t endpos); static PyObject * -pattern_match(PyObject *self, PyObject *args, PyObject *kwargs) +_sre_SRE_Pattern_match(PyObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; static char *_keywords[] = {"pattern", "pos", "endpos", NULL}; @@ -563,22 +627,20 @@ "O|nn:match", _keywords, &pattern, &pos, &endpos)) goto exit; - return_value = pattern_match_impl((PatternObject *)self, pattern, pos, endpos); + return_value = _sre_SRE_Pattern_match_impl((PatternObject *)self, pattern, pos, endpos); exit: return return_value; } static PyObject * -pattern_match_impl(PatternObject *self, PyObject *pattern, Py_ssize_t pos, Py_ssize_t endpos) -/*[clinic end generated code: checksum=63e59c5f3019efe6c1f3acdec42b2d3595e14a09]*/ +_sre_SRE_Pattern_match_impl(PatternObject *self, PyObject *pattern, Py_ssize_t pos, Py_ssize_t endpos) +/*[clinic end generated code: checksum=5b7424430c7827917a2115c4789d13db78e7f606]*/ { SRE_STATE state; Py_ssize_t status; - PyObject *string; - - string = state_init(&state, (PatternObject *)self, pattern, pos, endpos); - if (!string) + + if (!state_init(&state, self, pattern, pos, endpos)) return NULL; state.ptr = state.start; @@ -593,25 +655,57 @@ state_fini(&state); - return (PyObject *)pattern_new_match(self, &state, status); + return pattern_new_match(self, &state, status); } -static PyObject* -pattern_fullmatch(PatternObject* self, PyObject* args, PyObject* kw) +/*[clinic input] +_sre.SRE_Pattern.fullmatch + + self: self(type="PatternObject *") + pattern: object + pos: Py_ssize_t = 0 + endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize + +Matches against all of the string +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern_fullmatch__doc__, +"fullmatch(pattern, pos=0, endpos=sys.maxsize)\n" +"Matches against all of the string"); + +#define _SRE_SRE_PATTERN_FULLMATCH_METHODDEF \ + {"fullmatch", (PyCFunction)_sre_SRE_Pattern_fullmatch, METH_VARARGS|METH_KEYWORDS, _sre_SRE_Pattern_fullmatch__doc__}, + +static PyObject * +_sre_SRE_Pattern_fullmatch_impl(PatternObject *self, PyObject *pattern, Py_ssize_t pos, Py_ssize_t endpos); + +static PyObject * +_sre_SRE_Pattern_fullmatch(PyObject *self, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"pattern", "pos", "endpos", NULL}; + PyObject *pattern; + Py_ssize_t pos = 0; + Py_ssize_t endpos = PY_SSIZE_T_MAX; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O|nn:fullmatch", _keywords, + &pattern, &pos, &endpos)) + goto exit; + return_value = _sre_SRE_Pattern_fullmatch_impl((PatternObject *)self, pattern, pos, endpos); + +exit: + return return_value; +} + +static PyObject * +_sre_SRE_Pattern_fullmatch_impl(PatternObject *self, PyObject *pattern, Py_ssize_t pos, Py_ssize_t endpos) +/*[clinic end generated code: checksum=4be3d0da84b1f28bc1f4bc8787765082e8e27075]*/ { SRE_STATE state; Py_ssize_t status; - PyObject* string; - Py_ssize_t start = 0; - Py_ssize_t end = PY_SSIZE_T_MAX; - static char* kwlist[] = { "pattern", "pos", "endpos", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kw, "O|nn:fullmatch", kwlist, - &string, &start, &end)) - return NULL; - - string = state_init(&state, self, string, start, end); - if (!string) + if (!state_init(&state, self, pattern, pos, endpos)) return NULL; state.match_all = 1; @@ -630,22 +724,58 @@ return pattern_new_match(self, &state, status); } -static PyObject* -pattern_search(PatternObject* self, PyObject* args, PyObject* kw) +/*[clinic input] +_sre.SRE_Pattern.search + + self: self(type="PatternObject *") + pattern: object + pos: Py_ssize_t = 0 + endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize + +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]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern_search__doc__, +"search(pattern, pos=0, endpos=sys.maxsize)\n" +"Scan through string looking for a match, and return a corresponding match object instance.\n" +"\n" +"Return None if no position in the string matches."); + +#define _SRE_SRE_PATTERN_SEARCH_METHODDEF \ + {"search", (PyCFunction)_sre_SRE_Pattern_search, METH_VARARGS|METH_KEYWORDS, _sre_SRE_Pattern_search__doc__}, + +static PyObject * +_sre_SRE_Pattern_search_impl(PatternObject *self, PyObject *pattern, Py_ssize_t pos, Py_ssize_t endpos); + +static PyObject * +_sre_SRE_Pattern_search(PyObject *self, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"pattern", "pos", "endpos", NULL}; + PyObject *pattern; + Py_ssize_t pos = 0; + Py_ssize_t endpos = PY_SSIZE_T_MAX; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O|nn:search", _keywords, + &pattern, &pos, &endpos)) + goto exit; + return_value = _sre_SRE_Pattern_search_impl((PatternObject *)self, pattern, pos, endpos); + +exit: + return return_value; +} + +static PyObject * +_sre_SRE_Pattern_search_impl(PatternObject *self, PyObject *pattern, Py_ssize_t pos, Py_ssize_t endpos) +/*[clinic end generated code: checksum=2b5d36950d86ef14a984897f965220953a05cd6c]*/ { SRE_STATE state; Py_ssize_t status; - PyObject* string; - Py_ssize_t start = 0; - Py_ssize_t end = PY_SSIZE_T_MAX; - static char* kwlist[] = { "pattern", "pos", "endpos", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kw, "O|nn:search", kwlist, - &string, &start, &end)) - return NULL; - - string = state_init(&state, self, string, start, end); - if (!string) + if (!state_init(&state, self, pattern, pos, endpos)) return NULL; TRACE(("|%p|%p|SEARCH\n", PatternObject_GetCode(self), state.ptr)); @@ -709,23 +839,56 @@ } #endif -static PyObject* -pattern_findall(PatternObject* self, PyObject* args, PyObject* kw) +/*[clinic input] +_sre.SRE_Pattern.findall + + self: self(type="PatternObject *") + source: object + pos: Py_ssize_t = 0 + endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize + +Return a list of all non-overlapping matches of pattern in string. +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern_findall__doc__, +"findall(source, pos=0, endpos=sys.maxsize)\n" +"Return a list of all non-overlapping matches of pattern in string."); + +#define _SRE_SRE_PATTERN_FINDALL_METHODDEF \ + {"findall", (PyCFunction)_sre_SRE_Pattern_findall, METH_VARARGS|METH_KEYWORDS, _sre_SRE_Pattern_findall__doc__}, + +static PyObject * +_sre_SRE_Pattern_findall_impl(PatternObject *self, PyObject *source, Py_ssize_t pos, Py_ssize_t endpos); + +static PyObject * +_sre_SRE_Pattern_findall(PyObject *self, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"source", "pos", "endpos", NULL}; + PyObject *source; + Py_ssize_t pos = 0; + Py_ssize_t endpos = PY_SSIZE_T_MAX; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O|nn:findall", _keywords, + &source, &pos, &endpos)) + goto exit; + return_value = _sre_SRE_Pattern_findall_impl((PatternObject *)self, source, pos, endpos); + +exit: + return return_value; +} + +static PyObject * +_sre_SRE_Pattern_findall_impl(PatternObject *self, PyObject *source, Py_ssize_t pos, Py_ssize_t endpos) +/*[clinic end generated code: checksum=4057154b98f04acd236c05fc8b0e8acdd34f7afe]*/ { SRE_STATE state; PyObject* list; Py_ssize_t status; Py_ssize_t i, b, e; - PyObject* string; - Py_ssize_t start = 0; - Py_ssize_t end = PY_SSIZE_T_MAX; - static char* kwlist[] = { "source", "pos", "endpos", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kw, "O|nn:findall", kwlist, - &string, &start, &end)) - return NULL; - - string = state_init(&state, self, string, start, end); + PyObject* string = state_init(&state, self, source, pos, endpos); if (!string) return NULL; @@ -806,14 +969,55 @@ } -static PyObject* -pattern_finditer(PatternObject* pattern, PyObject* args, PyObject* kw) +/*[clinic input] +_sre.SRE_Pattern.finditer + + self: self(type="PatternObject *") + source: 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]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern_finditer__doc__, +"finditer(source, pos=0, endpos=sys.maxsize)\n" +"Return an iterator over all non-overlapping matches for the RE pattern in string. For each match, the iterator returns a match object."); + +#define _SRE_SRE_PATTERN_FINDITER_METHODDEF \ + {"finditer", (PyCFunction)_sre_SRE_Pattern_finditer, METH_VARARGS|METH_KEYWORDS, _sre_SRE_Pattern_finditer__doc__}, + +static PyObject * +_sre_SRE_Pattern_finditer_impl(PatternObject *self, PyObject *source, Py_ssize_t pos, Py_ssize_t endpos); + +static PyObject * +_sre_SRE_Pattern_finditer(PyObject *self, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"source", "pos", "endpos", NULL}; + PyObject *source; + Py_ssize_t pos = 0; + Py_ssize_t endpos = PY_SSIZE_T_MAX; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O|nn:finditer", _keywords, + &source, &pos, &endpos)) + goto exit; + return_value = _sre_SRE_Pattern_finditer_impl((PatternObject *)self, source, pos, endpos); + +exit: + return return_value; +} + +static PyObject * +_sre_SRE_Pattern_finditer_impl(PatternObject *self, PyObject *source, Py_ssize_t pos, Py_ssize_t endpos) +/*[clinic end generated code: checksum=ed37072d7f20d6ac5ef2aa093b7646160f21c33f]*/ { PyObject* scanner; PyObject* search; PyObject* iterator; - scanner = pattern_scanner(pattern, args, kw); + scanner = pattern_scanner(self, source, pos, endpos); if (!scanner) return NULL; @@ -828,8 +1032,92 @@ return iterator; } -static PyObject* -pattern_split(PatternObject* self, PyObject* args, PyObject* kw) +/*[clinic input] +_sre.SRE_Pattern.scanner + + self: self(type="PatternObject *") + source: object + pos: Py_ssize_t = 0 + endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize + +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern_scanner__doc__, +"scanner(source, pos=0, endpos=sys.maxsize)"); + +#define _SRE_SRE_PATTERN_SCANNER_METHODDEF \ + {"scanner", (PyCFunction)_sre_SRE_Pattern_scanner, METH_VARARGS|METH_KEYWORDS, _sre_SRE_Pattern_scanner__doc__}, + +static PyObject * +_sre_SRE_Pattern_scanner_impl(PatternObject *self, PyObject *source, Py_ssize_t pos, Py_ssize_t endpos); + +static PyObject * +_sre_SRE_Pattern_scanner(PyObject *self, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"source", "pos", "endpos", NULL}; + PyObject *source; + Py_ssize_t pos = 0; + Py_ssize_t endpos = PY_SSIZE_T_MAX; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O|nn:scanner", _keywords, + &source, &pos, &endpos)) + goto exit; + return_value = _sre_SRE_Pattern_scanner_impl((PatternObject *)self, source, pos, endpos); + +exit: + return return_value; +} + +static PyObject * +_sre_SRE_Pattern_scanner_impl(PatternObject *self, PyObject *source, Py_ssize_t pos, Py_ssize_t endpos) +/*[clinic end generated code: checksum=af443455dd19804948e6dec1fab324b0d62c4656]*/ +{ + return pattern_scanner(self, source, pos, endpos); +} + +/*[clinic input] +_sre.SRE_Pattern.split + + self: self(type="PatternObject *") + source: object + maxsplit: Py_ssize_t = 0 + +Split string by the occurrences of pattern. +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern_split__doc__, +"split(source, maxsplit=0)\n" +"Split string by the occurrences of pattern."); + +#define _SRE_SRE_PATTERN_SPLIT_METHODDEF \ + {"split", (PyCFunction)_sre_SRE_Pattern_split, METH_VARARGS|METH_KEYWORDS, _sre_SRE_Pattern_split__doc__}, + +static PyObject * +_sre_SRE_Pattern_split_impl(PatternObject *self, PyObject *source, Py_ssize_t maxsplit); + +static PyObject * +_sre_SRE_Pattern_split(PyObject *self, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"source", "maxsplit", NULL}; + PyObject *source; + Py_ssize_t maxsplit = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O|n:split", _keywords, + &source, &maxsplit)) + goto exit; + return_value = _sre_SRE_Pattern_split_impl((PatternObject *)self, source, maxsplit); + +exit: + return return_value; +} + +static PyObject * +_sre_SRE_Pattern_split_impl(PatternObject *self, PyObject *source, Py_ssize_t maxsplit) +/*[clinic end generated code: checksum=208015aae3c6e8bcb8e816cecb39afdac95745f8]*/ { SRE_STATE state; PyObject* list; @@ -839,14 +1127,7 @@ Py_ssize_t i; void* last; - PyObject* string; - Py_ssize_t maxsplit = 0; - static char* kwlist[] = { "source", "maxsplit", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kw, "O|n:split", kwlist, - &string, &maxsplit)) - return NULL; - - string = state_init(&state, self, string, 0, PY_SSIZE_T_MAX); + PyObject* string = state_init(&state, self, source, 0, PY_SSIZE_T_MAX); if (!string) return NULL; @@ -1132,36 +1413,128 @@ } -static PyObject* -pattern_sub(PatternObject* self, PyObject* args, PyObject* kw) +/*[clinic input] +_sre.SRE_Pattern.sub + + self: self(type="PatternObject *") + 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]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern_sub__doc__, +"sub(repl, string, count=0)\n" +"Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl."); + +#define _SRE_SRE_PATTERN_SUB_METHODDEF \ + {"sub", (PyCFunction)_sre_SRE_Pattern_sub, METH_VARARGS|METH_KEYWORDS, _sre_SRE_Pattern_sub__doc__}, + +static PyObject * +_sre_SRE_Pattern_sub_impl(PatternObject *self, PyObject *repl, PyObject *string, Py_ssize_t count); + +static PyObject * +_sre_SRE_Pattern_sub(PyObject *self, PyObject *args, PyObject *kwargs) { - PyObject* ptemplate; - PyObject* string; + PyObject *return_value = NULL; + static char *_keywords[] = {"repl", "string", "count", NULL}; + PyObject *repl; + 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; - - return pattern_subx(self, ptemplate, string, count, 0); + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "OO|n:sub", _keywords, + &repl, &string, &count)) + goto exit; + return_value = _sre_SRE_Pattern_sub_impl((PatternObject *)self, repl, string, count); + +exit: + return return_value; } -static PyObject* -pattern_subn(PatternObject* self, PyObject* args, PyObject* kw) +static PyObject * +_sre_SRE_Pattern_sub_impl(PatternObject *self, PyObject *repl, PyObject *string, Py_ssize_t count) +/*[clinic end generated code: checksum=16b25da5bf7c5f2b34c7827ada45169a2156e1db]*/ { - PyObject* ptemplate; - PyObject* string; + return pattern_subx(self, repl, string, count, 0); +} + +/*[clinic input] +_sre.SRE_Pattern.subn + + self: self(type="PatternObject *") + 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]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern_subn__doc__, +"subn(repl, string, count=0)\n" +"Return the tuple (new_string, number_of_subs_made) found by replacing the leftmost non-overlapping occurrences of pattern with the replacement repl."); + +#define _SRE_SRE_PATTERN_SUBN_METHODDEF \ + {"subn", (PyCFunction)_sre_SRE_Pattern_subn, METH_VARARGS|METH_KEYWORDS, _sre_SRE_Pattern_subn__doc__}, + +static PyObject * +_sre_SRE_Pattern_subn_impl(PatternObject *self, PyObject *repl, PyObject *string, Py_ssize_t count); + +static PyObject * +_sre_SRE_Pattern_subn(PyObject *self, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"repl", "string", "count", NULL}; + PyObject *repl; + 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; - - return pattern_subx(self, ptemplate, string, count, 1); + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "OO|n:subn", _keywords, + &repl, &string, &count)) + goto exit; + return_value = _sre_SRE_Pattern_subn_impl((PatternObject *)self, repl, string, count); + +exit: + return return_value; } -static PyObject* -pattern_copy(PatternObject* self, PyObject *unused) +static PyObject * +_sre_SRE_Pattern_subn_impl(PatternObject *self, PyObject *repl, PyObject *string, Py_ssize_t count) +/*[clinic end generated code: checksum=e70f815efffc4e9991bb0d92dd4c0f4d8427a152]*/ +{ + return pattern_subx(self, repl, string, count, 1); +} + +/*[clinic input] +_sre.SRE_Pattern.__copy__ + + self: self(type="PatternObject *") +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern___copy____doc__, +"__copy__()"); + +#define _SRE_SRE_PATTERN___COPY___METHODDEF \ + {"__copy__", (PyCFunction)_sre_SRE_Pattern___copy__, METH_NOARGS, _sre_SRE_Pattern___copy____doc__}, + +static PyObject * +_sre_SRE_Pattern___copy___impl(PatternObject *self); + +static PyObject * +_sre_SRE_Pattern___copy__(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = _sre_SRE_Pattern___copy___impl((PatternObject *)self); + + return return_value; +} + +static PyObject * +_sre_SRE_Pattern___copy___impl(PatternObject *self) +/*[clinic end generated code: checksum=98c5cfb2670cfa79d5312aeeab50b9d581df4bc4]*/ { #ifdef USE_BUILTIN_COPY PatternObject* copy; @@ -1188,8 +1561,42 @@ #endif } -static PyObject* -pattern_deepcopy(PatternObject* self, PyObject* memo) +/*[clinic input] +_sre.SRE_Pattern.__deepcopy__ + + self: self(type="PatternObject *") + memo: object +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern___deepcopy____doc__, +"__deepcopy__(memo)"); + +#define _SRE_SRE_PATTERN___DEEPCOPY___METHODDEF \ + {"__deepcopy__", (PyCFunction)_sre_SRE_Pattern___deepcopy__, METH_VARARGS|METH_KEYWORDS, _sre_SRE_Pattern___deepcopy____doc__}, + +static PyObject * +_sre_SRE_Pattern___deepcopy___impl(PatternObject *self, PyObject *memo); + +static PyObject * +_sre_SRE_Pattern___deepcopy__(PyObject *self, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"memo", NULL}; + PyObject *memo; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O:__deepcopy__", _keywords, + &memo)) + goto exit; + return_value = _sre_SRE_Pattern___deepcopy___impl((PatternObject *)self, memo); + +exit: + return return_value; +} + +static PyObject * +_sre_SRE_Pattern___deepcopy___impl(PatternObject *self, PyObject *memo) +/*[clinic end generated code: checksum=1a02a57f69c41a0b4012fe47cea8e81aca949807]*/ { #ifdef USE_BUILTIN_COPY PatternObject* copy; @@ -1291,61 +1698,20 @@ return result; } -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[] = { - PATTERN_MATCH_METHODDEF - {"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}, + _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} }; @@ -1391,26 +1757,57 @@ static int _validate(PatternObject *self); /* Forward */ +/*[clinic input] +_sre.compile + + pattern: object + flags: int + code: object(subclass_of=='&PyList_Type') + groups: Py_ssize_t = 0 + groupindex: object = NULL + indexgroup: object = NULL +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_compile__doc__, +"compile(pattern, flags, code, groups=0, groupindex=None, indexgroup=None)"); + +#define _SRE_COMPILE_METHODDEF \ + {"compile", (PyCFunction)_sre_compile, METH_VARARGS|METH_KEYWORDS, _sre_compile__doc__}, + 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); + +static PyObject * +_sre_compile(PyModuleDef *module, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"pattern", "flags", "code", "groups", "groupindex", "indexgroup", NULL}; + PyObject *pattern; + int flags; + PyObject *code; + Py_ssize_t groups = 0; + PyObject *groupindex = NULL; + PyObject *indexgroup = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "OiO|nOO:compile", _keywords, + &pattern, &flags, &code, &groups, &groupindex, &indexgroup)) + goto exit; + return_value = _sre_compile_impl(module, pattern, flags, code, groups, groupindex, indexgroup); + +exit: + return return_value; +} + +static PyObject * +_sre_compile_impl(PyModuleDef *module, PyObject *pattern, int flags, PyObject *code, Py_ssize_t groups, PyObject *groupindex, PyObject *indexgroup) +/*[clinic end generated code: checksum=4143306eabc4593cefe9043b6a89ea105b2eb9aa]*/ { /* "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); @@ -2023,13 +2420,50 @@ 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 + + self: self(type="MatchObject *") + template: object + +Return the string obtained by doing backslash substitution on the string template, as done by the sub() method. +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_SRE_Match_expand__doc__, +"expand(template)\n" +"Return the string obtained by doing backslash substitution on the string template, as done by the sub() method."); + +#define _SRE_SRE_MATCH_EXPAND_METHODDEF \ + {"expand", (PyCFunction)_sre_SRE_Match_expand, METH_VARARGS|METH_KEYWORDS, _sre_SRE_Match_expand__doc__}, + +static PyObject * +_sre_SRE_Match_expand_impl(MatchObject *self, PyObject *template); + +static PyObject * +_sre_SRE_Match_expand(PyObject *self, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"template", NULL}; + PyObject *template; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O:expand", _keywords, + &template)) + goto exit; + return_value = _sre_SRE_Match_expand_impl((MatchObject *)self, template); + +exit: + return return_value; +} + +static PyObject * +_sre_SRE_Match_expand_impl(MatchObject *self, PyObject *template) +/*[clinic end generated code: checksum=30290a1147a6075befb9528f376dd83014528c26]*/ { /* delegate to Python code */ return call( SRE_PY_MODULE, "_expand", - PyTuple_Pack(3, self->pattern, self, ptemplate) + PyTuple_Pack(3, self->pattern, self, template) ); } @@ -2068,16 +2502,54 @@ return result; } -static PyObject* -match_groups(MatchObject* self, PyObject* args, PyObject* kw) +/*[clinic input] +_sre.SRE_Match.groups + + self: self(type="MatchObject *") + default: object = None + +Return a tuple containing all the subgroups of the match, from 1. + +The default argument is used for groups that did not participate in the match. +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_SRE_Match_groups__doc__, +"groups(default=None)\n" +"Return a tuple containing all the subgroups of the match, from 1.\n" +"\n" +"The default argument is used for groups that did not participate in the match."); + +#define _SRE_SRE_MATCH_GROUPS_METHODDEF \ + {"groups", (PyCFunction)_sre_SRE_Match_groups, METH_VARARGS|METH_KEYWORDS, _sre_SRE_Match_groups__doc__}, + +static PyObject * +_sre_SRE_Match_groups_impl(MatchObject *self, PyObject *default_value); + +static PyObject * +_sre_SRE_Match_groups(PyObject *self, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"default", NULL}; + PyObject *default_value = Py_None; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|O:groups", _keywords, + &default_value)) + goto exit; + return_value = _sre_SRE_Match_groups_impl((MatchObject *)self, default_value); + +exit: + return return_value; +} + +static PyObject * +_sre_SRE_Match_groups_impl(MatchObject *self, PyObject *default_value) +/*[clinic end generated code: checksum=9051288b31d8cdfc0c8b701bb7cc75ee0d727f70]*/ { 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; + PyObject* def = default_value; result = PyTuple_New(self->groups-1); if (!result) @@ -2096,17 +2568,55 @@ return result; } -static PyObject* -match_groupdict(MatchObject* self, PyObject* args, PyObject* kw) +/*[clinic input] +_sre.SRE_Match.groupdict + + self: self(type="MatchObject *") + default: object = None + +Return a dictionary containing all the named subgroups of the match, keyed by the subgroup name. + +The default argument is used for groups that did not participate in the match. +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_SRE_Match_groupdict__doc__, +"groupdict(default=None)\n" +"Return a dictionary containing all the named subgroups of the match, keyed by the subgroup name.\n" +"\n" +"The default argument is used for groups that did not participate in the match."); + +#define _SRE_SRE_MATCH_GROUPDICT_METHODDEF \ + {"groupdict", (PyCFunction)_sre_SRE_Match_groupdict, METH_VARARGS|METH_KEYWORDS, _sre_SRE_Match_groupdict__doc__}, + +static PyObject * +_sre_SRE_Match_groupdict_impl(MatchObject *self, PyObject *default_value); + +static PyObject * +_sre_SRE_Match_groupdict(PyObject *self, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"default", NULL}; + PyObject *default_value = Py_None; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|O:groupdict", _keywords, + &default_value)) + goto exit; + return_value = _sre_SRE_Match_groupdict_impl((MatchObject *)self, default_value); + +exit: + return return_value; +} + +static PyObject * +_sre_SRE_Match_groupdict_impl(MatchObject *self, PyObject *default_value) +/*[clinic end generated code: checksum=ad3b852f759e0ea57bccb5f9379e6ba4df79caa9]*/ { 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; + PyObject* def = default_value; result = PyDict_New(); if (!result || !self->pattern->groupindex) @@ -2266,8 +2776,34 @@ return regs; } -static PyObject* -match_copy(MatchObject* self, PyObject *unused) +/*[clinic input] +_sre.SRE_Match.__copy__ + + self: self(type="MatchObject *") +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_SRE_Match___copy____doc__, +"__copy__()"); + +#define _SRE_SRE_MATCH___COPY___METHODDEF \ + {"__copy__", (PyCFunction)_sre_SRE_Match___copy__, METH_NOARGS, _sre_SRE_Match___copy____doc__}, + +static PyObject * +_sre_SRE_Match___copy___impl(MatchObject *self); + +static PyObject * +_sre_SRE_Match___copy__(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = _sre_SRE_Match___copy___impl((MatchObject *)self); + + return return_value; +} + +static PyObject * +_sre_SRE_Match___copy___impl(MatchObject *self) +/*[clinic end generated code: checksum=34eafc594aa5ce78145cdcb2a2a99e36f2811369]*/ { #ifdef USE_BUILTIN_COPY MatchObject* copy; @@ -2297,8 +2833,42 @@ #endif } -static PyObject* -match_deepcopy(MatchObject* self, PyObject* memo) +/*[clinic input] +_sre.SRE_Match.__deepcopy__ + + self: self(type="MatchObject *") + memo: object +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_SRE_Match___deepcopy____doc__, +"__deepcopy__(memo)"); + +#define _SRE_SRE_MATCH___DEEPCOPY___METHODDEF \ + {"__deepcopy__", (PyCFunction)_sre_SRE_Match___deepcopy__, METH_VARARGS|METH_KEYWORDS, _sre_SRE_Match___deepcopy____doc__}, + +static PyObject * +_sre_SRE_Match___deepcopy___impl(MatchObject *self, PyObject *memo); + +static PyObject * +_sre_SRE_Match___deepcopy__(PyObject *self, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"memo", NULL}; + PyObject *memo; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O:__deepcopy__", _keywords, + &memo)) + goto exit; + return_value = _sre_SRE_Match___deepcopy___impl((MatchObject *)self, memo); + +exit: + return return_value; +} + +static PyObject * +_sre_SRE_Match___deepcopy___impl(MatchObject *self, PyObject *memo) +/*[clinic end generated code: checksum=6f3969601698ca74296a6d27110d6bb8d2a2bdaf]*/ { #ifdef USE_BUILTIN_COPY MatchObject* copy; @@ -2341,35 +2911,16 @@ "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}, + _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} }; @@ -2549,8 +3100,34 @@ PyObject_DEL(self); } -static PyObject* -scanner_match(ScannerObject* self, PyObject *unused) +/*[clinic input] +_sre.SRE_Scanner.match + + self: self(type="ScannerObject *") +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_SRE_Scanner_match__doc__, +"match()"); + +#define _SRE_SRE_SCANNER_MATCH_METHODDEF \ + {"match", (PyCFunction)_sre_SRE_Scanner_match, METH_NOARGS, _sre_SRE_Scanner_match__doc__}, + +static PyObject * +_sre_SRE_Scanner_match_impl(ScannerObject *self); + +static PyObject * +_sre_SRE_Scanner_match(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = _sre_SRE_Scanner_match_impl((ScannerObject *)self); + + return return_value; +} + +static PyObject * +_sre_SRE_Scanner_match_impl(ScannerObject *self) +/*[clinic end generated code: checksum=e2cd5527a9b4a38e2f1069451185cba17c299925]*/ { SRE_STATE* state = &self->state; PyObject* match; @@ -2576,8 +3153,34 @@ } -static PyObject* -scanner_search(ScannerObject* self, PyObject *unused) +/*[clinic input] +_sre.SRE_Scanner.search + + self: self(type="ScannerObject *") +[clinic start generated code]*/ + +PyDoc_STRVAR(_sre_SRE_Scanner_search__doc__, +"search()"); + +#define _SRE_SRE_SCANNER_SEARCH_METHODDEF \ + {"search", (PyCFunction)_sre_SRE_Scanner_search, METH_NOARGS, _sre_SRE_Scanner_search__doc__}, + +static PyObject * +_sre_SRE_Scanner_search_impl(ScannerObject *self); + +static PyObject * +_sre_SRE_Scanner_search(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = _sre_SRE_Scanner_search_impl((ScannerObject *)self); + + return return_value; +} + +static PyObject * +_sre_SRE_Scanner_search_impl(ScannerObject *self) +/*[clinic end generated code: checksum=919e721c34bad29215224ec03b815c49effd88ae]*/ { SRE_STATE* state = &self->state; PyObject* match; @@ -2603,8 +3206,8 @@ } static PyMethodDef scanner_methods[] = { - {"match", (PyCFunction) scanner_match, METH_NOARGS}, - {"search", (PyCFunction) scanner_search, METH_NOARGS}, + _SRE_SRE_SCANNER_MATCH_METHODDEF + _SRE_SRE_SCANNER_SEARCH_METHODDEF {NULL, NULL} }; @@ -2646,43 +3249,33 @@ 0, /* tp_getset */ }; -static PyObject* -pattern_scanner(PatternObject* pattern, PyObject* args, PyObject* kw) +static PyObject * +pattern_scanner(PatternObject *self, PyObject *source, Py_ssize_t pos, Py_ssize_t endpos) { + ScannerObject* scanner; + + /* create scanner object */ + scanner = PyObject_NEW(ScannerObject, &Scanner_Type); + if (!scanner) + return NULL; + scanner->pattern = NULL; + /* create search state object */ - - ScannerObject* self; - - PyObject* string; - Py_ssize_t start = 0; - Py_ssize_t end = PY_SSIZE_T_MAX; - static char* kwlist[] = { "source", "pos", "endpos", NULL }; - if (!PyArg_ParseTupleAndKeywords(args, kw, "O|nn:scanner", kwlist, - &string, &start, &end)) - return NULL; - - /* create scanner object */ - self = PyObject_NEW(ScannerObject, &Scanner_Type); - if (!self) - return NULL; - self->pattern = NULL; - - string = state_init(&self->state, pattern, string, start, end); - if (!string) { - Py_DECREF(self); + if (!state_init(&scanner->state, self, source, pos, endpos)) { + Py_DECREF(scanner); return NULL; } - Py_INCREF(pattern); - self->pattern = (PyObject*) pattern; - - return (PyObject*) self; + Py_INCREF(self); + scanner->pattern = (PyObject*) self; + + return (PyObject*) scanner; } 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} };