diff -r 247f12fecf2b Modules/_sre.c --- a/Modules/_sre.c Sun Jan 05 12:00:31 2014 -0800 +++ b/Modules/_sre.c Mon Jan 06 18:02:25 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] +module _sre +class _sre.SRE_Pattern +class _sre.SRE_Match +class _sre.SRE_Scanner +[clinic]*/ +/*[clinic checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + +/*[clinic] +_sre.getcodesize +[clinic]*/ + +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 checksum: 385e6020b92b3f9d787a14dca59b5a6da5a0f19e]*/ { return PyLong_FromSize_t(sizeof(SRE_CODE)); } +/*[clinic] +_sre.getlower + + character: int + flags: int + / + +[clinic]*/ + +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 checksum: 4b05906ca71a7f4807394e5a025f51338e1de2f9]*/ +{ if (flags & SRE_FLAG_LOCALE) return Py_BuildValue("i", sre_lower_locale(character)); if (flags & SRE_FLAG_UNICODE) @@ -526,19 +593,56 @@ return sre_ucs4_search(state, pattern); } -static PyObject* -pattern_match(PatternObject* self, PyObject* args, PyObject* kw) +/*[clinic] +_sre.SRE_Pattern.match + + self: self(type="PatternObject *") + pattern: object + pos: Py_ssize_t = 0 + endpos: Py_ssize_t = 1000000000 + +Matches zero or more characters at the beginning of the stringb +[clinic]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern_match__doc__, +"match(pattern, pos=0, endpos=1000000000)\n" +"Matches zero or more characters at the beginning of the stringb"); + +#define _SRE_SRE_PATTERN_MATCH_METHODDEF \ + {"match", (PyCFunction)_sre_SRE_Pattern_match, METH_VARARGS|METH_KEYWORDS, _sre_SRE_Pattern_match__doc__}, + +static PyObject * +_sre_SRE_Pattern_match_impl(PatternObject *self, PyObject *pattern, Py_ssize_t pos, Py_ssize_t endpos); + +static PyObject * +_sre_SRE_Pattern_match(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 = 1000000000; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O|nn:match", _keywords, + &pattern, &pos, &endpos)) + goto exit; + return_value = _sre_SRE_Pattern_match_impl((PatternObject *)self, pattern, pos, endpos); + +exit: + return return_value; +} + +static PyObject * +_sre_SRE_Pattern_match_impl(PatternObject *self, PyObject *pattern, Py_ssize_t pos, Py_ssize_t endpos) +/*[clinic checksum: 81e401994031bd111d0ed292334ae879f0bccc33]*/ { 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:match", kwlist, - &string, &start, &end)) - return NULL; + PyObject *string = pattern; + Py_ssize_t start = pos; + Py_ssize_t end = endpos; string = state_init(&state, self, string, start, end); if (!string) @@ -559,19 +663,56 @@ return pattern_new_match(self, &state, status); } -static PyObject* -pattern_fullmatch(PatternObject* self, PyObject* args, PyObject* kw) +/*[clinic] +_sre.SRE_Pattern.fullmatch + + self: self(type="PatternObject *") + pattern: object + pos: Py_ssize_t = 0 + endpos: Py_ssize_t = 1000000000 + +Matches against all of the string +[clinic]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern_fullmatch__doc__, +"fullmatch(pattern, pos=0, endpos=1000000000)\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 = 1000000000; + + 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 checksum: d5a9e91140e36ffd1a7dab2e7901ec961671fcd3]*/ { 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; + PyObject *string = pattern; + Py_ssize_t start = pos; + Py_ssize_t end = endpos; string = state_init(&state, self, string, start, end); if (!string) @@ -593,19 +734,56 @@ return pattern_new_match(self, &state, status); } -static PyObject* -pattern_search(PatternObject* self, PyObject* args, PyObject* kw) +/*[clinic] +_sre.SRE_Pattern.search + + self: self(type="PatternObject *") + pattern: object + pos: Py_ssize_t = 0 + endpos: Py_ssize_t = 1000000000 + +Scan through string looking for a match, and return a corresponding match object instance. Return None if no position in the string matches. +[clinic]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern_search__doc__, +"search(pattern, pos=0, endpos=1000000000)\n" +"Scan through string looking for a match, and return a corresponding match object instance. 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 = 1000000000; + + 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 checksum: e16835b8ed15fdbc4c60dcbe7931207290964ab8]*/ { 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; + PyObject *string = pattern; + Py_ssize_t start = pos; + Py_ssize_t end = endpos; string = state_init(&state, self, string, start, end); if (!string) @@ -672,21 +850,58 @@ } #endif -static PyObject* -pattern_findall(PatternObject* self, PyObject* args, PyObject* kw) +/*[clinic] +_sre.SRE_Pattern.findall + + self: self(type="PatternObject *") + source: object + pos: Py_ssize_t = 0 + endpos: Py_ssize_t = 1000000000 + +Return a list of all non-overlapping matches of pattern in string. +[clinic]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern_findall__doc__, +"findall(source, pos=0, endpos=1000000000)\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 = 1000000000; + + 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 checksum: 2e9706b1733fc4b2ffb4cc9f699a59b4d1da596d]*/ { 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; + PyObject* string = source; + Py_ssize_t start = pos; + Py_ssize_t end = endpos; string = state_init(&state, self, string, start, end); if (!string) @@ -769,14 +984,55 @@ } -static PyObject* -pattern_finditer(PatternObject* pattern, PyObject* args, PyObject* kw) +/*[clinic] +_sre.SRE_Pattern.finditer + + self: self(type="PatternObject *") + source: object + pos: Py_ssize_t = 0 + endpos: Py_ssize_t = 1000000000 + +Return an iterator over all non-overlapping matches for the RE pattern in string. For each match, the iterator returns a match object. +[clinic]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern_finditer__doc__, +"finditer(source, pos=0, endpos=1000000000)\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 = 1000000000; + + 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 checksum: 3f5acbecae1fce4fe3dd744b812ce878834749f4]*/ { PyObject* scanner; PyObject* search; PyObject* iterator; - scanner = pattern_scanner(pattern, args, kw); + scanner = pattern_scanner(self, source, pos, endpos); if (!scanner) return NULL; @@ -791,8 +1047,92 @@ return iterator; } -static PyObject* -pattern_split(PatternObject* self, PyObject* args, PyObject* kw) +/*[clinic] +_sre.SRE_Pattern.scanner + + self: self(type="PatternObject *") + source: object + pos: Py_ssize_t = 0 + endpos: Py_ssize_t = 1000000000 + +[clinic]*/ + +PyDoc_STRVAR(_sre_SRE_Pattern_scanner__doc__, +"scanner(source, pos=0, endpos=1000000000)"); + +#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 = 1000000000; + + 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 checksum: 504af5f1d4abb1be7aed2f0ea505a389921d51d7]*/ +{ + return pattern_scanner(self, source, pos, endpos); +} + +/*[clinic] +_sre.SRE_Pattern.split + + self: self(type="PatternObject *") + source: object + maxsplit: Py_ssize_t = 0 + +Split string by the occurrences of pattern. +[clinic]*/ + +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 checksum: 208015aae3c6e8bcb8e816cecb39afdac95745f8]*/ { SRE_STATE state; PyObject* list; @@ -802,12 +1142,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; + PyObject* string = source; string = state_init(&state, self, string, 0, PY_SSIZE_T_MAX); if (!string) @@ -1095,36 +1430,128 @@ } -static PyObject* -pattern_sub(PatternObject* self, PyObject* args, PyObject* kw) +/*[clinic] +_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]*/ + +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 checksum: 16b25da5bf7c5f2b34c7827ada45169a2156e1db]*/ { - PyObject* ptemplate; - PyObject* string; + return pattern_subx(self, repl, string, count, 0); +} + +/*[clinic] +_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]*/ + +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 checksum: e70f815efffc4e9991bb0d92dd4c0f4d8427a152]*/ +{ + return pattern_subx(self, repl, string, count, 1); +} + +/*[clinic] +_sre.SRE_Pattern.__copy__ + + self: self(type="PatternObject *") +[clinic]*/ + +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 checksum: 98c5cfb2670cfa79d5312aeeab50b9d581df4bc4]*/ { #ifdef USE_BUILTIN_COPY PatternObject* copy; @@ -1151,8 +1578,42 @@ #endif } -static PyObject* -pattern_deepcopy(PatternObject* self, PyObject* memo) +/*[clinic] +_sre.SRE_Pattern.__deepcopy__ + + self: self(type="PatternObject *") + memo: object +[clinic]*/ + +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 checksum: 1a02a57f69c41a0b4012fe47cea8e81aca949807]*/ { #ifdef USE_BUILTIN_COPY PatternObject* copy; @@ -1254,66 +1715,20 @@ 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}, + _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} }; @@ -1359,26 +1774,57 @@ static int _validate(PatternObject *self); /* Forward */ +/*[clinic] +_sre.compile + + pattern: object + flags: int + code: object(type='PyList_Type') + groups: Py_ssize_t = 0 + groupindex: object = NULL + indexgroup: object = NULL +[clinic]*/ + +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, &PyList_Type, &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 checksum: f72c431c04d227679a3baf9d31bdf63b5a82f9c9]*/ { /* "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); @@ -1991,13 +2437,50 @@ return match_getslice_by_index(self, match_getindex(self, index), def); } -static PyObject* -match_expand(MatchObject* self, PyObject* ptemplate) +/*[clinic] +_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]*/ + +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 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) ); } @@ -2036,16 +2519,54 @@ return result; } -static PyObject* -match_groups(MatchObject* self, PyObject* args, PyObject* kw) +/*[clinic] +_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]*/ + +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_value", 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 checksum: a096437883e4498e969eee041c63ec5cbb400e0d]*/ { 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) @@ -2064,17 +2585,55 @@ return result; } -static PyObject* -match_groupdict(MatchObject* self, PyObject* args, PyObject* kw) +/*[clinic] +_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]*/ + +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_value", 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 checksum: b42d83ff2715ef269c2d847ec13062a56320a176]*/ { 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) @@ -2234,8 +2793,34 @@ return regs; } -static PyObject* -match_copy(MatchObject* self, PyObject *unused) +/*[clinic] +_sre.SRE_Match.__copy__ + + self: self(type="MatchObject *") +[clinic]*/ + +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 checksum: 34eafc594aa5ce78145cdcb2a2a99e36f2811369]*/ { #ifdef USE_BUILTIN_COPY MatchObject* copy; @@ -2265,8 +2850,42 @@ #endif } -static PyObject* -match_deepcopy(MatchObject* self, PyObject* memo) +/*[clinic] +_sre.SRE_Match.__deepcopy__ + + self: self(type="MatchObject *") + memo: object +[clinic]*/ + +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 checksum: 6f3969601698ca74296a6d27110d6bb8d2a2bdaf]*/ { #ifdef USE_BUILTIN_COPY MatchObject* copy; @@ -2309,35 +2928,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} }; @@ -2517,8 +3117,34 @@ PyObject_DEL(self); } -static PyObject* -scanner_match(ScannerObject* self, PyObject *unused) +/*[clinic] +_sre.SRE_Scanner.match + + self: self(type="ScannerObject *") +[clinic]*/ + +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 checksum: e2cd5527a9b4a38e2f1069451185cba17c299925]*/ { SRE_STATE* state = &self->state; PyObject* match; @@ -2544,8 +3170,34 @@ } -static PyObject* -scanner_search(ScannerObject* self, PyObject *unused) +/*[clinic] +_sre.SRE_Scanner.search + + self: self(type="ScannerObject *") +[clinic]*/ + +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 checksum: 919e721c34bad29215224ec03b815c49effd88ae]*/ { SRE_STATE* state = &self->state; PyObject* match; @@ -2571,8 +3223,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} }; @@ -2614,43 +3266,39 @@ 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) { /* 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)) + ScannerObject* scanner; + + PyObject* string = source; + Py_ssize_t start = pos; + Py_ssize_t end = endpos; + + /* create scanner object */ + scanner = PyObject_NEW(ScannerObject, &Scanner_Type); + if (!scanner) 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); + scanner->pattern = NULL; + + string = state_init(&scanner->state, self, string, start, end); if (!string) { - Py_DECREF(self); + 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} };