def _dnsname_to_stdlib(name):
def idna_encode(name):<EOL><INDENT>"""<STR_LIT>"""<EOL>from pipenv.patched.notpip._vendor import idna<EOL>try:<EOL><INDENT>for prefix in [u'<STR_LIT>', u'<STR_LIT:.>']:<EOL><INDENT>if name.startswith(prefix):<EOL><INDENT>name = name[len(prefix):]<EOL>return prefix.encode('<STR_LIT:ascii>') + idna.encode(name)<EOL><DEDENT><DEDENT>return idna.encode(name)<EOL><DEDENT>except idna.core.IDNAError:<EOL><INDENT>return None<EOL><DEDENT><DEDENT>name = idna_encode(name)<EOL>if name is None:<EOL><INDENT>return None<EOL><DEDENT>elif sys.version_info >= (<NUM_LIT:3>, <NUM_LIT:0>):<EOL><INDENT>name = name.decode('<STR_LIT:utf-8>')<EOL><DEDENT>return name<EOL>
Converts a dNSName SubjectAlternativeName field to the form used by the
standard library on the given Python version.

Cryptography produces a dNSName as a unicode string that was idna-decoded
from ASCII bytes. We need to idna-encode that string to get it back, and
then on Python 3 we also need to convert to unicode via UTF-8 (the stdlib
uses PyUnicode_FromStringAndSize on it, which decodes via UTF-8).

If the name cannot be idna-encoded then we return None signalling that
the name given should be skipped.