Skip to content

Commit 65c845e

Browse files
[3.12] gh-109631: Allow interruption of short repeated regex matches (GH-109867) (#109886)
gh-109631: Allow interruption of short repeated regex matches (GH-109867) Counting for signal checking now continues in new match from the point where it ended in the previous match instead of starting from 0. (cherry picked from commit 8ac2085) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent d09a314 commit 65c845e

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:mod:`re` functions such as :func:`re.findall`, :func:`re.split`,
2+
:func:`re.search` and :func:`re.sub` which perform short repeated matches
3+
can now be interrupted by user.

Modules/_sre/sre.h

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ typedef struct {
9494
size_t data_stack_base;
9595
/* current repeat context */
9696
SRE_REPEAT *repeat;
97+
unsigned int sigcount;
9798
} SRE_STATE;
9899

99100
typedef struct {

Modules/_sre/sre_lib.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
563563
Py_ssize_t alloc_pos, ctx_pos = -1;
564564
Py_ssize_t ret = 0;
565565
int jump;
566-
unsigned int sigcount=0;
566+
unsigned int sigcount = state->sigcount;
567567

568568
SRE(match_context)* ctx;
569569
SRE(match_context)* nextctx;
@@ -1565,8 +1565,10 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
15651565
ctx_pos = ctx->last_ctx_pos;
15661566
jump = ctx->jump;
15671567
DATA_POP_DISCARD(ctx);
1568-
if (ctx_pos == -1)
1568+
if (ctx_pos == -1) {
1569+
state->sigcount = sigcount;
15691570
return ret;
1571+
}
15701572
DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
15711573

15721574
switch (jump) {

0 commit comments

Comments
 (0)