# Buggy code
def write(filename, rate, data):
    """
    Write a numpy array as a WAV file

    Parameters
    ----------
    filename : string or open file handle
        Output wav file
    rate : int
        The sample rate (in samples/sec).
    data : ndarray
        A 1-D or 2-D numpy array of either integer or float data-type.

    Notes
    -----
    * The file can be an open file or a filename.

    * Writes a simple uncompressed WAV file.
    * The bits-per-sample will be determined by the data-type.
    * To write multiple-channels, use a 2-D array of shape
      (Nsamples, Nchannels).

    """
    if hasattr(filename, "write"):
        fid = filename
    else:
        fid = open(filename, "wb")

    try:
        dkind = data.dtype.kind
        if not (
            dkind == "i" or dkind == "f" or (dkind == "u" and data.dtype.itemsize == 1)
        ):
            raise ValueError("Unsupported data type '%s'" % data.dtype)

        fid.write(b"RIFF")
        fid.write(b"\x00\x00\x00\x00")
        fid.write(b"WAVE")
        # fmt chunk
        fid.write(b"fmt ")
        if dkind == "f":
            comp = 3
        else:
            comp = 1
        if data.ndim == 1:
            noc = 1
        else:
            noc = data.shape[1]
        bits = data.dtype.itemsize * 8
        sbytes = rate * (bits // 8) * noc
        ba = noc * (bits // 8)
        fid.write(struct.pack("<ihHIIHH", 16, comp, noc, rate, sbytes, ba, bits))
        # data chunk
        fid.write(b"data")
        fid.write(struct.pack("<i", data.nbytes))
        if data.dtype.byteorder == ">" or (
            data.dtype.byteorder == "=" and sys.byteorder == "big"
        ):
            data = data.byteswap()
        if sys.version_info[0] >= 3:
            fid.write(data.ravel().data)
        else:
            fid.write(data.tostring())
        # Determine file size and place it in correct
        #  position at start of the file.
        size = fid.tell()
        fid.seek(4)
        fid.write(struct.pack("<i", size - 8))

    finally:
        if not hasattr(filename, "write"):
            fid.close()
        else:
            fid.seek(0)


# Traceback (ValueError)
======================================================================
ERROR: test_wavfile.test_write_roundtrip(True, 32000, dtype('>i8'), 5)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/nose/case.py", line 198, in runTest
self.test(*self.arg)
File "/build/buildd-python-scipy_0.13.0~b1-1-hurd-i386-jz8INl/python-scipy-0.13.0~b1/debian/python3-scipy/usr/lib/python3/dist-packages/scipy/io/tests/test_wavfile.py", line 70, in _check_roundtrip
wavfile.write(tmpfile, rate, data)
File "/build/buildd-python-scipy_0.13.0~b1-1-hurd-i386-jz8INl/python-scipy-0.13.0~b1/debian/python3-scipy/usr/lib/python3/dist-packages/scipy/io/wavfile.py", line 248, in write
fid.seek(4)
ValueError: cannot expose native-only dtype 'q' in non-native byte order '>' via buffer interface

----------------------------------------------------------------------

# Fixed code
def write(filename, rate, data):
    """
    Write a numpy array as a WAV file

    Parameters
    ----------
    filename : string or open file handle
        Output wav file
    rate : int
        The sample rate (in samples/sec).
    data : ndarray
        A 1-D or 2-D numpy array of either integer or float data-type.

    Notes
    -----
    * The file can be an open file or a filename.

    * Writes a simple uncompressed WAV file.
    * The bits-per-sample will be determined by the data-type.
    * To write multiple-channels, use a 2-D array of shape
      (Nsamples, Nchannels).

    """
    if hasattr(filename, "write"):
        fid = filename
    else:
        fid = open(filename, "wb")

    try:
        dkind = data.dtype.kind
        if not (
            dkind == "i" or dkind == "f" or (dkind == "u" and data.dtype.itemsize == 1)
        ):
            raise ValueError("Unsupported data type '%s'" % data.dtype)

        fid.write(b"RIFF")
        fid.write(b"\x00\x00\x00\x00")
        fid.write(b"WAVE")
        # fmt chunk
        fid.write(b"fmt ")
        if dkind == "f":
            comp = 3
        else:
            comp = 1
        if data.ndim == 1:
            noc = 1
        else:
            noc = data.shape[1]
        bits = data.dtype.itemsize * 8
        sbytes = rate * (bits // 8) * noc
        ba = noc * (bits // 8)
        fid.write(struct.pack("<ihHIIHH", 16, comp, noc, rate, sbytes, ba, bits))
        # data chunk
        fid.write(b"data")
        fid.write(struct.pack("<i", data.nbytes))
        if data.dtype.byteorder == ">" or (
            data.dtype.byteorder == "=" and sys.byteorder == "big"
        ):
            data = data.byteswap()
        _array_tofile(fid, data)

        # Determine file size and place it in correct
        #  position at start of the file.
        size = fid.tell()
        fid.seek(4)
        fid.write(struct.pack("<i", size - 8))

    finally:
        if not hasattr(filename, "write"):
            fid.close()
        else:
            fid.seek(0)