Skip to content

Commit 573eff3

Browse files
Output more details in the re tracing (GH-111357)
1 parent 31c05b7 commit 573eff3

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

Modules/_sre/sre.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,19 @@ static unsigned int sre_toupper(unsigned int ch) {
107107

108108
#if VERBOSE == 0
109109
# define INIT_TRACE(state)
110+
# define DO_TRACE 0
110111
# define TRACE(v)
111112
#elif VERBOSE == 1
112113
# define INIT_TRACE(state) int _debug = (state)->debug
114+
# define DO_TRACE (_debug)
113115
# define TRACE(v) do { \
114116
if (_debug) { \
115117
printf v; \
116118
} \
117119
} while (0)
118120
#elif VERBOSE == 2
119121
# define INIT_TRACE(state)
122+
# define DO_TRACE 1
120123
# define TRACE(v) printf v
121124
#else
122125
# error VERBOSE must be 0, 1 or 2

Modules/_sre/sre_lib.h

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,19 @@ SRE(count)(SRE_STATE* state, const SRE_CODE* pattern, Py_ssize_t maxcount)
373373
state->lastindex = ctx->lastindex; \
374374
} while (0)
375375

376+
#define LAST_PTR_PUSH() \
377+
do { \
378+
TRACE(("push last_ptr: %zd", \
379+
PTR_TO_INDEX(ctx->u.rep->last_ptr))); \
380+
DATA_PUSH(&ctx->u.rep->last_ptr); \
381+
} while (0)
382+
#define LAST_PTR_POP() \
383+
do { \
384+
DATA_POP(&ctx->u.rep->last_ptr); \
385+
TRACE(("pop last_ptr: %zd", \
386+
PTR_TO_INDEX(ctx->u.rep->last_ptr))); \
387+
} while (0)
388+
376389
#define RETURN_ERROR(i) do { return i; } while(0)
377390
#define RETURN_FAILURE do { ret = 0; goto exit; } while(0)
378391
#define RETURN_SUCCESS do { ret = 1; goto exit; } while(0)
@@ -449,25 +462,47 @@ do { \
449462
#define DATA_LOOKUP_AT(t,p,pos) \
450463
DATA_STACK_LOOKUP_AT(state,t,p,pos)
451464

465+
#define PTR_TO_INDEX(ptr) \
466+
((ptr) ? ((char*)(ptr) - (char*)state->beginning) / state->charsize : -1)
467+
468+
#if VERBOSE
469+
# define MARK_TRACE(label, lastmark) \
470+
do if (DO_TRACE) { \
471+
TRACE(("%s %d marks:", (label), (lastmark)+1)); \
472+
for (int j = 0; j <= (lastmark); j++) { \
473+
if (j && (j & 1) == 0) { \
474+
TRACE((" ")); \
475+
} \
476+
TRACE((" %zd", PTR_TO_INDEX(state->mark[j]))); \
477+
} \
478+
TRACE(("\n")); \
479+
} while (0)
480+
#else
481+
# define MARK_TRACE(label, lastmark)
482+
#endif
452483
#define MARK_PUSH(lastmark) \
453484
do if (lastmark >= 0) { \
485+
MARK_TRACE("push", (lastmark)); \
454486
size_t _marks_size = (lastmark+1) * sizeof(void*); \
455487
DATA_STACK_PUSH(state, state->mark, _marks_size); \
456488
} while (0)
457489
#define MARK_POP(lastmark) \
458490
do if (lastmark >= 0) { \
459491
size_t _marks_size = (lastmark+1) * sizeof(void*); \
460492
DATA_STACK_POP(state, state->mark, _marks_size, 1); \
493+
MARK_TRACE("pop", (lastmark)); \
461494
} while (0)
462495
#define MARK_POP_KEEP(lastmark) \
463496
do if (lastmark >= 0) { \
464497
size_t _marks_size = (lastmark+1) * sizeof(void*); \
465498
DATA_STACK_POP(state, state->mark, _marks_size, 0); \
499+
MARK_TRACE("pop keep", (lastmark)); \
466500
} while (0)
467501
#define MARK_POP_DISCARD(lastmark) \
468502
do if (lastmark >= 0) { \
469503
size_t _marks_size = (lastmark+1) * sizeof(void*); \
470504
DATA_STACK_POP_DISCARD(state, _marks_size); \
505+
MARK_TRACE("pop discard", (lastmark)); \
471506
} while (0)
472507

473508
#define JUMP_NONE 0
@@ -1150,11 +1185,11 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
11501185
LASTMARK_SAVE();
11511186
MARK_PUSH(ctx->lastmark);
11521187
/* zero-width match protection */
1153-
DATA_PUSH(&ctx->u.rep->last_ptr);
1188+
LAST_PTR_PUSH();
11541189
ctx->u.rep->last_ptr = state->ptr;
11551190
DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2,
11561191
ctx->u.rep->pattern+3);
1157-
DATA_POP(&ctx->u.rep->last_ptr);
1192+
LAST_PTR_POP();
11581193
if (ret) {
11591194
MARK_POP_DISCARD(ctx->lastmark);
11601195
RETURN_ON_ERROR(ret);
@@ -1235,11 +1270,11 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
12351270

12361271
ctx->u.rep->count = ctx->count;
12371272
/* zero-width match protection */
1238-
DATA_PUSH(&ctx->u.rep->last_ptr);
1273+
LAST_PTR_PUSH();
12391274
ctx->u.rep->last_ptr = state->ptr;
12401275
DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
12411276
ctx->u.rep->pattern+3);
1242-
DATA_POP(&ctx->u.rep->last_ptr);
1277+
LAST_PTR_POP();
12431278
if (ret) {
12441279
RETURN_ON_ERROR(ret);
12451280
RETURN_SUCCESS;

0 commit comments

Comments
 (0)