From 33881d8ceb6cb525066c3f1fcbc707b589a96276 Mon Sep 17 00:00:00 2001 From: Nityanand Mathur Date: Sun, 15 Nov 2026 22:28:26 +0530 Subject: [PATCH 1/6] Keep initial SRE match context on the C stack [skip ci] --- Modules/_sre/sre_lib.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Modules/_sre/sre_lib.h b/Modules/_sre/sre_lib.h index fbdbaefb399193d..0c5296a3b43ec65 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) { @@ -1933,3 +1934,4 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern) /* vim:ts=4:sw=4:et */ + From 2e17aa0e7623d929bf5393332d4dc6db49b213a8 Mon Sep 17 00:00:00 2001 From: Nityanand Mathur Date: Sun, 15 Nov 2026 22:28:42 +0530 Subject: [PATCH 2/6] Test initial and nested SRE match-context restoration [skip ci] --- Lib/test/test_re.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 41ee35958b77314..7bb1f5ad4dfe54f 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 @@ -3428,3 +3455,4 @@ def test_deprecated_module_attributes(self): if __name__ == "__main__": unittest.main() + From f56be050d983412c0e67e7b4dba84299ba3e2893 Mon Sep 17 00:00:00 2001 From: Nityanand Mathur Date: Sun, 15 Nov 2026 22:29:09 +0530 Subject: [PATCH 3/6] Preserve existing end-of-file whitespace [skip ci] --- Modules/_sre/sre_lib.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Modules/_sre/sre_lib.h b/Modules/_sre/sre_lib.h index 0c5296a3b43ec65..309246d0594013e 100644 --- a/Modules/_sre/sre_lib.h +++ b/Modules/_sre/sre_lib.h @@ -1934,4 +1934,3 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern) /* vim:ts=4:sw=4:et */ - From e402550304942a4528253f6ac40f31ef37eb6c80 Mon Sep 17 00:00:00 2001 From: Nityanand Mathur Date: Sun, 15 Nov 2026 22:29:31 +0530 Subject: [PATCH 4/6] Preserve existing end-of-file whitespace [skip ci] --- Lib/test/test_re.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 7bb1f5ad4dfe54f..3ef806d7830e379 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -3455,4 +3455,3 @@ def test_deprecated_module_attributes(self): if __name__ == "__main__": unittest.main() - From 9a61bcf74f3ea5f2b29c178d24eb1adf1a5ad9da Mon Sep 17 00:00:00 2001 From: Nityanand Mathur Date: Sun, 15 Nov 2026 23:02:26 +0530 Subject: [PATCH 5/6] gh-162056: Add NEWS entry for initial SRE context allocation [skip ci] --- .../Library/2026-11-15-17-31-56.gh-issue-162056.SreCtx.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-15-17-31-56.gh-issue-162056.SreCtx.rst 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. From 87262136fe657516f262dcc3d52f88f7f068a62d Mon Sep 17 00:00:00 2001 From: Nityanand Mathur Date: Sun, 15 Nov 2026 23:13:34 +0530 Subject: [PATCH 6/6] gh-162056: Enable upstream pull request validation No source changes. Allow the normal upstream CI checks to validate this proposal after the initial commits intentionally skipped CI.