Skip to content

Commit

Permalink
[3.11] gh-109631: Allow interruption of short repeated regex matches (G…
Browse files Browse the repository at this point in the history
…H-109867) (GH-109885)

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 <storchaka@gmail.com>
  • Loading branch information
miss-islington and serhiy-storchaka committed Sep 26, 2023
1 parent 3ab0621 commit 97f7e9d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions Modules/_sre/sre.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ typedef struct {
size_t data_stack_base;
/* current repeat context */
SRE_REPEAT *repeat;
unsigned int sigcount;
} SRE_STATE;

typedef struct {
Expand Down
6 changes: 4 additions & 2 deletions Modules/_sre/sre_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ret = 0;
int jump;
unsigned int sigcount=0;
unsigned int sigcount = state->sigcount;

SRE(match_context)* ctx;
SRE(match_context)* nextctx;
Expand Down Expand Up @@ -1565,8 +1565,10 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
ctx_pos = ctx->last_ctx_pos;
jump = ctx->jump;
DATA_POP_DISCARD(ctx);
if (ctx_pos == -1)
if (ctx_pos == -1) {
state->sigcount = sigcount;
return ret;
}
DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos);

switch (jump) {
Expand Down

0 comments on commit 97f7e9d

Please sign in to comment.