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
Put a reference to the cjk module into the codec capsule
  • Loading branch information
erlend-aasland committed Apr 17, 2023
commit bdcfe4475f5f5396e7b88c58fcee6bdc74f1c16a
17 changes: 10 additions & 7 deletions Modules/cjkcodecs/cjkcodecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,15 @@ getmultibytecodec(void)
static void
destroy_codec_capsule(PyObject *capsule)
{
void *data = PyCapsule_GetPointer(capsule, CODEC_CAPSULE);
fprintf(stderr, "uncapsulating %s\n", ((codec_capsule *)data)->codec->encoding);
PyMem_Free(data);
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(const MultibyteCodec *codec)
capsulate_codec(PyObject *mod, const MultibyteCodec *codec)
{
fprintf(stderr, "capsulating %s\n", codec->encoding);
codec_capsule *data = PyMem_Malloc(sizeof(codec_capsule));
Expand All @@ -302,18 +304,19 @@ capsulate_codec(const MultibyteCodec *codec)
return NULL;
}
data->codec = codec;
data->cjk_module = Py_NewRef(mod);
return data;
}

static PyObject *
_getcodec(const MultibyteCodec *codec)
_getcodec(PyObject *self, const MultibyteCodec *codec)
{
PyObject *cofunc = getmultibytecodec();
if (cofunc == NULL) {
return NULL;
}

codec_capsule *data = capsulate_codec(codec);
codec_capsule *data = capsulate_codec(self, codec);
if (data == NULL) {
Py_DECREF(cofunc);
return NULL;
Expand Down Expand Up @@ -349,7 +352,7 @@ getcodec(PyObject *self, PyObject *encoding)
for (int i = 0; i < st->num_codecs; i++) {
const MultibyteCodec *codec = &st->codec_list[i];
if (strcmp(codec->encoding, enc) == 0) {
return _getcodec(codec);
return _getcodec(self, codec);
}
}

Expand Down
1 change: 1 addition & 0 deletions Modules/cjkcodecs/multibytecodec.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ typedef struct {

typedef struct {
const MultibyteCodec *codec;
PyObject *cjk_module;
} codec_capsule;

#define MAP_CAPSULE "multibytecodec.map"
Expand Down