Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
Next Next commit
Make MultibyteCodecObject own a reference to the codec module that ow…
…ns its codec memory
  • Loading branch information
erlend-aasland committed Apr 17, 2023
commit e376c475fb53dac3c56bd58df6750df26cd86e31
2 changes: 0 additions & 2 deletions Modules/cjkcodecs/cjkcodecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,13 @@ destroy_codec_capsule(PyObject *capsule)
{
void *ptr = PyCapsule_GetPointer(capsule, CODEC_CAPSULE);
codec_capsule *data = (codec_capsule *)ptr;
fprintf(stderr, "uncapsulating %s\n", data->codec->encoding);
Py_DECREF(data->cjk_module);
PyMem_Free(ptr);
}

static codec_capsule *
capsulate_codec(PyObject *mod, const MultibyteCodec *codec)
{
fprintf(stderr, "capsulating %s\n", codec->encoding);
codec_capsule *data = PyMem_Malloc(sizeof(codec_capsule));
if (data == NULL) {
PyErr_NoMemory();
Expand Down
13 changes: 12 additions & 1 deletion Modules/cjkcodecs/multibytecodec.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,17 @@ static struct PyMethodDef multibytecodec_methods[] = {
};

static int
multibytecodec_traverse(PyObject *self, visitproc visit, void *arg)
multibytecodec_clear(MultibyteCodecObject *self)
{
Py_CLEAR(self->cjk_module);
return 0;
}

static int
multibytecodec_traverse(MultibyteCodecObject *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->cjk_module);
return 0;
}

Expand All @@ -731,6 +739,7 @@ multibytecodec_dealloc(MultibyteCodecObject *self)
{
PyObject_GC_UnTrack(self);
PyTypeObject *tp = Py_TYPE(self);
(void)multibytecodec_clear(self);
tp->tp_free(self);
Py_DECREF(tp);
}
Expand All @@ -740,6 +749,7 @@ static PyType_Slot multibytecodec_slots[] = {
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_methods, multibytecodec_methods},
{Py_tp_traverse, multibytecodec_traverse},
{Py_tp_clear, multibytecodec_clear},
{0, NULL},
};

Expand Down Expand Up @@ -1969,6 +1979,7 @@ _multibytecodec___create_codec(PyObject *module, PyObject *arg)
if (self == NULL)
return NULL;
self->codec = codec;
self->cjk_module = Py_NewRef(data->cjk_module);

PyObject_GC_Track(self);
return (PyObject *)self;
Expand Down
1 change: 1 addition & 0 deletions Modules/cjkcodecs/multibytecodec.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ typedef struct {
typedef struct {
PyObject_HEAD
MultibyteCodec *codec;
PyObject *cjk_module;
} MultibyteCodecObject;

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