[Python-checkins] cpython (2.7): Issue #1575020: Fixed support of 24-bit wave files on big-endian platforms.

serhiy.storchaka python-checkins at python.org
Sat Nov 9 22:17:11 CET 2013


http://hg.python.org/cpython/rev/5fbcb4aa48fa
changeset:   87024:5fbcb4aa48fa
branch:      2.7
parent:      87021:be8f9beca8aa
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Nov 09 23:09:44 2013 +0200
summary:
  Issue #1575020: Fixed support of 24-bit wave files on big-endian platforms.

files:
  Lib/test/test_wave.py |   3 ---
  Lib/wave.py           |  21 +++++++++++++--------
  Misc/NEWS             |   2 ++
  3 files changed, 15 insertions(+), 11 deletions(-)


diff --git a/Lib/test/test_wave.py b/Lib/test/test_wave.py
--- a/Lib/test/test_wave.py
+++ b/Lib/test/test_wave.py
@@ -49,9 +49,6 @@
         frames = audiotests.byteswap2(frames)
 
 
- at unittest.skipIf(sys.byteorder == 'big',
-                 '24-bit wave files are supported only on little-endian '
-                 'platforms')
 class WavePCM24Test(audiotests.AudioWriteTests,
         audiotests.AudioTestsWithSourceFile,
         unittest.TestCase):
diff --git a/Lib/wave.py b/Lib/wave.py
--- a/Lib/wave.py
+++ b/Lib/wave.py
@@ -82,14 +82,15 @@
 
 _array_fmts = None, 'b', 'h', None, 'i'
 
-# Determine endian-ness
 import struct
-if struct.pack("h", 1) == "\000\001":
-    big_endian = 1
-else:
-    big_endian = 0
+import sys
+from chunk import Chunk
 
-from chunk import Chunk
+def _byteswap3(data):
+    ba = bytearray(data)
+    ba[::3] = data[2::3]
+    ba[2::3] = data[::3]
+    return bytes(ba)
 
 class Wave_read:
     """Variables used in this class:
@@ -231,7 +232,7 @@
             self._data_seek_needed = 0
         if nframes == 0:
             return ''
-        if self._sampwidth > 1 and big_endian:
+        if self._sampwidth in (2, 4) and sys.byteorder == 'big':
             # unfortunately the fromfile() method does not take
             # something that only looks like a file object, so
             # we have to reach into the innards of the chunk object
@@ -252,6 +253,8 @@
             data = data.tostring()
         else:
             data = self._data_chunk.read(nframes * self._framesize)
+            if self._sampwidth == 3 and sys.byteorder == 'big':
+                data = _byteswap3(data)
         if self._convert and data:
             data = self._convert(data)
         self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
@@ -419,7 +422,7 @@
         nframes = len(data) // (self._sampwidth * self._nchannels)
         if self._convert:
             data = self._convert(data)
-        if self._sampwidth > 1 and big_endian:
+        if self._sampwidth in (2, 4) and sys.byteorder == 'big':
             import array
             data = array.array(_array_fmts[self._sampwidth], data)
             assert data.itemsize == self._sampwidth
@@ -427,6 +430,8 @@
             data.tofile(self._file)
             self._datawritten = self._datawritten + len(data) * self._sampwidth
         else:
+            if self._sampwidth == 3 and sys.byteorder == 'big':
+                data = _byteswap3(data)
             self._file.write(data)
             self._datawritten = self._datawritten + len(data)
         self._nframeswritten = self._nframeswritten + nframes
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@
 Library
 -------
 
+- Issue #1575020: Fixed support of 24-bit wave files on big-endian platforms.
+
 - Issue #19480: HTMLParser now accepts all valid start-tag names as defined
   by the HTML5 standard.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list