Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wave.py does not always write proper length in header #55335

Closed
jtidman mannequin opened this issue Feb 5, 2011 · 4 comments
Closed

Wave.py does not always write proper length in header #55335

jtidman mannequin opened this issue Feb 5, 2011 · 4 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@jtidman
Copy link
Mannequin

jtidman mannequin commented Feb 5, 2011

BPO 11126
Nosy @terryjreedy, @serhiy-storchaka
Files
  • patch.txt: Patch file
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2013-09-03.20:49:15.622>
    created_at = <Date 2011-02-05.00:00:47.959>
    labels = ['invalid', 'type-bug', 'library']
    title = 'Wave.py does not always write proper length in header'
    updated_at = <Date 2013-09-03.20:49:15.621>
    user = 'https://bugs.python.org/jtidman'

    bugs.python.org fields:

    activity = <Date 2013-09-03.20:49:15.621>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-09-03.20:49:15.622>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2011-02-05.00:00:47.959>
    creator = 'jtidman'
    dependencies = []
    files = ['20682']
    hgrepos = []
    issue_num = 11126
    keywords = []
    message_count = 4.0
    messages = ['127956', '128424', '128474', '192066']
    nosy_count = 3.0
    nosy_names = ['terry.reedy', 'jtidman', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue11126'
    versions = ['Python 2.7', 'Python 3.3', 'Python 3.4']

    @jtidman
    Copy link
    Mannequin Author

    jtidman mannequin commented Feb 5, 2011

    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.

    @jtidman jtidman mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Feb 5, 2011
    @terryjreedy
    Copy link
    Member

    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.

    @jtidman
    Copy link
    Mannequin Author

    jtidman mannequin commented Feb 12, 2011

    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.

    @serhiy-storchaka
    Copy link
    Member

    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?

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants