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 shouldn't try to close an open file at all costs
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: biosthezerg
Priority: normal Keywords:

Created on 2020-09-09 13:00 by biosthezerg, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg376634 - (view) Author: Karel (biosthezerg) Date: 2020-09-09 13:00
The standard `wave` library tries to close a file unconditionally. I believe this is wrong.

From the docs:
Wave_write.close(): Make sure nframes is correct, and close the file if it was opened by wave. *This method is called upon object collection.* [...]

This leads to problems when an exception is throw when a file is open. See the following MWE:
```
from io import BytesIO
import wave
audio_out = BytesIO() 
with wave.open(audio_out, "w") as file_out:
    raise Exception('aaa!') 

Error: # channels not specified
```

Even when I remove the context manager, so it shouldn't close the file automatically, it still does, the following:
```
file_out = wave.open(audio_out, "w")
raise Exception('aaa!')
```
doesn't throw an error at me immediately, but when the file_out variable is running out of scope, I get a printout saying: `Exception ignored in: <bound method Wave_write.__del__ of <wave.Wave_write object at 0x7ff549064eb8>> [...] wave.Error: # channels not specified`.

Expected behaviour: When I use a Wave_write object as a context manager, it tries to close the file unconditionally (as it does now). When I call `wave.open` manually, no automatic file closing is done.
History
Date User Action Args
2022-04-11 14:59:35adminsetgithub: 85918
2020-09-09 13:00:36biosthezergcreate