@@ -397,6 +397,25 @@ _sre_unicode_tolower_impl(PyObject *module, int character)
397
397
return sre_lower_unicode (character );
398
398
}
399
399
400
+ LOCAL (void )
401
+ state_clean_repeat_data (SRE_STATE * state )
402
+ {
403
+ SRE_REPEAT * rep = state -> repeat ;
404
+ state -> repeat = NULL ;
405
+ while (rep ) {
406
+ SRE_REPEAT * prev = rep -> prev ;
407
+ PyMem_Free (rep );
408
+ rep = prev ;
409
+ }
410
+ rep = state -> repstack ;
411
+ state -> repstack = NULL ;
412
+ while (rep ) {
413
+ SRE_REPEAT * next = rep -> next ;
414
+ PyMem_Free (rep );
415
+ rep = next ;
416
+ }
417
+ }
418
+
400
419
LOCAL (void )
401
420
state_reset (SRE_STATE * state )
402
421
{
@@ -406,8 +425,7 @@ state_reset(SRE_STATE* state)
406
425
state -> lastmark = -1 ;
407
426
state -> lastindex = -1 ;
408
427
409
- state -> repeat = NULL ;
410
-
428
+ state_clean_repeat_data (state );
411
429
data_stack_dealloc (state );
412
430
}
413
431
@@ -511,6 +529,11 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
511
529
state -> pos = start ;
512
530
state -> endpos = end ;
513
531
532
+ #ifdef Py_DEBUG
533
+ state -> fail_after_count = pattern -> fail_after_count ;
534
+ state -> fail_after_exc = pattern -> fail_after_exc ; // borrowed ref
535
+ #endif
536
+
514
537
return string ;
515
538
err :
516
539
/* We add an explicit cast here because MSVC has a bug when
@@ -524,15 +547,21 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
524
547
}
525
548
526
549
LOCAL (void )
527
- state_fini (SRE_STATE * state )
550
+ state_fini (SRE_STATE * state , PatternObject * pattern )
528
551
{
529
552
if (state -> buffer .buf )
530
553
PyBuffer_Release (& state -> buffer );
531
554
Py_XDECREF (state -> string );
555
+ state_clean_repeat_data (state );
532
556
data_stack_dealloc (state );
533
557
/* See above PyMem_Free() for why we explicitly cast here. */
534
558
PyMem_Free ((void * ) state -> mark );
535
559
state -> mark = NULL ;
560
+ #ifdef Py_DEBUG
561
+ if (pattern ) {
562
+ pattern -> fail_after_count = -1 ;
563
+ }
564
+ #endif
536
565
}
537
566
538
567
/* calculate offset from start of string */
@@ -619,6 +648,9 @@ pattern_traverse(PatternObject *self, visitproc visit, void *arg)
619
648
Py_VISIT (self -> groupindex );
620
649
Py_VISIT (self -> indexgroup );
621
650
Py_VISIT (self -> pattern );
651
+ #ifdef Py_DEBUG
652
+ Py_VISIT (self -> fail_after_exc );
653
+ #endif
622
654
return 0 ;
623
655
}
624
656
@@ -628,6 +660,9 @@ pattern_clear(PatternObject *self)
628
660
Py_CLEAR (self -> groupindex );
629
661
Py_CLEAR (self -> indexgroup );
630
662
Py_CLEAR (self -> pattern );
663
+ #ifdef Py_DEBUG
664
+ Py_CLEAR (self -> fail_after_exc );
665
+ #endif
631
666
return 0 ;
632
667
}
633
668
@@ -690,7 +725,7 @@ _sre_SRE_Pattern_match_impl(PatternObject *self, PyTypeObject *cls,
690
725
Py_ssize_t status ;
691
726
PyObject * match ;
692
727
693
- if (!state_init (& state , ( PatternObject * ) self , string , pos , endpos ))
728
+ if (!state_init (& state , self , string , pos , endpos ))
694
729
return NULL ;
695
730
696
731
INIT_TRACE (& state );
@@ -702,12 +737,12 @@ _sre_SRE_Pattern_match_impl(PatternObject *self, PyTypeObject *cls,
702
737
703
738
TRACE (("|%p|%p|END\n" , PatternObject_GetCode (self ), state .ptr ));
704
739
if (PyErr_Occurred ()) {
705
- state_fini (& state );
740
+ state_fini (& state , self );
706
741
return NULL ;
707
742
}
708
743
709
744
match = pattern_new_match (module_state , self , & state , status );
710
- state_fini (& state );
745
+ state_fini (& state , self );
711
746
return match ;
712
747
}
713
748
@@ -747,12 +782,12 @@ _sre_SRE_Pattern_fullmatch_impl(PatternObject *self, PyTypeObject *cls,
747
782
748
783
TRACE (("|%p|%p|END\n" , PatternObject_GetCode (self ), state .ptr ));
749
784
if (PyErr_Occurred ()) {
750
- state_fini (& state );
785
+ state_fini (& state , self );
751
786
return NULL ;
752
787
}
753
788
754
789
match = pattern_new_match (module_state , self , & state , status );
755
- state_fini (& state );
790
+ state_fini (& state , self );
756
791
return match ;
757
792
}
758
793
@@ -792,12 +827,12 @@ _sre_SRE_Pattern_search_impl(PatternObject *self, PyTypeObject *cls,
792
827
TRACE (("|%p|%p|END\n" , PatternObject_GetCode (self ), state .ptr ));
793
828
794
829
if (PyErr_Occurred ()) {
795
- state_fini (& state );
830
+ state_fini (& state , self );
796
831
return NULL ;
797
832
}
798
833
799
834
match = pattern_new_match (module_state , self , & state , status );
800
- state_fini (& state );
835
+ state_fini (& state , self );
801
836
return match ;
802
837
}
803
838
@@ -826,7 +861,7 @@ _sre_SRE_Pattern_findall_impl(PatternObject *self, PyObject *string,
826
861
827
862
list = PyList_New (0 );
828
863
if (!list ) {
829
- state_fini (& state );
864
+ state_fini (& state , self );
830
865
return NULL ;
831
866
}
832
867
@@ -888,12 +923,12 @@ _sre_SRE_Pattern_findall_impl(PatternObject *self, PyObject *string,
888
923
state .start = state .ptr ;
889
924
}
890
925
891
- state_fini (& state );
926
+ state_fini (& state , self );
892
927
return list ;
893
928
894
929
error :
895
930
Py_DECREF (list );
896
- state_fini (& state );
931
+ state_fini (& state , self );
897
932
return NULL ;
898
933
899
934
}
@@ -989,7 +1024,7 @@ _sre_SRE_Pattern_split_impl(PatternObject *self, PyObject *string,
989
1024
990
1025
list = PyList_New (0 );
991
1026
if (!list ) {
992
- state_fini (& state );
1027
+ state_fini (& state , self );
993
1028
return NULL ;
994
1029
}
995
1030
@@ -1053,12 +1088,12 @@ _sre_SRE_Pattern_split_impl(PatternObject *self, PyObject *string,
1053
1088
if (status < 0 )
1054
1089
goto error ;
1055
1090
1056
- state_fini (& state );
1091
+ state_fini (& state , self );
1057
1092
return list ;
1058
1093
1059
1094
error :
1060
1095
Py_DECREF (list );
1061
- state_fini (& state );
1096
+ state_fini (& state , self );
1062
1097
return NULL ;
1063
1098
1064
1099
}
@@ -1185,7 +1220,7 @@ pattern_subx(_sremodulestate* module_state,
1185
1220
list = PyList_New (0 );
1186
1221
if (!list ) {
1187
1222
Py_DECREF (filter );
1188
- state_fini (& state );
1223
+ state_fini (& state , self );
1189
1224
return NULL ;
1190
1225
}
1191
1226
@@ -1271,7 +1306,7 @@ pattern_subx(_sremodulestate* module_state,
1271
1306
goto error ;
1272
1307
}
1273
1308
1274
- state_fini (& state );
1309
+ state_fini (& state , self );
1275
1310
1276
1311
Py_DECREF (filter );
1277
1312
@@ -1303,7 +1338,7 @@ pattern_subx(_sremodulestate* module_state,
1303
1338
1304
1339
error :
1305
1340
Py_DECREF (list );
1306
- state_fini (& state );
1341
+ state_fini (& state , self );
1307
1342
Py_DECREF (filter );
1308
1343
return NULL ;
1309
1344
@@ -1381,6 +1416,29 @@ _sre_SRE_Pattern___deepcopy__(PatternObject *self, PyObject *memo)
1381
1416
return Py_NewRef (self );
1382
1417
}
1383
1418
1419
+ #ifdef Py_DEBUG
1420
+ /*[clinic input]
1421
+ _sre.SRE_Pattern._fail_after
1422
+
1423
+ count: int
1424
+ exception: object
1425
+ /
1426
+
1427
+ For debugging.
1428
+ [clinic start generated code]*/
1429
+
1430
+ static PyObject *
1431
+ _sre_SRE_Pattern__fail_after_impl (PatternObject * self , int count ,
1432
+ PyObject * exception )
1433
+ /*[clinic end generated code: output=9a6bf12135ac50c2 input=ef80a45c66c5499d]*/
1434
+ {
1435
+ self -> fail_after_count = count ;
1436
+ Py_INCREF (exception );
1437
+ Py_XSETREF (self -> fail_after_exc , exception );
1438
+ Py_RETURN_NONE ;
1439
+ }
1440
+ #endif /* Py_DEBUG */
1441
+
1384
1442
static PyObject *
1385
1443
pattern_repr (PatternObject * obj )
1386
1444
{
@@ -1506,6 +1564,11 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
1506
1564
self -> pattern = NULL ;
1507
1565
self -> groupindex = NULL ;
1508
1566
self -> indexgroup = NULL ;
1567
+ #ifdef Py_DEBUG
1568
+ self -> fail_after_count = -1 ;
1569
+ self -> fail_after_exc = NULL ;
1570
+ self -> fail_after_exc = Py_NewRef (PyExc_RuntimeError );
1571
+ #endif
1509
1572
1510
1573
self -> codesize = n ;
1511
1574
@@ -2680,7 +2743,7 @@ scanner_dealloc(ScannerObject* self)
2680
2743
PyTypeObject * tp = Py_TYPE (self );
2681
2744
2682
2745
PyObject_GC_UnTrack (self );
2683
- state_fini (& self -> state );
2746
+ state_fini (& self -> state , self -> pattern );
2684
2747
(void )scanner_clear (self );
2685
2748
tp -> tp_free (self );
2686
2749
Py_DECREF (tp );
@@ -2826,7 +2889,8 @@ pattern_scanner(_sremodulestate *module_state,
2826
2889
return NULL ;
2827
2890
}
2828
2891
2829
- scanner -> pattern = Py_NewRef (self );
2892
+ Py_INCREF (self );
2893
+ scanner -> pattern = self ;
2830
2894
2831
2895
PyObject_GC_Track (scanner );
2832
2896
return (PyObject * ) scanner ;
@@ -3020,6 +3084,7 @@ static PyMethodDef pattern_methods[] = {
3020
3084
_SRE_SRE_PATTERN_SCANNER_METHODDEF
3021
3085
_SRE_SRE_PATTERN___COPY___METHODDEF
3022
3086
_SRE_SRE_PATTERN___DEEPCOPY___METHODDEF
3087
+ _SRE_SRE_PATTERN__FAIL_AFTER_METHODDEF
3023
3088
{"__class_getitem__" , Py_GenericAlias , METH_O |METH_CLASS ,
3024
3089
PyDoc_STR ("See PEP 585" )},
3025
3090
{NULL , NULL }
0 commit comments