# Buggy code
def _read_array_header(fp, version):
    """
    see read_array_header_1_0
    """
    # Read an unsigned, little-endian short int which has the length of the
    # header.
    import struct

    if version == (1, 0):
        hlength_type = "<H"
    elif version == (2, 0):
        hlength_type = "<I"
    else:
        raise ValueError("Invalid version %r" % version)

    hlength_str = _read_bytes(fp, struct.calcsize(hlength_type), "array header length")
    header_length = struct.unpack(hlength_type, hlength_str)[0]
    header = _read_bytes(fp, header_length, "array header")

    # The header is a pretty-printed string representation of a literal
    # Python dictionary with trailing newlines padded to a ARRAY_ALIGN byte
    # boundary. The keys are strings.
    #   "shape" : tuple of int
    #   "fortran_order" : bool
    #   "descr" : dtype.descr
    header = _filter_header(header)
    try:
        d = safe_eval(header)
    except SyntaxError as e:
        msg = "Cannot parse header: %r\nException: %r"
        raise ValueError(msg % (header, e))
    if not isinstance(d, dict):
        msg = "Header is not a dictionary: %r"
        raise ValueError(msg % d)
    keys = sorted(d.keys())
    if keys != ["descr", "fortran_order", "shape"]:
        msg = "Header does not contain the correct keys: %r"
        raise ValueError(msg % (keys,))

    # Sanity-check the values.
    if not isinstance(d["shape"], tuple) or not numpy.all(
        [isinstance(x, (int, long)) for x in d["shape"]]
    ):
        msg = "shape is not valid: %r"
        raise ValueError(msg % (d["shape"],))
    if not isinstance(d["fortran_order"], bool):
        msg = "fortran_order is not a valid bool: %r"
        raise ValueError(msg % (d["fortran_order"],))
    try:
        dtype = numpy.dtype(d["descr"])
    except TypeError as e:
        msg = "descr is not a valid dtype descriptor: %r"
        raise ValueError(msg % (d["descr"],))

    return d["shape"], d["fortran_order"], dtype


# Traceback (ValueError)
import numpy as np

t = np.dtype('i1, i4, i1', align=True)
d = np.zeros(1, t)

np.save("test.npy", d)
data = np.load("test.npy")


Traceback (most recent call last):
File "D:\Projects\Cuda\Cuda_Git\pathwise\liinc\model\feeds\numpy_bug.py", line 8, in <module>
data = np.load("test.npy")
File "D:\Projects\Cuda\Cuda_Git\pathwise\pathwise\vendors\lib64\x64\python\numpy\lib\npyio.py", line 314, in load
return format.read_array(fid)
File "D:\Projects\Cuda\Cuda_Git\pathwise\pathwise\vendors\lib64\x64\python\numpy\lib\format.py", line 440, in read_array
shape, fortran_order, dtype = read_array_header_1_0(fp)
File "D:\Projects\Cuda\Cuda_Git\pathwise\pathwise\vendors\lib64\x64\python\numpy\lib\format.py", line 358, in read_array_header_1_0
dtype = numpy.dtype(d['descr'])
ValueError: two fields with the same name

# Fixed code
def _read_array_header(fp, version):
    """
    see read_array_header_1_0
    """
    # Read an unsigned, little-endian short int which has the length of the
    # header.
    import struct

    if version == (1, 0):
        hlength_type = "<H"
    elif version == (2, 0):
        hlength_type = "<I"
    else:
        raise ValueError("Invalid version %r" % version)

    hlength_str = _read_bytes(fp, struct.calcsize(hlength_type), "array header length")
    header_length = struct.unpack(hlength_type, hlength_str)[0]
    header = _read_bytes(fp, header_length, "array header")

    # The header is a pretty-printed string representation of a literal
    # Python dictionary with trailing newlines padded to a ARRAY_ALIGN byte
    # boundary. The keys are strings.
    #   "shape" : tuple of int
    #   "fortran_order" : bool
    #   "descr" : dtype.descr
    header = _filter_header(header)
    try:
        d = safe_eval(header)
    except SyntaxError as e:
        msg = "Cannot parse header: %r\nException: %r"
        raise ValueError(msg % (header, e))
    if not isinstance(d, dict):
        msg = "Header is not a dictionary: %r"
        raise ValueError(msg % d)
    keys = sorted(d.keys())
    if keys != ["descr", "fortran_order", "shape"]:
        msg = "Header does not contain the correct keys: %r"
        raise ValueError(msg % (keys,))

    # Sanity-check the values.
    if not isinstance(d["shape"], tuple) or not numpy.all(
        [isinstance(x, (int, long)) for x in d["shape"]]
    ):
        msg = "shape is not valid: %r"
        raise ValueError(msg % (d["shape"],))
    if not isinstance(d["fortran_order"], bool):
        msg = "fortran_order is not a valid bool: %r"
        raise ValueError(msg % (d["fortran_order"],))
    try:
        dtype = descr_to_dtype(d["descr"])
    except TypeError as e:
        msg = "descr is not a valid dtype descriptor: %r"
        raise ValueError(msg % (d["descr"],))

    return d["shape"], d["fortran_order"], dtype