Skip to content

Commit

Permalink
pythongh-109631: Allow interruption of short repeated regex matches
Browse files Browse the repository at this point in the history
Counting for signal checking now continues in new match from the point where
it ended in the previous match instead of starting from 0.
  • Loading branch information
serhiy-storchaka committed Sep 25, 2023
1 parent d73c12b commit cb45bd2
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 @@ -95,6 +95,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 @@ -564,7 +564,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 @@ -1567,8 +1567,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 cb45bd2

Please sign in to comment.