From 1b5088cf008325ad20eb924e4491fed4b4d49551 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 18 Mar 2020 19:48:57 +0900 Subject: [PATCH 1/2] bpo-1635741: Port _heapq module to multiphase initialization. --- ...2020-03-18-19-48-53.bpo-1635741.ELEihr.rst | 1 + Modules/_heapqmodule.c | 30 ++++++++++++------- 2 files changed, 20 insertions(+), 11 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-03-18-19-48-53.bpo-1635741.ELEihr.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-03-18-19-48-53.bpo-1635741.ELEihr.rst b/Misc/NEWS.d/next/Core and Builtins/2020-03-18-19-48-53.bpo-1635741.ELEihr.rst new file mode 100644 index 00000000000000..a30559db558b00 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-03-18-19-48-53.bpo-1635741.ELEihr.rst @@ -0,0 +1 @@ +Port _heapq module to multiphase initialization. diff --git a/Modules/_heapqmodule.c b/Modules/_heapqmodule.c index 6bc18b5f82fb82..b56a3b64fb18ab 100644 --- a/Modules/_heapqmodule.c +++ b/Modules/_heapqmodule.c @@ -555,7 +555,6 @@ _heapq__heapify_max(PyObject *module, PyObject *heap) return heapify_internal(heap, siftup_max); } - static PyMethodDef heapq_methods[] = { _HEAPQ_HEAPPUSH_METHODDEF _HEAPQ_HEAPPUSHPOP_METHODDEF @@ -694,13 +693,29 @@ Believe me, real good tape sorts were quite spectacular to watch!\n\ From all times, sorting has always been a Great Art! :-)\n"); +static int +heapq_exec(PyObject *m) +{ + PyObject *about = PyUnicode_DecodeUTF8(__about__, strlen(__about__), NULL); + if (PyModule_AddObject(m, "__about__", about) < 0) { + Py_DECREF(about); + return -1; + } + return 0; +} + +static struct PyModuleDef_Slot heapq_slots[] = { + {Py_mod_exec, heapq_exec}, + {0, NULL} +}; + static struct PyModuleDef _heapqmodule = { PyModuleDef_HEAD_INIT, "_heapq", module_doc, - -1, + 0, heapq_methods, - NULL, + heapq_slots, NULL, NULL, NULL @@ -709,13 +724,6 @@ static struct PyModuleDef _heapqmodule = { PyMODINIT_FUNC PyInit__heapq(void) { - PyObject *m, *about; - - m = PyModule_Create(&_heapqmodule); - if (m == NULL) - return NULL; - about = PyUnicode_DecodeUTF8(__about__, strlen(__about__), NULL); - PyModule_AddObject(m, "__about__", about); - return m; + return PyModuleDef_Init(&_heapqmodule); } From f86b764ad4d216f53ee521c6da258a14cf7a15e9 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 18 Mar 2020 22:09:22 +0900 Subject: [PATCH 2/2] Update Modules/_heapqmodule.c Co-Authored-By: Victor Stinner --- Modules/_heapqmodule.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Modules/_heapqmodule.c b/Modules/_heapqmodule.c index b56a3b64fb18ab..4e85e046d385a7 100644 --- a/Modules/_heapqmodule.c +++ b/Modules/_heapqmodule.c @@ -696,7 +696,7 @@ From all times, sorting has always been a Great Art! :-)\n"); static int heapq_exec(PyObject *m) { - PyObject *about = PyUnicode_DecodeUTF8(__about__, strlen(__about__), NULL); + PyObject *about = PyUnicode_FromString(__about__); if (PyModule_AddObject(m, "__about__", about) < 0) { Py_DECREF(about); return -1; @@ -726,4 +726,3 @@ PyInit__heapq(void) { return PyModuleDef_Init(&_heapqmodule); } -