gh-153953: Increase test coverage for the wave module#153954
Open
fedonman wants to merge 13 commits into
Open
Conversation
Add tests for previously-uncovered paths in Lib/wave.py, all reachable through the public API: * Wave_write parameter validation: rejecting bad channel counts, sample widths, compression types and formats; the "not set" errors from the getters; the "cannot change parameters after starting to write" guards on every setter; and tell(). * Wave_read error handling: rejecting an unknown WAVE_FORMAT_EXTENSIBLE subformat, raising EOFError on a truncated fmt chunk, skipping unknown chunks, getfp(), and closing the file when opening a malformed path fails. * wave.open() rejecting an invalid mode. This raises line coverage of Lib/wave.py under test_wave from 317 to 345 of 449 executable lines. Test-only change; no behavior change.
vstinner
reviewed
Jul 18, 2026
Co-authored-by: Victor Stinner <vstinner@python.org>
The test relied on the _wave_file helper and FMT_PCM constant, which only exist on WaveReadErrorTest. Build the WAVE file with the wave module so the test is self-contained.
Contributor
Author
|
@vstinner comments addressed, you may review again. |
vstinner
reviewed
Jul 23, 2026
Comment on lines
+555
to
+567
| def open_writer(self): | ||
| w = wave.open(io.BytesIO(), 'wb') | ||
| self.addCleanup(self._close_quietly, w) | ||
| return w | ||
|
|
||
| @staticmethod | ||
| def _close_quietly(w): | ||
| # A writer whose required parameters are never set raises on close; | ||
| # swallow that so it does not mask the behaviour under test. | ||
| try: | ||
| w.close() | ||
| except wave.Error: | ||
| pass |
Member
There was a problem hiding this comment.
I would prefer to not ignore error on close(). I suggest setting all parameters so close() doesn't fail. Nitpick: I also prefer declaring the close function before it's being used by open_writer():
Suggested change
| def open_writer(self): | |
| w = wave.open(io.BytesIO(), 'wb') | |
| self.addCleanup(self._close_quietly, w) | |
| return w | |
| @staticmethod | |
| def _close_quietly(w): | |
| # A writer whose required parameters are never set raises on close; | |
| # swallow that so it does not mask the behaviour under test. | |
| try: | |
| w.close() | |
| except wave.Error: | |
| pass | |
| @staticmethod | |
| def _close(w): | |
| try: | |
| # Make sure that all parameters are set | |
| w.setnchannels(1) | |
| w.setsampwidth(2) | |
| w.setframerate(44100) | |
| except wave.Error: | |
| # Ignore "cannot change parameters after starting to write" error | |
| pass | |
| w.close() | |
| def open_writer(self): | |
| w = wave.open(io.BytesIO(), 'wb') | |
| self.addCleanup(self._close, w) | |
| return w |
Comment on lines
+612
to
+619
| def test_tell_reports_frames_written(self): | ||
| w = self.open_writer() | ||
| w.setnchannels(1) | ||
| w.setsampwidth(2) | ||
| w.setframerate(44100) | ||
| self.assertEqual(w.tell(), 0) | ||
| w.writeframes(b'\x00\x00' * 5) | ||
| self.assertEqual(w.tell(), 5) |
Member
There was a problem hiding this comment.
I suggest testing getnframes() at the same time, use a shorter test name, and write twice (just in case):
Suggested change
| def test_tell_reports_frames_written(self): | |
| w = self.open_writer() | |
| w.setnchannels(1) | |
| w.setsampwidth(2) | |
| w.setframerate(44100) | |
| self.assertEqual(w.tell(), 0) | |
| w.writeframes(b'\x00\x00' * 5) | |
| self.assertEqual(w.tell(), 5) | |
| def test_tell(self): | |
| def check_nframes(nframes): | |
| self.assertEqual(w.tell(), nframes) | |
| self.assertEqual(w.getnframes(), nframes) | |
| w = self.open_writer() | |
| w.setnchannels(1) | |
| w.setsampwidth(2) | |
| w.setframerate(44100) | |
| check_nframes(0) | |
| frame = b'\x00\x00' | |
| w.writeframes(frame * 5) | |
| check_nframes(5) | |
| w.writeframes(frame * 3) | |
| check_nframes(8) |
Comment on lines
+621
to
+640
| def test_cannot_change_params_after_write(self): | ||
| setters = ( | ||
| ('setnchannels', (1,)), | ||
| ('setsampwidth', (2,)), | ||
| ('setframerate', (44100,)), | ||
| ('setnframes', (10,)), | ||
| ('setcomptype', ('NONE', 'not compressed')), | ||
| ('setformat', (wave.WAVE_FORMAT_PCM,)), | ||
| ('setparams', ((1, 2, 44100, 0, 'NONE', 'not compressed'),)), | ||
| ) | ||
| for name, args in setters: | ||
| with self.subTest(setter=name): | ||
| w = self.open_writer() | ||
| w.setnchannels(1) | ||
| w.setsampwidth(2) | ||
| w.setframerate(44100) | ||
| w.writeframes(b'\x00\x00') | ||
| with self.assertRaisesRegex(wave.Error, | ||
| 'cannot change parameters'): | ||
| getattr(w, name)(*args) |
Member
There was a problem hiding this comment.
We can reuse the same writer object for all tests, no need to create a fresh writer at each iteration:
Suggested change
| def test_cannot_change_params_after_write(self): | |
| setters = ( | |
| ('setnchannels', (1,)), | |
| ('setsampwidth', (2,)), | |
| ('setframerate', (44100,)), | |
| ('setnframes', (10,)), | |
| ('setcomptype', ('NONE', 'not compressed')), | |
| ('setformat', (wave.WAVE_FORMAT_PCM,)), | |
| ('setparams', ((1, 2, 44100, 0, 'NONE', 'not compressed'),)), | |
| ) | |
| for name, args in setters: | |
| with self.subTest(setter=name): | |
| w = self.open_writer() | |
| w.setnchannels(1) | |
| w.setsampwidth(2) | |
| w.setframerate(44100) | |
| w.writeframes(b'\x00\x00') | |
| with self.assertRaisesRegex(wave.Error, | |
| 'cannot change parameters'): | |
| getattr(w, name)(*args) | |
| def test_cannot_change_params_after_write(self): | |
| w = self.open_writer() | |
| w.setnchannels(1) | |
| w.setsampwidth(2) | |
| w.setframerate(44100) | |
| w.writeframes(b'\x00\x00') | |
| setters = ( | |
| ('setnchannels', (1,)), | |
| ('setsampwidth', (2,)), | |
| ('setframerate', (44100,)), | |
| ('setnframes', (10,)), | |
| ('setcomptype', ('NONE', 'not compressed')), | |
| ('setformat', (wave.WAVE_FORMAT_PCM,)), | |
| ('setparams', ((1, 2, 44100, 0, 'NONE', 'not compressed'),)), | |
| ) | |
| for name, args in setters: | |
| with self.subTest(setter=name): | |
| with self.assertRaisesRegex(wave.Error, | |
| 'cannot change parameters'): | |
| getattr(w, name)(*args) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds tests for previously-uncovered code paths in
Lib/wave.py, all reachable through the public API and deterministic across platforms.Wave_writeparameter validationsetnchannels()rejecting a non-positive channel countgetnchannels()/getsampwidth()/getframerate()/getparams()raising when parameters are unsetsetsampwidth()rejecting out-of-range widthssetcomptype()/setformat()rejecting unsupported valuessetnchannels,setsampwidth,setframerate,setnframes,setcomptype,setformat,setparams)tell()Wave_readerror handlingWAVE_FORMAT_EXTENSIBLEfile whose SubFormat GUID is not PCMEOFErroron a truncatedfmtchunk (missing header / missing sample width)fmtanddatagetfp()wave.open()Measured with the stdlib
tracemodule while runningtest_wave, line coverage ofLib/wave.pyrises from 317/449 to 345/449 executable lines.This is a test-only change with no behavior change, so no
Misc/NEWS.dentry is included../python -m test -R 3:3 test_wavereports no reference leaks.Fixes #153953.
wavemodule #153953