def to_bytes(value):
    """ str to bytes (py3k) """
    vtype = type(value)
    if vtype == bytes or vtype == type(None):
        return value
    try:
        return vtype.encode(value)
    except UnicodeEncodeError:
        pass
    return value
