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