Skip to content

Commit e376c47

Browse files
Make MultibyteCodecObject own a reference to the codec module that owns its codec memory
1 parent bdcfe44 commit e376c47

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

Modules/cjkcodecs/cjkcodecs.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,13 @@ destroy_codec_capsule(PyObject *capsule)
289289
{
290290
void *ptr = PyCapsule_GetPointer(capsule, CODEC_CAPSULE);
291291
codec_capsule *data = (codec_capsule *)ptr;
292-
fprintf(stderr, "uncapsulating %s\n", data->codec->encoding);
293292
Py_DECREF(data->cjk_module);
294293
PyMem_Free(ptr);
295294
}
296295

297296
static codec_capsule *
298297
capsulate_codec(PyObject *mod, const MultibyteCodec *codec)
299298
{
300-
fprintf(stderr, "capsulating %s\n", codec->encoding);
301299
codec_capsule *data = PyMem_Malloc(sizeof(codec_capsule));
302300
if (data == NULL) {
303301
PyErr_NoMemory();

Modules/cjkcodecs/multibytecodec.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,17 @@ static struct PyMethodDef multibytecodec_methods[] = {
720720
};
721721

722722
static int
723-
multibytecodec_traverse(PyObject *self, visitproc visit, void *arg)
723+
multibytecodec_clear(MultibyteCodecObject *self)
724+
{
725+
Py_CLEAR(self->cjk_module);
726+
return 0;
727+
}
728+
729+
static int
730+
multibytecodec_traverse(MultibyteCodecObject *self, visitproc visit, void *arg)
724731
{
725732
Py_VISIT(Py_TYPE(self));
733+
Py_VISIT(self->cjk_module);
726734
return 0;
727735
}
728736

@@ -731,6 +739,7 @@ multibytecodec_dealloc(MultibyteCodecObject *self)
731739
{
732740
PyObject_GC_UnTrack(self);
733741
PyTypeObject *tp = Py_TYPE(self);
742+
(void)multibytecodec_clear(self);
734743
tp->tp_free(self);
735744
Py_DECREF(tp);
736745
}
@@ -740,6 +749,7 @@ static PyType_Slot multibytecodec_slots[] = {
740749
{Py_tp_getattro, PyObject_GenericGetAttr},
741750
{Py_tp_methods, multibytecodec_methods},
742751
{Py_tp_traverse, multibytecodec_traverse},
752+
{Py_tp_clear, multibytecodec_clear},
743753
{0, NULL},
744754
};
745755

@@ -1969,6 +1979,7 @@ _multibytecodec___create_codec(PyObject *module, PyObject *arg)
19691979
if (self == NULL)
19701980
return NULL;
19711981
self->codec = codec;
1982+
self->cjk_module = Py_NewRef(data->cjk_module);
19721983

19731984
PyObject_GC_Track(self);
19741985
return (PyObject *)self;

Modules/cjkcodecs/multibytecodec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ typedef struct {
6363
typedef struct {
6464
PyObject_HEAD
6565
MultibyteCodec *codec;
66+
PyObject *cjk_module;
6667
} MultibyteCodecObject;
6768

6869
#define MultibyteCodec_Check(state, op) Py_IS_TYPE((op), state->multibytecodec_type)

0 commit comments

Comments
 (0)