From: Sachin Shah <39803835+inventshah@users.noreply.github.com> Date: Sun, 13 Jul 2025 05:49:12 +0000 (-0400) Subject: gh-136523: Fix wave.Wave_write emitting an unraisable when open raises (GH-136529) X-Git-Tag: v3.15.0a1~994 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=171de05b4884d1353044417ea51a4efcb55ba633;p=thirdparty%2FPython%2Fcpython.git gh-136523: Fix wave.Wave_write emitting an unraisable when open raises (GH-136529) --- diff --git a/Lib/test/test_wave.py b/Lib/test/test_wave.py index 6c3362857fc2..226b1aa84bd7 100644 --- a/Lib/test/test_wave.py +++ b/Lib/test/test_wave.py @@ -2,6 +2,7 @@ import unittest from test import audiotests from test import support import io +import os import struct import sys import wave @@ -196,6 +197,14 @@ class WaveLowLevelTest(unittest.TestCase): with self.assertRaisesRegex(wave.Error, 'bad sample width'): wave.open(io.BytesIO(b)) + def test_open_in_write_raises(self): + # gh-136523: Wave_write.__del__ should not throw + with support.catch_unraisable_exception() as cm: + with self.assertRaises(OSError): + wave.open(os.curdir, "wb") + support.gc_collect() + self.assertIsNone(cm.unraisable) + if __name__ == '__main__': unittest.main() diff --git a/Lib/wave.py b/Lib/wave.py index 929609fa5240..5af745e2217e 100644 --- a/Lib/wave.py +++ b/Lib/wave.py @@ -427,6 +427,8 @@ class Wave_write: _datawritten -- the size of the audio samples actually written """ + _file = None + def __init__(self, f): self._i_opened_the_file = None if isinstance(f, str): diff --git a/Misc/NEWS.d/next/Library/2025-07-11-03-39-15.gh-issue-136523.s7caKL.rst b/Misc/NEWS.d/next/Library/2025-07-11-03-39-15.gh-issue-136523.s7caKL.rst new file mode 100644 index 000000000000..71ec66a37ef4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-07-11-03-39-15.gh-issue-136523.s7caKL.rst @@ -0,0 +1 @@ +Fix :class:`wave.Wave_write` emitting an unraisable when open raises.