Skip to content

Commit

Permalink
Remove 2.6 compatibility code:
Browse files Browse the repository at this point in the history
now heapqueue items must implement the "<" operator.

The compatibility code could not work: all 3.0 objects have a __lt__ method
(which returns NotImplemented)

Twisted will have to adapt its DelayedCall class.
  • Loading branch information
amauryfa committed Jun 17, 2008
1 parent 35c8658 commit 2ba198d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 22 deletions.
6 changes: 3 additions & 3 deletions Lib/test/test_heapq.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ class TestHeapC(TestHeap):
module = c_heapq

def test_comparison_operator(self):
# Issue 3501: Make sure heapq works with both __lt__ and __le__
# Issue 3501: Make sure heapq works with both __lt__
# For python 3.0, __le__ alone is not enough
def hsort(data, comp):
data = [comp(x) for x in data]
self.module.heapify(data)
Expand All @@ -215,9 +216,8 @@ def __le__(self, other):
return self.x >= other.x
data = [random.random() for i in range(100)]
target = sorted(data, reverse=True)
print("HASATTR", hasattr(LE(0), "__lt__"), LE(0).__lt__)
self.assertEqual(hsort(data, LT), target)
self.assertEqual(hsort(data, LE), target)
self.assertRaises(TypeError, data, LE)


#==============================================================================
Expand Down
20 changes: 1 addition & 19 deletions Modules/_heapqmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,10 @@ which was written by Kevin O'Connor, augmented by Tim Peters,

#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 compatability by trying both.
*/
static int
cmp_lt(PyObject *x, PyObject *y)
{
int cmp;
static PyObject *lt = NULL;

if (lt == NULL) {
lt = PyUnicode_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;
return PyObject_RichCompareBool(x, y, Py_LT);
}

static int
Expand Down

0 comments on commit 2ba198d

Please sign in to comment.