@@ -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)
198215static int
199216data_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 ));
0 commit comments