cast str or bytes to bytes

def cast_bytes(s, encoding='utf8', errors='strict'):
    """cast str or bytes to bytes"""
    if isinstance(s, bytes):
        return s
    elif isinstance(s, str):
        return s.encode(encoding, errors)
    else:
        raise TypeError("Expected unicode or bytes, got %r" % s)