diff Lib/wave.py @ 84944:4f3b6eff2ede

#17616: Improve context manager tests, fix bugs in close method and mode docs. 'mode' docs fix: the file must always be opened in binary in Python3. Bug in Wave_write.close: when the close method calls the check that the header exists and it raises an error, the _file attribute never gets set to None, so the next close tries to close the file again and we get an ignored traceback in the __del__ method. The fix is to set _file to None in a finally clause. This represents a behavior change...in theory a program could be checking for the error on close and then doing a recovery action on the still open file and closing it again. But this change will only go into 3.4, so I think that behavior change is acceptable given that it would be pretty weird and unlikely logic to begin with.
author R David Murray <rdmurray@bitdance.com>
date Wed, 31 Jul 2013 20:48:26 -0400
parents 8327780d3841
children a14ec46de0a4
line wrap: on
line diff
--- a/Lib/wave.py	Thu Aug 01 00:12:06 2013 +0200
+++ b/Lib/wave.py	Wed Jul 31 20:48:26 2013 -0400
@@ -448,11 +448,13 @@
 
     def close(self):
         if self._file:
-            self._ensure_header_written(0)
-            if self._datalength != self._datawritten:
-                self._patchheader()
-            self._file.flush()
-            self._file = None
+            try:
+                self._ensure_header_written(0)
+                if self._datalength != self._datawritten:
+                    self._patchheader()
+                self._file.flush()
+            finally:
+                self._file = None
         if self._i_opened_the_file:
             self._i_opened_the_file.close()
             self._i_opened_the_file = None