summaryrefslogtreecommitdiff
path: root/wsgitools/internal.py
blob: 86a9d5a44636468f50a42f8e9dcef4df203612d7 (plain)
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
36
37
38
39
40
41
42
import sys
import typing

def bytes2str(bstr: bytes) -> str:
    assert isinstance(bstr, bytes)
    return bstr.decode("iso-8859-1") # always successful

def str2bytes(sstr: str) -> bytes:
    assert isinstance(sstr, str)
    return sstr.encode("iso-8859-1") # might fail, but spec says it doesn't

def textopen(filename: str, mode: str) -> typing.TextIO:
    # We use the same encoding as for all wsgi strings here.
    return open(filename, mode, encoding="iso-8859-1")

Environ = typing.Dict[str, typing.Any]

HeaderList = typing.List[typing.Tuple[str, str]]

if sys.version_info >= (3, 11):
    OptExcInfo = typing.Optional[
        typing.Tuple[type[BaseException], BaseException, typing.Any]
    ]
else:
    OptExcInfo = typing.Optional[
        typing.Tuple[typing.Any, BaseException, typing.Any]
    ]

WriteCallback = typing.Callable[[bytes], None]

if sys.version_info >= (3, 11):
    class StartResponse(typing.Protocol):
        def __call__(
            self, status: str, headers: HeaderList, exc_info: OptExcInfo = None
        ) -> WriteCallback:
            ...
else:
    StartResponse = typing.Callable[
        [str, HeaderList, OptExcInfo], WriteCallback
    ]

WsgiApp = typing.Callable[[Environ, StartResponse], typing.Iterable[bytes]]