Skip to content

Commit 85ed337

Browse files
author
Erlend E. Aasland
committed
Support multi-phase init
1 parent 5eb463f commit 85ed337

File tree

1 file changed

+68
-33
lines changed

1 file changed

+68
-33
lines changed

Modules/cjkcodecs/multibytecodec.c

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,56 +1967,91 @@ _multibytecodec___create_codec(PyObject *module, PyObject *arg)
19671967
return (PyObject *)self;
19681968
}
19691969

1970-
static struct PyMethodDef __methods[] = {
1971-
_MULTIBYTECODEC___CREATE_CODEC_METHODDEF
1972-
{NULL, NULL},
1973-
};
1970+
static int
1971+
_multibytecodec_traverse(PyObject *mod, visitproc visit, void *arg)
1972+
{
1973+
_multibytecodec_state *state = _multibytecodec_get_state(mod);
1974+
Py_VISIT(state->multibytecodec_type);
1975+
Py_VISIT(state->encoder_type);
1976+
Py_VISIT(state->decoder_type);
1977+
Py_VISIT(state->reader_type);
1978+
Py_VISIT(state->writer_type);
1979+
return 0;
1980+
}
19741981

1982+
static int
1983+
_multibytecodec_clear(PyObject *mod)
1984+
{
1985+
_multibytecodec_state *state = _multibytecodec_get_state(mod);
1986+
Py_CLEAR(state->multibytecodec_type);
1987+
Py_CLEAR(state->encoder_type);
1988+
Py_CLEAR(state->decoder_type);
1989+
Py_CLEAR(state->reader_type);
1990+
Py_CLEAR(state->writer_type);
1991+
return 0;
1992+
}
19751993

1976-
static struct PyModuleDef _multibytecodecmodule = {
1977-
.m_base = PyModuleDef_HEAD_INIT,
1978-
.m_name = "_multibytecodec",
1979-
.m_size = sizeof(_multibytecodec_state),
1980-
.m_methods = __methods,
1981-
};
1994+
static void
1995+
_multibytecodec_free(void *m)
1996+
{
1997+
_multibytecodec_clear((PyObject *)m);
1998+
}
19821999

19832000
#define CREATE_TYPE(module, type, spec) \
19842001
do { \
19852002
type = (PyTypeObject *)PyType_FromModuleAndSpec(module, spec, NULL); \
19862003
if (!type) { \
1987-
goto error; \
2004+
return -1; \
19882005
} \
19892006
} while (0)
19902007

19912008
#define ADD_TYPE(module, type) \
19922009
do { \
19932010
if (PyModule_AddType(module, type) < 0) { \
1994-
goto error; \
2011+
return -1; \
19952012
} \
19962013
} while (0)
19972014

2015+
static int
2016+
_multibytecodec_exec(PyObject *mod)
2017+
{
2018+
_multibytecodec_state *state = _multibytecodec_get_state(mod);
2019+
CREATE_TYPE(mod, state->multibytecodec_type, &multibytecodec_spec);
2020+
CREATE_TYPE(mod, state->encoder_type, &encoder_spec);
2021+
CREATE_TYPE(mod, state->decoder_type, &decoder_spec);
2022+
CREATE_TYPE(mod, state->reader_type, &reader_spec);
2023+
CREATE_TYPE(mod, state->writer_type, &writer_spec);
2024+
2025+
ADD_TYPE(mod, state->encoder_type);
2026+
ADD_TYPE(mod, state->decoder_type);
2027+
ADD_TYPE(mod, state->reader_type);
2028+
ADD_TYPE(mod, state->writer_type);
2029+
return 0;
2030+
}
2031+
2032+
static struct PyMethodDef __methods[] = {
2033+
_MULTIBYTECODEC___CREATE_CODEC_METHODDEF
2034+
{NULL, NULL},
2035+
};
2036+
2037+
static PyModuleDef_Slot _multibytecodec_slots[] = {
2038+
{Py_mod_exec, _multibytecodec_exec},
2039+
{0, NULL}
2040+
};
2041+
2042+
static struct PyModuleDef _multibytecodecmodule = {
2043+
.m_base = PyModuleDef_HEAD_INIT,
2044+
.m_name = "_multibytecodec",
2045+
.m_size = sizeof(_multibytecodec_state),
2046+
.m_methods = __methods,
2047+
.m_slots = _multibytecodec_slots,
2048+
.m_traverse = _multibytecodec_traverse,
2049+
.m_clear = _multibytecodec_clear,
2050+
.m_free = _multibytecodec_free,
2051+
};
2052+
19982053
PyMODINIT_FUNC
19992054
PyInit__multibytecodec(void)
20002055
{
2001-
PyObject *m = PyModule_Create(&_multibytecodecmodule);
2002-
if (m == NULL)
2003-
goto error;
2004-
2005-
_multibytecodec_state *state = _multibytecodec_get_state(m);
2006-
CREATE_TYPE(m, state->multibytecodec_type, &multibytecodec_spec);
2007-
CREATE_TYPE(m, state->encoder_type, &encoder_spec);
2008-
CREATE_TYPE(m, state->decoder_type, &decoder_spec);
2009-
CREATE_TYPE(m, state->reader_type, &reader_spec);
2010-
CREATE_TYPE(m, state->writer_type, &writer_spec);
2011-
2012-
ADD_TYPE(m, state->encoder_type);
2013-
ADD_TYPE(m, state->decoder_type);
2014-
ADD_TYPE(m, state->reader_type);
2015-
ADD_TYPE(m, state->writer_type);
2016-
2017-
return m;
2018-
2019-
error:
2020-
Py_XDECREF(m);
2021-
return NULL;
2056+
return PyModuleDef_Init(&_multibytecodecmodule);
20222057
}

0 commit comments

Comments
 (0)