1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
from __future__ import annotations
from w3lib._types import StrOrBytes
def to_unicode(
text: StrOrBytes, encoding: str | None = None, errors: str = "strict"
) -> str:
"""Return the unicode representation of a bytes object `text`. If `text`
is already an unicode object, return it as-is."""
if isinstance(text, str):
return text
if not isinstance(text, (bytes, str)):
raise TypeError(
f"to_unicode must receive bytes or str, got {type(text).__name__}"
)
if encoding is None:
encoding = "utf-8"
return text.decode(encoding, errors)
def to_bytes(
text: StrOrBytes, encoding: str | None = None, errors: str = "strict"
) -> bytes:
"""Return the binary representation of `text`. If `text`
is already a bytes object, return it as-is."""
if isinstance(text, bytes):
return text
if not isinstance(text, str):
raise TypeError(
f"to_bytes must receive str or bytes, got {type(text).__name__}"
)
if encoding is None:
encoding = "utf-8"
return text.encode(encoding, errors)
|