Skip to content

gh-149306: Improve error messages in wave module to include the offending value#149307

Open
htjworld wants to merge 2 commits intopython:mainfrom
htjworld:gh-149306-wave-error-messages
Open

gh-149306: Improve error messages in wave module to include the offending value#149307
htjworld wants to merge 2 commits intopython:mainfrom
htjworld:gh-149306-wave-error-messages

Conversation

@htjworld
Copy link
Copy Markdown
Contributor

@htjworld htjworld commented May 3, 2026

Summary

Include the offending value in wave.Error messages raised by:

  • Wave_read._read_fmt_chunk (parses WAV file header)
  • Wave_write.setnchannels
  • Wave_write.setsampwidth
  • Wave_write.setframerate

Before:

wave.Error: bad # of channels
wave.Error: bad sample width
wave.Error: bad frame rate

After:

wave.Error: bad # of channels: 0
wave.Error: bad sample width: 0
wave.Error: bad frame rate: -1

Motivation

Wave_read: Values come from the WAV file header — they never appear
in the traceback. The error message is the only debugging clue.

Wave_write: When values originate from variables, configuration files,
or parsed data rather than literals, the traceback shows the call site but
not the value itself. For example, w.setframerate(config['rate']) raises
bad frame rate with no indication of what config['rate'] was.

This extends the clear-feedback approach introduced in gh-144976, which fixed
the validation order in setframerate to raise immediately at the point of
the mistake. Showing the offending value is the natural next step.

For setframerate, the original value (before int(round())) is shown rather
than the rounded result, so callers see exactly what they passed.

Precedent

Notes

  • Error type is unchanged; only the message text is refined.
  • The bits/bytes unit ambiguity in bad sample width predates this PR and can be addressed separately.
  • NaN/Inf handling in setframerate is pre-existing behaviour and out of scope here.

… offending value

Include the offending value in wave.Error messages raised by
Wave_read._read_fmt_chunk and Wave_write setters (setnchannels,
setsampwidth, setframerate). For setframerate, the original value is
shown rather than the rounded one to aid debugging.
@htjworld htjworld force-pushed the gh-149306-wave-error-messages branch 2 times, most recently from cc28830 to d1ba708 Compare May 3, 2026 02:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant