compat.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from typing import Any, Union
  2. from .core import decode, encode
  3. def ToASCII(label: str) -> bytes:
  4. """Compatibility shim for :rfc:`3490` ``ToASCII``.
  5. Delegates to :func:`idna.encode` (IDNA 2008). Provided to ease porting
  6. of code written against the legacy :mod:`encodings.idna` API; new code
  7. should call :func:`idna.encode` directly.
  8. :param label: The label or domain to encode.
  9. :returns: The encoded form as ASCII :class:`bytes`.
  10. """
  11. return encode(label)
  12. def ToUnicode(label: Union[bytes, bytearray]) -> str:
  13. """Compatibility shim for :rfc:`3490` ``ToUnicode``.
  14. Delegates to :func:`idna.decode` (IDNA 2008). Provided to ease porting
  15. of code written against the legacy :mod:`encodings.idna` API; new code
  16. should call :func:`idna.decode` directly.
  17. :param label: The label or domain to decode.
  18. :returns: The decoded Unicode form.
  19. """
  20. return decode(label)
  21. def nameprep(s: Any) -> None:
  22. """Stub for :rfc:`3491` Nameprep, which is not used by IDNA 2008.
  23. IDNA 2008 (:rfc:`5891`) replaces Nameprep with the per-codepoint
  24. validity classes from :rfc:`5892`; this function exists only to
  25. return a clear error if legacy code attempts to call it.
  26. :raises NotImplementedError: Always.
  27. """
  28. raise NotImplementedError("IDNA 2008 does not utilise nameprep protocol")