Skip to content

Commit 288f527

Browse files
committed
simplify patch, fix test robustness to running env
1 parent 66e64b6 commit 288f527

File tree

4 files changed

+17
-40
lines changed

4 files changed

+17
-40
lines changed

Lib/test/support/os_helper.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -169,26 +169,6 @@ def make_bad_fd():
169169
unlink(TESTFN)
170170

171171

172-
@contextlib.contextmanager
173-
def unwritable_filepath():
174-
"""
175-
Create a filepath that is not writable by the current user.
176-
"""
177-
import tempfile
178-
fd, path = tempfile.mkstemp()
179-
original_permissions = stat.S_IMODE(os.lstat(path).st_mode)
180-
os.close(fd)
181-
182-
try:
183-
os.chmod(path, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
184-
yield path
185-
finally:
186-
try:
187-
os.chmod(path, original_permissions)
188-
os.remove(path)
189-
except OSError as e:
190-
pass
191-
192172
_can_symlink = None
193173

194174

Lib/test/test_wave.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
from test import audiotests
33
from test import support
4-
from test.support.os_helper import unwritable_filepath, skip_unless_working_chmod
4+
from test.support import os_helper
55
import io
66
import struct
77
import sys
@@ -197,21 +197,16 @@ def test_read_wrong_sample_width(self):
197197
with self.assertRaisesRegex(wave.Error, 'bad sample width'):
198198
wave.open(io.BytesIO(b))
199199

200-
@skip_unless_working_chmod
201-
def test_write_to_protected_file(self):
200+
def test_write_to_protected_location(self):
202201
# gh-136523: Wave_write.__del__ should not throw
203-
stderr = io.StringIO()
204-
sys.stderr = stderr
205-
try:
202+
with support.catch_unraisable_exception() as cm:
206203
try:
207-
with unwritable_filepath() as path:
208-
with wave.open(path, "wb"):
209-
pass
210-
except PermissionError:
204+
with os_helper.temp_dir() as path:
205+
wave.open(path, "wb")
206+
except IsADirectoryError:
211207
pass
212-
self.assertEqual(stderr.getvalue(), "")
213-
finally:
214-
sys.stderr = sys.__stderr__
208+
support.gc_collect()
209+
self.assertIsNone(cm.unraisable)
215210

216211

217212
if __name__ == '__main__':

Lib/wave.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,17 +427,19 @@ class Wave_write:
427427
_datawritten -- the size of the audio samples actually written
428428
"""
429429

430+
_file = None
431+
430432
def __init__(self, f):
431433
self._i_opened_the_file = None
434+
if isinstance(f, str):
435+
f = builtins.open(f, 'wb')
436+
self._i_opened_the_file = f
432437
try:
433-
if isinstance(f, str):
434-
f = builtins.open(f, 'wb')
435-
self._i_opened_the_file = f
438+
self.initfp(f)
436439
except:
437-
f = None
440+
if self._i_opened_the_file:
441+
f.close()
438442
raise
439-
finally:
440-
self.initfp(f)
441443

442444
def initfp(self, file):
443445
self._file = file
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Fix ``wave.Wave_write.__del__`` raising :exc:`AttributeError` when attempting to open an unwritable file.
1+
Fix :class:`wave.Wave_write` emitting an unraisable :exc:`AttributeError` when attempting to open an unwritable file.

0 commit comments

Comments
 (0)