mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 10:26:02 +00:00 
			
		
		
		
	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
This commit is contained in:
		
							parent
							
								
									74ec02e949
								
							
						
					
					
						commit
						b305c69d10
					
				
					 3 changed files with 31 additions and 5 deletions
				
			
		|  | @ -49,8 +49,14 @@ static const char copyright[] = | ||||||
| 
 | 
 | ||||||
| #include <ctype.h> | #include <ctype.h> | ||||||
| 
 | 
 | ||||||
| /* defining this one enables tracing */ | /* Defining this one controls tracing:
 | ||||||
| #undef VERBOSE |  * 0 -- disabled | ||||||
|  |  * 1 -- only if the DEBUG flag set | ||||||
|  |  * 2 -- always | ||||||
|  |  */ | ||||||
|  | #ifndef VERBOSE | ||||||
|  | #  define VERBOSE 0 | ||||||
|  | #endif | ||||||
| 
 | 
 | ||||||
| /* -------------------------------------------------------------------- */ | /* -------------------------------------------------------------------- */ | ||||||
| 
 | 
 | ||||||
|  | @ -70,10 +76,21 @@ static const char copyright[] = | ||||||
| #define SRE_ERROR_MEMORY -9 /* out of memory */ | #define SRE_ERROR_MEMORY -9 /* out of memory */ | ||||||
| #define SRE_ERROR_INTERRUPTED -10 /* signal handler raised exception */ | #define SRE_ERROR_INTERRUPTED -10 /* signal handler raised exception */ | ||||||
| 
 | 
 | ||||||
| #if defined(VERBOSE) | #if VERBOSE == 0 | ||||||
| #define TRACE(v) printf v | #  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 | #else | ||||||
| #define TRACE(v) | #  error VERBOSE must be 0, 1 or 2 | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| /* -------------------------------------------------------------------- */ | /* -------------------------------------------------------------------- */ | ||||||
|  | @ -198,6 +215,7 @@ data_stack_dealloc(SRE_STATE* state) | ||||||
| static int | static int | ||||||
| data_stack_grow(SRE_STATE* state, Py_ssize_t size) | data_stack_grow(SRE_STATE* state, Py_ssize_t size) | ||||||
| { | { | ||||||
|  |     INIT_TRACE(state); | ||||||
|     Py_ssize_t minsize, cursize; |     Py_ssize_t minsize, cursize; | ||||||
|     minsize = state->data_stack_base+size; |     minsize = state->data_stack_base+size; | ||||||
|     cursize = state->data_stack_size; |     cursize = state->data_stack_size; | ||||||
|  | @ -449,6 +467,7 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string, | ||||||
|     state->charsize = charsize; |     state->charsize = charsize; | ||||||
|     state->match_all = 0; |     state->match_all = 0; | ||||||
|     state->must_advance = 0; |     state->must_advance = 0; | ||||||
|  |     state->debug = ((pattern->flags & SRE_FLAG_DEBUG) != 0); | ||||||
| 
 | 
 | ||||||
|     state->beginning = ptr; |     state->beginning = ptr; | ||||||
| 
 | 
 | ||||||
|  | @ -641,6 +660,7 @@ _sre_SRE_Pattern_match_impl(PatternObject *self, PyTypeObject *cls, | ||||||
|     if (!state_init(&state, (PatternObject *)self, string, pos, endpos)) |     if (!state_init(&state, (PatternObject *)self, string, pos, endpos)) | ||||||
|         return NULL; |         return NULL; | ||||||
| 
 | 
 | ||||||
|  |     INIT_TRACE(&state); | ||||||
|     state.ptr = state.start; |     state.ptr = state.start; | ||||||
| 
 | 
 | ||||||
|     TRACE(("|%p|%p|MATCH\n", PatternObject_GetCode(self), state.ptr)); |     TRACE(("|%p|%p|MATCH\n", PatternObject_GetCode(self), state.ptr)); | ||||||
|  | @ -684,6 +704,7 @@ _sre_SRE_Pattern_fullmatch_impl(PatternObject *self, PyTypeObject *cls, | ||||||
|     if (!state_init(&state, self, string, pos, endpos)) |     if (!state_init(&state, self, string, pos, endpos)) | ||||||
|         return NULL; |         return NULL; | ||||||
| 
 | 
 | ||||||
|  |     INIT_TRACE(&state); | ||||||
|     state.ptr = state.start; |     state.ptr = state.start; | ||||||
| 
 | 
 | ||||||
|     TRACE(("|%p|%p|FULLMATCH\n", PatternObject_GetCode(self), state.ptr)); |     TRACE(("|%p|%p|FULLMATCH\n", PatternObject_GetCode(self), state.ptr)); | ||||||
|  | @ -730,6 +751,7 @@ _sre_SRE_Pattern_search_impl(PatternObject *self, PyTypeObject *cls, | ||||||
|     if (!state_init(&state, self, string, pos, endpos)) |     if (!state_init(&state, self, string, pos, endpos)) | ||||||
|         return NULL; |         return NULL; | ||||||
| 
 | 
 | ||||||
|  |     INIT_TRACE(&state); | ||||||
|     TRACE(("|%p|%p|SEARCH\n", PatternObject_GetCode(self), state.ptr)); |     TRACE(("|%p|%p|SEARCH\n", PatternObject_GetCode(self), state.ptr)); | ||||||
| 
 | 
 | ||||||
|     status = sre_search(&state, PatternObject_GetCode(self)); |     status = sre_search(&state, PatternObject_GetCode(self)); | ||||||
|  |  | ||||||
|  | @ -84,6 +84,7 @@ typedef struct { | ||||||
|     int charsize; /* character size */ |     int charsize; /* character size */ | ||||||
|     int match_all; |     int match_all; | ||||||
|     int must_advance; |     int must_advance; | ||||||
|  |     int debug; | ||||||
|     /* marks */ |     /* marks */ | ||||||
|     int lastmark; |     int lastmark; | ||||||
|     int lastindex; |     int lastindex; | ||||||
|  |  | ||||||
|  | @ -209,6 +209,7 @@ SRE(count)(SRE_STATE* state, const SRE_CODE* pattern, Py_ssize_t maxcount) | ||||||
|     const SRE_CHAR* ptr = (const SRE_CHAR *)state->ptr; |     const SRE_CHAR* ptr = (const SRE_CHAR *)state->ptr; | ||||||
|     const SRE_CHAR* end = (const SRE_CHAR *)state->end; |     const SRE_CHAR* end = (const SRE_CHAR *)state->end; | ||||||
|     Py_ssize_t i; |     Py_ssize_t i; | ||||||
|  |     INIT_TRACE(state); | ||||||
| 
 | 
 | ||||||
|     /* adjust end */ |     /* adjust end */ | ||||||
|     if (maxcount < end - ptr && maxcount != SRE_MAXREPEAT) |     if (maxcount < end - ptr && maxcount != SRE_MAXREPEAT) | ||||||
|  | @ -567,6 +568,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) | ||||||
| 
 | 
 | ||||||
|     SRE(match_context)* ctx; |     SRE(match_context)* ctx; | ||||||
|     SRE(match_context)* nextctx; |     SRE(match_context)* nextctx; | ||||||
|  |     INIT_TRACE(state); | ||||||
| 
 | 
 | ||||||
|     TRACE(("|%p|%p|ENTER\n", pattern, state->ptr)); |     TRACE(("|%p|%p|ENTER\n", pattern, state->ptr)); | ||||||
| 
 | 
 | ||||||
|  | @ -1639,6 +1641,7 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern) | ||||||
|     SRE_CODE* charset = NULL; |     SRE_CODE* charset = NULL; | ||||||
|     SRE_CODE* overlap = NULL; |     SRE_CODE* overlap = NULL; | ||||||
|     int flags = 0; |     int flags = 0; | ||||||
|  |     INIT_TRACE(state); | ||||||
| 
 | 
 | ||||||
|     if (ptr > end) |     if (ptr > end) | ||||||
|         return 0; |         return 0; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Serhiy Storchaka
						Serhiy Storchaka