Skip to content

Commit ac8a7b9

Browse files
author
Erlend Egeberg Aasland
authored
gh-94401: Remove unneeded bz2 module state (#94402)
1 parent 63c772d commit ac8a7b9

File tree

1 file changed

+11
-51
lines changed

1 file changed

+11
-51
lines changed

Modules/_bz2module.c

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,6 @@ OutputBuffer_OnError(_BlocksOutputBuffer *buffer)
8181
#define RELEASE_LOCK(obj) PyThread_release_lock((obj)->lock)
8282

8383

84-
typedef struct {
85-
PyTypeObject *bz2_compressor_type;
86-
PyTypeObject *bz2_decompressor_type;
87-
} _bz2_state;
88-
89-
static inline _bz2_state*
90-
get_bz2_state(PyObject *module)
91-
{
92-
void *state = PyModule_GetState(module);
93-
assert(state != NULL);
94-
return (_bz2_state *)state;
95-
}
96-
9784
typedef struct {
9885
PyObject_HEAD
9986
bz_stream bzs;
@@ -775,67 +762,40 @@ static PyType_Spec bz2_decompressor_type_spec = {
775762
static int
776763
_bz2_exec(PyObject *module)
777764
{
778-
_bz2_state *state = get_bz2_state(module);
779-
state->bz2_compressor_type = (PyTypeObject *)PyType_FromModuleAndSpec(module,
765+
PyTypeObject *bz2_compressor_type = (PyTypeObject *)PyType_FromModuleAndSpec(module,
780766
&bz2_compressor_type_spec, NULL);
781-
if (state->bz2_compressor_type == NULL) {
767+
if (bz2_compressor_type == NULL) {
782768
return -1;
783769
}
784-
785-
if (PyModule_AddType(module, state->bz2_compressor_type) < 0) {
770+
int rc = PyModule_AddType(module, bz2_compressor_type);
771+
Py_DECREF(bz2_compressor_type);
772+
if (rc < 0) {
786773
return -1;
787774
}
788775

789-
state->bz2_decompressor_type = (PyTypeObject *)PyType_FromModuleAndSpec(module,
776+
PyTypeObject *bz2_decompressor_type = (PyTypeObject *)PyType_FromModuleAndSpec(module,
790777
&bz2_decompressor_type_spec, NULL);
791-
if (state->bz2_decompressor_type == NULL) {
778+
if (bz2_decompressor_type == NULL) {
792779
return -1;
793780
}
794-
795-
if (PyModule_AddType(module, state->bz2_decompressor_type) < 0) {
781+
rc = PyModule_AddType(module, bz2_decompressor_type);
782+
Py_DECREF(bz2_decompressor_type);
783+
if (rc < 0) {
796784
return -1;
797785
}
798786

799787
return 0;
800788
}
801789

802-
static int
803-
_bz2_traverse(PyObject *module, visitproc visit, void *arg)
804-
{
805-
_bz2_state *state = get_bz2_state(module);
806-
Py_VISIT(state->bz2_compressor_type);
807-
Py_VISIT(state->bz2_decompressor_type);
808-
return 0;
809-
}
810-
811-
static int
812-
_bz2_clear(PyObject *module)
813-
{
814-
_bz2_state *state = get_bz2_state(module);
815-
Py_CLEAR(state->bz2_compressor_type);
816-
Py_CLEAR(state->bz2_decompressor_type);
817-
return 0;
818-
}
819-
820-
static void
821-
_bz2_free(void *module)
822-
{
823-
_bz2_clear((PyObject *)module);
824-
}
825-
826790
static struct PyModuleDef_Slot _bz2_slots[] = {
827791
{Py_mod_exec, _bz2_exec},
828792
{0, NULL}
829793
};
830794

831795
static struct PyModuleDef _bz2module = {
832-
PyModuleDef_HEAD_INIT,
796+
.m_base = PyModuleDef_HEAD_INIT,
833797
.m_name = "_bz2",
834-
.m_size = sizeof(_bz2_state),
835798
.m_slots = _bz2_slots,
836-
.m_traverse = _bz2_traverse,
837-
.m_clear = _bz2_clear,
838-
.m_free = _bz2_free,
839799
};
840800

841801
PyMODINIT_FUNC

0 commit comments

Comments
 (0)