@@ -562,17 +562,36 @@ pattern_error(Py_ssize_t status)
562562 }
563563}
564564
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+
565584static void
566585pattern_dealloc(PatternObject* self)
567586{
568587 PyTypeObject *tp = Py_TYPE(self);
569588
570- if (self->weakreflist != NULL)
589+ PyObject_GC_UnTrack(self);
590+ if (self->weakreflist != NULL) {
571591 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);
576595 Py_DECREF(tp);
577596}
578597
@@ -1396,7 +1415,7 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
13961415
13971416 n = PyList_GET_SIZE(code);
13981417 /* 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);
14001419 if (!self)
14011420 return NULL;
14021421 self->weakreflist = NULL;
@@ -1416,6 +1435,7 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
14161435 break;
14171436 }
14181437 }
1438+ PyObject_GC_Track(self);
14191439
14201440 if (PyErr_Occurred()) {
14211441 Py_DECREF(self);
@@ -1937,15 +1957,33 @@ _validate(PatternObject *self)
19371957/* -------------------------------------------------------------------- */
19381958/* match methods */
19391959
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+
19401979static void
19411980match_dealloc(MatchObject* self)
19421981{
19431982 PyTypeObject *tp = Py_TYPE(self);
19441983
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);
19491987 Py_DECREF(tp);
19501988}
19511989
@@ -2391,9 +2429,9 @@ pattern_new_match(_sremodulestate* module_state,
23912429
23922430 /* create match object (with room for extra group marks) */
23932431 /* 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));
23972435 if (!match)
23982436 return NULL;
23992437
@@ -2426,6 +2464,7 @@ pattern_new_match(_sremodulestate* module_state,
24262464
24272465 match->lastindex = state->lastindex;
24282466
2467+ PyObject_GC_Track(match);
24292468 return (PyObject*) match;
24302469
24312470 } else if (status == 0) {
@@ -2444,14 +2483,30 @@ pattern_new_match(_sremodulestate* module_state,
24442483/* -------------------------------------------------------------------- */
24452484/* scanner methods (experimental) */
24462485
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+
24472501static void
24482502scanner_dealloc(ScannerObject* self)
24492503{
24502504 PyTypeObject *tp = Py_TYPE(self);
24512505
2506+ PyObject_GC_UnTrack(self);
24522507 state_fini(&self->state);
2453- Py_XDECREF( self->pattern );
2454- PyObject_Free (self);
2508+ (void)scanner_clear( self);
2509+ tp->tp_free (self);
24552510 Py_DECREF(tp);
24562511}
24572512
@@ -2548,7 +2603,7 @@ pattern_scanner(_sremodulestate *module_state,
25482603 ScannerObject* scanner;
25492604
25502605 /* create scanner object */
2551- scanner = PyObject_New (ScannerObject, module_state->Scanner_Type);
2606+ scanner = PyObject_GC_New (ScannerObject, module_state->Scanner_Type);
25522607 if (!scanner)
25532608 return NULL;
25542609 scanner->pattern = NULL;
@@ -2562,6 +2617,7 @@ pattern_scanner(_sremodulestate *module_state,
25622617 Py_INCREF(self);
25632618 scanner->pattern = (PyObject*) self;
25642619
2620+ PyObject_GC_Track(scanner);
25652621 return (PyObject*) scanner;
25662622}
25672623
@@ -2683,6 +2739,8 @@ static PyType_Slot pattern_slots[] = {
26832739 {Py_tp_methods, pattern_methods},
26842740 {Py_tp_members, pattern_members},
26852741 {Py_tp_getset, pattern_getset},
2742+ {Py_tp_traverse, pattern_traverse},
2743+ {Py_tp_clear, pattern_clear},
26862744 {0, NULL},
26872745};
26882746
@@ -2691,7 +2749,7 @@ static PyType_Spec pattern_spec = {
26912749 .basicsize = sizeof(PatternObject),
26922750 .itemsize = sizeof(SRE_CODE),
26932751 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
2694- Py_TPFLAGS_DISALLOW_INSTANTIATION),
2752+ Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_HAVE_GC ),
26952753 .slots = pattern_slots,
26962754};
26972755
@@ -2741,6 +2799,8 @@ static PyType_Slot match_slots[] = {
27412799 {Py_tp_methods, match_methods},
27422800 {Py_tp_members, match_members},
27432801 {Py_tp_getset, match_getset},
2802+ {Py_tp_traverse, match_traverse},
2803+ {Py_tp_clear, match_clear},
27442804
27452805 /* As mapping.
27462806 *
@@ -2757,7 +2817,7 @@ static PyType_Spec match_spec = {
27572817 .basicsize = sizeof(MatchObject),
27582818 .itemsize = sizeof(Py_ssize_t),
27592819 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
2760- Py_TPFLAGS_DISALLOW_INSTANTIATION),
2820+ Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_HAVE_GC ),
27612821 .slots = match_slots,
27622822};
27632823
@@ -2777,14 +2837,16 @@ static PyType_Slot scanner_slots[] = {
27772837 {Py_tp_dealloc, scanner_dealloc},
27782838 {Py_tp_methods, scanner_methods},
27792839 {Py_tp_members, scanner_members},
2840+ {Py_tp_traverse, scanner_traverse},
2841+ {Py_tp_clear, scanner_clear},
27802842 {0, NULL},
27812843};
27822844
27832845static PyType_Spec scanner_spec = {
27842846 .name = "_" SRE_MODULE ".SRE_Scanner",
27852847 .basicsize = sizeof(ScannerObject),
27862848 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
2787- Py_TPFLAGS_DISALLOW_INSTANTIATION),
2849+ Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_HAVE_GC ),
27882850 .slots = scanner_slots,
27892851};
27902852
0 commit comments