[Python-checkins] cpython: #17616: wave.open now supports the 'with' statement.
r.david.murray
python-checkins at python.org
Wed Jul 31 19:50:06 CEST 2013
http://hg.python.org/cpython/rev/8327780d3841
changeset: 84932:8327780d3841
user: R David Murray <rdmurray at bitdance.com>
date: Wed Jul 31 13:46:08 2013 -0400
summary:
#17616: wave.open now supports the 'with' statement.
Feature and tests by ClClaudiu.Popa, I added the doc changes.
files:
Doc/library/aifc.rst | 3 ++-
Doc/library/wave.rst | 5 +++++
Doc/whatsnew/3.4.rst | 5 ++++-
Lib/test/test_wave.py | 29 +++++++++++++++++++----------
Lib/wave.py | 13 +++++++++++++
Misc/NEWS | 2 ++
6 files changed, 45 insertions(+), 12 deletions(-)
diff --git a/Doc/library/aifc.rst b/Doc/library/aifc.rst
--- a/Doc/library/aifc.rst
+++ b/Doc/library/aifc.rst
@@ -51,7 +51,8 @@
used for writing, the file object should be seekable, unless you know ahead of
time how many samples you are going to write in total and use
:meth:`writeframesraw` and :meth:`setnframes`.
- Objects returned by :func:`.open` also supports the :keyword:`with` statement.
+ The :func:`.open` function may be used in a :keyword:`with` statement. When
+ the :keyword:`with` block completes, the :meth:`~aifc.close` method is called.
.. versionchanged:: 3.4
Support for the :keyword:`with` statement was added.
diff --git a/Doc/library/wave.rst b/Doc/library/wave.rst
--- a/Doc/library/wave.rst
+++ b/Doc/library/wave.rst
@@ -39,6 +39,11 @@
:meth:`close` method is called; it is the caller's responsibility to close
the file object.
+ The :func:`.open` function may be used in a :keyword:`with` statement. When
+ the :keyword:`with` block completes, the :meth:`Wave_read.close()
+ <wave.Wave_read.close>` or :meth:`Wave_write.close()
+ <wave.Wave_write.close()>` method is called.
+
.. function:: openfp(file, mode)
diff --git a/Doc/whatsnew/3.4.rst b/Doc/whatsnew/3.4.rst
--- a/Doc/whatsnew/3.4.rst
+++ b/Doc/whatsnew/3.4.rst
@@ -239,8 +239,11 @@
The :meth:`~wave.getparams` method now returns a namedtuple rather than a
plain tuple. (Contributed by Claudiu Popa in :issue:`17487`.)
+:meth:`wave.open` now supports the context manager protocol. (Contributed
+by Claudiu Popa in :issue:`17616`.)
+
stat
----
+----
The stat module is now backed by a C implementation in :mod:`_stat`. A C
implementation is required as most of the values aren't standardized and
diff --git a/Lib/test/test_wave.py b/Lib/test/test_wave.py
--- a/Lib/test/test_wave.py
+++ b/Lib/test/test_wave.py
@@ -1,7 +1,5 @@
-from test.support import TESTFN, run_unittest
-import os
+from test.support import TESTFN, unlink
import wave
-import struct
import unittest
nchannels = 2
@@ -17,10 +15,7 @@
def tearDown(self):
if self.f is not None:
self.f.close()
- try:
- os.remove(TESTFN)
- except OSError:
- pass
+ unlink(TESTFN)
def test_it(self, test_rounding=False):
self.f = wave.open(TESTFN, 'wb')
@@ -74,9 +69,23 @@
self.assertEqual(params.comptype, self.f.getcomptype())
self.assertEqual(params.compname, self.f.getcompname())
+ def test_context_manager(self):
+ self.f = wave.open(TESTFN, 'wb')
+ self.f.setnchannels(nchannels)
+ self.f.setsampwidth(sampwidth)
+ self.f.setframerate(framerate)
+ self.f.close()
-def test_main():
- run_unittest(TestWave)
+ with wave.open(TESTFN) as f:
+ self.assertFalse(f.getfp().closed)
+ self.assertIs(f.getfp(), None)
+
+ with open(TESTFN, 'wb') as testfile:
+ with self.assertRaises(wave.Error):
+ with wave.open(testfile, 'wb'):
+ pass
+ self.assertEqual(testfile.closed, False)
+
if __name__ == '__main__':
- test_main()
+ unittest.main()
diff --git a/Lib/wave.py b/Lib/wave.py
--- a/Lib/wave.py
+++ b/Lib/wave.py
@@ -167,6 +167,13 @@
def __del__(self):
self.close()
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ self.close()
+
#
# User visible methods.
#
@@ -323,6 +330,12 @@
def __del__(self):
self.close()
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ self.close()
+
#
# User visible methods.
#
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -173,6 +173,8 @@
Library
-------
+- Issue #17616: wave.open now supports the context manager protocol.
+
- Issue #18599: Fix name attribute of _sha1.sha1() object. It now returns
'SHA1' instead of 'SHA'.
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list