compat.py 1.3 KB

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