def verify_reshape(data, dtype, shape, allow_none=False):
    """Verify data is of type dtype and try to reshaped to shape."""

    try:
        t = np.dtype(dtype) if dtype is not None else None
    except __HOLE__:
        raise DataTypeError("Invalid data type: %s" % dtype)
    if data is None and allow_none:
        return np.asarray([], dtype=t)
    elif data is None:
        raise DataValueError("Invalid data: None is not allowed!")
    else:
        try:
            a = np.asarray(data, dtype=t)
        except ValueError:
            raise DataValueError("Invalid data: cannot convert to %s!" % dtype)
        try:
            # Destructively modify shape.  Fails if data are not
            # contiguous, but that's what we want anyway.
            a.shape = shape
            return a
        except ValueError:
            raise DataValueError("Invalid data: expected %d values, got %d!" %
                                 (np.prod(shape), np.asarray(data).size))
dataset/ETHPy150Open OP2/PyOP2/pyop2/utils.py/verify_reshape