2022-10-31 20:43:06 -05:00
|
|
|
from typing import AnyStr, Optional, overload
|
|
|
|
|
|
|
|
|
|
|
|
@overload
|
|
|
|
def utf8bytes(maybe_text: AnyStr) -> bytes:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@overload
|
|
|
|
def utf8bytes(maybe_text: None) -> None:
|
|
|
|
pass
|
2020-01-21 15:35:58 +09:00
|
|
|
|
|
|
|
|
|
|
|
def utf8bytes(maybe_text):
|
|
|
|
if maybe_text is None:
|
2022-10-31 20:43:06 -05:00
|
|
|
return None
|
|
|
|
if isinstance(maybe_text, bytes):
|
2020-01-21 15:35:58 +09:00
|
|
|
return maybe_text
|
2022-05-14 12:57:27 -05:00
|
|
|
return maybe_text.encode("utf-8")
|
2020-01-21 15:35:58 +09:00
|
|
|
|
|
|
|
|
2022-10-31 20:43:06 -05:00
|
|
|
@overload
|
|
|
|
def utf8text(maybe_bytes: AnyStr, errors="strict") -> str:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@overload
|
|
|
|
def utf8text(maybe_bytes: None, errors="strict") -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def utf8text(maybe_bytes, errors="strict") -> Optional[str]:
|
2020-01-21 15:35:58 +09:00
|
|
|
if maybe_bytes is None:
|
2022-10-31 20:43:06 -05:00
|
|
|
return None
|
|
|
|
if isinstance(maybe_bytes, str):
|
2020-01-21 15:35:58 +09:00
|
|
|
return maybe_bytes
|
2022-05-14 12:57:27 -05:00
|
|
|
return maybe_bytes.decode("utf-8", errors)
|