]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-109631: Allow interruption of short repeated regex matches (GH-109867)
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 26 Sep 2023 07:56:33 +0000 (10:56 +0300)
committerGitHub <noreply@github.com>
Tue, 26 Sep 2023 07:56:33 +0000 (10:56 +0300)
Counting for signal checking now continues in new match from the point where
it ended in the previous match instead of starting from 0.

Misc/NEWS.d/next/Library/2023-09-25-23-00-37.gh-issue-109631.eWSqpO.rst [new file with mode: 0644]
Modules/_sre/sre.h
Modules/_sre/sre_lib.h

diff --git a/Misc/NEWS.d/next/Library/2023-09-25-23-00-37.gh-issue-109631.eWSqpO.rst b/Misc/NEWS.d/next/Library/2023-09-25-23-00-37.gh-issue-109631.eWSqpO.rst
new file mode 100644 (file)
index 0000000..58af2e5
--- /dev/null
@@ -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.
index f60078d6bb999bee12e46be101c2035741984176..83d89d57b111997ee6163d0201084812c7638cf2 100644 (file)
@@ -95,6 +95,7 @@ typedef struct {
     size_t data_stack_base;
     /* current repeat context */
     SRE_REPEAT *repeat;
+    unsigned int sigcount;
 } SRE_STATE;
 
 typedef struct {
index ae80009fd63bbebedb3dce31d99d3555c22339cd..3c805aeeca09743ab41b7e4fc50360fbbb914e76 100644 (file)
@@ -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;
@@ -1567,8 +1567,10 @@ exit:
     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) {