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