Skip to content
Open
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
27 changes: 27 additions & 0 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 8 additions & 7 deletions Modules/_sre/sre_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
Loading