@@ -563,17 +563,36 @@ pattern_error(Py_ssize_t status)
563
563
}
564
564
}
565
565
566
+ static int
567
+ pattern_traverse (PatternObject * self , visitproc visit , void * arg )
568
+ {
569
+ Py_VISIT (Py_TYPE (self ));
570
+ Py_VISIT (self -> groupindex );
571
+ Py_VISIT (self -> indexgroup );
572
+ Py_VISIT (self -> pattern );
573
+ return 0 ;
574
+ }
575
+
576
+ static int
577
+ pattern_clear (PatternObject * self )
578
+ {
579
+ Py_CLEAR (self -> groupindex );
580
+ Py_CLEAR (self -> indexgroup );
581
+ Py_CLEAR (self -> pattern );
582
+ return 0 ;
583
+ }
584
+
566
585
static void
567
586
pattern_dealloc (PatternObject * self )
568
587
{
569
588
PyTypeObject * tp = Py_TYPE (self );
570
589
571
- if (self -> weakreflist != NULL )
590
+ PyObject_GC_UnTrack (self );
591
+ if (self -> weakreflist != NULL ) {
572
592
PyObject_ClearWeakRefs ((PyObject * ) self );
573
- Py_XDECREF (self -> pattern );
574
- Py_XDECREF (self -> groupindex );
575
- Py_XDECREF (self -> indexgroup );
576
- PyObject_Free (self );
593
+ }
594
+ (void )pattern_clear (self );
595
+ tp -> tp_free (self );
577
596
Py_DECREF (tp );
578
597
}
579
598
@@ -1397,7 +1416,7 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
1397
1416
1398
1417
n = PyList_GET_SIZE (code );
1399
1418
/* coverity[ampersand_in_size] */
1400
- self = PyObject_NewVar (PatternObject , module_state -> Pattern_Type , n );
1419
+ self = PyObject_GC_NewVar (PatternObject , module_state -> Pattern_Type , n );
1401
1420
if (!self )
1402
1421
return NULL ;
1403
1422
self -> weakreflist = NULL ;
@@ -1417,6 +1436,7 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
1417
1436
break ;
1418
1437
}
1419
1438
}
1439
+ PyObject_GC_Track (self );
1420
1440
1421
1441
if (PyErr_Occurred ()) {
1422
1442
Py_DECREF (self );
@@ -1938,15 +1958,33 @@ _validate(PatternObject *self)
1938
1958
/* -------------------------------------------------------------------- */
1939
1959
/* match methods */
1940
1960
1961
+ static int
1962
+ match_traverse (MatchObject * self , visitproc visit , void * arg )
1963
+ {
1964
+ Py_VISIT (Py_TYPE (self ));
1965
+ Py_VISIT (self -> string );
1966
+ Py_VISIT (self -> regs );
1967
+ Py_VISIT (self -> pattern );
1968
+ return 0 ;
1969
+ }
1970
+
1971
+ static int
1972
+ match_clear (MatchObject * self )
1973
+ {
1974
+ Py_CLEAR (self -> string );
1975
+ Py_CLEAR (self -> regs );
1976
+ Py_CLEAR (self -> pattern );
1977
+ return 0 ;
1978
+ }
1979
+
1941
1980
static void
1942
1981
match_dealloc (MatchObject * self )
1943
1982
{
1944
1983
PyTypeObject * tp = Py_TYPE (self );
1945
1984
1946
- Py_XDECREF (self -> regs );
1947
- Py_XDECREF (self -> string );
1948
- Py_DECREF (self -> pattern );
1949
- PyObject_Free (self );
1985
+ PyObject_GC_UnTrack (self );
1986
+ (void )match_clear (self );
1987
+ tp -> tp_free (self );
1950
1988
Py_DECREF (tp );
1951
1989
}
1952
1990
@@ -2392,9 +2430,9 @@ pattern_new_match(_sremodulestate* module_state,
2392
2430
2393
2431
/* create match object (with room for extra group marks) */
2394
2432
/* coverity[ampersand_in_size] */
2395
- match = PyObject_NewVar (MatchObject ,
2396
- module_state -> Match_Type ,
2397
- 2 * (pattern -> groups + 1 ));
2433
+ match = PyObject_GC_NewVar (MatchObject ,
2434
+ module_state -> Match_Type ,
2435
+ 2 * (pattern -> groups + 1 ));
2398
2436
if (!match )
2399
2437
return NULL ;
2400
2438
@@ -2427,6 +2465,7 @@ pattern_new_match(_sremodulestate* module_state,
2427
2465
2428
2466
match -> lastindex = state -> lastindex ;
2429
2467
2468
+ PyObject_GC_Track (match );
2430
2469
return (PyObject * ) match ;
2431
2470
2432
2471
} else if (status == 0 ) {
@@ -2445,14 +2484,30 @@ pattern_new_match(_sremodulestate* module_state,
2445
2484
/* -------------------------------------------------------------------- */
2446
2485
/* scanner methods (experimental) */
2447
2486
2487
+ static int
2488
+ scanner_traverse (ScannerObject * self , visitproc visit , void * arg )
2489
+ {
2490
+ Py_VISIT (Py_TYPE (self ));
2491
+ Py_VISIT (self -> pattern );
2492
+ return 0 ;
2493
+ }
2494
+
2495
+ static int
2496
+ scanner_clear (ScannerObject * self )
2497
+ {
2498
+ Py_CLEAR (self -> pattern );
2499
+ return 0 ;
2500
+ }
2501
+
2448
2502
static void
2449
2503
scanner_dealloc (ScannerObject * self )
2450
2504
{
2451
2505
PyTypeObject * tp = Py_TYPE (self );
2452
2506
2507
+ PyObject_GC_UnTrack (self );
2453
2508
state_fini (& self -> state );
2454
- Py_XDECREF ( self -> pattern );
2455
- PyObject_Free (self );
2509
+ ( void ) scanner_clear ( self );
2510
+ tp -> tp_free (self );
2456
2511
Py_DECREF (tp );
2457
2512
}
2458
2513
@@ -2549,7 +2604,7 @@ pattern_scanner(_sremodulestate *module_state,
2549
2604
ScannerObject * scanner ;
2550
2605
2551
2606
/* create scanner object */
2552
- scanner = PyObject_New (ScannerObject , module_state -> Scanner_Type );
2607
+ scanner = PyObject_GC_New (ScannerObject , module_state -> Scanner_Type );
2553
2608
if (!scanner )
2554
2609
return NULL ;
2555
2610
scanner -> pattern = NULL ;
@@ -2563,6 +2618,7 @@ pattern_scanner(_sremodulestate *module_state,
2563
2618
Py_INCREF (self );
2564
2619
scanner -> pattern = (PyObject * ) self ;
2565
2620
2621
+ PyObject_GC_Track (scanner );
2566
2622
return (PyObject * ) scanner ;
2567
2623
}
2568
2624
@@ -2684,6 +2740,8 @@ static PyType_Slot pattern_slots[] = {
2684
2740
{Py_tp_methods , pattern_methods },
2685
2741
{Py_tp_members , pattern_members },
2686
2742
{Py_tp_getset , pattern_getset },
2743
+ {Py_tp_traverse , pattern_traverse },
2744
+ {Py_tp_clear , pattern_clear },
2687
2745
{0 , NULL },
2688
2746
};
2689
2747
@@ -2692,7 +2750,7 @@ static PyType_Spec pattern_spec = {
2692
2750
.basicsize = sizeof (PatternObject ),
2693
2751
.itemsize = sizeof (SRE_CODE ),
2694
2752
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
2695
- Py_TPFLAGS_DISALLOW_INSTANTIATION ),
2753
+ Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_HAVE_GC ),
2696
2754
.slots = pattern_slots ,
2697
2755
};
2698
2756
@@ -2742,6 +2800,8 @@ static PyType_Slot match_slots[] = {
2742
2800
{Py_tp_methods , match_methods },
2743
2801
{Py_tp_members , match_members },
2744
2802
{Py_tp_getset , match_getset },
2803
+ {Py_tp_traverse , match_traverse },
2804
+ {Py_tp_clear , match_clear },
2745
2805
2746
2806
/* As mapping.
2747
2807
*
@@ -2758,7 +2818,7 @@ static PyType_Spec match_spec = {
2758
2818
.basicsize = sizeof (MatchObject ),
2759
2819
.itemsize = sizeof (Py_ssize_t ),
2760
2820
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
2761
- Py_TPFLAGS_DISALLOW_INSTANTIATION ),
2821
+ Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_HAVE_GC ),
2762
2822
.slots = match_slots ,
2763
2823
};
2764
2824
@@ -2778,14 +2838,16 @@ static PyType_Slot scanner_slots[] = {
2778
2838
{Py_tp_dealloc , scanner_dealloc },
2779
2839
{Py_tp_methods , scanner_methods },
2780
2840
{Py_tp_members , scanner_members },
2841
+ {Py_tp_traverse , scanner_traverse },
2842
+ {Py_tp_clear , scanner_clear },
2781
2843
{0 , NULL },
2782
2844
};
2783
2845
2784
2846
static PyType_Spec scanner_spec = {
2785
2847
.name = "_" SRE_MODULE ".SRE_Scanner" ,
2786
2848
.basicsize = sizeof (ScannerObject ),
2787
2849
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
2788
- Py_TPFLAGS_DISALLOW_INSTANTIATION ),
2850
+ Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_HAVE_GC ),
2789
2851
.slots = scanner_slots ,
2790
2852
};
2791
2853
0 commit comments