File tree Expand file tree Collapse file tree 4 files changed +17
-40
lines changed
Expand file tree Collapse file tree 4 files changed +17
-40
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11import unittest
22from test import audiotests
33from test import support
4- from test .support . os_helper import unwritable_filepath , skip_unless_working_chmod
4+ from test .support import os_helper
55import io
66import struct
77import 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
217212if __name__ == '__main__' :
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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.
You can’t perform that action at this time.
0 commit comments