Skip to content

Commit 5d38517

Browse files
authored
bpo-1635741: Port _bz2 extension module to multiphase initialization(PEP 489) (GH-18050)
https://bugs.python.org/issue1635741
1 parent 8edfc47 commit 5d38517

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port _bz2 extension module to multiphase initialization (:pep:`489`).

Modules/_bz2module.c

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -728,13 +728,45 @@ static PyTypeObject BZ2Decompressor_Type = {
728728

729729
/* Module initialization. */
730730

731+
static int
732+
_bz2_exec(PyObject *module)
733+
{
734+
if (PyType_Ready(&BZ2Compressor_Type) < 0) {
735+
return -1;
736+
}
737+
if (PyType_Ready(&BZ2Decompressor_Type) < 0) {
738+
return -1;
739+
}
740+
741+
Py_INCREF(&BZ2Compressor_Type);
742+
if (PyModule_AddObject(module, "BZ2Compressor",
743+
(PyObject *)&BZ2Compressor_Type) < 0) {
744+
Py_DECREF(&BZ2Compressor_Type);
745+
return -1;
746+
}
747+
748+
Py_INCREF(&BZ2Decompressor_Type);
749+
if (PyModule_AddObject(module, "BZ2Decompressor",
750+
(PyObject *)&BZ2Decompressor_Type) < 0) {
751+
Py_INCREF(&BZ2Decompressor_Type);
752+
return -1;
753+
}
754+
755+
return 0;
756+
}
757+
758+
static struct PyModuleDef_Slot _bz2_slots[] = {
759+
{Py_mod_exec, _bz2_exec},
760+
{0, NULL}
761+
};
762+
731763
static struct PyModuleDef _bz2module = {
732764
PyModuleDef_HEAD_INIT,
733765
"_bz2",
734766
NULL,
735-
-1,
736-
NULL,
767+
0,
737768
NULL,
769+
_bz2_slots,
738770
NULL,
739771
NULL,
740772
NULL
@@ -743,23 +775,5 @@ static struct PyModuleDef _bz2module = {
743775
PyMODINIT_FUNC
744776
PyInit__bz2(void)
745777
{
746-
PyObject *m;
747-
748-
if (PyType_Ready(&BZ2Compressor_Type) < 0)
749-
return NULL;
750-
if (PyType_Ready(&BZ2Decompressor_Type) < 0)
751-
return NULL;
752-
753-
m = PyModule_Create(&_bz2module);
754-
if (m == NULL)
755-
return NULL;
756-
757-
Py_INCREF(&BZ2Compressor_Type);
758-
PyModule_AddObject(m, "BZ2Compressor", (PyObject *)&BZ2Compressor_Type);
759-
760-
Py_INCREF(&BZ2Decompressor_Type);
761-
PyModule_AddObject(m, "BZ2Decompressor",
762-
(PyObject *)&BZ2Decompressor_Type);
763-
764-
return m;
778+
return PyModuleDef_Init(&_bz2module);
765779
}

0 commit comments

Comments
 (0)