Skip to content

heapq.merge silently drops an input stream when key raises StopIteration #158717

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: heapq.merge docstring: "Similar to sorted(itertools.chain(*iterables)) but returns a generator" and "If key is not None, applies a key function to each element to determine its sort order." sorted propagates an exception from the key function instead of silently discarding input.

Expected: Consumption raises StopIteration or RuntimeError caused by StopIteration.

Actual: Consumption succeeds with [2, 4], silently omitting 1 and 3.

import heapq
from itertools import chain

data = {"lists": [[1, 3], [2, 4]], "raise_StopIteration_on": 1}
lists = data["lists"]

def key(x):
    if x == data["raise_StopIteration_on"]:
        raise StopIteration
    return x

def outcome(f):
    try:
        return ("result", f())
    except Exception as e:
        if isinstance(e, RuntimeError) and isinstance(e.__cause__, StopIteration):
            e = e.__cause__
        return ("exception", type(e).__name__)

if not all(isinstance(xs, list) and all(type(x) is int for x in xs)
           and xs == sorted(xs) for xs in lists):
    print("REFUTATION REJECTED:", "input is not ascending integer lists")
else:
    actual = outcome(lambda: list(heapq.merge(*lists, key=key)))
    expected = outcome(lambda: sorted(chain.from_iterable(lists), key=key))
    if expected == ("exception", "StopIteration") and actual[0] == "result":
        print("REFUTATION CONFIRMED:", data, "actual:", actual, "expected:", expected)
    else:
        print("REFUTATION REJECTED:", "no silent successful omission demonstrated",
              data, "actual:", actual, "expected:", expected)

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library heapq:

REFUTATION CONFIRMED: {'lists': [[1, 3], [2, 4]], 'raise_StopIteration_on': 1} actual: ('result', [2, 4]) expected: ('exception', 'StopIteration')

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/heapq-20261003-061723-c1

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions