Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 429b59e

Browse files
Issue #20998: Fixed re.fullmatch() of repeated single character pattern
with ignore case. Original patch by Matthew Barnett.
1 parent 946cfc3 commit 429b59e

5 files changed

Lines changed: 25 additions & 19 deletions

File tree

Lib/test/test_re.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,11 @@ def test_keyword_parameters(self):
12231223
pat.scanner(string='abracadabra', pos=3, endpos=10).search().span(),
12241224
(7, 9))
12251225

1226+
def test_bug_20998(self):
1227+
# Issue #20998: Fullmatch of repeated single character pattern
1228+
# with ignore case.
1229+
self.assertEqual(re.fullmatch('[a-c]+', 'ABC', re.I).span(), (0, 3))
1230+
12261231

12271232
class PatternReprTests(unittest.TestCase):
12281233
def check(self, pattern, expected):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Core and Builtins
2323
Library
2424
-------
2525

26+
- Issue #20998: Fixed re.fullmatch() of repeated single character pattern
27+
with ignore case. Original patch by Matthew Barnett.
28+
2629
- Issue #21075: fileinput.FileInput now reads bytes from standard stream if
2730
binary mode is specified. Patch by Sam Kimbrel.
2831

Modules/_sre.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,14 @@ pattern_dealloc(PatternObject* self)
505505
}
506506

507507
LOCAL(Py_ssize_t)
508-
sre_match(SRE_STATE* state, SRE_CODE* pattern)
508+
sre_match(SRE_STATE* state, SRE_CODE* pattern, int match_all)
509509
{
510510
if (state->charsize == 1)
511-
return sre_ucs1_match(state, pattern);
511+
return sre_ucs1_match(state, pattern, match_all);
512512
if (state->charsize == 2)
513-
return sre_ucs2_match(state, pattern);
513+
return sre_ucs2_match(state, pattern, match_all);
514514
assert(state->charsize == 4);
515-
return sre_ucs4_match(state, pattern);
515+
return sre_ucs4_match(state, pattern, match_all);
516516
}
517517

518518
LOCAL(Py_ssize_t)
@@ -576,7 +576,7 @@ pattern_match(PatternObject *self, PyObject *args, PyObject *kwargs)
576576

577577
TRACE(("|%p|%p|MATCH\n", PatternObject_GetCode(self), state.ptr));
578578

579-
status = sre_match(&state, PatternObject_GetCode(self));
579+
status = sre_match(&state, PatternObject_GetCode(self), 0);
580580

581581
TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr));
582582
if (PyErr_Occurred())
@@ -609,12 +609,11 @@ pattern_fullmatch(PatternObject* self, PyObject* args, PyObject* kw)
609609
if (!string)
610610
return NULL;
611611

612-
state.match_all = 1;
613612
state.ptr = state.start;
614613

615614
TRACE(("|%p|%p|FULLMATCH\n", PatternObject_GetCode(self), state.ptr));
616615

617-
status = sre_match(&state, PatternObject_GetCode(self));
616+
status = sre_match(&state, PatternObject_GetCode(self), 1);
618617

619618
TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr));
620619
if (PyErr_Occurred())
@@ -2572,7 +2571,7 @@ scanner_match(ScannerObject* self, PyObject *unused)
25722571

25732572
state->ptr = state->start;
25742573

2575-
status = sre_match(state, PatternObject_GetCode(self->pattern));
2574+
status = sre_match(state, PatternObject_GetCode(self->pattern), 0);
25762575
if (PyErr_Occurred())
25772576
return NULL;
25782577

Modules/sre.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ typedef struct {
8686
SRE_REPEAT *repeat;
8787
/* hooks */
8888
SRE_TOLOWER_HOOK lower;
89-
int match_all;
9089
} SRE_STATE;
9190

9291
typedef struct {

Modules/sre_lib.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ SRE(charset)(SRE_CODE* set, SRE_CODE ch)
173173
}
174174
}
175175

176-
LOCAL(Py_ssize_t) SRE(match)(SRE_STATE* state, SRE_CODE* pattern);
176+
LOCAL(Py_ssize_t) SRE(match)(SRE_STATE* state, SRE_CODE* pattern, int match_all);
177177

178178
LOCAL(Py_ssize_t)
179179
SRE(count)(SRE_STATE* state, SRE_CODE* pattern, Py_ssize_t maxcount)
@@ -259,7 +259,7 @@ SRE(count)(SRE_STATE* state, SRE_CODE* pattern, Py_ssize_t maxcount)
259259
/* repeated single character pattern */
260260
TRACE(("|%p|%p|COUNT SUBPATTERN\n", pattern, ptr));
261261
while ((SRE_CHAR*) state->ptr < end) {
262-
i = SRE(match)(state, pattern);
262+
i = SRE(match)(state, pattern, 0);
263263
if (i < 0)
264264
return i;
265265
if (!i)
@@ -490,7 +490,7 @@ typedef struct {
490490
/* check if string matches the given pattern. returns <0 for
491491
error, 0 for failure, and 1 for success */
492492
LOCAL(Py_ssize_t)
493-
SRE(match)(SRE_STATE* state, SRE_CODE* pattern)
493+
SRE(match)(SRE_STATE* state, SRE_CODE* pattern, int match_all)
494494
{
495495
SRE_CHAR* end = (SRE_CHAR *)state->end;
496496
Py_ssize_t alloc_pos, ctx_pos = -1;
@@ -507,7 +507,7 @@ SRE(match)(SRE_STATE* state, SRE_CODE* pattern)
507507
ctx->last_ctx_pos = -1;
508508
ctx->jump = JUMP_NONE;
509509
ctx->pattern = pattern;
510-
ctx->match_all = state->match_all;
510+
ctx->match_all = match_all;
511511
ctx_pos = alloc_pos;
512512

513513
entrance:
@@ -739,7 +739,7 @@ SRE(match)(SRE_STATE* state, SRE_CODE* pattern)
739739
RETURN_FAILURE;
740740

741741
if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS &&
742-
(!ctx->match_all || ctx->ptr == state->end)) {
742+
ctx->ptr == state->end) {
743743
/* tail is empty. we're finished */
744744
state->ptr = ctx->ptr;
745745
RETURN_SUCCESS;
@@ -824,7 +824,7 @@ SRE(match)(SRE_STATE* state, SRE_CODE* pattern)
824824
}
825825

826826
if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS &&
827-
(!ctx->match_all || ctx->ptr == state->end)) {
827+
(!match_all || ctx->ptr == state->end)) {
828828
/* tail is empty. we're finished */
829829
state->ptr = ctx->ptr;
830830
RETURN_SUCCESS;
@@ -1269,7 +1269,7 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
12691269
state->ptr = ptr - (prefix_len - prefix_skip - 1);
12701270
if (flags & SRE_INFO_LITERAL)
12711271
return 1; /* we got all of it */
1272-
status = SRE(match)(state, pattern + 2*prefix_skip);
1272+
status = SRE(match)(state, pattern + 2*prefix_skip, 0);
12731273
if (status != 0)
12741274
return status;
12751275
/* close but no cigar -- try again */
@@ -1302,7 +1302,7 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
13021302
state->ptr = ++ptr;
13031303
if (flags & SRE_INFO_LITERAL)
13041304
return 1; /* we got all of it */
1305-
status = SRE(match)(state, pattern + 2);
1305+
status = SRE(match)(state, pattern + 2, 0);
13061306
if (status != 0)
13071307
break;
13081308
}
@@ -1317,7 +1317,7 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
13171317
TRACE(("|%p|%p|SEARCH CHARSET\n", pattern, ptr));
13181318
state->start = ptr;
13191319
state->ptr = ptr;
1320-
status = SRE(match)(state, pattern);
1320+
status = SRE(match)(state, pattern, 0);
13211321
if (status != 0)
13221322
break;
13231323
ptr++;
@@ -1327,7 +1327,7 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
13271327
while (ptr <= end) {
13281328
TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
13291329
state->start = state->ptr = ptr++;
1330-
status = SRE(match)(state, pattern);
1330+
status = SRE(match)(state, pattern, 0);
13311331
if (status != 0)
13321332
break;
13331333
}

0 commit comments

Comments
 (0)