Skip to content

Commit 4657a8a

Browse files
authored
bpo-1635741: Port _heapq module to multiphase initialization (GH19057)
1 parent 56bfdeb commit 4657a8a

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port _heapq module to multiphase initialization.

Modules/_heapqmodule.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,6 @@ _heapq__heapify_max(PyObject *module, PyObject *heap)
555555
return heapify_internal(heap, siftup_max);
556556
}
557557

558-
559558
static PyMethodDef heapq_methods[] = {
560559
_HEAPQ_HEAPPUSH_METHODDEF
561560
_HEAPQ_HEAPPUSHPOP_METHODDEF
@@ -694,13 +693,29 @@ Believe me, real good tape sorts were quite spectacular to watch!\n\
694693
From all times, sorting has always been a Great Art! :-)\n");
695694

696695

696+
static int
697+
heapq_exec(PyObject *m)
698+
{
699+
PyObject *about = PyUnicode_FromString(__about__);
700+
if (PyModule_AddObject(m, "__about__", about) < 0) {
701+
Py_DECREF(about);
702+
return -1;
703+
}
704+
return 0;
705+
}
706+
707+
static struct PyModuleDef_Slot heapq_slots[] = {
708+
{Py_mod_exec, heapq_exec},
709+
{0, NULL}
710+
};
711+
697712
static struct PyModuleDef _heapqmodule = {
698713
PyModuleDef_HEAD_INIT,
699714
"_heapq",
700715
module_doc,
701-
-1,
716+
0,
702717
heapq_methods,
703-
NULL,
718+
heapq_slots,
704719
NULL,
705720
NULL,
706721
NULL
@@ -709,13 +724,5 @@ static struct PyModuleDef _heapqmodule = {
709724
PyMODINIT_FUNC
710725
PyInit__heapq(void)
711726
{
712-
PyObject *m, *about;
713-
714-
m = PyModule_Create(&_heapqmodule);
715-
if (m == NULL)
716-
return NULL;
717-
about = PyUnicode_DecodeUTF8(__about__, strlen(__about__), NULL);
718-
PyModule_AddObject(m, "__about__", about);
719-
return m;
727+
return PyModuleDef_Init(&_heapqmodule);
720728
}
721-

0 commit comments

Comments
 (0)