@@ -36,7 +36,11 @@ siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
36
36
while (pos > startpos ) {
37
37
parentpos = (pos - 1 ) >> 1 ;
38
38
parent = arr [parentpos ];
39
+ Py_INCREF (newitem );
40
+ Py_INCREF (parent );
39
41
cmp = PyObject_RichCompareBool (newitem , parent , Py_LT );
42
+ Py_DECREF (parent );
43
+ Py_DECREF (newitem );
40
44
if (cmp < 0 )
41
45
return -1 ;
42
46
if (size != PyList_GET_SIZE (heap )) {
@@ -78,10 +82,13 @@ siftup(PyListObject *heap, Py_ssize_t pos)
78
82
/* Set childpos to index of smaller child. */
79
83
childpos = 2 * pos + 1 ; /* leftmost child position */
80
84
if (childpos + 1 < endpos ) {
81
- cmp = PyObject_RichCompareBool (
82
- arr [childpos ],
83
- arr [childpos + 1 ],
84
- Py_LT );
85
+ PyObject * a = arr [childpos ];
86
+ PyObject * b = arr [childpos + 1 ];
87
+ Py_INCREF (a );
88
+ Py_INCREF (b );
89
+ cmp = PyObject_RichCompareBool (a , b , Py_LT );
90
+ Py_DECREF (a );
91
+ Py_DECREF (b );
85
92
if (cmp < 0 )
86
93
return -1 ;
87
94
childpos += ((unsigned )cmp ^ 1 ); /* increment when cmp==0 */
@@ -264,7 +271,10 @@ _heapq_heappushpop_impl(PyObject *module, PyObject *heap, PyObject *item)
264
271
return item ;
265
272
}
266
273
267
- cmp = PyObject_RichCompareBool (PyList_GET_ITEM (heap , 0 ), item , Py_LT );
274
+ PyObject * top = PyList_GET_ITEM (heap , 0 );
275
+ Py_INCREF (top );
276
+ cmp = PyObject_RichCompareBool (top , item , Py_LT );
277
+ Py_DECREF (top );
268
278
if (cmp < 0 )
269
279
return NULL ;
270
280
if (cmp == 0 ) {
@@ -420,7 +430,11 @@ siftdown_max(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
420
430
while (pos > startpos ) {
421
431
parentpos = (pos - 1 ) >> 1 ;
422
432
parent = arr [parentpos ];
433
+ Py_INCREF (parent );
434
+ Py_INCREF (newitem );
423
435
cmp = PyObject_RichCompareBool (parent , newitem , Py_LT );
436
+ Py_DECREF (parent );
437
+ Py_DECREF (newitem );
424
438
if (cmp < 0 )
425
439
return -1 ;
426
440
if (size != PyList_GET_SIZE (heap )) {
@@ -462,10 +476,13 @@ siftup_max(PyListObject *heap, Py_ssize_t pos)
462
476
/* Set childpos to index of smaller child. */
463
477
childpos = 2 * pos + 1 ; /* leftmost child position */
464
478
if (childpos + 1 < endpos ) {
465
- cmp = PyObject_RichCompareBool (
466
- arr [childpos + 1 ],
467
- arr [childpos ],
468
- Py_LT );
479
+ PyObject * a = arr [childpos + 1 ];
480
+ PyObject * b = arr [childpos ];
481
+ Py_INCREF (a );
482
+ Py_INCREF (b );
483
+ cmp = PyObject_RichCompareBool (a , b , Py_LT );
484
+ Py_DECREF (a );
485
+ Py_DECREF (b );
469
486
if (cmp < 0 )
470
487
return -1 ;
471
488
childpos += ((unsigned )cmp ^ 1 ); /* increment when cmp==0 */
0 commit comments