diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 41ee35958b77314..3ef806d7830e379 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -65,6 +65,33 @@ def checkTemplateError(self, pattern, repl, string, errmsg, pos=None): if pos is not None: self.assertEqual(err.pos, pos) + def test_repeated_ignorecase_set_context(self): + # Case-insensitive sets use the recursive count fallback. Exercise + # its return to the initial context and to nested contexts after the + # data stack has grown, with greedy, lazy and possessive repeats. + for suffix in ('', '?', '+'): + for tail in ('c', '\u0100', '\U00010000'): + source = '(?i:[ab])+' + suffix + tail + unit = 'aB' + tail + nested_source = '(' + source + ')+d' + text = unit * 100 + 'd' + for isbytes in (False, True): + with self.subTest(suffix=suffix, tail=tail, isbytes=isbytes): + if isbytes: + p, s = source.encode(), unit.encode() + nested, long_s = nested_source.encode(), text.encode() + else: + p, s = source, unit + nested, long_s = nested_source, text + self.assertEqual(re.fullmatch(p, s).group(), s) + m = re.fullmatch(nested, long_s) + self.assertEqual(m.group(1), s) + self.assertEqual(m.span(1), (99 * len(s), 100 * len(s))) + scanner = re.compile(p).scanner(s * 2) + self.assertEqual(scanner.search().span(), (0, len(s))) + self.assertEqual(scanner.search().span(), (len(s), 2 * len(s))) + self.assertIsNone(scanner.search()) + def test_error_is_PatternError_alias(self): assert re.error is re.PatternError diff --git a/Misc/NEWS.d/next/Library/2026-11-15-17-31-56.gh-issue-162056.SreCtx.rst b/Misc/NEWS.d/next/Library/2026-11-15-17-31-56.gh-issue-162056.SreCtx.rst new file mode 100644 index 000000000000000..0981bc605407ba9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-15-17-31-56.gh-issue-162056.SreCtx.rst @@ -0,0 +1,3 @@ +Avoid allocating the initial match context on the data stack in :mod:`re`. +Matches that do not need nested contexts or saved marks can avoid allocating +that stack; backtracking continues to use the existing resizable data stack. diff --git a/Modules/_sre/sre_lib.h b/Modules/_sre/sre_lib.h index fbdbaefb399193d..309246d0594013e 100644 --- a/Modules/_sre/sre_lib.h +++ b/Modules/_sre/sre_lib.h @@ -466,7 +466,7 @@ do { \ #define DATA_STACK_LOOKUP_AT(state, type, ptr, pos) \ do { \ TRACE(("looking up %s at %zd\n", Py_STRINGIFY(type), pos)); \ - ptr = (type*)(state->data_stack+pos); \ + ptr = (pos) == -1 ? &initial_ctx : (type*)(state->data_stack+pos); \ } while (0) #define DATA_STACK_PUSH(state, data, size) \ @@ -667,17 +667,18 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) int jump; unsigned int sigcount = state->sigcount; - SRE(match_context)* ctx; + /* Keep the initial context on the C stack. Only nested contexts and + saved marks need the resizable data stack. */ + SRE(match_context) initial_ctx; + SRE(match_context)* ctx = &initial_ctx; SRE(match_context)* nextctx; INIT_TRACE(state); TRACE(("|%p|%p|ENTER\n", pattern, state->ptr)); - DATA_ALLOC(SRE(match_context), ctx); ctx->last_ctx_pos = -1; ctx->jump = JUMP_NONE; ctx->toplevel = toplevel; - ctx_pos = alloc_pos; #if USE_COMPUTED_GOTOS #include "sre_targets.h" @@ -1678,13 +1679,13 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) } exit: - ctx_pos = ctx->last_ctx_pos; - jump = ctx->jump; - DATA_POP_DISCARD(ctx); if (ctx_pos == -1) { state->sigcount = sigcount; return ret; } + ctx_pos = ctx->last_ctx_pos; + jump = ctx->jump; + DATA_POP_DISCARD(ctx); DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos); switch (jump) {