Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 452bab4

Browse files
Issue #16685: Added support for writing any bytes-like objects in the aifc,
sunau, and wave modules.
1 parent 7714ebb commit 452bab4

8 files changed

Lines changed: 51 additions & 0 deletions

File tree

Doc/library/aifc.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,18 @@ number of frames must be filled in.
225225
Write data to the output file. This method can only be called after the audio
226226
file parameters have been set.
227227

228+
.. versionchanged:: 3.4
229+
Any :term:`bytes-like object`\ s are now accepted.
230+
228231

229232
.. method:: aifc.writeframesraw(data)
230233

231234
Like :meth:`writeframes`, except that the header of the audio file is not
232235
updated.
233236

237+
.. versionchanged:: 3.4
238+
Any :term:`bytes-like object`\ s are now accepted.
239+
234240

235241
.. method:: aifc.close()
236242

Doc/library/sunau.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,17 @@ AU_write objects, as returned by :func:`.open` above, have the following methods
250250

251251
Write audio frames, without correcting *nframes*.
252252

253+
.. versionchanged:: 3.4
254+
Any :term:`bytes-like object`\ s are now accepted.
255+
253256

254257
.. method:: AU_write.writeframes(data)
255258

256259
Write audio frames and make sure *nframes* is correct.
257260

261+
.. versionchanged:: 3.4
262+
Any :term:`bytes-like object`\ s are now accepted.
263+
258264

259265
.. method:: AU_write.close()
260266

Doc/library/wave.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,18 @@ Wave_write objects, as returned by :func:`.open`, have the following methods:
208208

209209
Write audio frames, without correcting *nframes*.
210210

211+
.. versionchanged:: 3.4
212+
Any :term:`bytes-like object`\ s are now accepted.
213+
211214

212215
.. method:: Wave_write.writeframes(data)
213216

214217
Write audio frames and make sure *nframes* is correct. Can raise an
215218
exception if a file is not seekable.
216219

220+
.. versionchanged:: 3.4
221+
Any :term:`bytes-like object`\ s are now accepted.
222+
217223

218224
Note that it is invalid to set any parameters after calling :meth:`writeframes`
219225
or :meth:`writeframesraw`, and any attempt to do so will raise

Lib/aifc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,8 @@ def tell(self):
692692
return self._nframeswritten
693693

694694
def writeframesraw(self, data):
695+
if not isinstance(data, (bytes, bytearray)):
696+
data = memoryview(data).cast('B')
695697
self._ensure_header_written(len(data))
696698
nframes = len(data) // (self._sampwidth * self._nchannels)
697699
if self._convert:

Lib/sunau.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ def tell(self):
415415
return self._nframeswritten
416416

417417
def writeframesraw(self, data):
418+
if not isinstance(data, (bytes, bytearray)):
419+
data = memoryview(data).cast('B')
418420
self._ensure_header_written()
419421
if self._comptype == 'ULAW':
420422
import audioop

Lib/test/audiotests.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,30 @@ def test_write(self):
146146

147147
self.check_file(TESTFN, self.nframes, self.frames)
148148

149+
def test_write_bytearray(self):
150+
f = self.create_file(TESTFN)
151+
f.setnframes(self.nframes)
152+
f.writeframes(bytearray(self.frames))
153+
f.close()
154+
155+
self.check_file(TESTFN, self.nframes, self.frames)
156+
157+
def test_write_array(self):
158+
f = self.create_file(TESTFN)
159+
f.setnframes(self.nframes)
160+
f.writeframes(array.array('h', self.frames))
161+
f.close()
162+
163+
self.check_file(TESTFN, self.nframes, self.frames)
164+
165+
def test_write_memoryview(self):
166+
f = self.create_file(TESTFN)
167+
f.setnframes(self.nframes)
168+
f.writeframes(memoryview(self.frames))
169+
f.close()
170+
171+
self.check_file(TESTFN, self.nframes, self.frames)
172+
149173
def test_incompleted_write(self):
150174
with open(TESTFN, 'wb') as testfile:
151175
testfile.write(b'ababagalamaga')

Lib/wave.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@ def tell(self):
435435
return self._nframeswritten
436436

437437
def writeframesraw(self, data):
438+
if not isinstance(data, (bytes, bytearray)):
439+
data = memoryview(data).cast('B')
438440
self._ensure_header_written(len(data))
439441
nframes = len(data) // (self._sampwidth * self._nchannels)
440442
if self._convert:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ Core and Builtins
4747
Library
4848
-------
4949

50+
- Issue #16685: Added support for writing any bytes-like objects in the aifc,
51+
sunau, and wave modules.
52+
5053
- Issue #5202: Added support for unseekable files in the wave module.
5154

5255
- Issue #19544 and Issue #1180: Restore global option to ignore

0 commit comments

Comments
 (0)