This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: wave module forgets to close file on exception
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: akuchling, amerinese, georg.brandl, polivares
Priority: normal Keywords:

Created on 2006-11-26 13:59 by amerinese, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg30667 - (view) Author: amerinese (amerinese) Date: 2006-11-26 13:59
I am using python 2.4 on Windows XP SP2

The wave module function:
f = wave.open(file)

In the case that file is a path to the file and the file is not yet opened, wave.open(file) may raise an exception if the file is opened and it does not fulfill the format of a WAV file.  However, it forgets to close the file when the exception is raised keeping other functions from accessing the file (at least until the file is garbage collected).

The regular file opening idiom doesn't work
f = wave.open(file)
try:
   ## do something with the wav file
finally:
   f.close()
Since wave.open(file) raises an exception before return the file name, f can't be closed, but the file is open.

The reason I know this is because I try to delete the file if trying to open it raises an RIFF or not a WAV file exception and it claims the file is locked.
msg30668 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2006-12-19 19:59
Try putting the Wave.open() inside the try...finally.

msg30669 - (view) Author: Patricio Olivares (polivares) Date: 2007-03-15 03:57
wave.open expects either a str or a file object. When it gets
a str, it opens the file, works on it, and closes the file. All of
this in the inner scope of the wave.open function.

But if the file pointed by the str is not a correct wav format,
then wave.open throws wave.Error but *doesn't close the file*. It
assumes that the file will be garbage collected and then closed but
that does not happen. I believe that it has to do with the "Note"
at <http://docs.python.org/ref/customization.html#l2h-177>

The problem is noted mostly on the interactive interpreter on
windows  because on windows you can't delete/move a file if it's being used
by another process so you need to close the interpreter to release
the file.
msg30670 - (view) Author: Patricio Olivares (polivares) Date: 2007-03-15 04:08
Simple patch added: [1681153]
msg30671 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-15 07:42
Fixed with said patch.
History
Date User Action Args
2022-04-11 14:56:21adminsetgithub: 44275
2006-11-26 13:59:37amerinesecreate