cpython: 6a599249e8b7 (original) (raw)
Mercurial > cpython
changeset 87148:6a599249e8b7
Issue #5202: Added support for unseekable files in the wave module. [#5202]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Sat, 16 Nov 2013 13:04:00 +0200 |
parents | cc8e56886807 |
children | b96f4ee1b08b |
files | Doc/library/wave.rst Lib/test/audiotests.py Lib/test/test_aifc.py Lib/wave.py Misc/NEWS |
diffstat | 5 files changed, 90 insertions(+), 34 deletions(-)[+] [-] Doc/library/wave.rst 10 Lib/test/audiotests.py 60 Lib/test/test_aifc.py 44 Lib/wave.py 8 Misc/NEWS 2 |
line wrap: on
line diff
--- a/Doc/library/wave.rst
+++ b/Doc/library/wave.rst
@@ -19,7 +19,7 @@ The :mod:wave
module defines the follo
.. function:: open(file, mode=None)
If file is a string, open the file by that name, otherwise treat it as a
- file-like object. mode can be:
'rb'
Read only mode. @@ -43,6 +43,8 @@ The :mod:wave
module defines the follo <wave.Wave_read.close>or :meth:
Wave_write.close() <wave.Wave_write.close()>` method is called. - .. versionchanged:: 3.4
Added support for unseekable files.[](#l1.17)
.. function:: openfp(file, mode) @@ -154,7 +156,8 @@ Wave_write objects, as returned by :func .. method:: Wave_write.close() Make sure nframes is correct, and close the file if it was opened by
- :mod:
wave
. This method is called upon object collection. Can raise an - exception if nframes is not correct and a file is not seekable.
.. method:: Wave_write.setnchannels(n) @@ -208,7 +211,8 @@ Wave_write objects, as returned by :func .. method:: Wave_write.writeframes(data)
- Write audio frames and make sure nframes is correct. Can raise an
- exception if a file is not seekable.
Note that it is invalid to set any parameters after calling :meth:writeframes
--- a/Lib/test/audiotests.py +++ b/Lib/test/audiotests.py @@ -21,6 +21,13 @@ def byteswap4(data): a.byteswap() return a.tobytes() +class UnseekableIO(io.FileIO):
+ class AudioTests: close_fd = False @@ -177,6 +184,59 @@ class AudioWriteTests(AudioTests): self.assertEqual(testfile.read(13), b'ababagalamaga') self.check_file(testfile, self.nframes, self.frames)
- def test_unseekable_read(self):
with self.create_file(TESTFN) as f:[](#l2.22)
f.setnframes(self.nframes)[](#l2.23)
f.writeframes(self.frames)[](#l2.24)
with UnseekableIO(TESTFN, 'rb') as testfile:[](#l2.26)
self.check_file(testfile, self.nframes, self.frames)[](#l2.27)
- def test_unseekable_write(self):
with UnseekableIO(TESTFN, 'wb') as testfile:[](#l2.30)
with self.create_file(testfile) as f:[](#l2.31)
f.setnframes(self.nframes)[](#l2.32)
f.writeframes(self.frames)[](#l2.33)
self.check_file(TESTFN, self.nframes, self.frames)[](#l2.35)
- def test_unseekable_incompleted_write(self):
with UnseekableIO(TESTFN, 'wb') as testfile:[](#l2.38)
testfile.write(b'ababagalamaga')[](#l2.39)
f = self.create_file(testfile)[](#l2.40)
f.setnframes(self.nframes + 1)[](#l2.41)
try:[](#l2.42)
f.writeframes(self.frames)[](#l2.43)
except OSError:[](#l2.44)
pass[](#l2.45)
try:[](#l2.46)
f.close()[](#l2.47)
except OSError:[](#l2.48)
pass[](#l2.49)
with open(TESTFN, 'rb') as testfile:[](#l2.51)
self.assertEqual(testfile.read(13), b'ababagalamaga')[](#l2.52)
self.check_file(testfile, self.nframes + 1, self.frames)[](#l2.53)
- def test_unseekable_overflowed_write(self):
with UnseekableIO(TESTFN, 'wb') as testfile:[](#l2.56)
testfile.write(b'ababagalamaga')[](#l2.57)
f = self.create_file(testfile)[](#l2.58)
f.setnframes(self.nframes - 1)[](#l2.59)
try:[](#l2.60)
f.writeframes(self.frames)[](#l2.61)
except OSError:[](#l2.62)
pass[](#l2.63)
try:[](#l2.64)
f.close()[](#l2.65)
except OSError:[](#l2.66)
pass[](#l2.67)
with open(TESTFN, 'rb') as testfile:[](#l2.69)
self.assertEqual(testfile.read(13), b'ababagalamaga')[](#l2.70)
framesize = self.nchannels * self.sampwidth[](#l2.71)
self.check_file(testfile, self.nframes - 1, self.frames[:-framesize])[](#l2.72)
+ class AudioTestsWithSourceFile(AudioTests):
--- a/Lib/test/test_aifc.py +++ b/Lib/test/test_aifc.py @@ -8,10 +8,17 @@ import struct import aifc -class AifcPCM8Test(audiotests.AudioWriteTests,
audiotests.AudioTestsWithSourceFile,[](#l3.8)
unittest.TestCase):[](#l3.9)
+class AifcTest(audiotests.AudioWriteTests,
module = aifcaudiotests.AudioTestsWithSourceFile):[](#l3.11)
- close_fd = True
- test_unseekable_read = None
- test_unseekable_write = None
- test_unseekable_incompleted_write = None
- test_unseekable_overflowed_write = None
+ + +class AifcPCM8Test(AifcTest, unittest.TestCase): sndfilename = 'pluck-pcm8.aiff' sndfilenframes = 3307 nchannels = 2 @@ -26,13 +33,9 @@ class AifcPCM8Test(audiotests.AudioWrite 11FA 3EFB BCFC 66FF CF04 4309 C10E 5112 EE17 8216 7F14 8012 [](#l3.25) 490E 520D EF0F CE0F E40C 630A 080A 2B0B 510E 8B11 B60E 440A [](#l3.26) """)
-class AifcPCM16Test(audiotests.AudioWriteTests,
+class AifcPCM16Test(AifcTest, unittest.TestCase): sndfilename = 'pluck-pcm16.aiff' sndfilenframes = 3307 nchannels = 2 @@ -49,13 +52,9 @@ class AifcPCM16Test(audiotests.AudioWrit EEE21753 82071665 7FFF1443 8004128F 49A20EAF 52BB0DBA EFB40F60 CE3C0FBF [](#l3.40) E4B30CEC 63430A5C 08C80A20 2BBB0B08 514A0E43 8BCF1139 B6F60EEB 44120A5E [](#l3.41) """)
-class AifcPCM24Test(audiotests.AudioWriteTests,
+class AifcPCM24Test(AifcTest, unittest.TestCase): sndfilename = 'pluck-pcm24.aiff' sndfilenframes = 3307 nchannels = 2 @@ -78,13 +77,9 @@ class AifcPCM24Test(audiotests.AudioWrit E4B49C0CEA2D 6344A80A5A7C 08C8FE0A1FFE 2BB9860B0A0E [](#l3.55) 51486F0E44E1 8BCC64113B05 B6F4EC0EEB36 4413170A5B48 [](#l3.56) """)
-class AifcPCM32Test(audiotests.AudioWriteTests,
+class AifcPCM32Test(AifcTest, unittest.TestCase): sndfilename = 'pluck-pcm32.aiff' sndfilenframes = 3307 nchannels = 2 @@ -107,13 +102,9 @@ class AifcPCM32Test(audiotests.AudioWrit E4B49CC00CEA2D90 6344A8800A5A7CA0 08C8FE800A1FFEE0 2BB986C00B0A0E00 [](#l3.70) 51486F800E44E190 8BCC6480113B0580 B6F4EC000EEB3630 441317800A5B48A0 [](#l3.71) """)
-class AifcULAWTest(audiotests.AudioWriteTests,
+class AifcULAWTest(AifcTest, unittest.TestCase): sndfilename = 'pluck-ulaw.aifc' sndfilenframes = 3307 nchannels = 2 @@ -132,13 +123,9 @@ class AifcULAWTest(audiotests.AudioWrite """) if sys.byteorder != 'big': frames = audiotests.byteswap2(frames)
-class AifcALAWTest(audiotests.AudioWriteTests,
+class AifcALAWTest(AifcTest, unittest.TestCase): sndfilename = 'pluck-alaw.aifc' sndfilenframes = 3307 nchannels = 2 @@ -157,7 +144,6 @@ class AifcALAWTest(audiotests.AudioWrite """) if sys.byteorder != 'big': frames = audiotests.byteswap2(frames)
class AifcMiscTest(audiotests.AudioTests, unittest.TestCase):
--- a/Lib/wave.py +++ b/Lib/wave.py @@ -491,14 +491,18 @@ class Wave_write: if not self._nframes: self._nframes = initlength // (self._nchannels * self._sampwidth) self._datalength = self._nframes * self._nchannels * self._sampwidth
self._form_length_pos = self._file.tell()[](#l4.7)
try:[](#l4.8)
self._form_length_pos = self._file.tell()[](#l4.9)
except (AttributeError, OSError):[](#l4.10)
self._form_length_pos = None[](#l4.11) self._file.write(struct.pack('<L4s4sLHHLLHH4s',[](#l4.12) 36 + self._datalength, b'WAVE', b'fmt ', 16,[](#l4.13) WAVE_FORMAT_PCM, self._nchannels, self._framerate,[](#l4.14) self._nchannels * self._framerate * self._sampwidth,[](#l4.15) self._nchannels * self._sampwidth,[](#l4.16) self._sampwidth * 8, b'data'))[](#l4.17)
self._data_length_pos = self._file.tell()[](#l4.18)
if self._form_length_pos is not None:[](#l4.19)
self._data_length_pos = self._file.tell()[](#l4.20) self._file.write(struct.pack('<L', self._datalength))[](#l4.21) self._headerwritten = True[](#l4.22)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -47,6 +47,8 @@ Core and Builtins Library ------- +- Issue #5202: Added support for unseekable files in the wave module. +