Issue #16685: Added support for writing any bytes-like objects in the aifc,
sunau, and wave modules.
diff --git a/Lib/aifc.py b/Lib/aifc.py
index 18f236d..c1c8ea7 100644
--- a/Lib/aifc.py
+++ b/Lib/aifc.py
@@ -692,6 +692,8 @@
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 1880a01..3c24492 100644
--- a/Lib/sunau.py
+++ b/Lib/sunau.py
@@ -415,6 +415,8 @@
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 c22f0a1..0e9175d 100644
--- a/Lib/test/audiotests.py
+++ b/Lib/test/audiotests.py
@@ -146,6 +146,30 @@
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 3d01817..7de1cd0 100644
--- a/Lib/wave.py
+++ b/Lib/wave.py
@@ -435,6 +435,8 @@
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: