# HG changeset patch # User MRAB # Date 1360017343 0 # Node ID 57c6563f28feeeb2cb54ecb9230d1effec51852d # Parent c4e6e560e6f5a12461c5bc9d76f3bf789b5df0ed Issue #16203: Proposal: add re.fullmatch() method diff -r c4e6e560e6f5 -r 57c6563f28fe Lib/re.py --- a/Lib/re.py Mon Feb 04 18:29:47 2013 +0200 +++ b/Lib/re.py Mon Feb 04 22:35:43 2013 +0000 @@ -85,16 +85,17 @@ \\ Matches a literal backslash. This module exports the following functions: - match Match a regular expression pattern to the beginning of a string. - search Search a string for the presence of a pattern. - sub Substitute occurrences of a pattern found in a string. - subn Same as sub, but also return the number of substitutions made. - split Split a string by the occurrences of a pattern. - findall Find all occurrences of a pattern in a string. - finditer Return an iterator yielding a match object for each match. - compile Compile a pattern into a RegexObject. - purge Clear the regular expression cache. - escape Backslash all non-alphanumerics in a string. + match Match a regular expression pattern to the beginning of a string. + fullmatch Match a regular expression pattern to all of a string. + search Search a string for the presence of a pattern. + sub Substitute occurrences of a pattern found in a string. + subn Same as sub, but also return the number of substitutions made. + split Split a string by the occurrences of a pattern. + findall Find all occurrences of a pattern in a string. + finditer Return an iterator yielding a match object for each match. + compile Compile a pattern into a RegexObject. + purge Clear the regular expression cache. + escape Backslash all non-alphanumerics in a string. Some of the functions in this module takes flags as optional parameters: A ASCII For string patterns, make \w, \W, \b, \B, \d, \D @@ -124,7 +125,7 @@ import functools # public symbols -__all__ = [ "match", "search", "sub", "subn", "split", "findall", +__all__ = [ "match", "fullmatch", "search", "sub", "subn", "split", "findall", "compile", "purge", "template", "escape", "A", "I", "L", "M", "S", "X", "U", "ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE", "UNICODE", "error" ] @@ -155,6 +156,11 @@ a match object, or None if no match was found.""" return _compile(pattern, flags).match(string) +def fullmatch(pattern, string, flags=0): + """Try to apply the pattern to all of the string, returning + a match object, or None if no match was found.""" + return _compile(pattern, flags).fullmatch(string) + def search(pattern, string, flags=0): """Scan through string looking for a match to the pattern, returning a match object, or None if no match was found.""" diff -r c4e6e560e6f5 -r 57c6563f28fe Lib/test/test_re.py --- a/Lib/test/test_re.py Mon Feb 04 18:29:47 2013 +0200 +++ b/Lib/test/test_re.py Mon Feb 04 22:35:43 2013 +0000 @@ -980,6 +980,26 @@ self.assertEqual(re.findall(r"(?i)(a)\1", "aa \u0100"), ['a']) self.assertEqual(re.match(r"(?s).{1,3}", "\u0100\u0100").span(), (0, 2)) + def test_bug_16203(self): + # Issue 16203: Proposal: add re.fullmatch() method. + self.assertEqual(re.fullmatch(r"a", "a").span(), (0, 1)) + self.assertEqual(re.fullmatch(r"a|ab", "ab").span(), (0, 2)) + self.assertEqual(re.fullmatch(r".*?$", "abc").span(), (0, 3)) + self.assertEqual(re.fullmatch(r".*?", "abc").span(), (0, 3)) + self.assertEqual(re.fullmatch(r"a.*?b", "ab").span(), (0, 2)) + self.assertEqual(re.fullmatch(r"a.*?b", "abb").span(), (0, 3)) + self.assertEqual(re.fullmatch(r"a.*?b", "axxb").span(), (0, 4)) + self.assertEqual(re.fullmatch(r"abc$", "abc\n"), None) + self.assertEqual(re.fullmatch(r"abc\Z", "abc\n"), None) + self.assertEqual(re.fullmatch(r"(?m)abc$", "abc\n"), None) + self.assertEqual(re.fullmatch(r"ab(?=c)cd", "abcd").span(), (0, 4)) + self.assertEqual(re.fullmatch(r"ab(?<=b)cd", "abcd").span(), (0, 4)) + self.assertEqual(re.fullmatch(r"(?=a|ab)ab", "ab").span(), (0, 2)) + + self.assertEqual(re.compile(r"bc").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3)) + self.assertEqual(re.compile(r".*?$").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3)) + self.assertEqual(re.compile(r".*?").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3)) + def run_re_tests(): from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR if verbose: diff -r c4e6e560e6f5 -r 57c6563f28fe Modules/_sre.c --- a/Modules/_sre.c Mon Feb 04 18:29:47 2013 +0200 +++ b/Modules/_sre.c Mon Feb 04 22:35:43 2013 +0000 @@ -4,24 +4,25 @@ * regular expression matching engine * * partial history: - * 1999-10-24 fl created (based on existing template matcher code) - * 2000-03-06 fl first alpha, sort of - * 2000-08-01 fl fixes for 1.6b1 - * 2000-08-07 fl use PyOS_CheckStack() if available - * 2000-09-20 fl added expand method - * 2001-03-20 fl lots of fixes for 2.1b2 - * 2001-04-15 fl export copyright as Python attribute, not global - * 2001-04-28 fl added __copy__ methods (work in progress) - * 2001-05-14 fl fixes for 1.5.2 compatibility - * 2001-07-01 fl added BIGCHARSET support (from Martin von Loewis) - * 2001-10-18 fl fixed group reset issue (from Matthew Mueller) - * 2001-10-20 fl added split primitive; reenable unicode for 1.6/2.0/2.1 - * 2001-10-21 fl added sub/subn primitive - * 2001-10-24 fl added finditer primitive (for 2.2 only) - * 2001-12-07 fl fixed memory leak in sub/subn (Guido van Rossum) - * 2002-11-09 fl fixed empty sub/subn return type - * 2003-04-18 mvl fully support 4-byte codes - * 2003-10-17 gn implemented non recursive scheme + * 1999-10-24 fl created (based on existing template matcher code) + * 2000-03-06 fl first alpha, sort of + * 2000-08-01 fl fixes for 1.6b1 + * 2000-08-07 fl use PyOS_CheckStack() if available + * 2000-09-20 fl added expand method + * 2001-03-20 fl lots of fixes for 2.1b2 + * 2001-04-15 fl export copyright as Python attribute, not global + * 2001-04-28 fl added __copy__ methods (work in progress) + * 2001-05-14 fl fixes for 1.5.2 compatibility + * 2001-07-01 fl added BIGCHARSET support (from Martin von Loewis) + * 2001-10-18 fl fixed group reset issue (from Matthew Mueller) + * 2001-10-20 fl added split primitive; reenable unicode for 1.6/2.0/2.1 + * 2001-10-21 fl added sub/subn primitive + * 2001-10-24 fl added finditer primitive (for 2.2 only) + * 2001-12-07 fl fixed memory leak in sub/subn (Guido van Rossum) + * 2002-11-09 fl fixed empty sub/subn return type + * 2003-04-18 mvl fully support 4-byte codes + * 2003-10-17 gn implemented non recursive scheme + * 2013-02-04 mrab added fullmatch primitive * * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved. * @@ -745,11 +746,12 @@ #define JUMP_ASSERT 12 #define JUMP_ASSERT_NOT 13 -#define DO_JUMP(jumpvalue, jumplabel, nextpattern) \ +#define DO_JUMP(jumpvalue, jumplabel, nextpattern, matchall) \ DATA_ALLOC(SRE_MATCH_CONTEXT, nextctx); \ nextctx->last_ctx_pos = ctx_pos; \ nextctx->jump = jumpvalue; \ nextctx->pattern = nextpattern; \ + nextctx->match_all = matchall; \ ctx_pos = alloc_pos; \ ctx = nextctx; \ goto entrance; \ @@ -768,6 +770,7 @@ SRE_CODE chr; SRE_REPEAT* rep; } u; + int match_all; } SRE_MATCH_CONTEXT; /* check if string matches the given pattern. returns <0 for @@ -790,6 +793,7 @@ ctx->last_ctx_pos = -1; ctx->jump = JUMP_NONE; ctx->pattern = pattern; + ctx->match_all = state->match_all; ctx_pos = alloc_pos; entrance: @@ -861,6 +865,8 @@ case SRE_OP_SUCCESS: /* end of pattern */ TRACE(("|%p|%p|SUCCESS\n", ctx->pattern, ctx->ptr)); + if (ctx->match_all && ctx->ptr != state->end) + RETURN_FAILURE; state->ptr = ctx->ptr; RETURN_SUCCESS; @@ -969,7 +975,7 @@ !SRE_CHARSET(ctx->pattern + 3, (SRE_CODE) SRE_CHARGET(state, ctx->ptr, 0)))) continue; state->ptr = ctx->ptr; - DO_JUMP(JUMP_BRANCH, jump_branch, ctx->pattern+1); + DO_JUMP(JUMP_BRANCH, jump_branch, ctx->pattern+1, ctx->match_all); if (ret) { if (ctx->u.rep) MARK_POP_DISCARD(ctx->lastmark); @@ -1016,7 +1022,8 @@ if (ctx->count < (Py_ssize_t) ctx->pattern[1]) RETURN_FAILURE; - if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS) { + if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS && + (!ctx->match_all || ctx->ptr == state->end)) { /* tail is empty. we're finished */ state->ptr = ctx->ptr; RETURN_SUCCESS; @@ -1039,7 +1046,7 @@ break; state->ptr = ctx->ptr; DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1, - ctx->pattern+ctx->pattern[0]); + ctx->pattern+ctx->pattern[0], ctx->match_all); if (ret) { RETURN_ON_ERROR(ret); RETURN_SUCCESS; @@ -1056,7 +1063,7 @@ while (ctx->count >= (Py_ssize_t) ctx->pattern[1]) { state->ptr = ctx->ptr; DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2, - ctx->pattern+ctx->pattern[0]); + ctx->pattern+ctx->pattern[0], ctx->match_all); if (ret) { RETURN_ON_ERROR(ret); RETURN_SUCCESS; @@ -1101,7 +1108,8 @@ ctx->ptr += state->charsize * ctx->count; } - if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS) { + if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS && + (!ctx->match_all || ctx->ptr == state->end)) { /* tail is empty. we're finished */ state->ptr = ctx->ptr; RETURN_SUCCESS; @@ -1113,7 +1121,7 @@ || ctx->count <= (Py_ssize_t)ctx->pattern[2]) { state->ptr = ctx->ptr; DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one, - ctx->pattern+ctx->pattern[0]); + ctx->pattern+ctx->pattern[0], ctx->match_all); if (ret) { RETURN_ON_ERROR(ret); RETURN_SUCCESS; @@ -1152,7 +1160,7 @@ state->repeat = ctx->u.rep; state->ptr = ctx->ptr; - DO_JUMP(JUMP_REPEAT, jump_repeat, ctx->pattern+ctx->pattern[0]); + DO_JUMP(JUMP_REPEAT, jump_repeat, ctx->pattern+ctx->pattern[0], ctx->match_all); state->repeat = ctx->u.rep->prev; PyObject_FREE(ctx->u.rep); @@ -1184,7 +1192,7 @@ /* not enough matches */ ctx->u.rep->count = ctx->count; DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1, - ctx->u.rep->pattern+3); + ctx->u.rep->pattern+3, ctx->match_all); if (ret) { RETURN_ON_ERROR(ret); RETURN_SUCCESS; @@ -1206,7 +1214,7 @@ DATA_PUSH(&ctx->u.rep->last_ptr); ctx->u.rep->last_ptr = state->ptr; DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2, - ctx->u.rep->pattern+3); + ctx->u.rep->pattern+3, ctx->match_all); DATA_POP(&ctx->u.rep->last_ptr); if (ret) { MARK_POP_DISCARD(ctx->lastmark); @@ -1222,7 +1230,7 @@ /* cannot match more repeated items here. make sure the tail matches */ state->repeat = ctx->u.rep->prev; - DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, ctx->pattern); + DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, ctx->pattern, ctx->match_all); RETURN_ON_SUCCESS(ret); state->repeat = ctx->u.rep; state->ptr = ctx->ptr; @@ -1247,7 +1255,7 @@ /* not enough matches */ ctx->u.rep->count = ctx->count; DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1, - ctx->u.rep->pattern+3); + ctx->u.rep->pattern+3, ctx->match_all); if (ret) { RETURN_ON_ERROR(ret); RETURN_SUCCESS; @@ -1261,7 +1269,7 @@ /* see if the tail matches */ state->repeat = ctx->u.rep->prev; - DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, ctx->pattern); + DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, ctx->pattern, ctx->match_all); if (ret) { RETURN_ON_ERROR(ret); RETURN_SUCCESS; @@ -1278,7 +1286,7 @@ ctx->u.rep->count = ctx->count; DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3, - ctx->u.rep->pattern+3); + ctx->u.rep->pattern+3, ctx->match_all); if (ret) { RETURN_ON_ERROR(ret); RETURN_SUCCESS; @@ -1370,7 +1378,7 @@ state->ptr = ctx->ptr - state->charsize * ctx->pattern[1]; if (state->ptr < state->beginning) RETURN_FAILURE; - DO_JUMP(JUMP_ASSERT, jump_assert, ctx->pattern+2); + DO_JUMP(JUMP_ASSERT, jump_assert, ctx->pattern+2, 0); RETURN_ON_FAILURE(ret); ctx->pattern += ctx->pattern[0]; break; @@ -1382,7 +1390,7 @@ ctx->ptr, ctx->pattern[1])); state->ptr = ctx->ptr - state->charsize * ctx->pattern[1]; if (state->ptr >= state->beginning) { - DO_JUMP(JUMP_ASSERT_NOT, jump_assert_not, ctx->pattern+2); + DO_JUMP(JUMP_ASSERT_NOT, jump_assert_not, ctx->pattern+2, 0); if (ret) { RETURN_ON_ERROR(ret); RETURN_FAILURE; @@ -1900,6 +1908,44 @@ } static PyObject* +pattern_fullmatch(PatternObject* self, PyObject* args, PyObject* kw) +{ + 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; + + string = state_init(&state, self, string, start, end); + if (!string) + return NULL; + + state.match_all = 1; + state.ptr = state.start; + + TRACE(("|%p|%p|MATCH\n", PatternObject_GetCode(self), state.ptr)); + + if (state.logical_charsize == 1) { + status = sre_match(&state, PatternObject_GetCode(self)); + } else { + status = sre_umatch(&state, PatternObject_GetCode(self)); + } + + TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr)); + if (PyErr_Occurred()) + return NULL; + + state_fini(&state); + + return pattern_new_match(self, &state, status); +} + +static PyObject* pattern_search(PatternObject* self, PyObject* args, PyObject* kw) { SRE_STATE state; @@ -2531,6 +2577,10 @@ "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\ @@ -2566,6 +2616,8 @@ 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, diff -r c4e6e560e6f5 -r 57c6563f28fe Modules/sre.h --- a/Modules/sre.h Mon Feb 04 18:29:47 2013 +0200 +++ b/Modules/sre.h Mon Feb 04 22:35:43 2013 +0000 @@ -84,6 +84,7 @@ SRE_REPEAT *repeat; /* hooks */ SRE_TOLOWER_HOOK lower; + int match_all; } SRE_STATE; typedef struct {