#include <ctype.h>
-/* defining this one enables tracing */
-#undef VERBOSE
+/* Defining this one controls tracing:
+ * 0 -- disabled
+ * 1 -- only if the DEBUG flag set
+ * 2 -- always
+ */
+#ifndef VERBOSE
+# define VERBOSE 0
+#endif
/* -------------------------------------------------------------------- */
#define SRE_ERROR_MEMORY -9 /* out of memory */
#define SRE_ERROR_INTERRUPTED -10 /* signal handler raised exception */
-#if defined(VERBOSE)
-#define TRACE(v) printf v
+#if VERBOSE == 0
+# define INIT_TRACE(state)
+# define TRACE(v)
+#elif VERBOSE == 1
+# define INIT_TRACE(state) int _debug = (state)->debug
+# define TRACE(v) do { \
+ if (_debug) { \
+ printf v; \
+ } \
+ } while (0)
+#elif VERBOSE == 2
+# define INIT_TRACE(state)
+# define TRACE(v) printf v
#else
-#define TRACE(v)
+# error VERBOSE must be 0, 1 or 2
#endif
/* -------------------------------------------------------------------- */
static int
data_stack_grow(SRE_STATE* state, Py_ssize_t size)
{
+ INIT_TRACE(state);
Py_ssize_t minsize, cursize;
minsize = state->data_stack_base+size;
cursize = state->data_stack_size;
state->charsize = charsize;
state->match_all = 0;
state->must_advance = 0;
+ state->debug = ((pattern->flags & SRE_FLAG_DEBUG) != 0);
state->beginning = ptr;
if (!state_init(&state, (PatternObject *)self, string, pos, endpos))
return NULL;
+ INIT_TRACE(&state);
state.ptr = state.start;
TRACE(("|%p|%p|MATCH\n", PatternObject_GetCode(self), state.ptr));
if (!state_init(&state, self, string, pos, endpos))
return NULL;
+ INIT_TRACE(&state);
state.ptr = state.start;
TRACE(("|%p|%p|FULLMATCH\n", PatternObject_GetCode(self), state.ptr));
if (!state_init(&state, self, string, pos, endpos))
return NULL;
+ INIT_TRACE(&state);
TRACE(("|%p|%p|SEARCH\n", PatternObject_GetCode(self), state.ptr));
status = sre_search(&state, PatternObject_GetCode(self));
const SRE_CHAR* ptr = (const SRE_CHAR *)state->ptr;
const SRE_CHAR* end = (const SRE_CHAR *)state->end;
Py_ssize_t i;
+ INIT_TRACE(state);
/* adjust end */
if (maxcount < end - ptr && maxcount != SRE_MAXREPEAT)
SRE(match_context)* ctx;
SRE(match_context)* nextctx;
+ INIT_TRACE(state);
TRACE(("|%p|%p|ENTER\n", pattern, state->ptr));
SRE_CODE* charset = NULL;
SRE_CODE* overlap = NULL;
int flags = 0;
+ INIT_TRACE(state);
if (ptr > end)
return 0;