Skip to content

wave.Wave_write silently loses audio data on short writes #158770

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: The module docstring, under 'Writing WAVE files', says: 'The open file pointer must have methods write(), tell(), seek(), and close().' It describes writeframesraw(data) as 'write audio frames without patching up the file header'. Binary raw streams may return short write counts.

Expected: readframes(-1) returns b'\x13\'.

Actual: readframes(-1) returns b'\x13'; the WAV header declares two audio bytes although only one was written.

import io
import wave

case = {'payload': b'\x13\\', 'cap': 1, 'rate': 8000}

class ShortStream(io.BytesIO):
    def write(self, data):
        return super().write(data[:case['cap']] if self.tell() >= 44 else data)

stream = ShortStream()
payload = case['payload']
if not (stream.writable() and stream.seekable()
        and all(callable(getattr(stream, m, None))
                for m in ('write', 'tell', 'seek', 'close'))
        and case['rate'] > 0 and 0 < case['cap'] < len(payload)):
    print('REFUTATION REJECTED:', 'invalid input')
else:
    expected = bytes(payload)  # Mono, 8-bit PCM: every byte is a complete frame.
    try:
        writer = wave.open(stream, 'wb')
        writer.setparams((1, 1, case['rate'], 0, 'NONE', 'not compressed'))
        writer.writeframesraw(payload)
        writer.close()
        stream.seek(0)
        with wave.open(stream, 'rb') as reader:
            actual = reader.readframes(-1)
    except Exception as error:
        actual = repr(error)
    if actual != expected:
        print('REFUTATION CONFIRMED:', case, 'actual:', actual, 'expected:', expected)
    else:
        print('REFUTATION REJECTED:', 'audio matches expected bytes')

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

REFUTATION CONFIRMED: {'payload': b'\x13\\', 'cap': 1, 'rate': 8000} actual: b'\x13' expected: b'\x13\\'

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-111327-c3

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