@@ -2391,6 +2391,25 @@ scanner_dealloc(ScannerObject* self)
2391
2391
PyObject_DEL (self );
2392
2392
}
2393
2393
2394
+ static int
2395
+ scanner_begin (ScannerObject * self )
2396
+ {
2397
+ if (self -> executing ) {
2398
+ PyErr_SetString (PyExc_ValueError ,
2399
+ "regular expression scanner already executing" );
2400
+ return 0 ;
2401
+ }
2402
+ self -> executing = 1 ;
2403
+ return 1 ;
2404
+ }
2405
+
2406
+ static void
2407
+ scanner_end (ScannerObject * self )
2408
+ {
2409
+ assert (self -> executing );
2410
+ self -> executing = 0 ;
2411
+ }
2412
+
2394
2413
/*[clinic input]
2395
2414
_sre.SRE_Scanner.match
2396
2415
@@ -2404,16 +2423,23 @@ _sre_SRE_Scanner_match_impl(ScannerObject *self)
2404
2423
PyObject * match ;
2405
2424
Py_ssize_t status ;
2406
2425
2407
- if (state -> start == NULL )
2426
+ if (!scanner_begin (self )) {
2427
+ return NULL ;
2428
+ }
2429
+ if (state -> start == NULL ) {
2430
+ scanner_end (self );
2408
2431
Py_RETURN_NONE ;
2432
+ }
2409
2433
2410
2434
state_reset (state );
2411
2435
2412
2436
state -> ptr = state -> start ;
2413
2437
2414
2438
status = sre_match (state , PatternObject_GetCode (self -> pattern ));
2415
- if (PyErr_Occurred ())
2439
+ if (PyErr_Occurred ()) {
2440
+ scanner_end (self );
2416
2441
return NULL ;
2442
+ }
2417
2443
2418
2444
match = pattern_new_match ((PatternObject * ) self -> pattern ,
2419
2445
state , status );
@@ -2425,6 +2451,7 @@ _sre_SRE_Scanner_match_impl(ScannerObject *self)
2425
2451
state -> start = state -> ptr ;
2426
2452
}
2427
2453
2454
+ scanner_end (self );
2428
2455
return match ;
2429
2456
}
2430
2457
@@ -2442,16 +2469,23 @@ _sre_SRE_Scanner_search_impl(ScannerObject *self)
2442
2469
PyObject * match ;
2443
2470
Py_ssize_t status ;
2444
2471
2445
- if (state -> start == NULL )
2472
+ if (!scanner_begin (self )) {
2473
+ return NULL ;
2474
+ }
2475
+ if (state -> start == NULL ) {
2476
+ scanner_end (self );
2446
2477
Py_RETURN_NONE ;
2478
+ }
2447
2479
2448
2480
state_reset (state );
2449
2481
2450
2482
state -> ptr = state -> start ;
2451
2483
2452
2484
status = sre_search (state , PatternObject_GetCode (self -> pattern ));
2453
- if (PyErr_Occurred ())
2485
+ if (PyErr_Occurred ()) {
2486
+ scanner_end (self );
2454
2487
return NULL ;
2488
+ }
2455
2489
2456
2490
match = pattern_new_match ((PatternObject * ) self -> pattern ,
2457
2491
state , status );
@@ -2463,6 +2497,7 @@ _sre_SRE_Scanner_search_impl(ScannerObject *self)
2463
2497
state -> start = state -> ptr ;
2464
2498
}
2465
2499
2500
+ scanner_end (self );
2466
2501
return match ;
2467
2502
}
2468
2503
@@ -2476,6 +2511,7 @@ pattern_scanner(PatternObject *self, PyObject *string, Py_ssize_t pos, Py_ssize_
2476
2511
if (!scanner )
2477
2512
return NULL ;
2478
2513
scanner -> pattern = NULL ;
2514
+ scanner -> executing = 0 ;
2479
2515
2480
2516
/* create search state object */
2481
2517
if (!state_init (& scanner -> state , self , string , pos , endpos )) {
0 commit comments