def to_bytes(seq):
    """convert a sequence to a bytes type"""
    if isinstance(seq, bytes):
        return seq
    elif isinstance(seq, bytearray):
        return bytes(seq)
    elif isinstance(seq, memoryview):
        return seq.tobytes()
    elif isinstance(seq, str):
        raise TypeError(
            'unicode strings are not supported, please encode to bytes: {!r}'
            .format(seq))
    else:
        return bytes(bytearray(seq))
