@@ -49,8 +49,14 @@ static const char copyright[] =
49
49
50
50
#include <ctype.h>
51
51
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
54
60
55
61
/* -------------------------------------------------------------------- */
56
62
@@ -70,10 +76,21 @@ static const char copyright[] =
70
76
#define SRE_ERROR_MEMORY -9 /* out of memory */
71
77
#define SRE_ERROR_INTERRUPTED -10 /* signal handler raised exception */
72
78
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
75
92
#else
76
- #define TRACE ( v )
93
+ # error VERBOSE must be 0, 1 or 2
77
94
#endif
78
95
79
96
/* -------------------------------------------------------------------- */
@@ -198,6 +215,7 @@ data_stack_dealloc(SRE_STATE* state)
198
215
static int
199
216
data_stack_grow (SRE_STATE * state , Py_ssize_t size )
200
217
{
218
+ INIT_TRACE (state );
201
219
Py_ssize_t minsize , cursize ;
202
220
minsize = state -> data_stack_base + size ;
203
221
cursize = state -> data_stack_size ;
@@ -449,6 +467,7 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
449
467
state -> charsize = charsize ;
450
468
state -> match_all = 0 ;
451
469
state -> must_advance = 0 ;
470
+ state -> debug = ((pattern -> flags & SRE_FLAG_DEBUG ) != 0 );
452
471
453
472
state -> beginning = ptr ;
454
473
@@ -641,6 +660,7 @@ _sre_SRE_Pattern_match_impl(PatternObject *self, PyTypeObject *cls,
641
660
if (!state_init (& state , (PatternObject * )self , string , pos , endpos ))
642
661
return NULL ;
643
662
663
+ INIT_TRACE (& state );
644
664
state .ptr = state .start ;
645
665
646
666
TRACE (("|%p|%p|MATCH\n" , PatternObject_GetCode (self ), state .ptr ));
@@ -684,6 +704,7 @@ _sre_SRE_Pattern_fullmatch_impl(PatternObject *self, PyTypeObject *cls,
684
704
if (!state_init (& state , self , string , pos , endpos ))
685
705
return NULL ;
686
706
707
+ INIT_TRACE (& state );
687
708
state .ptr = state .start ;
688
709
689
710
TRACE (("|%p|%p|FULLMATCH\n" , PatternObject_GetCode (self ), state .ptr ));
@@ -730,6 +751,7 @@ _sre_SRE_Pattern_search_impl(PatternObject *self, PyTypeObject *cls,
730
751
if (!state_init (& state , self , string , pos , endpos ))
731
752
return NULL ;
732
753
754
+ INIT_TRACE (& state );
733
755
TRACE (("|%p|%p|SEARCH\n" , PatternObject_GetCode (self ), state .ptr ));
734
756
735
757
status = sre_search (& state , PatternObject_GetCode (self ));
0 commit comments