@@ -29,7 +29,11 @@ siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
29
29
while (pos > startpos ) {
30
30
parentpos = (pos - 1 ) >> 1 ;
31
31
parent = arr [parentpos ];
32
+ Py_INCREF (newitem );
33
+ Py_INCREF (parent );
32
34
cmp = PyObject_RichCompareBool (newitem , parent , Py_LT );
35
+ Py_DECREF (parent );
36
+ Py_DECREF (newitem );
33
37
if (cmp < 0 )
34
38
return -1 ;
35
39
if (size != PyList_GET_SIZE (heap )) {
@@ -71,10 +75,13 @@ siftup(PyListObject *heap, Py_ssize_t pos)
71
75
/* Set childpos to index of smaller child. */
72
76
childpos = 2 * pos + 1 ; /* leftmost child position */
73
77
if (childpos + 1 < endpos ) {
74
- cmp = PyObject_RichCompareBool (
75
- arr [childpos ],
76
- arr [childpos + 1 ],
77
- Py_LT );
78
+ PyObject * a = arr [childpos ];
79
+ PyObject * b = arr [childpos + 1 ];
80
+ Py_INCREF (a );
81
+ Py_INCREF (b );
82
+ cmp = PyObject_RichCompareBool (a , b , Py_LT );
83
+ Py_DECREF (a );
84
+ Py_DECREF (b );
78
85
if (cmp < 0 )
79
86
return -1 ;
80
87
childpos += ((unsigned )cmp ^ 1 ); /* increment when cmp==0 */
@@ -229,7 +236,10 @@ heappushpop(PyObject *self, PyObject *args)
229
236
return item ;
230
237
}
231
238
232
- cmp = PyObject_RichCompareBool (PyList_GET_ITEM (heap , 0 ), item , Py_LT );
239
+ PyObject * top = PyList_GET_ITEM (heap , 0 );
240
+ Py_INCREF (top );
241
+ cmp = PyObject_RichCompareBool (top , item , Py_LT );
242
+ Py_DECREF (top );
233
243
if (cmp < 0 )
234
244
return NULL ;
235
245
if (cmp == 0 ) {
@@ -383,7 +393,11 @@ siftdown_max(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
383
393
while (pos > startpos ) {
384
394
parentpos = (pos - 1 ) >> 1 ;
385
395
parent = arr [parentpos ];
396
+ Py_INCREF (parent );
397
+ Py_INCREF (newitem );
386
398
cmp = PyObject_RichCompareBool (parent , newitem , Py_LT );
399
+ Py_DECREF (parent );
400
+ Py_DECREF (newitem );
387
401
if (cmp < 0 )
388
402
return -1 ;
389
403
if (size != PyList_GET_SIZE (heap )) {
@@ -425,10 +439,13 @@ siftup_max(PyListObject *heap, Py_ssize_t pos)
425
439
/* Set childpos to index of smaller child. */
426
440
childpos = 2 * pos + 1 ; /* leftmost child position */
427
441
if (childpos + 1 < endpos ) {
428
- cmp = PyObject_RichCompareBool (
429
- arr [childpos + 1 ],
430
- arr [childpos ],
431
- Py_LT );
442
+ PyObject * a = arr [childpos + 1 ];
443
+ PyObject * b = arr [childpos ];
444
+ Py_INCREF (a );
445
+ Py_INCREF (b );
446
+ cmp = PyObject_RichCompareBool (a , b , Py_LT );
447
+ Py_DECREF (a );
448
+ Py_DECREF (b );
432
449
if (cmp < 0 )
433
450
return -1 ;
434
451
childpos += ((unsigned )cmp ^ 1 ); /* increment when cmp==0 */
0 commit comments