Skip to content

Commit b305c69

Browse files
gh-106508: Improve debugging of the _sre module (GH-106509)
Now the VERBOSE macro can control tracing on per-pattern basis: * 0 -- disabled * 1 -- only if the DEBUG flag set * 2 -- always
1 parent 74ec02e commit b305c69

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

Modules/_sre/sre.c

+27-5
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ static const char copyright[] =
4949

5050
#include <ctype.h>
5151

52-
/* defining this one enables tracing */
53-
#undef VERBOSE
52+
/* Defining this one controls tracing:
53+
* 0 -- disabled
54+
* 1 -- only if the DEBUG flag set
55+
* 2 -- always
56+
*/
57+
#ifndef VERBOSE
58+
# define VERBOSE 0
59+
#endif
5460

5561
/* -------------------------------------------------------------------- */
5662

@@ -70,10 +76,21 @@ static const char copyright[] =
7076
#define SRE_ERROR_MEMORY -9 /* out of memory */
7177
#define SRE_ERROR_INTERRUPTED -10 /* signal handler raised exception */
7278

73-
#if defined(VERBOSE)
74-
#define TRACE(v) printf v
79+
#if VERBOSE == 0
80+
# define INIT_TRACE(state)
81+
# define TRACE(v)
82+
#elif VERBOSE == 1
83+
# define INIT_TRACE(state) int _debug = (state)->debug
84+
# define TRACE(v) do { \
85+
if (_debug) { \
86+
printf v; \
87+
} \
88+
} while (0)
89+
#elif VERBOSE == 2
90+
# define INIT_TRACE(state)
91+
# define TRACE(v) printf v
7592
#else
76-
#define TRACE(v)
93+
# error VERBOSE must be 0, 1 or 2
7794
#endif
7895

7996
/* -------------------------------------------------------------------- */
@@ -198,6 +215,7 @@ data_stack_dealloc(SRE_STATE* state)
198215
static int
199216
data_stack_grow(SRE_STATE* state, Py_ssize_t size)
200217
{
218+
INIT_TRACE(state);
201219
Py_ssize_t minsize, cursize;
202220
minsize = state->data_stack_base+size;
203221
cursize = state->data_stack_size;
@@ -449,6 +467,7 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
449467
state->charsize = charsize;
450468
state->match_all = 0;
451469
state->must_advance = 0;
470+
state->debug = ((pattern->flags & SRE_FLAG_DEBUG) != 0);
452471

453472
state->beginning = ptr;
454473

@@ -641,6 +660,7 @@ _sre_SRE_Pattern_match_impl(PatternObject *self, PyTypeObject *cls,
641660
if (!state_init(&state, (PatternObject *)self, string, pos, endpos))
642661
return NULL;
643662

663+
INIT_TRACE(&state);
644664
state.ptr = state.start;
645665

646666
TRACE(("|%p|%p|MATCH\n", PatternObject_GetCode(self), state.ptr));
@@ -684,6 +704,7 @@ _sre_SRE_Pattern_fullmatch_impl(PatternObject *self, PyTypeObject *cls,
684704
if (!state_init(&state, self, string, pos, endpos))
685705
return NULL;
686706

707+
INIT_TRACE(&state);
687708
state.ptr = state.start;
688709

689710
TRACE(("|%p|%p|FULLMATCH\n", PatternObject_GetCode(self), state.ptr));
@@ -730,6 +751,7 @@ _sre_SRE_Pattern_search_impl(PatternObject *self, PyTypeObject *cls,
730751
if (!state_init(&state, self, string, pos, endpos))
731752
return NULL;
732753

754+
INIT_TRACE(&state);
733755
TRACE(("|%p|%p|SEARCH\n", PatternObject_GetCode(self), state.ptr));
734756

735757
status = sre_search(&state, PatternObject_GetCode(self));

Modules/_sre/sre.h

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ typedef struct {
8484
int charsize; /* character size */
8585
int match_all;
8686
int must_advance;
87+
int debug;
8788
/* marks */
8889
int lastmark;
8990
int lastindex;

Modules/_sre/sre_lib.h

+3
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ SRE(count)(SRE_STATE* state, const SRE_CODE* pattern, Py_ssize_t maxcount)
209209
const SRE_CHAR* ptr = (const SRE_CHAR *)state->ptr;
210210
const SRE_CHAR* end = (const SRE_CHAR *)state->end;
211211
Py_ssize_t i;
212+
INIT_TRACE(state);
212213

213214
/* adjust end */
214215
if (maxcount < end - ptr && maxcount != SRE_MAXREPEAT)
@@ -567,6 +568,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
567568

568569
SRE(match_context)* ctx;
569570
SRE(match_context)* nextctx;
571+
INIT_TRACE(state);
570572

571573
TRACE(("|%p|%p|ENTER\n", pattern, state->ptr));
572574

@@ -1639,6 +1641,7 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern)
16391641
SRE_CODE* charset = NULL;
16401642
SRE_CODE* overlap = NULL;
16411643
int flags = 0;
1644+
INIT_TRACE(state);
16421645

16431646
if (ptr > end)
16441647
return 0;

0 commit comments

Comments
 (0)