@@ -218,6 +218,85 @@ data_stack_grow(SRE_STATE* state, Py_ssize_t size)
218
218
return 0 ;
219
219
}
220
220
221
+ /* memory pool functions for SRE_REPEAT, this can avoid memory
222
+ leak when SRE(match) function terminates abruptly.
223
+ state->repeat_pool_used is a doubly-linked list, so that we
224
+ can remove a SRE_REPEAT node from it.
225
+ state->repeat_pool_unused is a singly-linked list, we put/get
226
+ node at the head. */
227
+ static SRE_REPEAT *
228
+ repeat_pool_malloc (SRE_STATE * state )
229
+ {
230
+ SRE_REPEAT * repeat ;
231
+
232
+ if (state -> repeat_pool_unused ) {
233
+ /* remove from unused pool (singly-linked list) */
234
+ repeat = state -> repeat_pool_unused ;
235
+ state -> repeat_pool_unused = repeat -> pool_next ;
236
+ }
237
+ else {
238
+ repeat = PyObject_Malloc (sizeof (SRE_REPEAT ));
239
+ if (!repeat ) {
240
+ return NULL ;
241
+ }
242
+ }
243
+
244
+ /* add to used pool (doubly-linked list) */
245
+ SRE_REPEAT * temp = state -> repeat_pool_used ;
246
+ if (temp ) {
247
+ temp -> pool_prev = repeat ;
248
+ }
249
+ repeat -> pool_prev = NULL ;
250
+ repeat -> pool_next = temp ;
251
+ state -> repeat_pool_used = repeat ;
252
+
253
+ return repeat ;
254
+ }
255
+
256
+ static void
257
+ repeat_pool_free (SRE_STATE * state , SRE_REPEAT * repeat )
258
+ {
259
+ SRE_REPEAT * prev = repeat -> pool_prev ;
260
+ SRE_REPEAT * next = repeat -> pool_next ;
261
+
262
+ /* remove from used pool (doubly-linked list) */
263
+ if (prev ) {
264
+ prev -> pool_next = next ;
265
+ }
266
+ else {
267
+ state -> repeat_pool_used = next ;
268
+ }
269
+ if (next ) {
270
+ next -> pool_prev = prev ;
271
+ }
272
+
273
+ /* add to unused pool (singly-linked list) */
274
+ repeat -> pool_next = state -> repeat_pool_unused ;
275
+ state -> repeat_pool_unused = repeat ;
276
+ }
277
+
278
+ static void
279
+ repeat_pool_clear (SRE_STATE * state )
280
+ {
281
+ /* clear used pool */
282
+ SRE_REPEAT * next = state -> repeat_pool_used ;
283
+ state -> repeat_pool_used = NULL ;
284
+ while (next ) {
285
+ SRE_REPEAT * temp = next ;
286
+ next = temp -> pool_next ;
287
+ PyObject_Free (temp );
288
+ }
289
+
290
+ /* clear unused pool */
291
+ next = state -> repeat_pool_unused ;
292
+ state -> repeat_pool_unused = NULL ;
293
+ while (next ) {
294
+ SRE_REPEAT * temp = next ;
295
+ next = temp -> pool_next ;
296
+ PyObject_Free (temp );
297
+ }
298
+ }
299
+
221
300
/* generate 8-bit version */
222
301
223
302
#define SRE_CHAR Py_UCS1
@@ -463,6 +542,11 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
463
542
state -> pos = start ;
464
543
state -> endpos = end ;
465
544
545
+ #ifdef Py_DEBUG
546
+ state -> fail_after_count = pattern -> fail_after_count ;
547
+ state -> fail_after_exc = pattern -> fail_after_exc ; // borrowed ref
548
+ #endif
549
+
466
550
return string ;
467
551
err :
468
552
/* We add an explicit cast here because MSVC has a bug when
@@ -485,6 +569,8 @@ state_fini(SRE_STATE* state)
485
569
/* See above PyMem_Del for why we explicitly cast here. */
486
570
PyMem_Free ((void * ) state -> mark );
487
571
state -> mark = NULL ;
572
+ /* SRE_REPEAT pool */
573
+ repeat_pool_clear (state );
488
574
}
489
575
490
576
/* calculate offset from start of string */
@@ -571,6 +657,9 @@ pattern_traverse(PatternObject *self, visitproc visit, void *arg)
571
657
Py_VISIT (self -> groupindex );
572
658
Py_VISIT (self -> indexgroup );
573
659
Py_VISIT (self -> pattern );
660
+ #ifdef Py_DEBUG
661
+ Py_VISIT (self -> fail_after_exc );
662
+ #endif
574
663
return 0 ;
575
664
}
576
665
@@ -580,6 +669,9 @@ pattern_clear(PatternObject *self)
580
669
Py_CLEAR (self -> groupindex );
581
670
Py_CLEAR (self -> indexgroup );
582
671
Py_CLEAR (self -> pattern );
672
+ #ifdef Py_DEBUG
673
+ Py_CLEAR (self -> fail_after_exc );
674
+ #endif
583
675
return 0 ;
584
676
}
585
677
@@ -642,7 +734,7 @@ _sre_SRE_Pattern_match_impl(PatternObject *self, PyTypeObject *cls,
642
734
Py_ssize_t status ;
643
735
PyObject * match ;
644
736
645
- if (!state_init (& state , ( PatternObject * ) self , string , pos , endpos ))
737
+ if (!state_init (& state , self , string , pos , endpos ))
646
738
return NULL ;
647
739
648
740
state .ptr = state .start ;
@@ -1330,6 +1422,29 @@ _sre_SRE_Pattern___deepcopy__(PatternObject *self, PyObject *memo)
1330
1422
return Py_NewRef (self );
1331
1423
}
1332
1424
1425
+ #ifdef Py_DEBUG
1426
+ /*[clinic input]
1427
+ _sre.SRE_Pattern._fail_after
1428
+
1429
+ count: int
1430
+ exception: object
1431
+ /
1432
+
1433
+ For debugging.
1434
+ [clinic start generated code]*/
1435
+
1436
+ static PyObject *
1437
+ _sre_SRE_Pattern__fail_after_impl (PatternObject * self , int count ,
1438
+ PyObject * exception )
1439
+ /*[clinic end generated code: output=9a6bf12135ac50c2 input=ef80a45c66c5499d]*/
1440
+ {
1441
+ self -> fail_after_count = count ;
1442
+ Py_INCREF (exception );
1443
+ Py_XSETREF (self -> fail_after_exc , exception );
1444
+ Py_RETURN_NONE ;
1445
+ }
1446
+ #endif /* Py_DEBUG */
1447
+
1333
1448
static PyObject *
1334
1449
pattern_repr (PatternObject * obj )
1335
1450
{
@@ -1456,6 +1571,10 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
1456
1571
self -> pattern = NULL ;
1457
1572
self -> groupindex = NULL ;
1458
1573
self -> indexgroup = NULL ;
1574
+ #ifdef Py_DEBUG
1575
+ self -> fail_after_count = -1 ;
1576
+ self -> fail_after_exc = NULL ;
1577
+ #endif
1459
1578
1460
1579
self -> codesize = n ;
1461
1580
@@ -2552,7 +2671,8 @@ pattern_new_match(_sremodulestate* module_state,
2552
2671
if (!match )
2553
2672
return NULL ;
2554
2673
2555
- match -> pattern = (PatternObject * )Py_NewRef (pattern );
2674
+ Py_INCREF (pattern );
2675
+ match -> pattern = pattern ;
2556
2676
2557
2677
match -> string = Py_NewRef (state -> string );
2558
2678
@@ -2688,7 +2808,7 @@ _sre_SRE_Scanner_match_impl(ScannerObject *self, PyTypeObject *cls)
2688
2808
return NULL ;
2689
2809
}
2690
2810
2691
- match = pattern_new_match (module_state , ( PatternObject * ) self -> pattern ,
2811
+ match = pattern_new_match (module_state , self -> pattern ,
2692
2812
state , status );
2693
2813
2694
2814
if (status == 0 )
@@ -2738,7 +2858,7 @@ _sre_SRE_Scanner_search_impl(ScannerObject *self, PyTypeObject *cls)
2738
2858
return NULL ;
2739
2859
}
2740
2860
2741
- match = pattern_new_match (module_state , ( PatternObject * ) self -> pattern ,
2861
+ match = pattern_new_match (module_state , self -> pattern ,
2742
2862
state , status );
2743
2863
2744
2864
if (status == 0 )
@@ -2774,7 +2894,8 @@ pattern_scanner(_sremodulestate *module_state,
2774
2894
return NULL ;
2775
2895
}
2776
2896
2777
- scanner -> pattern = Py_NewRef (self );
2897
+ Py_INCREF (self );
2898
+ scanner -> pattern = self ;
2778
2899
2779
2900
PyObject_GC_Track (scanner );
2780
2901
return (PyObject * ) scanner ;
@@ -2968,6 +3089,7 @@ static PyMethodDef pattern_methods[] = {
2968
3089
_SRE_SRE_PATTERN_SCANNER_METHODDEF
2969
3090
_SRE_SRE_PATTERN___COPY___METHODDEF
2970
3091
_SRE_SRE_PATTERN___DEEPCOPY___METHODDEF
3092
+ _SRE_SRE_PATTERN__FAIL_AFTER_METHODDEF
2971
3093
{"__class_getitem__" , Py_GenericAlias , METH_O |METH_CLASS ,
2972
3094
PyDoc_STR ("See PEP 585" )},
2973
3095
{NULL , NULL }
0 commit comments