Skip to content

[3.11] gh-109631: Allow interruption of short repeated regex matches (GH-109867) #109885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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