Skip to content

Heap became unsorted while calling heappush #132110

@PC-Killer

Description

@PC-Killer

Bug report

Bug description:

Read the code below:

import heapq


heap = []
heapq.heappush(heap, (2, 2))
heapq.heappush(heap, (1, 2))
heapq.heappush(heap, (1, -1))
print(heap)

It is very simple, and obviously, it should print "[(1, -1), (1, 2), (2, 2)]".
But here is what it prints:

PC-Killer@nixos ~> python --version ; cat -n ~/python_work/bugs/unordered_heap.py ; python ~/python_work/bugs/unordered_heap.py
Python 3.14.0a4
     1  import heapq
     2
     3
     4  heap = []
     5  heapq.heappush(heap, (2, 2))
     6  heapq.heappush(heap, (1, 2))
     7  heapq.heappush(heap, (1, -1))
     8  print(heap)
[(1, -1), (2, 2), (1, 2)]

The heap became unsorted.

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions