Skip to content

bpo-42972: Fully implement GC protocol for re types #26368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 81 additions & 19 deletions Modules/_sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,17 +563,36 @@ pattern_error(Py_ssize_t status)
}
}

static int
pattern_traverse(PatternObject *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->groupindex);
Py_VISIT(self->indexgroup);
Py_VISIT(self->pattern);
return 0;
}

static int
pattern_clear(PatternObject *self)
{
Py_CLEAR(self->groupindex);
Py_CLEAR(self->indexgroup);
Py_CLEAR(self->pattern);
return 0;
}

static void
pattern_dealloc(PatternObject* self)
{
PyTypeObject *tp = Py_TYPE(self);

if (self->weakreflist != NULL)
PyObject_GC_UnTrack(self);
if (self->weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject *) self);
Py_XDECREF(self->pattern);
Py_XDECREF(self->groupindex);
Py_XDECREF(self->indexgroup);
PyObject_Free(self);
}
(void)pattern_clear(self);
tp->tp_free(self);
Py_DECREF(tp);
}

Expand Down Expand Up @@ -1397,7 +1416,7 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,

n = PyList_GET_SIZE(code);
/* coverity[ampersand_in_size] */
self = PyObject_NewVar(PatternObject, module_state->Pattern_Type, n);
self = PyObject_GC_NewVar(PatternObject, module_state->Pattern_Type, n);
if (!self)
return NULL;
self->weakreflist = NULL;
Expand All @@ -1417,6 +1436,7 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
break;
}
}
PyObject_GC_Track(self);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pablogsal I'm unsure if this it the correct place to start tracking the newly created pattern object. My reasoning was that AFAICS, it seems to be fully initialised here, unless I've missed something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it's the right place.

It must be called before the first DECREF(self), otherwise PyObject_GC_UnTrack() crash in dealloc.

Usually, it's better to only track an object once it's fully initialized, but here it's more convient to do it here to be able to simply call DECREF() on error. The bare minimum is that traverse must be crash when an object started to be tracked. I added a test in debug mode for that: see traverse(visit_validate) in PyObject_GC_Track().


if (PyErr_Occurred()) {
Py_DECREF(self);
Expand Down Expand Up @@ -1938,15 +1958,33 @@ _validate(PatternObject *self)
/* -------------------------------------------------------------------- */
/* match methods */

static int
match_traverse(MatchObject *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->string);
Py_VISIT(self->regs);
Py_VISIT(self->pattern);
return 0;
}

static int
match_clear(MatchObject *self)
{
Py_CLEAR(self->string);
Py_CLEAR(self->regs);
Py_CLEAR(self->pattern);
return 0;
}

static void
match_dealloc(MatchObject* self)
{
PyTypeObject *tp = Py_TYPE(self);

Py_XDECREF(self->regs);
Py_XDECREF(self->string);
Py_DECREF(self->pattern);
PyObject_Free(self);
PyObject_GC_UnTrack(self);
(void)match_clear(self);
tp->tp_free(self);
Py_DECREF(tp);
}

Expand Down Expand Up @@ -2392,9 +2430,9 @@ pattern_new_match(_sremodulestate* module_state,

/* create match object (with room for extra group marks) */
/* coverity[ampersand_in_size] */
match = PyObject_NewVar(MatchObject,
module_state->Match_Type,
2*(pattern->groups+1));
match = PyObject_GC_NewVar(MatchObject,
module_state->Match_Type,
2*(pattern->groups+1));
if (!match)
return NULL;

Expand Down Expand Up @@ -2427,6 +2465,7 @@ pattern_new_match(_sremodulestate* module_state,

match->lastindex = state->lastindex;

PyObject_GC_Track(match);
return (PyObject*) match;

} else if (status == 0) {
Expand All @@ -2445,14 +2484,30 @@ pattern_new_match(_sremodulestate* module_state,
/* -------------------------------------------------------------------- */
/* scanner methods (experimental) */

static int
scanner_traverse(ScannerObject *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->pattern);
return 0;
}

static int
scanner_clear(ScannerObject *self)
{
Py_CLEAR(self->pattern);
return 0;
}

static void
scanner_dealloc(ScannerObject* self)
{
PyTypeObject *tp = Py_TYPE(self);

PyObject_GC_UnTrack(self);
state_fini(&self->state);
Py_XDECREF(self->pattern);
PyObject_Free(self);
(void)scanner_clear(self);
tp->tp_free(self);
Py_DECREF(tp);
}

Expand Down Expand Up @@ -2549,7 +2604,7 @@ pattern_scanner(_sremodulestate *module_state,
ScannerObject* scanner;

/* create scanner object */
scanner = PyObject_New(ScannerObject, module_state->Scanner_Type);
scanner = PyObject_GC_New(ScannerObject, module_state->Scanner_Type);
if (!scanner)
return NULL;
scanner->pattern = NULL;
Expand All @@ -2563,6 +2618,7 @@ pattern_scanner(_sremodulestate *module_state,
Py_INCREF(self);
scanner->pattern = (PyObject*) self;

PyObject_GC_Track(scanner);
return (PyObject*) scanner;
}

Expand Down Expand Up @@ -2684,6 +2740,8 @@ static PyType_Slot pattern_slots[] = {
{Py_tp_methods, pattern_methods},
{Py_tp_members, pattern_members},
{Py_tp_getset, pattern_getset},
{Py_tp_traverse, pattern_traverse},
{Py_tp_clear, pattern_clear},
{0, NULL},
};

Expand All @@ -2692,7 +2750,7 @@ static PyType_Spec pattern_spec = {
.basicsize = sizeof(PatternObject),
.itemsize = sizeof(SRE_CODE),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
Py_TPFLAGS_DISALLOW_INSTANTIATION),
Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_HAVE_GC),
.slots = pattern_slots,
};

Expand Down Expand Up @@ -2742,6 +2800,8 @@ static PyType_Slot match_slots[] = {
{Py_tp_methods, match_methods},
{Py_tp_members, match_members},
{Py_tp_getset, match_getset},
{Py_tp_traverse, match_traverse},
{Py_tp_clear, match_clear},

/* As mapping.
*
Expand All @@ -2758,7 +2818,7 @@ static PyType_Spec match_spec = {
.basicsize = sizeof(MatchObject),
.itemsize = sizeof(Py_ssize_t),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
Py_TPFLAGS_DISALLOW_INSTANTIATION),
Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_HAVE_GC),
.slots = match_slots,
};

Expand All @@ -2778,14 +2838,16 @@ static PyType_Slot scanner_slots[] = {
{Py_tp_dealloc, scanner_dealloc},
{Py_tp_methods, scanner_methods},
{Py_tp_members, scanner_members},
{Py_tp_traverse, scanner_traverse},
{Py_tp_clear, scanner_clear},
{0, NULL},
};

static PyType_Spec scanner_spec = {
.name = "_" SRE_MODULE ".SRE_Scanner",
.basicsize = sizeof(ScannerObject),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
Py_TPFLAGS_DISALLOW_INSTANTIATION),
Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_HAVE_GC),
.slots = scanner_slots,
};

Expand Down