cpython: a62f59667c9e (original) (raw)
Mercurial > cpython
changeset 85534:a62f59667c9e
Issue #18878: sunau.open now supports the context manager protocol. Based on patches by Claudiu Popa and R. David Murray. [#18878]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Thu, 05 Sep 2013 17:01:53 +0300 |
parents | ac27d979078a |
children | cecec75c2250 86ab7b7c173e |
files | Doc/whatsnew/3.4.rst Lib/sunau.py Lib/test/test_sunau.py Misc/NEWS |
diffstat | 4 files changed, 72 insertions(+), 15 deletions(-)[+] [-] Doc/whatsnew/3.4.rst 2 Lib/sunau.py 31 Lib/test/test_sunau.py 51 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Doc/whatsnew/3.4.rst
+++ b/Doc/whatsnew/3.4.rst
@@ -374,6 +374,8 @@ sunau
The :meth:~sunau.getparams
method now returns a namedtuple rather than a
plain tuple. (Contributed by Claudiu Popa in :issue:18901
.)
+:meth:sunau.open
now supports the context manager protocol (:issue:18878
).
+
urllib
------
--- a/Lib/sunau.py +++ b/Lib/sunau.py @@ -168,6 +168,12 @@ class Au_read: if self._file: 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 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()[](#l2.33)
if self._nframeswritten != self._nframes or \[](#l2.34)
self._datalength != self._datawritten:[](#l2.35)
self._patchheader()[](#l2.36)
self._file.flush()[](#l2.37)
if self._opened and self._file:[](#l2.38)
self._file.close()[](#l2.39)
self._file = None[](#l2.40)
if self._file:[](#l2.41)
try:[](#l2.42)
self._ensure_header_written()[](#l2.43)
if self._nframeswritten != self._nframes or \[](#l2.44)
self._datalength != self._datawritten:[](#l2.45)
self._patchheader()[](#l2.46)
self._file.flush()[](#l2.47)
finally:[](#l2.48)
if self._opened and self._file:[](#l2.49)
self._file.close()[](#l2.50)
self._file = None[](#l2.51)
--- a/Lib/test/test_sunau.py +++ b/Lib/test/test_sunau.py @@ -1,4 +1,4 @@ -from test.support import run_unittest, TESTFN +from test.support import TESTFN, unlink import unittest import pickle import os @@ -18,10 +18,7 @@ class SunAUTest(unittest.TestCase): def tearDown(self): if self.f is not None: self.f.close()
try:[](#l3.13)
os.remove(TESTFN)[](#l3.14)
except OSError:[](#l3.15)
pass[](#l3.16)
unlink(TESTFN)[](#l3.17)
def test_lin(self): self.f = sunau.open(TESTFN, 'w') @@ -84,9 +81,49 @@ class SunAUTest(unittest.TestCase): dump = pickle.dumps(params) self.assertEqual(pickle.loads(dump), params)
- def test_write_context_manager_calls_close(self):
# Close checks for a minimum header and will raise an error[](#l3.26)
# if it is not set, so this proves that close is called.[](#l3.27)
with self.assertRaises(sunau.Error):[](#l3.28)
with sunau.open(TESTFN, 'wb') as f:[](#l3.29)
pass[](#l3.30)
with self.assertRaises(sunau.Error):[](#l3.31)
with open(TESTFN, 'wb') as testfile:[](#l3.32)
with sunau.open(testfile):[](#l3.33)
pass[](#l3.34)
- def test_context_manager_with_open_file(self):
with open(TESTFN, 'wb') as testfile:[](#l3.39)
with sunau.open(testfile) as f:[](#l3.40)
f.setnchannels(nchannels)[](#l3.41)
f.setsampwidth(sampwidth)[](#l3.42)
f.setframerate(framerate)[](#l3.43)
self.assertFalse(testfile.closed)[](#l3.44)
with open(TESTFN, 'rb') as testfile:[](#l3.45)
with sunau.open(testfile) as f:[](#l3.46)
self.assertFalse(f.getfp().closed)[](#l3.47)
params = f.getparams()[](#l3.48)
self.assertEqual(params[0], nchannels)[](#l3.49)
self.assertEqual(params[1], sampwidth)[](#l3.50)
self.assertEqual(params[2], framerate)[](#l3.51)
self.assertIsNone(f.getfp())[](#l3.52)
self.assertFalse(testfile.closed)[](#l3.53)
- def test_context_manager_with_filename(self):
# If the file doesn't get closed, this test won't fail, but it will[](#l3.56)
# produce a resource leak warning.[](#l3.57)
with sunau.open(TESTFN, 'wb') as f:[](#l3.58)
f.setnchannels(nchannels)[](#l3.59)
f.setsampwidth(sampwidth)[](#l3.60)
f.setframerate(framerate)[](#l3.61)
with sunau.open(TESTFN) as f:[](#l3.62)
self.assertFalse(f.getfp().closed)[](#l3.63)
params = f.getparams()[](#l3.64)
self.assertEqual(params[0], nchannels)[](#l3.65)
self.assertEqual(params[1], sampwidth)[](#l3.66)
self.assertEqual(params[2], framerate)[](#l3.67)
self.assertIsNone(f.getfp())[](#l3.68)
+ if name == "main": unittest.main()
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -54,6 +54,9 @@ Core and Builtins Library ------- +- Issue #18878: sunau.open now supports the context manager protocol. Based on