summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-10-26 13:42:42 (GMT)
committerGitHub <noreply@github.com>2023-10-26 13:42:42 (GMT)
commit573eff3e2ec36b5ec77c3601592a652e524abe21 (patch)
tree42beb4162f4d86f08e515f7536baebd7666f7cfe /Modules
parent31c05b72c15885ad5ff298de39456d8baed28448 (diff)
downloadcpython-573eff3e2ec36b5ec77c3601592a652e524abe21.zip
cpython-573eff3e2ec36b5ec77c3601592a652e524abe21.tar.gz
cpython-573eff3e2ec36b5ec77c3601592a652e524abe21.tar.bz2
Output more details in the re tracing (GH-111357)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_sre/sre.c3
-rw-r--r--Modules/_sre/sre_lib.h43
2 files changed, 42 insertions, 4 deletions
diff --git a/Modules/_sre/sre.c b/Modules/_sre/sre.c
index 0f134b1..d451974 100644
--- a/Modules/_sre/sre.c
+++ b/Modules/_sre/sre.c
@@ -107,9 +107,11 @@ static unsigned int sre_toupper(unsigned int ch) {
#if VERBOSE == 0
# define INIT_TRACE(state)
+# define DO_TRACE 0
# define TRACE(v)
#elif VERBOSE == 1
# define INIT_TRACE(state) int _debug = (state)->debug
+# define DO_TRACE (_debug)
# define TRACE(v) do { \
if (_debug) { \
printf v; \
@@ -117,6 +119,7 @@ static unsigned int sre_toupper(unsigned int ch) {
} while (0)
#elif VERBOSE == 2
# define INIT_TRACE(state)
+# define DO_TRACE 1
# define TRACE(v) printf v
#else
# error VERBOSE must be 0, 1 or 2
diff --git a/Modules/_sre/sre_lib.h b/Modules/_sre/sre_lib.h
index 92dd725..f5497d9 100644
--- a/Modules/_sre/sre_lib.h
+++ b/Modules/_sre/sre_lib.h
@@ -373,6 +373,19 @@ SRE(count)(SRE_STATE* state, const SRE_CODE* pattern, Py_ssize_t maxcount)
state->lastindex = ctx->lastindex; \
} while (0)
+#define LAST_PTR_PUSH() \
+ do { \
+ TRACE(("push last_ptr: %zd", \
+ PTR_TO_INDEX(ctx->u.rep->last_ptr))); \
+ DATA_PUSH(&ctx->u.rep->last_ptr); \
+ } while (0)
+#define LAST_PTR_POP() \
+ do { \
+ DATA_POP(&ctx->u.rep->last_ptr); \
+ TRACE(("pop last_ptr: %zd", \
+ PTR_TO_INDEX(ctx->u.rep->last_ptr))); \
+ } while (0)
+
#define RETURN_ERROR(i) do { return i; } while(0)
#define RETURN_FAILURE do { ret = 0; goto exit; } while(0)
#define RETURN_SUCCESS do { ret = 1; goto exit; } while(0)
@@ -449,8 +462,27 @@ do { \
#define DATA_LOOKUP_AT(t,p,pos) \
DATA_STACK_LOOKUP_AT(state,t,p,pos)
+#define PTR_TO_INDEX(ptr) \
+ ((ptr) ? ((char*)(ptr) - (char*)state->beginning) / state->charsize : -1)
+
+#if VERBOSE
+# define MARK_TRACE(label, lastmark) \
+ do if (DO_TRACE) { \
+ TRACE(("%s %d marks:", (label), (lastmark)+1)); \
+ for (int j = 0; j <= (lastmark); j++) { \
+ if (j && (j & 1) == 0) { \
+ TRACE((" ")); \
+ } \
+ TRACE((" %zd", PTR_TO_INDEX(state->mark[j]))); \
+ } \
+ TRACE(("\n")); \
+ } while (0)
+#else
+# define MARK_TRACE(label, lastmark)
+#endif
#define MARK_PUSH(lastmark) \
do if (lastmark >= 0) { \
+ MARK_TRACE("push", (lastmark)); \
size_t _marks_size = (lastmark+1) * sizeof(void*); \
DATA_STACK_PUSH(state, state->mark, _marks_size); \
} while (0)
@@ -458,16 +490,19 @@ do { \
do if (lastmark >= 0) { \
size_t _marks_size = (lastmark+1) * sizeof(void*); \
DATA_STACK_POP(state, state->mark, _marks_size, 1); \
+ MARK_TRACE("pop", (lastmark)); \
} while (0)
#define MARK_POP_KEEP(lastmark) \
do if (lastmark >= 0) { \
size_t _marks_size = (lastmark+1) * sizeof(void*); \
DATA_STACK_POP(state, state->mark, _marks_size, 0); \
+ MARK_TRACE("pop keep", (lastmark)); \
} while (0)
#define MARK_POP_DISCARD(lastmark) \
do if (lastmark >= 0) { \
size_t _marks_size = (lastmark+1) * sizeof(void*); \
DATA_STACK_POP_DISCARD(state, _marks_size); \
+ MARK_TRACE("pop discard", (lastmark)); \
} while (0)
#define JUMP_NONE 0
@@ -1150,11 +1185,11 @@ dispatch:
LASTMARK_SAVE();
MARK_PUSH(ctx->lastmark);
/* zero-width match protection */
- DATA_PUSH(&ctx->u.rep->last_ptr);
+ LAST_PTR_PUSH();
ctx->u.rep->last_ptr = state->ptr;
DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2,
ctx->u.rep->pattern+3);
- DATA_POP(&ctx->u.rep->last_ptr);
+ LAST_PTR_POP();
if (ret) {
MARK_POP_DISCARD(ctx->lastmark);
RETURN_ON_ERROR(ret);
@@ -1235,11 +1270,11 @@ dispatch:
ctx->u.rep->count = ctx->count;
/* zero-width match protection */
- DATA_PUSH(&ctx->u.rep->last_ptr);
+ LAST_PTR_PUSH();
ctx->u.rep->last_ptr = state->ptr;
DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
ctx->u.rep->pattern+3);
- DATA_POP(&ctx->u.rep->last_ptr);
+ LAST_PTR_POP();
if (ret) {
RETURN_ON_ERROR(ret);
RETURN_SUCCESS;