From ab1f4674ad14eb1430489a458c7654aa1ac9c51e Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Thu, 15 Mar 2007 07:41:30 +0000 Subject: Patch #1681153: the wave module now closes a file object it opened if initialization failed. --- Lib/wave.py | 14 ++++++++++++-- Misc/NEWS | 3 +++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Lib/wave.py b/Lib/wave.py index 08c51ba..b993b40 100644 --- a/Lib/wave.py +++ b/Lib/wave.py @@ -159,7 +159,12 @@ class Wave_read: f = __builtin__.open(f, 'rb') self._i_opened_the_file = f # else, assume it is an open file object already - self.initfp(f) + try: + self.initfp(f) + except: + if self._i_opened_the_file: + f.close() + raise def __del__(self): self.close() @@ -297,7 +302,12 @@ class Wave_write: if isinstance(f, basestring): f = __builtin__.open(f, 'wb') self._i_opened_the_file = f - self.initfp(f) + try: + self.initfp(f) + except: + if self._i_opened_the_file: + f.close() + raise def initfp(self, file): self._file = file diff --git a/Misc/NEWS b/Misc/NEWS index ec9ef6e..f69d1b5 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -173,6 +173,9 @@ Core and builtins Library ------- +- Patch #1681153: the wave module now closes a file object it opened if + initialization failed. + - Bug #767111: fix long-standing bug in urllib which caused an AttributeError instead of an IOError when the server's response didn't contain a valid HTTP status line. -- cgit v0.12