aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2021-01-08 15:43:59 +0100
committerGitHub <noreply@github.com>2021-01-08 15:43:59 +0100
commit11ef53aefbecfac18b63cee518a7184f771f708c (patch)
tree22334fa8cc5a2510b518e2bc6ef91e10cf2d942b /Modules
parentbpo-42866: Fix refleak in CJK getcodec() (GH-24165) (diff)
downloadcpython-11ef53aefbecfac18b63cee518a7184f771f708c.tar.gz
cpython-11ef53aefbecfac18b63cee518a7184f771f708c.tar.bz2
cpython-11ef53aefbecfac18b63cee518a7184f771f708c.zip
bpo-42866: Add traverse func to _multibytecodec.MultibyteCodec (GH-24166)
Convert _multibytecodec.MultibyteCodec type to a GC type and adds a traverse function.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/cjkcodecs/multibytecodec.c16
1 files changed, 13 insertions, 3 deletions
diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c
index 4f34b8a82fb..5070c983d40 100644
--- a/Modules/cjkcodecs/multibytecodec.c
+++ b/Modules/cjkcodecs/multibytecodec.c
@@ -721,11 +721,19 @@ static struct PyMethodDef multibytecodec_methods[] = {
{NULL, NULL},
};
+static int
+multibytecodec_traverse(PyObject *self, visitproc visit, void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ return 0;
+}
+
static void
multibytecodec_dealloc(MultibyteCodecObject *self)
{
+ PyObject_GC_UnTrack(self);
PyTypeObject *tp = Py_TYPE(self);
- PyObject_Free(self);
+ tp->tp_free(self);
Py_DECREF(tp);
}
@@ -733,13 +741,14 @@ static PyType_Slot multibytecodec_slots[] = {
{Py_tp_dealloc, multibytecodec_dealloc},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_methods, multibytecodec_methods},
+ {Py_tp_traverse, multibytecodec_traverse},
{0, NULL},
};
static PyType_Spec multibytecodec_spec = {
.name = MODULE_NAME ".MultibyteCodec",
.basicsize = sizeof(MultibyteCodecObject),
- .flags = Py_TPFLAGS_DEFAULT,
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.slots = multibytecodec_slots,
};
@@ -1944,11 +1953,12 @@ _multibytecodec___create_codec(PyObject *module, PyObject *arg)
return NULL;
_multibytecodec_state *state = _multibytecodec_get_state(module);
- self = PyObject_New(MultibyteCodecObject, state->multibytecodec_type);
+ self = PyObject_GC_New(MultibyteCodecObject, state->multibytecodec_type);
if (self == NULL)
return NULL;
self->codec = codec;
+ PyObject_GC_Track(self);
return (PyObject *)self;
}