diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-11-21 11:04:22 +0200 |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-11-21 11:04:22 +0200 |
commit | 96297a42f8aed84a25efd5689622d351f26d59e1 (patch) | |
tree | 06ef426953756b68bf389a3796b4b73740dde9f5 /Lib/wave.py | |
parent | f0a230d62325e33297adf44770b7d94180250d23 (diff) | |
parent | 8d8a49a496eb86472243ef2a5fe7911227beb26e (diff) | |
download | cpython-96297a42f8aed84a25efd5689622d351f26d59e1.tar.gz |
Issue #19633: Fixed writing not compressed 16- and 32-bit wave files on
big-endian platforms.
Temporary forbidden test_unseekable_incompleted_write fornot compressed 16-
and 32-bit wave file on big-endian platforms.
Diffstat (limited to 'Lib/wave.py')
-rw-r--r-- | Lib/wave.py | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/Lib/wave.py b/Lib/wave.py index 4a223451bf..672d04b8c9 100644 --- a/Lib/wave.py +++ b/Lib/wave.py @@ -18,7 +18,7 @@ This returns an instance of a class with the following public methods: getcomptype() -- returns compression type ('NONE' for linear samples) getcompname() -- returns human-readable version of compression type ('not compressed' linear samples) - getparams() -- returns a tuple consisting of all of the + getparams() -- returns a namedtuple consisting of all of the above in the above order getmarkers() -- returns None (for compatibility with the aifc module) @@ -85,6 +85,7 @@ _array_fmts = None, 'b', 'h', None, 'i' import struct import sys from chunk import Chunk +from collections import namedtuple def _byteswap3(data): ba = bytearray(data) @@ -92,6 +93,9 @@ def _byteswap3(data): ba[2::3] = data[::3] return bytes(ba) +_wave_params = namedtuple('_wave_params', + 'nchannels sampwidth framerate nframes comptype compname') + class Wave_read: """Variables used in this class: @@ -169,6 +173,13 @@ class Wave_read: def __del__(self): self.close() + + def __enter__(self): + return self + + def __exit__(self, *args): + self.close() + # # User visible methods. # @@ -207,9 +218,9 @@ class Wave_read: return self._compname def getparams(self): - return self.getnchannels(), self.getsampwidth(), \ - self.getframerate(), self.getnframes(), \ - self.getcomptype(), self.getcompname() + return _wave_params(self.getnchannels(), self.getsampwidth(), + self.getframerate(), self.getnframes(), + self.getcomptype(), self.getcompname()) def getmarkers(self): return None @@ -328,6 +339,12 @@ class Wave_write: def __del__(self): self.close() + def __enter__(self): + return self + + def __exit__(self, *args): + self.close() + # # User visible methods. # @@ -402,8 +419,8 @@ class Wave_write: def getparams(self): if not self._nchannels or not self._sampwidth or not self._framerate: raise Error('not all parameters set') - return self._nchannels, self._sampwidth, self._framerate, \ - self._nframes, self._comptype, self._compname + return _wave_params(self._nchannels, self._sampwidth, self._framerate, + self._nframes, self._comptype, self._compname) def setmark(self, id, pos, name): raise Error('setmark() not supported') @@ -418,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: @@ -476,14 +495,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() + try: + self._form_length_pos = self._file.tell() + except (AttributeError, OSError): + self._form_length_pos = None self._file.write(struct.pack('<L4s4sLHHLLHH4s', 36 + self._datalength, b'WAVE', b'fmt ', 16, WAVE_FORMAT_PCM, self._nchannels, self._framerate, self._nchannels * self._framerate * self._sampwidth, self._nchannels * self._sampwidth, self._sampwidth * 8, b'data')) - self._data_length_pos = self._file.tell() + if self._form_length_pos is not None: + self._data_length_pos = self._file.tell() self._file.write(struct.pack('<L', self._datalength)) self._headerwritten = True |