https://github.com/python/cpython/commit/677b0e314ccbb7f5cae014fa4edd944f6e5bf857
commit: 677b0e314ccbb7f5cae014fa4edd944f6e5bf857
branch: 3.14
author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com>
committer: serhiy-storchaka <storch...@gmail.com>
date: 2025-07-16T07:26:52Z
summary:

[3.14] gh-136523: Fix wave.Wave_write emitting an unraisable when open raises 
(GH-136529) (GH-136606)

(cherry picked from commit 171de05b4884d1353044417ea51a4efcb55ba633)

Co-authored-by: Sachin Shah <39803835+invents...@users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2025-07-11-03-39-15.gh-issue-136523.s7caKL.rst
M Lib/test/test_wave.py
M Lib/wave.py

diff --git a/Lib/test/test_wave.py b/Lib/test/test_wave.py
index 5e771c8de969ec..346a343761a7c1 100644
--- a/Lib/test/test_wave.py
+++ b/Lib/test/test_wave.py
@@ -2,6 +2,7 @@
 from test import audiotests
 from test import support
 import io
+import os
 import struct
 import sys
 import wave
@@ -222,6 +223,14 @@ def test_read_wrong_sample_width(self):
         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 a34af244c3e224..b8476e264868fc 100644
--- a/Lib/wave.py
+++ b/Lib/wave.py
@@ -441,6 +441,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 00000000000000..71ec66a37ef4c3
--- /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.

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: arch...@mail-archive.com

Reply via email to