Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ba198d

Browse files
committed
Remove 2.6 compatibility code:
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.
1 parent 35c8658 commit 2ba198d

2 files changed

Lines changed: 4 additions & 22 deletions

File tree

Lib/test/test_heapq.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ class TestHeapC(TestHeap):
198198
module = c_heapq
199199

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

222222

223223
#==============================================================================

Modules/_heapqmodule.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,10 @@ which was written by Kevin O'Connor, augmented by Tim Peters,
88

99
#include "Python.h"
1010

11-
/* Older implementations of heapq used Py_LE for comparisons. Now, it uses
12-
Py_LT so it will match min(), sorted(), and bisect(). Unfortunately, some
13-
client code (Twisted for example) relied on Py_LE, so this little function
14-
restores compatability by trying both.
15-
*/
1611
static int
1712
cmp_lt(PyObject *x, PyObject *y)
1813
{
19-
int cmp;
20-
static PyObject *lt = NULL;
21-
22-
if (lt == NULL) {
23-
lt = PyUnicode_FromString("__lt__");
24-
if (lt == NULL)
25-
return -1;
26-
}
27-
if (PyObject_HasAttr(x, lt))
28-
return PyObject_RichCompareBool(x, y, Py_LT);
29-
cmp = PyObject_RichCompareBool(y, x, Py_LE);
30-
if (cmp != -1)
31-
cmp = 1 - cmp;
32-
return cmp;
14+
return PyObject_RichCompareBool(x, y, Py_LT);
3315
}
3416

3517
static int

0 commit comments

Comments
 (0)