[Python-checkins] bpo-1635741: Port _bz2 extension module to multiphase initialization(PEP 489) (GH-18050)

Hai Shi webhook-mailer at python.org
Tue Feb 18 06:17:46 EST 2020


https://github.com/python/cpython/commit/5d38517aa1836542a5417b724c093bcb245f0f47
commit: 5d38517aa1836542a5417b724c093bcb245f0f47
branch: master
author: Hai Shi <shihai1992 at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-02-18T03:17:38-08:00
summary:

bpo-1635741: Port _bz2 extension module to multiphase initialization(PEP 489) (GH-18050)



https://bugs.python.org/issue1635741

files:
A Misc/NEWS.d/next/Core and Builtins/2020-01-18-11-06-28.bpo-1635741.OKROOt.rst
M Modules/_bz2module.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-01-18-11-06-28.bpo-1635741.OKROOt.rst b/Misc/NEWS.d/next/Core and Builtins/2020-01-18-11-06-28.bpo-1635741.OKROOt.rst
new file mode 100644
index 0000000000000..d3f12a747963a
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-01-18-11-06-28.bpo-1635741.OKROOt.rst	
@@ -0,0 +1 @@
+Port _bz2 extension module to multiphase initialization (:pep:`489`).
\ No newline at end of file
diff --git a/Modules/_bz2module.c b/Modules/_bz2module.c
index 31bbf66104119..fe5880989873e 100644
--- a/Modules/_bz2module.c
+++ b/Modules/_bz2module.c
@@ -728,13 +728,45 @@ static PyTypeObject BZ2Decompressor_Type = {
 
 /* Module initialization. */
 
+static int
+_bz2_exec(PyObject *module)
+{
+    if (PyType_Ready(&BZ2Compressor_Type) < 0) {
+        return -1;
+    }
+    if (PyType_Ready(&BZ2Decompressor_Type) < 0) {
+        return -1;
+    }
+
+    Py_INCREF(&BZ2Compressor_Type);
+    if (PyModule_AddObject(module, "BZ2Compressor",
+                           (PyObject *)&BZ2Compressor_Type) < 0) {
+        Py_DECREF(&BZ2Compressor_Type);
+        return -1;
+    }
+
+    Py_INCREF(&BZ2Decompressor_Type);
+    if (PyModule_AddObject(module, "BZ2Decompressor",
+                           (PyObject *)&BZ2Decompressor_Type) < 0) {
+        Py_INCREF(&BZ2Decompressor_Type);
+        return -1;
+    }
+
+    return 0;
+}
+
+static struct PyModuleDef_Slot _bz2_slots[] = {
+    {Py_mod_exec, _bz2_exec},
+    {0, NULL}
+};
+
 static struct PyModuleDef _bz2module = {
     PyModuleDef_HEAD_INIT,
     "_bz2",
     NULL,
-    -1,
-    NULL,
+    0,
     NULL,
+    _bz2_slots,
     NULL,
     NULL,
     NULL
@@ -743,23 +775,5 @@ static struct PyModuleDef _bz2module = {
 PyMODINIT_FUNC
 PyInit__bz2(void)
 {
-    PyObject *m;
-
-    if (PyType_Ready(&BZ2Compressor_Type) < 0)
-        return NULL;
-    if (PyType_Ready(&BZ2Decompressor_Type) < 0)
-        return NULL;
-
-    m = PyModule_Create(&_bz2module);
-    if (m == NULL)
-        return NULL;
-
-    Py_INCREF(&BZ2Compressor_Type);
-    PyModule_AddObject(m, "BZ2Compressor", (PyObject *)&BZ2Compressor_Type);
-
-    Py_INCREF(&BZ2Decompressor_Type);
-    PyModule_AddObject(m, "BZ2Decompressor",
-                       (PyObject *)&BZ2Decompressor_Type);
-
-    return m;
+    return PyModuleDef_Init(&_bz2module);
 }



More information about the Python-checkins mailing list