Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2cbdc2a

Browse files
committed
Cleaning up recursive pieces left in the reorganization.
1 parent 5469324 commit 2cbdc2a

1 file changed

Lines changed: 16 additions & 119 deletions

File tree

Modules/_sre.c

Lines changed: 16 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -64,37 +64,6 @@ static char copyright[] =
6464
/* -------------------------------------------------------------------- */
6565
/* optional features */
6666

67-
/* prevent run-away recursion (bad patterns on long strings) */
68-
69-
#if !defined(USE_STACKCHECK)
70-
#if defined(MS_WIN64) || defined(__LP64__) || defined(_LP64)
71-
/* require smaller recursion limit for a number of 64-bit platforms:
72-
Win64 (MS_WIN64), Linux64 (__LP64__), Monterey (64-bit AIX) (_LP64) */
73-
/* FIXME: maybe the limit should be 40000 / sizeof(void*) ? */
74-
#define USE_RECURSION_LIMIT 7500
75-
#else
76-
77-
#if defined(__GNUC__) && defined(WITH_THREAD) && defined(__FreeBSD__)
78-
/* the pthreads library on FreeBSD has a fixed 1MB stack size for the
79-
* initial (or "primary") thread, which is insufficient for the default
80-
* recursion limit. gcc 3.x at the default optimisation
81-
* level (-O3) uses stack space more aggressively than gcc 2.95.
82-
*/
83-
#if (__GNUC__ > 2)
84-
#define USE_RECURSION_LIMIT 6500
85-
#else
86-
#define USE_RECURSION_LIMIT 7500
87-
#endif
88-
89-
#else
90-
#define USE_RECURSION_LIMIT 10000
91-
#endif
92-
#endif
93-
#endif
94-
95-
/* enables usage of recursive scheme */
96-
#undef USE_RECURSION
97-
9867
/* enables fast searching */
9968
#define USE_FAST_SEARCH
10069

@@ -536,10 +505,10 @@ SRE_CHARSET(SRE_CODE* set, SRE_CODE ch)
536505
}
537506
}
538507

539-
LOCAL(int) SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level);
508+
LOCAL(int) SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern);
540509

541510
LOCAL(int)
542-
SRE_COUNT(SRE_STATE* state, SRE_CODE* pattern, int maxcount, int level)
511+
SRE_COUNT(SRE_STATE* state, SRE_CODE* pattern, int maxcount)
543512
{
544513
SRE_CODE chr;
545514
SRE_CHAR* ptr = state->ptr;
@@ -609,7 +578,7 @@ SRE_COUNT(SRE_STATE* state, SRE_CODE* pattern, int maxcount, int level)
609578
/* repeated single character pattern */
610579
TRACE(("|%p|%p|COUNT SUBPATTERN\n", pattern, ptr));
611580
while ((SRE_CHAR*) state->ptr < end) {
612-
i = SRE_MATCH(state, pattern, level);
581+
i = SRE_MATCH(state, pattern);
613582
if (i < 0)
614583
return i;
615584
if (!i)
@@ -827,7 +796,7 @@ typedef struct {
827796
/* check if string matches the given pattern. returns <0 for
828797
error, 0 for failure, and 1 for success */
829798
LOCAL(int)
830-
SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
799+
SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
831800
{
832801
SRE_CHAR* end = state->end;
833802
int alloc_pos, ctx_pos = -1;
@@ -837,17 +806,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
837806
SRE_MATCH_CONTEXT* ctx;
838807
SRE_MATCH_CONTEXT* nextctx;
839808

840-
TRACE(("|%p|%p|ENTER %d\n", pattern, state->ptr, level));
841-
842-
#if defined(USE_STACKCHECK)
843-
if (level % 10 == 0 && PyOS_CheckStack())
844-
return SRE_ERROR_RECURSION_LIMIT;
845-
#endif
846-
847-
#if defined(USE_RECURSION_LIMIT)
848-
if (level > USE_RECURSION_LIMIT)
849-
return SRE_ERROR_RECURSION_LIMIT;
850-
#endif
809+
TRACE(("|%p|%p|ENTER\n", pattern, state->ptr));
851810

852811
DATA_ALLOC(SRE_MATCH_CONTEXT, ctx);
853812
ctx->last_ctx_pos = -1;
@@ -1029,11 +988,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
1029988
!SRE_CHARSET(ctx->pattern + 3, (SRE_CODE) *ctx->ptr)))
1030989
continue;
1031990
state->ptr = ctx->ptr;
1032-
#ifdef USE_RECURSION
1033-
ret = SRE_MATCH(state, ctx->pattern+1, level+1);
1034-
#else
1035991
DO_JUMP(JUMP_BRANCH, jump_branch, ctx->pattern+1);
1036-
#endif
1037992
if (ret) {
1038993
if (ctx->u.rep)
1039994
MARK_POP_DISCARD(ctx->lastmark);
@@ -1066,8 +1021,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
10661021

10671022
state->ptr = ctx->ptr;
10681023

1069-
ctx->count = SRE_COUNT(state, ctx->pattern+3, ctx->pattern[2],
1070-
level+1);
1024+
ctx->count = SRE_COUNT(state, ctx->pattern+3, ctx->pattern[2]);
10711025
RETURN_ON_ERROR(ctx->count);
10721026

10731027
ctx->ptr += ctx->count;
@@ -1101,13 +1055,8 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
11011055
if (ctx->count < (int) ctx->pattern[1])
11021056
break;
11031057
state->ptr = ctx->ptr;
1104-
#ifdef USE_RECURSION
1105-
ret = SRE_MATCH(state, ctx->pattern+ctx->pattern[0],
1106-
level+1);
1107-
#else
11081058
DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1,
11091059
ctx->pattern+ctx->pattern[0]);
1110-
#endif
11111060
if (ret) {
11121061
RETURN_ON_ERROR(ret);
11131062
RETURN_SUCCESS;
@@ -1123,13 +1072,8 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
11231072
/* general case */
11241073
while (ctx->count >= (int) ctx->pattern[1]) {
11251074
state->ptr = ctx->ptr;
1126-
#ifdef USE_RECURSION
1127-
ret = SRE_MATCH(state, ctx->pattern+ctx->pattern[0],
1128-
level+1);
1129-
#else
11301075
DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2,
11311076
ctx->pattern+ctx->pattern[0]);
1132-
#endif
11331077
if (ret) {
11341078
RETURN_ON_ERROR(ret);
11351079
RETURN_SUCCESS;
@@ -1164,7 +1108,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
11641108
else {
11651109
/* count using pattern min as the maximum */
11661110
ctx->count = SRE_COUNT(state, ctx->pattern+3,
1167-
ctx->pattern[1], level+1);
1111+
ctx->pattern[1]);
11681112
RETURN_ON_ERROR(ctx->count);
11691113
if (ctx->count < (int) ctx->pattern[1])
11701114
/* didn't match minimum number of times */
@@ -1184,19 +1128,14 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
11841128
while ((int)ctx->pattern[2] == 65535
11851129
|| ctx->count <= (int)ctx->pattern[2]) {
11861130
state->ptr = ctx->ptr;
1187-
#ifdef USE_RECURSION
1188-
ret = SRE_MATCH(state, ctx->pattern+ctx->pattern[0],
1189-
level+1);
1190-
#else
11911131
DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one,
11921132
ctx->pattern+ctx->pattern[0]);
1193-
#endif
11941133
if (ret) {
11951134
RETURN_ON_ERROR(ret);
11961135
RETURN_SUCCESS;
11971136
}
11981137
state->ptr = ctx->ptr;
1199-
ret = SRE_COUNT(state, ctx->pattern+3, 1, level+1);
1138+
ret = SRE_COUNT(state, ctx->pattern+3, 1);
12001139
RETURN_ON_ERROR(ret);
12011140
if (ret == 0)
12021141
break;
@@ -1224,11 +1163,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
12241163
state->repeat = ctx->u.rep;
12251164

12261165
state->ptr = ctx->ptr;
1227-
#ifdef USE_RECURSION
1228-
ret = SRE_MATCH(state, ctx->pattern+ctx->pattern[0], level+1);
1229-
#else
12301166
DO_JUMP(JUMP_REPEAT, jump_repeat, ctx->pattern+ctx->pattern[0]);
1231-
#endif
12321167
state->repeat = ctx->u.rep->prev;
12331168
free(ctx->u.rep);
12341169

@@ -1259,13 +1194,8 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
12591194
if (ctx->count < ctx->u.rep->pattern[1]) {
12601195
/* not enough matches */
12611196
ctx->u.rep->count = ctx->count;
1262-
#ifdef USE_RECURSION
1263-
/* RECURSIVE */
1264-
ret = SRE_MATCH(state, ctx->u.rep->pattern+3, level+1);
1265-
#else
12661197
DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,
12671198
ctx->u.rep->pattern+3);
1268-
#endif
12691199
if (ret) {
12701200
RETURN_ON_ERROR(ret);
12711201
RETURN_SUCCESS;
@@ -1286,13 +1216,8 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
12861216
/* zero-width match protection */
12871217
DATA_PUSH(&ctx->u.rep->last_ptr);
12881218
ctx->u.rep->last_ptr = state->ptr;
1289-
#ifdef USE_RECURSION
1290-
/* RECURSIVE */
1291-
ret = SRE_MATCH(state, ctx->u.rep->pattern+3, level+1);
1292-
#else
12931219
DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2,
12941220
ctx->u.rep->pattern+3);
1295-
#endif
12961221
DATA_POP(&ctx->u.rep->last_ptr);
12971222
if (ret) {
12981223
MARK_POP_DISCARD(ctx->lastmark);
@@ -1308,11 +1233,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
13081233
/* cannot match more repeated items here. make sure the
13091234
tail matches */
13101235
state->repeat = ctx->u.rep->prev;
1311-
#ifdef USE_RECURSION
1312-
ret = SRE_MATCH(state, ctx->pattern, level+1);
1313-
#else
13141236
DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, ctx->pattern);
1315-
#endif
13161237
RETURN_ON_SUCCESS(ret);
13171238
state->repeat = ctx->u.rep;
13181239
state->ptr = ctx->ptr;
@@ -1336,13 +1257,8 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
13361257
if (ctx->count < ctx->u.rep->pattern[1]) {
13371258
/* not enough matches */
13381259
ctx->u.rep->count = ctx->count;
1339-
#ifdef USE_RECURSION
1340-
/* RECURSIVE */
1341-
ret = SRE_MATCH(state, ctx->u.rep->pattern+3, level+1);
1342-
#else
13431260
DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,
13441261
ctx->u.rep->pattern+3);
1345-
#endif
13461262
if (ret) {
13471263
RETURN_ON_ERROR(ret);
13481264
RETURN_SUCCESS;
@@ -1356,11 +1272,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
13561272

13571273
/* see if the tail matches */
13581274
state->repeat = ctx->u.rep->prev;
1359-
#ifdef USE_RECURSION
1360-
ret = SRE_MATCH(state, ctx->pattern, level+1);
1361-
#else
13621275
DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, ctx->pattern);
1363-
#endif
13641276
if (ret) {
13651277
RETURN_ON_ERROR(ret);
13661278
RETURN_SUCCESS;
@@ -1376,13 +1288,8 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
13761288
RETURN_FAILURE;
13771289

13781290
ctx->u.rep->count = ctx->count;
1379-
#ifdef USE_RECURSION
1380-
/* RECURSIVE */
1381-
ret = SRE_MATCH(state, ctx->u.rep->pattern+3, level+1);
1382-
#else
13831291
DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
13841292
ctx->u.rep->pattern+3);
1385-
#endif
13861293
if (ret) {
13871294
RETURN_ON_ERROR(ret);
13881295
RETURN_SUCCESS;
@@ -1470,11 +1377,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
14701377
state->ptr = ctx->ptr - ctx->pattern[1];
14711378
if (state->ptr < state->beginning)
14721379
RETURN_FAILURE;
1473-
#ifdef USE_RECURSION
1474-
ret = SRE_MATCH(state, ctx->pattern+2, level+1);
1475-
#else
14761380
DO_JUMP(JUMP_ASSERT, jump_assert, ctx->pattern+2);
1477-
#endif
14781381
RETURN_ON_FAILURE(ret);
14791382
ctx->pattern += ctx->pattern[0];
14801383
break;
@@ -1486,11 +1389,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
14861389
ctx->ptr, ctx->pattern[1]));
14871390
state->ptr = ctx->ptr - ctx->pattern[1];
14881391
if (state->ptr >= state->beginning) {
1489-
#ifdef USE_RECURSION
1490-
ret = SRE_MATCH(state, ctx->pattern+2, level+1);
1491-
#else
14921392
DO_JUMP(JUMP_ASSERT_NOT, jump_assert_not, ctx->pattern+2);
1493-
#endif
14941393
if (ret) {
14951394
RETURN_ON_ERROR(ret);
14961395
RETURN_FAILURE;
@@ -1519,7 +1418,6 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
15191418
return ret;
15201419
DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos);
15211420

1522-
#ifndef USE_RECURSION
15231421
switch (jump) {
15241422
case JUMP_MAX_UNTIL_2:
15251423
TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", ctx->pattern, ctx->ptr));
@@ -1564,7 +1462,6 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
15641462
TRACE(("|%p|%p|RETURN %d\n", ctx->pattern, ctx->ptr, ret));
15651463
break;
15661464
}
1567-
#endif
15681465

15691466
return ret; /* should never get here */
15701467
}
@@ -1635,7 +1532,7 @@ SRE_SEARCH(SRE_STATE* state, SRE_CODE* pattern)
16351532
state->ptr = ptr + 1 - prefix_len + prefix_skip;
16361533
if (flags & SRE_INFO_LITERAL)
16371534
return 1; /* we got all of it */
1638-
status = SRE_MATCH(state, pattern + 2*prefix_skip, 1);
1535+
status = SRE_MATCH(state, pattern + 2*prefix_skip);
16391536
if (status != 0)
16401537
return status;
16411538
/* close but no cigar -- try again */
@@ -1666,7 +1563,7 @@ SRE_SEARCH(SRE_STATE* state, SRE_CODE* pattern)
16661563
state->ptr = ++ptr;
16671564
if (flags & SRE_INFO_LITERAL)
16681565
return 1; /* we got all of it */
1669-
status = SRE_MATCH(state, pattern + 2, 1);
1566+
status = SRE_MATCH(state, pattern + 2);
16701567
if (status != 0)
16711568
break;
16721569
}
@@ -1681,7 +1578,7 @@ SRE_SEARCH(SRE_STATE* state, SRE_CODE* pattern)
16811578
TRACE(("|%p|%p|SEARCH CHARSET\n", pattern, ptr));
16821579
state->start = ptr;
16831580
state->ptr = ptr;
1684-
status = SRE_MATCH(state, pattern, 1);
1581+
status = SRE_MATCH(state, pattern);
16851582
if (status != 0)
16861583
break;
16871584
ptr++;
@@ -1691,7 +1588,7 @@ SRE_SEARCH(SRE_STATE* state, SRE_CODE* pattern)
16911588
while (ptr <= end) {
16921589
TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
16931590
state->start = state->ptr = ptr++;
1694-
status = SRE_MATCH(state, pattern, 1);
1591+
status = SRE_MATCH(state, pattern);
16951592
if (status != 0)
16961593
break;
16971594
}
@@ -2114,10 +2011,10 @@ pattern_match(PatternObject* self, PyObject* args, PyObject* kw)
21142011
TRACE(("|%p|%p|MATCH\n", PatternObject_GetCode(self), state.ptr));
21152012

21162013
if (state.charsize == 1) {
2117-
status = sre_match(&state, PatternObject_GetCode(self), 1);
2014+
status = sre_match(&state, PatternObject_GetCode(self));
21182015
} else {
21192016
#if defined(HAVE_UNICODE)
2120-
status = sre_umatch(&state, PatternObject_GetCode(self), 1);
2017+
status = sre_umatch(&state, PatternObject_GetCode(self));
21212018
#endif
21222019
}
21232020

@@ -3311,10 +3208,10 @@ scanner_match(ScannerObject* self, PyObject* args)
33113208
state->ptr = state->start;
33123209

33133210
if (state->charsize == 1) {
3314-
status = sre_match(state, PatternObject_GetCode(self->pattern), 1);
3211+
status = sre_match(state, PatternObject_GetCode(self->pattern));
33153212
} else {
33163213
#if defined(HAVE_UNICODE)
3317-
status = sre_umatch(state, PatternObject_GetCode(self->pattern), 1);
3214+
status = sre_umatch(state, PatternObject_GetCode(self->pattern));
33183215
#endif
33193216
}
33203217

0 commit comments

Comments
 (0)