8000 bpo-42972: Fully support GC for pyexpat, unicodedata, and dbm/gdbm heap types by erlend-aasland · Pull Request #26376 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-42972: Fully support GC for pyexpat, unicodedata, and dbm/gdbm heap types #26376

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 3 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
bpo-42972: dbm/gdbm
  • Loading branch information
Erlend E. Aasland committed May 26, 2021
commit 2c255b08ba2543c7cad9bda5a0c5ebd4d24476c2
20 changes: 15 additions & 5 deletions Modules/_dbmmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ typedef struct {
static PyObject *
newdbmobject(_dbm_state *state, const char *file, int flags, int mode)
{
dbmobject *dp;

dp = PyObject_New(dbmobject, state->dbm_type);
if (dp == NULL)
dbmobject *dp = PyObject_GC_New(dbmobject, state->dbm_type);
if (dp == NULL) {
return NULL;
}
dp->di_size = -1;
dp->flags = flags;
PyObject_GC_Track(dp);

/* See issue #19296 */
if ( (dp->di_dbm = dbm_open((char *)file, flags, mode)) == 0 ) {
PyErr_SetFromErrnoWithFilename(state->dbm_error, file);
Expand All @@ -82,10 +83,17 @@ newdbmobject(_dbm_state *state, const char *file, int flags, int mode)
}

/* Methods */
static int
dbm_traverse(dbmobject *dp, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(dp));
return 0;
}

static void
dbm_dealloc(dbmobject *dp)
{
PyObject_GC_UnTrack(dp);
if (dp->di_dbm) {
dbm_close(dp->di_dbm);
}
Expand Down Expand Up @@ -397,6 +405,7 @@ static PyMethodDef dbm_methods[] = {

static PyType_Slot dbmtype_spec_slots[] = {
{Py_tp_dealloc, dbm_dealloc},
{Py_tp_traverse, dbm_traverse},
{Py_tp_methods, dbm_methods},
{Py_sq_contains, dbm_contains},
{Py_mp_length, dbm_length},
Expand All @@ -413,7 +422,8 @@ static PyType_Spec dbmtype_spec = {
// dbmtype_spec does not have Py_TPFLAGS_BASETYPE flag
// which prevents to create a subclass.
// So calling PyType_GetModuleState() in this file is always safe.
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
Py_TPFLAGS_HAVE_GC),
.slots = dbmtype_spec_slots,
};

Expand Down
15 changes: 13 additions & 2 deletions Modules/_gdbmmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ nextkey, reorganize, and sync.");
static PyObject *
newgdbmobject(_gdbm_state *state, const char *file, int flags, int mode)
{
gdbmobject *dp = PyObject_New(gdbmobject, state->gdbm_type);
gdbmobject *dp = PyObject_GC_New(gdbmobject, state->gdbm_type);
if (dp == NULL) {
return NULL;
}
dp->di_size = -1;
errno = 0;
PyObject_GC_Track(dp);

if ((dp->di_dbm = gdbm_open((char *)file, 0, flags, mode, NULL)) == 0) {
if (errno != 0) {
PyErr_SetFromErrnoWithFilename(state->gdbm_error, file);
Expand All @@ -94,10 +96,17 @@ newgdbmobject(_gdbm_state *state, const char *file, int flags, int mode)
}

/* Methods */
static int
gdbm_traverse(gdbmobject *dp, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(dp));
return 0;
}

static void
gdbm_dealloc(gdbmobject *dp)
{
PyObject_GC_UnTrack(dp);
if (dp->di_dbm) {
gdbm_close(dp->di_dbm);
}
Expand Down Expand Up @@ -554,6 +563,7 @@ static PyMethodDef gdbm_methods[] = {

static PyType_Slot gdbmtype_spec_slots[] = {
{Py_tp_dealloc, gdbm_dealloc},
{Py_tp_traverse, gdbm_traverse},
{Py_tp_methods, gdbm_methods},
{Py_sq_contains, gdbm_contains},
{Py_mp_length, gdbm_length},
Expand All @@ -570,7 +580,8 @@ static PyType_Spec gdbmtype_spec = {
// dbmtype_spec does not have Py_TPFLAGS_BASETYPE flag
// which prevents to create a subclass.
// So calling PyType_GetModuleState() in this file is always safe.
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
Py_TPFLAGS_HAVE_GC),
.slots = gdbmtype_spec_slots,
};

Expand Down
13 changes: 9 additions & 4 deletions Modules/_lsprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,13 @@ profiler_clear(ProfilerObject *pObj, PyObject* noarg)
Py_RETURN_NONE;
}

static int
profiler_traverse(ProfilerObject *op, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(op));
return 0;
}

static void
profiler_dealloc(ProfilerObject *op)
{
Expand Down Expand Up @@ -798,16 +805,14 @@ static PyType_Slot _lsprof_profiler_type_spec_slots[] = {
{Py_tp_methods, profiler_methods},
{Py_tp_dealloc, profiler_dealloc},
{Py_tp_init, profiler_init},
{Py_tp_alloc, PyType_GenericAlloc},
{Py_tp_new, PyType_GenericNew},
{Py_tp_free, PyObject_Del},
{Py_tp_traverse, profiler_traverse},
{0, 0}
};

static PyType_Spec _lsprof_profiler_type_spec = {
.name = "_lsprof.Profiler",
.basicsize = sizeof(ProfilerObject),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
.slots = _lsprof_profiler_type_spec_slots,
};

Expand Down
0