summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-11-16 14:01:31 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2013-11-16 14:01:31 +0200
commit452bab4acf57c8dd156b794fe0d1672e1ab29b43 (patch)
treed167584d1dd05c4ca5303c4ff91c2dfa927a5e06
parent7714ebbe0e7556bbfa6b768e434042b55474939c (diff)
downloadcpython-git-452bab4acf57c8dd156b794fe0d1672e1ab29b43.tar.gz
Issue #16685: Added support for writing any bytes-like objects in the aifc,
sunau, and wave modules.
-rw-r--r--Doc/library/aifc.rst6
-rw-r--r--Doc/library/sunau.rst6
-rw-r--r--Doc/library/wave.rst6
-rw-r--r--Lib/aifc.py2
-rw-r--r--Lib/sunau.py2
-rw-r--r--Lib/test/audiotests.py24
-rw-r--r--Lib/wave.py2
-rw-r--r--Misc/NEWS3
8 files changed, 51 insertions, 0 deletions
diff --git a/Doc/library/aifc.rst b/Doc/library/aifc.rst
index 48c3ea9435..9ffb5a3ffe 100644
--- a/Doc/library/aifc.rst
+++ b/Doc/library/aifc.rst
@@ -225,12 +225,18 @@ number of frames must be filled in.
Write data to the output file. This method can only be called after the audio
file parameters have been set.
+ .. versionchanged:: 3.4
+ Any :term:`bytes-like object`\ s are now accepted.
+
.. method:: aifc.writeframesraw(data)
Like :meth:`writeframes`, except that the header of the audio file is not
updated.
+ .. versionchanged:: 3.4
+ Any :term:`bytes-like object`\ s are now accepted.
+
.. method:: aifc.close()
diff --git a/Doc/library/sunau.rst b/Doc/library/sunau.rst
index 6455ed9c75..15c06b561a 100644
--- a/Doc/library/sunau.rst
+++ b/Doc/library/sunau.rst
@@ -250,11 +250,17 @@ AU_write objects, as returned by :func:`.open` above, have the following methods
Write audio frames, without correcting *nframes*.
+ .. versionchanged:: 3.4
+ Any :term:`bytes-like object`\ s are now accepted.
+
.. method:: AU_write.writeframes(data)
Write audio frames and make sure *nframes* is correct.
+ .. versionchanged:: 3.4
+ Any :term:`bytes-like object`\ s are now accepted.
+
.. method:: AU_write.close()
diff --git a/Doc/library/wave.rst b/Doc/library/wave.rst
index 189f21a3e2..c32e1fcd85 100644
--- a/Doc/library/wave.rst
+++ b/Doc/library/wave.rst
@@ -208,12 +208,18 @@ Wave_write objects, as returned by :func:`.open`, have the following methods:
Write audio frames, without correcting *nframes*.
+ .. versionchanged:: 3.4
+ Any :term:`bytes-like object`\ s are now accepted.
+
.. method:: Wave_write.writeframes(data)
Write audio frames and make sure *nframes* is correct. Can raise an
exception if a file is not seekable.
+ .. versionchanged:: 3.4
+ Any :term:`bytes-like object`\ s are now accepted.
+
Note that it is invalid to set any parameters after calling :meth:`writeframes`
or :meth:`writeframesraw`, and any attempt to do so will raise
diff --git a/Lib/aifc.py b/Lib/aifc.py
index 18f236da7a..c1c8ea7055 100644
--- a/Lib/aifc.py
+++ b/Lib/aifc.py
@@ -692,6 +692,8 @@ class Aifc_write:
return self._nframeswritten
def writeframesraw(self, data):
+ if not isinstance(data, (bytes, bytearray)):
+ data = memoryview(data).cast('B')
self._ensure_header_written(len(data))
nframes = len(data) // (self._sampwidth * self._nchannels)
if self._convert:
diff --git a/Lib/sunau.py b/Lib/sunau.py
index 1880a01d1a..3c244925d1 100644
--- a/Lib/sunau.py
+++ b/Lib/sunau.py
@@ -415,6 +415,8 @@ class Au_write:
return self._nframeswritten
def writeframesraw(self, data):
+ if not isinstance(data, (bytes, bytearray)):
+ data = memoryview(data).cast('B')
self._ensure_header_written()
if self._comptype == 'ULAW':
import audioop
diff --git a/Lib/test/audiotests.py b/Lib/test/audiotests.py
index c22f0a10a9..0e9175d8f4 100644
--- a/Lib/test/audiotests.py
+++ b/Lib/test/audiotests.py
@@ -146,6 +146,30 @@ class AudioWriteTests(AudioTests):
self.check_file(TESTFN, self.nframes, self.frames)
+ def test_write_bytearray(self):
+ f = self.create_file(TESTFN)
+ f.setnframes(self.nframes)
+ f.writeframes(bytearray(self.frames))
+ f.close()
+
+ self.check_file(TESTFN, self.nframes, self.frames)
+
+ def test_write_array(self):
+ f = self.create_file(TESTFN)
+ f.setnframes(self.nframes)
+ f.writeframes(array.array('h', self.frames))
+ f.close()
+
+ self.check_file(TESTFN, self.nframes, self.frames)
+
+ def test_write_memoryview(self):
+ f = self.create_file(TESTFN)
+ f.setnframes(self.nframes)
+ f.writeframes(memoryview(self.frames))
+ f.close()
+
+ self.check_file(TESTFN, self.nframes, self.frames)
+
def test_incompleted_write(self):
with open(TESTFN, 'wb') as testfile:
testfile.write(b'ababagalamaga')
diff --git a/Lib/wave.py b/Lib/wave.py
index 3d01817afd..7de1cd0f88 100644
--- a/Lib/wave.py
+++ b/Lib/wave.py
@@ -435,6 +435,8 @@ class Wave_write:
return self._nframeswritten
def writeframesraw(self, data):
+ if not isinstance(data, (bytes, bytearray)):
+ data = memoryview(data).cast('B')
self._ensure_header_written(len(data))
nframes = len(data) // (self._sampwidth * self._nchannels)
if self._convert:
diff --git a/Misc/NEWS b/Misc/NEWS
index 9fd34c299a..67b5dd0afd 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -47,6 +47,9 @@ Core and Builtins
Library
-------
+- Issue #16685: Added support for writing any bytes-like objects in the aifc,
+ sunau, and wave modules.
+
- Issue #5202: Added support for unseekable files in the wave module.
- Issue #19544 and Issue #1180: Restore global option to ignore