[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 8ac2085b80)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2023-10-02 08:36:29 -07:00 committed by GitHub
parent d09a3145b5
commit 65c845e065
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 2 deletions

View file

@ -0,0 +1,3 @@
:mod:`re` functions such as :func:`re.findall`, :func:`re.split`,
:func:`re.search` and :func:`re.sub` which perform short repeated matches
can now be interrupted by user.

View file

@ -94,6 +94,7 @@ typedef struct {
size_t data_stack_base; size_t data_stack_base;
/* current repeat context */ /* current repeat context */
SRE_REPEAT *repeat; SRE_REPEAT *repeat;
unsigned int sigcount;
} SRE_STATE; } SRE_STATE;
typedef struct { typedef struct {

View file

@ -563,7 +563,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
Py_ssize_t alloc_pos, ctx_pos = -1; Py_ssize_t alloc_pos, ctx_pos = -1;
Py_ssize_t ret = 0; Py_ssize_t ret = 0;
int jump; int jump;
unsigned int sigcount=0; unsigned int sigcount = state->sigcount;
SRE(match_context)* ctx; SRE(match_context)* ctx;
SRE(match_context)* nextctx; SRE(match_context)* nextctx;
@ -1565,8 +1565,10 @@ exit:
ctx_pos = ctx->last_ctx_pos; ctx_pos = ctx->last_ctx_pos;
jump = ctx->jump; jump = ctx->jump;
DATA_POP_DISCARD(ctx); DATA_POP_DISCARD(ctx);
if (ctx_pos == -1) if (ctx_pos == -1) {
state->sigcount = sigcount;
return ret; return ret;
}
DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos); DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);
switch (jump) { switch (jump) {