diff options
Diffstat (limited to 'Modules/_heapqmodule.c')
-rw-r--r-- | Modules/_heapqmodule.c | 71 |
1 files changed, 32 insertions, 39 deletions
diff --git a/Modules/_heapqmodule.c b/Modules/_heapqmodule.c index 495114b9a19..1066f94b002 100644 --- a/Modules/_heapqmodule.c +++ b/Modules/_heapqmodule.c @@ -8,30 +8,6 @@ annotated by François Pinard, and converted to C by Raymond Hettinger. #include "Python.h" -/* Older implementations of heapq used Py_LE for comparisons. Now, it uses - Py_LT so it will match min(), sorted(), and bisect(). Unfortunately, some - client code (Twisted for example) relied on Py_LE, so this little function - restores compatibility by trying both. -*/ -static int -cmp_lt(PyObject *x, PyObject *y) -{ - int cmp; - static PyObject *lt = NULL; - - if (lt == NULL) { - lt = PyString_FromString("__lt__"); - if (lt == NULL) - return -1; - } - if (PyObject_HasAttr(x, lt)) - return PyObject_RichCompareBool(x, y, Py_LT); - cmp = PyObject_RichCompareBool(y, x, Py_LE); - if (cmp != -1) - cmp = 1 - cmp; - return cmp; -} - static int _siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos) { @@ -52,7 +28,7 @@ _siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos) while (pos > startpos){ parentpos = (pos - 1) >> 1; parent = PyList_GET_ITEM(heap, parentpos); - cmp = cmp_lt(newitem, parent); + cmp = PyObject_RichCompareBool(newitem, parent, Py_LT); if (cmp == -1) { Py_DECREF(newitem); return -1; @@ -92,9 +68,10 @@ _siftup(PyListObject *heap, Py_ssize_t pos) /* Set childpos to index of smaller child. */ rightpos = childpos + 1; if (rightpos < endpos) { - cmp = cmp_lt( + cmp = PyObject_RichCompareBool( PyList_GET_ITEM(heap, childpos), - PyList_GET_ITEM(heap, rightpos)); + PyList_GET_ITEM(heap, rightpos), + Py_LT); if (cmp == -1) { Py_DECREF(newitem); return -1; @@ -237,7 +214,7 @@ heappushpop(PyObject *self, PyObject *args) return item; } - cmp = cmp_lt(PyList_GET_ITEM(heap, 0), item); + cmp = PyObject_RichCompareBool(PyList_GET_ITEM(heap, 0), item, Py_LT); if (cmp == -1) return NULL; if (cmp == 0) { @@ -336,7 +313,7 @@ nlargest(PyObject *self, PyObject *args) else goto sortit; } - cmp = cmp_lt(sol, elem); + cmp = PyObject_RichCompareBool(sol, elem, Py_LT); if (cmp == -1) { Py_DECREF(elem); goto fail; @@ -391,7 +368,7 @@ _siftdownmax(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos) while (pos > startpos){ parentpos = (pos - 1) >> 1; parent = PyList_GET_ITEM(heap, parentpos); - cmp = cmp_lt(parent, newitem); + cmp = PyObject_RichCompareBool(parent, newitem, Py_LT); if (cmp == -1) { Py_DECREF(newitem); return -1; @@ -431,9 +408,10 @@ _siftupmax(PyListObject *heap, Py_ssize_t pos) /* Set childpos to index of smaller child. */ rightpos = childpos + 1; if (rightpos < endpos) { - cmp = cmp_lt( + cmp = PyObject_RichCompareBool( PyList_GET_ITEM(heap, rightpos), - PyList_GET_ITEM(heap, childpos)); + PyList_GET_ITEM(heap, childpos), + Py_LT); if (cmp == -1) { Py_DECREF(newitem); return -1; @@ -506,7 +484,7 @@ nsmallest(PyObject *self, PyObject *args) else goto sortit; } - cmp = cmp_lt(elem, los); + cmp = PyObject_RichCompareBool(elem, los, Py_LT); if (cmp == -1) { Py_DECREF(elem); goto fail; @@ -593,7 +571,7 @@ maintains the heap invariant!\n"); PyDoc_STRVAR(__about__, "Heap queues\n\ \n\ -[explanation by François Pinard]\n\ +[explanation by Fran\xc3\xa7ois Pinard]\n\ \n\ Heaps are arrays for which a[k] <= a[2*k+1] and a[k] <= a[2*k+2] for\n\ all k, counting elements from 0. For the sake of comparison,\n\ @@ -684,14 +662,29 @@ backwards, and this was also used to avoid the rewinding time.\n\ Believe me, real good tape sorts were quite spectacular to watch!\n\ From all times, sorting has always been a Great Art! :-)\n"); + +static struct PyModuleDef _heapqmodule = { + PyModuleDef_HEAD_INIT, + "_heapq", + module_doc, + -1, + heapq_methods, + NULL, + NULL, + NULL, + NULL +}; + PyMODINIT_FUNC -init_heapq(void) +PyInit__heapq(void) { - PyObject *m; + PyObject *m, *about; - m = Py_InitModule3("_heapq", heapq_methods, module_doc); + m = PyModule_Create(&_heapqmodule); if (m == NULL) - return; - PyModule_AddObject(m, "__about__", PyString_FromString(__about__)); + return NULL; + about = PyUnicode_DecodeUTF8(__about__, strlen(__about__), NULL); + PyModule_AddObject(m, "__about__", about); + return m; } |