summaryrefslogtreecommitdiffstats
path: root/Lib/sunau.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-09-05 14:01:53 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-09-05 14:01:53 (GMT)
commit34d201374ce4bb2c693565ba5fa0c3aa94b490ad (patch)
treeead32b1fe07c35390ce7d14e455a02a2d4298e14 /Lib/sunau.py
parent555e57de1951aa74e94fcc07778b87fff9b61843 (diff)
downloadcpython-34d201374ce4bb2c693565ba5fa0c3aa94b490ad.zip
cpython-34d201374ce4bb2c693565ba5fa0c3aa94b490ad.tar.gz
cpython-34d201374ce4bb2c693565ba5fa0c3aa94b490ad.tar.bz2
Issue #18878: sunau.open now supports the context manager protocol. Based on
patches by Claudiu Popa and R. David Murray.
Diffstat (limited to 'Lib/sunau.py')
-rw-r--r--Lib/sunau.py31
1 files changed, 23 insertions, 8 deletions
diff --git a/Lib/sunau.py b/Lib/sunau.py
index 010ce23..efdc146 100644
--- a/Lib/sunau.py
+++ b/Lib/sunau.py
@@ -168,6 +168,12 @@ class Au_read:
if self._file:
self.close()
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ self.close()
+
def initfp(self, file):
self._file = file
self._soundpos = 0
@@ -303,6 +309,12 @@ class Au_write:
self.close()
self._file = None
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ self.close()
+
def initfp(self, file):
self._file = file
self._framerate = 0
@@ -410,14 +422,17 @@ class Au_write:
self._patchheader()
def close(self):
- self._ensure_header_written()
- if self._nframeswritten != self._nframes or \
- self._datalength != self._datawritten:
- self._patchheader()
- self._file.flush()
- if self._opened and self._file:
- self._file.close()
- self._file = None
+ if self._file:
+ try:
+ self._ensure_header_written()
+ if self._nframeswritten != self._nframes or \
+ self._datalength != self._datawritten:
+ self._patchheader()
+ self._file.flush()
+ finally:
+ if self._opened and self._file:
+ self._file.close()
+ self._file = None
#
# private methods