From e4b8db4ceac942660d453a93ce278251dbde5170 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Tue, 28 Jun 2022 23:07:27 +0200 Subject: [PATCH 1/2] Remove unneeded bz2 module state --- Modules/_bz2module.c | 63 +++++++++----------------------------------- 1 file changed, 12 insertions(+), 51 deletions(-) diff --git a/Modules/_bz2module.c b/Modules/_bz2module.c index 798e9efc628f05..6a3cc6458ad9b0 100644 --- a/Modules/_bz2module.c +++ b/Modules/_bz2module.c @@ -81,19 +81,6 @@ OutputBuffer_OnError(_BlocksOutputBuffer *buffer) #define RELEASE_LOCK(obj) PyThread_release_lock((obj)->lock) -typedef struct { - PyTypeObject *bz2_compressor_type; - PyTypeObject *bz2_decompressor_type; -} _bz2_state; - -static inline _bz2_state* -get_bz2_state(PyObject *module) -{ - void *state = PyModule_GetState(module); - assert(state != NULL); - return (_bz2_state *)state; -} - typedef struct { PyObject_HEAD bz_stream bzs; @@ -775,67 +762,41 @@ static PyType_Spec bz2_decompressor_type_spec = { static int _bz2_exec(PyObject *module) { - _bz2_state *state = get_bz2_state(module); - state->bz2_compressor_type = (PyTypeObject *)PyType_FromModuleAndSpec(module, + PyTypeObject *bz2_compressor_type = (PyTypeObject *)PyType_FromModuleAndSpec(module, &bz2_compressor_type_spec, NULL); - if (state->bz2_compressor_type == NULL) { + if (bz2_compressor_type == NULL) { return -1; } - - if (PyModule_AddType(module, state->bz2_compressor_type) < 0) { + int rc = PyModule_AddType(module, bz2_compressor_type); + Py_DECREF(bz2_compressor_type); + if (rc < 0) { return -1; } - state->bz2_decompressor_type = (PyTypeObject *)PyType_FromModuleAndSpec(module, + PyTypeObject *bz2_decompressor_type = (PyTypeObject *)PyType_FromModuleAndSpec(module, &bz2_decompressor_type_spec, NULL); - if (state->bz2_decompressor_type == NULL) { + if (bz2_decompressor_type == NULL) { return -1; } - - if (PyModule_AddType(module, state->bz2_decompressor_type) < 0) { + rc = PyModule_AddType(module, bz2_decompressor_type); + Py_DECREF(bz2_decompressor_type); + if (rc < 0) { return -1; } return 0; } -static int -_bz2_traverse(PyObject *module, visitproc visit, void *arg) -{ - _bz2_state *state = get_bz2_state(module); - Py_VISIT(state->bz2_compressor_type); - Py_VISIT(state->bz2_decompressor_type); - return 0; -} - -static int -_bz2_clear(PyObject *module) -{ - _bz2_state *state = get_bz2_state(module); - Py_CLEAR(state->bz2_compressor_type); - Py_CLEAR(state->bz2_decompressor_type); - return 0; -} - -static void -_bz2_free(void *module) -{ - _bz2_clear((PyObject *)module); -} - static struct PyModuleDef_Slot _bz2_slots[] = { {Py_mod_exec, _bz2_exec}, {0, NULL} }; static struct PyModuleDef _bz2module = { - PyModuleDef_HEAD_INIT, + .m_base = PyModuleDef_HEAD_INIT, .m_name = "_bz2", - .m_size = sizeof(_bz2_state), + .m_size = 0, .m_slots = _bz2_slots, - .m_traverse = _bz2_traverse, - .m_clear = _bz2_clear, - .m_free = _bz2_free, }; PyMODINIT_FUNC From 1174b16837e6ddd41593b9851a8f6b5141ad7992 Mon Sep 17 00:00:00 2001 From: Erlend Egeberg Aasland Date: Wed, 29 Jun 2022 11:21:26 +0200 Subject: [PATCH 2/2] Update Modules/_bz2module.c --- Modules/_bz2module.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Modules/_bz2module.c b/Modules/_bz2module.c index 6a3cc6458ad9b0..9304c13fbed5fc 100644 --- a/Modules/_bz2module.c +++ b/Modules/_bz2module.c @@ -795,7 +795,6 @@ static struct PyModuleDef_Slot _bz2_slots[] = { static struct PyModuleDef _bz2module = { .m_base = PyModuleDef_HEAD_INIT, .m_name = "_bz2", - .m_size = 0, .m_slots = _bz2_slots, };