|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import sys |
4 | 3 | import os |
5 | 4 | import socket |
6 | 5 | from time import mktime |
|
9 | 8 | from functools import wraps |
10 | 9 | from io import StringIO, BytesIO |
11 | 10 | from collections.abc import Callable |
12 | | -from typing import Any, TypeVar, cast, overload |
| 11 | +from typing import Any, TypeVar, cast |
13 | 12 |
|
14 | 13 | import email.charset |
15 | 14 | from email import generator |
|
25 | 24 | F = TypeVar('F', bound=Callable[..., Any]) |
26 | 25 |
|
27 | 26 |
|
28 | | -def to_native(x: str | bytes | None, charset: str = sys.getdefaultencoding(), |
29 | | - errors: str = 'strict') -> str | None: |
30 | | - if x is None or isinstance(x, str): |
31 | | - return x |
32 | | - return x.decode(charset, errors) |
33 | | - |
34 | | - |
35 | | -@overload |
36 | | -def to_unicode(x: None, charset: str = ..., errors: str = ...) -> None: ... |
37 | | -@overload |
38 | | -def to_unicode(x: str | bytes, charset: str = ..., errors: str = ...) -> str: ... |
39 | | -@overload |
40 | | -def to_unicode(x: Any, charset: str = ..., errors: str = ...) -> str | None: ... |
41 | | - |
42 | | -def to_unicode(x: Any, charset: str = sys.getdefaultencoding(), |
43 | | - errors: str = 'strict') -> str | None: |
44 | | - if x is None: |
45 | | - return None |
46 | | - if not isinstance(x, bytes): |
47 | | - return str(x) |
48 | | - return x.decode(charset, errors) |
49 | | - |
50 | | - |
51 | | -def to_bytes(x: str | bytes | bytearray | memoryview | None, |
52 | | - charset: str = sys.getdefaultencoding(), |
53 | | - errors: str = 'strict') -> bytes | None: |
54 | | - if x is None: |
55 | | - return None |
56 | | - if isinstance(x, (bytes, bytearray, memoryview)): |
57 | | - return bytes(x) |
58 | | - if isinstance(x, str): |
59 | | - return x.encode(charset, errors) |
60 | | - raise TypeError('Expected bytes') |
61 | | - |
62 | | - |
63 | 27 | def formataddr(pair: tuple[str | None, str]) -> str: |
64 | 28 | """ |
65 | 29 | Takes a 2-tuple of the form (realname, email_address) and returns RFC2822-like string. |
|
0 commit comments