import jinja2


def punycode_filter(val):
    """Jinja2 filter function 'punycode' encodes the value per RFC 3492"""
    if isinstance(val, jinja2.Undefined):
        return "[undefined]"
    return val.encode("punycode").decode("utf-8")