Skip to content

wave: Reject parameter changes after empty frame writes #158768

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: The wave documentation, under Wave_write objects, states: "Note that it is invalid to set any parameters after calling writeframes() or writeframesraw(), and any attempt to do so will raise wave.Error."

Expected: setnchannels(2) raises wave.Error after a successful writeframes(b'') or writeframesraw(b'').

Actual: After writeframes(b''), setnchannels(2) succeeds without an exception.

import io, wave

p = dict(channels=1, sample_width=1, frame_rate=8000,
         method="writeframes", data=b"", new_channels=2)
try:
    if not (p["channels"] > 0 and 1 <= p["sample_width"] <= 4
            and p["frame_rate"] > 0 and p["new_channels"] > 0
            and p["new_channels"] != p["channels"]
            and isinstance(p["data"], bytes)):
        raise ValueError("invalid PCM input")
    w = wave.open(io.BytesIO(), "wb")
    w.setparams((p["channels"], p["sample_width"], p["frame_rate"],
                 0, "NONE", "not compressed"))
    getattr(w, p["method"])(p["data"])
    expected = "raises wave.Error"  # Documentation forbids changes after either write.
    try:
        w.setnchannels(p["new_channels"])
        actual = "no exception"
    except wave.Error:
        actual = "raises wave.Error"
    if actual != expected:
        print("REFUTATION CONFIRMED:", p, "actual:", actual, "expected:", expected)
    else:
        print("REFUTATION REJECTED: documented wave.Error was raised")
except Exception as e:
    print("REFUTATION REJECTED:", type(e).__name__, str(e))

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

REFUTATION CONFIRMED: {'channels': 1, 'sample_width': 1, 'frame_rate': 8000, 'method': 'writeframes', 'data': b'', 'new_channels': 2} actual: no exception expected: raises wave.Error

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/wave-20261003-042850-c2

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