Skip to content

Commit 5eb463f

Browse files
author
Erlend E. Aasland
committed
Convert multibytecode type to heap type
1 parent 2e7e452 commit 5eb463f

File tree

2 files changed

+42
-46
lines changed

2 files changed

+42
-46
lines changed

Modules/cjkcodecs/multibytecodec.c

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ typedef struct {
1515
PyTypeObject *decoder_type;
1616
PyTypeObject *reader_type;
1717
PyTypeObject *writer_type;
18+
PyTypeObject *multibytecodec_type;
1819
} _multibytecodec_state;
1920

2021
static _multibytecodec_state *
@@ -26,14 +27,20 @@ _multibytecodec_get_state(PyObject *module)
2627
}
2728

2829
static struct PyModuleDef _multibytecodecmodule;
29-
#define clinic_get_state() \
30-
(_multibytecodec_get_state(_PyType_GetModuleByDef(type, &_multibytecodecmodule)))
30+
static _multibytecodec_state *
31+
_multibyte_codec_find_state_by_type(PyTypeObject *type)
32+
{
33+
PyObject *module = _PyType_GetModuleByDef(type, &_multibytecodecmodule);
34+
assert(module != NULL);
35+
return _multibytecodec_get_state(module);
36+
}
37+
#define clinic_get_state() _multibyte_codec_find_state_by_type(type)
3138

3239
/*[clinic input]
3340
module _multibytecodec
34-
class _multibytecodec.MultibyteCodec "MultibyteCodecObject *" "&MultibyteCodec_Type"
41+
class _multibytecodec.MultibyteCodec "MultibyteCodecObject *" "clinic_get_state->multibytecodec_type"
3542
[clinic start generated code]*/
36-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6ad689546cbb5450]*/
43+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=10de8b4f74379258]*/
3744

3845
typedef struct {
3946
PyObject *inobj;
@@ -710,39 +717,23 @@ static struct PyMethodDef multibytecodec_methods[] = {
710717
static void
711718
multibytecodec_dealloc(MultibyteCodecObject *self)
712719
{
720+
PyTypeObject *tp = Py_TYPE(self);
713721
PyObject_Free(self);
722+
Py_DECREF(tp);
714723
}
715724

716-
static PyTypeObject MultibyteCodec_Type = {
717-
PyVarObject_HEAD_INIT(NULL, 0)
718-
"MultibyteCodec", /* tp_name */
719-
sizeof(MultibyteCodecObject), /* tp_basicsize */
720-
0, /* tp_itemsize */
721-
/* methods */
722-
(destructor)multibytecodec_dealloc, /* tp_dealloc */
723-
0, /* tp_vectorcall_offset */
724-
0, /* tp_getattr */
725-
0, /* tp_setattr */
726-
0, /* tp_as_async */
727-
0, /* tp_repr */
728-
0, /* tp_as_number */
729-
0, /* tp_as_sequence */
730-
0, /* tp_as_mapping */
731-
0, /* tp_hash */
732-
0, /* tp_call */
733-
0, /* tp_str */
734-
PyObject_GenericGetAttr, /* tp_getattro */
735-
0, /* tp_setattro */
736-
0, /* tp_as_buffer */
737-
Py_TPFLAGS_DEFAULT, /* tp_flags */
738-
0, /* tp_doc */
739-
0, /* tp_traverse */
740-
0, /* tp_clear */
741-
0, /* tp_richcompare */
742-
0, /* tp_weaklistoffset */
743-
0, /* tp_iter */
744-
0, /* tp_iterext */
745-
multibytecodec_methods, /* tp_methods */
725+
static PyType_Slot multibytecodec_slots[] = {
726+
{Py_tp_dealloc, multibytecodec_dealloc},
727+
{Py_tp_getattro, PyObject_GenericGetAttr},
728+
{Py_tp_methods, multibytecodec_methods},
729+
{0, NULL},
730+
};
731+
732+
static PyType_Spec multibytecodec_spec = {
733+
.name = "MultibyteCodec",
734+
.basicsize = sizeof(MultibyteCodecObject),
735+
.flags = Py_TPFLAGS_DEFAULT,
736+
.slots = multibytecodec_slots,
746737
};
747738

748739

@@ -1043,7 +1034,9 @@ mbiencoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
10431034
codec = PyObject_GetAttrString((PyObject *)type, "codec");
10441035
if (codec == NULL)
10451036
goto errorexit;
1046-
if (!MultibyteCodec_Check(codec)) {
1037+
1038+
_multibytecodec_state *state = _multibyte_codec_find_state_by_type(type);
1039+
if (!MultibyteCodec_Check(state, codec)) {
10471040
PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
10481041
goto errorexit;
10491042
}
@@ -1320,7 +1313,9 @@ mbidecoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
13201313
codec = PyObject_GetAttrString((PyObject *)type, "codec");
13211314
if (codec == NULL)
13221315
goto errorexit;
1323-
if (!MultibyteCodec_Check(codec)) {
1316+
1317+
_multibytecodec_state *state = _multibyte_codec_find_state_by_type(type);
1318+
if (!MultibyteCodec_Check(state, codec)) {
13241319
PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
13251320
goto errorexit;
13261321
}
@@ -1640,7 +1635,9 @@ mbstreamreader_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
16401635
codec = PyObject_GetAttrString((PyObject *)type, "codec");
16411636
if (codec == NULL)
16421637
goto errorexit;
1643-
if (!MultibyteCodec_Check(codec)) {
1638+
1639+
_multibytecodec_state *state = _multibyte_codec_find_state_by_type(type);
1640+
if (!MultibyteCodec_Check(state, codec)) {
16441641
PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
16451642
goto errorexit;
16461643
}
@@ -1850,7 +1847,9 @@ mbstreamwriter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
18501847
codec = PyObject_GetAttrString((PyObject *)type, "codec");
18511848
if (codec == NULL)
18521849
goto errorexit;
1853-
if (!MultibyteCodec_Check(codec)) {
1850+
1851+
_multibytecodec_state *state = _multibyte_codec_find_state_by_type(type);
1852+
if (!MultibyteCodec_Check(state, codec)) {
18541853
PyErr_SetString(PyExc_TypeError, "codec is unexpected type");
18551854
goto errorexit;
18561855
}
@@ -1959,7 +1958,8 @@ _multibytecodec___create_codec(PyObject *module, PyObject *arg)
19591958
if (codec->codecinit != NULL && codec->codecinit(codec->config) != 0)
19601959
return NULL;
19611960

1962-
self = PyObject_New(MultibyteCodecObject, &MultibyteCodec_Type);
1961+
_multibytecodec_state *state = _multibytecodec_get_state(module);
1962+
self = PyObject_New(MultibyteCodecObject, state->multibytecodec_type);
19631963
if (self == NULL)
19641964
return NULL;
19651965
self->codec = codec;
@@ -1998,16 +1998,12 @@ do { \
19981998
PyMODINIT_FUNC
19991999
PyInit__multibytecodec(void)
20002000
{
2001-
PyObject *m = NULL;
2002-
2003-
if (PyType_Ready(&MultibyteCodec_Type) < 0)
2004-
goto error;
2005-
2006-
m = PyModule_Create(&_multibytecodecmodule);
2001+
PyObject *m = PyModule_Create(&_multibytecodecmodule);
20072002
if (m == NULL)
20082003
goto error;
20092004

20102005
_multibytecodec_state *state = _multibytecodec_get_state(m);
2006+
CREATE_TYPE(m, state->multibytecodec_type, &multibytecodec_spec);
20112007
CREATE_TYPE(m, state->encoder_type, &encoder_spec);
20122008
CREATE_TYPE(m, state->decoder_type, &decoder_spec);
20132009
CREATE_TYPE(m, state->reader_type, &reader_spec);

Modules/cjkcodecs/multibytecodec.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ typedef struct {
6565
MultibyteCodec *codec;
6666
} MultibyteCodecObject;
6767

68-
#define MultibyteCodec_Check(op) Py_IS_TYPE((op), &MultibyteCodec_Type)
68+
#define MultibyteCodec_Check(state, op) Py_IS_TYPE((op), state->multibytecodec_type)
6969

7070
#define _MultibyteStatefulCodec_HEAD \
7171
PyObject_HEAD \

0 commit comments

Comments
 (0)