def enc(val):
    """Returns the passed value utf-8 encoded if it is a string, or unchanged
    if it is already bytes.
    """
    try:
        return val.encode("utf-8")
    except AttributeError:
        # Not a string
        return val