This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Wave.py does not always write proper length in header
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: jtidman, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2011-02-05 00:00 by jtidman, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
patch.txt jtidman, 2011-02-05 00:00 Patch file
Messages (4)
msg127956 - (view) Author: (jtidman) Date: 2011-02-05 00:00
wave.py does not always honor the sampwidth setting, especially on little endian machines.  If sampwidth is not one and big_endian is not set, then datawritten will not be muliplied by sampwidth, causing the header to be incorrect, and the file to appear to contain less data than it chould.
msg128424 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-02-11 19:49
I agree that the _sampwidth multiplier is needed regardless of endianness.

The simplest option would be to pull the _datawritten statement out of the alternation, making the code read

        if self._sampwidth > 1 and big_endian:
            import array
            data = array.array(_array_fmts[self._sampwidth], data)
            data.byteswap()
            data.tofile(self._file)
        else:
            self._file.write(data)
        self._datawritten = self._datawritten + len(data) * self._sampwidth

Note: while _sampwidth is initialized to 0, _ensure_header_written() checks that it is not 0, and it is used elsewhere as a divisor.

The above adds a usually unneeded multiply by 1, but the alternative requires duplication of _file.write and two _datawritten statements

        if self._sampwidth > 1:
            if big_endian:
                import array
                data = array.array(_array_fmts[self._sampwidth], data)
                data.byteswap()
                data.tofile(self._file)
            else: # little_endian
                self._file.write(data)
            self._datawritten = self._datawritten + len(data) * self._sampwidth
        else: # _sampwidth == 1
            self._file.write(data)
            self._datawritten = self._datawritten + len(data)

This module is new to me. Can you think of any way to test this issue, perhaps by writing to StringIO file? This is not a heavily tested module ;-)

In 3.3, the openfp synonym for open could perhaps be deprecated.
msg128474 - (view) Author: (jtidman) Date: 2011-02-12 23:54
Yep, your solution is better.  I can provide some text files (lists of numbers) and two programs, wave2text.py and text2wav.py.  These are the programs I wrote that found this issue in the first place.  Add a few checks and you could run text2wave.py and then wave2text.py and verify that the results of these two steps match the original text files.

I look forward to working on this module with you.  Unfortunately the time frame would have to be sometime in 2011, as I am currently very busy.
msg192066 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-06-30 11:30
I don't see anything wrong in current code. In first alternation data is an array of sampwidth-sized items and the number of written bytes is len(data) * self._sampwidth. In second alternation data is raw bytes object and the number of written bytes is just len(data).

Could you please provide a sample script which exposes the wrong behavior?
History
Date User Action Args
2022-04-11 14:57:12adminsetgithub: 55335
2013-09-03 20:49:15serhiy.storchakasetstatus: pending -> closed
resolution: not a bug
stage: resolved
2013-08-06 12:19:33serhiy.storchakasetstatus: open -> pending
2013-06-30 11:30:10serhiy.storchakasetmessages: + msg192066
2013-06-30 08:15:32serhiy.storchakasetnosy: + serhiy.storchaka
2013-06-30 05:57:36terry.reedysetversions: + Python 3.3, Python 3.4, - Python 3.1, Python 3.2
2011-02-12 23:54:28jtidmansetmessages: + msg128474
2011-02-11 19:49:19terry.reedysetnosy: + terry.reedy

messages: + msg128424
versions: + Python 3.1, Python 3.2
2011-02-05 00:00:47jtidmancreate