@@ -15,6 +15,7 @@ typedef struct {
15
15
PyTypeObject * decoder_type ;
16
16
PyTypeObject * reader_type ;
17
17
PyTypeObject * writer_type ;
18
+ PyTypeObject * multibytecodec_type ;
18
19
} _multibytecodec_state ;
19
20
20
21
static _multibytecodec_state *
@@ -26,14 +27,20 @@ _multibytecodec_get_state(PyObject *module)
26
27
}
27
28
28
29
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)
31
38
32
39
/*[clinic input]
33
40
module _multibytecodec
34
- class _multibytecodec.MultibyteCodec "MultibyteCodecObject *" "&MultibyteCodec_Type "
41
+ class _multibytecodec.MultibyteCodec "MultibyteCodecObject *" "clinic_get_state->multibytecodec_type "
35
42
[clinic start generated code]*/
36
- /*[clinic end generated code: output=da39a3ee5e6b4b0d input=6ad689546cbb5450 ]*/
43
+ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=10de8b4f74379258 ]*/
37
44
38
45
typedef struct {
39
46
PyObject * inobj ;
@@ -710,39 +717,23 @@ static struct PyMethodDef multibytecodec_methods[] = {
710
717
static void
711
718
multibytecodec_dealloc (MultibyteCodecObject * self )
712
719
{
720
+ PyTypeObject * tp = Py_TYPE (self );
713
721
PyObject_Free (self );
722
+ Py_DECREF (tp );
714
723
}
715
724
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 ,
746
737
};
747
738
748
739
@@ -1043,7 +1034,9 @@ mbiencoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1043
1034
codec = PyObject_GetAttrString ((PyObject * )type , "codec" );
1044
1035
if (codec == NULL )
1045
1036
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 )) {
1047
1040
PyErr_SetString (PyExc_TypeError , "codec is unexpected type" );
1048
1041
goto errorexit ;
1049
1042
}
@@ -1320,7 +1313,9 @@ mbidecoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1320
1313
codec = PyObject_GetAttrString ((PyObject * )type , "codec" );
1321
1314
if (codec == NULL )
1322
1315
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 )) {
1324
1319
PyErr_SetString (PyExc_TypeError , "codec is unexpected type" );
1325
1320
goto errorexit ;
1326
1321
}
@@ -1640,7 +1635,9 @@ mbstreamreader_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1640
1635
codec = PyObject_GetAttrString ((PyObject * )type , "codec" );
1641
1636
if (codec == NULL )
1642
1637
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 )) {
1644
1641
PyErr_SetString (PyExc_TypeError , "codec is unexpected type" );
1645
1642
goto errorexit ;
1646
1643
}
@@ -1850,7 +1847,9 @@ mbstreamwriter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1850
1847
codec = PyObject_GetAttrString ((PyObject * )type , "codec" );
1851
1848
if (codec == NULL )
1852
1849
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 )) {
1854
1853
PyErr_SetString (PyExc_TypeError , "codec is unexpected type" );
1855
1854
goto errorexit ;
1856
1855
}
@@ -1959,7 +1958,8 @@ _multibytecodec___create_codec(PyObject *module, PyObject *arg)
1959
1958
if (codec -> codecinit != NULL && codec -> codecinit (codec -> config ) != 0 )
1960
1959
return NULL ;
1961
1960
1962
- self = PyObject_New (MultibyteCodecObject , & MultibyteCodec_Type );
1961
+ _multibytecodec_state * state = _multibytecodec_get_state (module );
1962
+ self = PyObject_New (MultibyteCodecObject , state -> multibytecodec_type );
1963
1963
if (self == NULL )
1964
1964
return NULL ;
1965
1965
self -> codec = codec ;
@@ -1998,16 +1998,12 @@ do { \
1998
1998
PyMODINIT_FUNC
1999
1999
PyInit__multibytecodec (void )
2000
2000
{
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 );
2007
2002
if (m == NULL )
2008
2003
goto error ;
2009
2004
2010
2005
_multibytecodec_state * state = _multibytecodec_get_state (m );
2006
+ CREATE_TYPE (m , state -> multibytecodec_type , & multibytecodec_spec );
2011
2007
CREATE_TYPE (m , state -> encoder_type , & encoder_spec );
2012
2008
CREATE_TYPE (m , state -> decoder_type , & decoder_spec );
2013
2009
CREATE_TYPE (m , state -> reader_type , & reader_spec );
0 commit comments