@@ -2511,6 +2511,25 @@ scanner_dealloc(ScannerObject* self)
2511
2511
Py_DECREF (tp );
2512
2512
}
2513
2513
2514
+ static int
2515
+ scanner_begin (ScannerObject * self )
2516
+ {
2517
+ if (self -> executing ) {
2518
+ PyErr_SetString (PyExc_ValueError ,
2519
+ "regular expression scanner already executing" );
2520
+ return 0 ;
2521
+ }
2522
+ self -> executing = 1 ;
2523
+ return 1 ;
2524
+ }
2525
+
2526
+ static void
2527
+ scanner_end (ScannerObject * self )
2528
+ {
2529
+ assert (self -> executing );
2530
+ self -> executing = 0 ;
2531
+ }
2532
+
2514
2533
/*[clinic input]
2515
2534
_sre.SRE_Scanner.match
2516
2535
@@ -2528,16 +2547,23 @@ _sre_SRE_Scanner_match_impl(ScannerObject *self, PyTypeObject *cls)
2528
2547
PyObject * match ;
2529
2548
Py_ssize_t status ;
2530
2549
2531
- if (state -> start == NULL )
2550
+ if (!scanner_begin (self )) {
2551
+ return NULL ;
2552
+ }
2553
+ if (state -> start == NULL ) {
2554
+ scanner_end (self );
2532
2555
Py_RETURN_NONE ;
2556
+ }
2533
2557
2534
2558
state_reset (state );
2535
2559
2536
2560
state -> ptr = state -> start ;
2537
2561
2538
2562
status = sre_match (state , PatternObject_GetCode (self -> pattern ));
2539
- if (PyErr_Occurred ())
2563
+ if (PyErr_Occurred ()) {
2564
+ scanner_end (self );
2540
2565
return NULL ;
2566
+ }
2541
2567
2542
2568
match = pattern_new_match (module_state , (PatternObject * ) self -> pattern ,
2543
2569
state , status );
@@ -2549,6 +2575,7 @@ _sre_SRE_Scanner_match_impl(ScannerObject *self, PyTypeObject *cls)
2549
2575
state -> start = state -> ptr ;
2550
2576
}
2551
2577
2578
+ scanner_end (self );
2552
2579
return match ;
2553
2580
}
2554
2581
@@ -2570,16 +2597,23 @@ _sre_SRE_Scanner_search_impl(ScannerObject *self, PyTypeObject *cls)
2570
2597
PyObject * match ;
2571
2598
Py_ssize_t status ;
2572
2599
2573
- if (state -> start == NULL )
2600
+ if (!scanner_begin (self )) {
2601
+ return NULL ;
2602
+ }
2603
+ if (state -> start == NULL ) {
2604
+ scanner_end (self );
2574
2605
Py_RETURN_NONE ;
2606
+ }
2575
2607
2576
2608
state_reset (state );
2577
2609
2578
2610
state -> ptr = state -> start ;
2579
2611
2580
2612
status = sre_search (state , PatternObject_GetCode (self -> pattern ));
2581
- if (PyErr_Occurred ())
2613
+ if (PyErr_Occurred ()) {
2614
+ scanner_end (self );
2582
2615
return NULL ;
2616
+ }
2583
2617
2584
2618
match = pattern_new_match (module_state , (PatternObject * ) self -> pattern ,
2585
2619
state , status );
@@ -2591,6 +2625,7 @@ _sre_SRE_Scanner_search_impl(ScannerObject *self, PyTypeObject *cls)
2591
2625
state -> start = state -> ptr ;
2592
2626
}
2593
2627
2628
+ scanner_end (self );
2594
2629
return match ;
2595
2630
}
2596
2631
@@ -2608,6 +2643,7 @@ pattern_scanner(_sremodulestate *module_state,
2608
2643
if (!scanner )
2609
2644
return NULL ;
2610
2645
scanner -> pattern = NULL ;
2646
+ scanner -> executing = 0 ;
2611
2647
2612
2648
/* create search state object */
2613
2649
if (!state_init (& scanner -> state , self , string , pos , endpos )) {
0 commit comments