|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Sequence |
| 4 | +from typing import TYPE_CHECKING, Final |
| 5 | + |
| 6 | +try: |
| 7 | + from native_internal import ( |
| 8 | + Buffer as Buffer, |
| 9 | + read_bool as read_bool, |
| 10 | + read_float as read_float, |
| 11 | + read_int as read_int, |
| 12 | + read_str as read_str, |
| 13 | + write_bool as write_bool, |
| 14 | + write_float as write_float, |
| 15 | + write_int as write_int, |
| 16 | + write_str as write_str, |
| 17 | + ) |
| 18 | +except ImportError: |
| 19 | + # TODO: temporary, remove this after we publish mypy-native on PyPI. |
| 20 | + if not TYPE_CHECKING: |
| 21 | + |
| 22 | + class Buffer: |
| 23 | + def __init__(self, source: bytes = b"") -> None: |
| 24 | + raise NotImplementedError |
| 25 | + |
| 26 | + def getvalue(self) -> bytes: |
| 27 | + raise NotImplementedError |
| 28 | + |
| 29 | + def read_int(data: Buffer) -> int: |
| 30 | + raise NotImplementedError |
| 31 | + |
| 32 | + def write_int(data: Buffer, value: int) -> None: |
| 33 | + raise NotImplementedError |
| 34 | + |
| 35 | + def read_str(data: Buffer) -> str: |
| 36 | + raise NotImplementedError |
| 37 | + |
| 38 | + def write_str(data: Buffer, value: str) -> None: |
| 39 | + raise NotImplementedError |
| 40 | + |
| 41 | + def read_bool(data: Buffer) -> bool: |
| 42 | + raise NotImplementedError |
| 43 | + |
| 44 | + def write_bool(data: Buffer, value: bool) -> None: |
| 45 | + raise NotImplementedError |
| 46 | + |
| 47 | + def read_float(data: Buffer) -> float: |
| 48 | + raise NotImplementedError |
| 49 | + |
| 50 | + def write_float(data: Buffer, value: float) -> None: |
| 51 | + raise NotImplementedError |
| 52 | + |
| 53 | + |
| 54 | +LITERAL_INT: Final = 1 |
| 55 | +LITERAL_STR: Final = 2 |
| 56 | +LITERAL_BOOL: Final = 3 |
| 57 | +LITERAL_FLOAT: Final = 4 |
| 58 | +LITERAL_COMPLEX: Final = 5 |
| 59 | +LITERAL_NONE: Final = 6 |
| 60 | + |
| 61 | + |
| 62 | +def read_literal(data: Buffer, marker: int) -> int | str | bool | float: |
| 63 | + if marker == LITERAL_INT: |
| 64 | + return read_int(data) |
| 65 | + elif marker == LITERAL_STR: |
| 66 | + return read_str(data) |
| 67 | + elif marker == LITERAL_BOOL: |
| 68 | + return read_bool(data) |
| 69 | + elif marker == LITERAL_FLOAT: |
| 70 | + return read_float(data) |
| 71 | + assert False, f"Unknown literal marker {marker}" |
| 72 | + |
| 73 | + |
| 74 | +def write_literal(data: Buffer, value: int | str | bool | float | complex | None) -> None: |
| 75 | + if isinstance(value, bool): |
| 76 | + write_int(data, LITERAL_BOOL) |
| 77 | + write_bool(data, value) |
| 78 | + elif isinstance(value, int): |
| 79 | + write_int(data, LITERAL_INT) |
| 80 | + write_int(data, value) |
| 81 | + elif isinstance(value, str): |
| 82 | + write_int(data, LITERAL_STR) |
| 83 | + write_str(data, value) |
| 84 | + elif isinstance(value, float): |
| 85 | + write_int(data, LITERAL_FLOAT) |
| 86 | + write_float(data, value) |
| 87 | + elif isinstance(value, complex): |
| 88 | + write_int(data, LITERAL_COMPLEX) |
| 89 | + write_float(data, value.real) |
| 90 | + write_float(data, value.imag) |
| 91 | + else: |
| 92 | + write_int(data, LITERAL_NONE) |
| 93 | + |
| 94 | + |
| 95 | +def read_int_opt(data: Buffer) -> int | None: |
| 96 | + if read_bool(data): |
| 97 | + return read_int(data) |
| 98 | + return None |
| 99 | + |
| 100 | + |
| 101 | +def write_int_opt(data: Buffer, value: int | None) -> None: |
| 102 | + if value is not None: |
| 103 | + write_bool(data, True) |
| 104 | + write_int(data, value) |
| 105 | + else: |
| 106 | + write_bool(data, False) |
| 107 | + |
| 108 | + |
| 109 | +def read_str_opt(data: Buffer) -> str | None: |
| 110 | + if read_bool(data): |
| 111 | + return read_str(data) |
| 112 | + return None |
| 113 | + |
| 114 | + |
| 115 | +def write_str_opt(data: Buffer, value: str | None) -> None: |
| 116 | + if value is not None: |
| 117 | + write_bool(data, True) |
| 118 | + write_str(data, value) |
| 119 | + else: |
| 120 | + write_bool(data, False) |
| 121 | + |
| 122 | + |
| 123 | +def read_int_list(data: Buffer) -> list[int]: |
| 124 | + size = read_int(data) |
| 125 | + return [read_int(data) for _ in range(size)] |
| 126 | + |
| 127 | + |
| 128 | +def write_int_list(data: Buffer, value: list[int]) -> None: |
| 129 | + write_int(data, len(value)) |
| 130 | + for item in value: |
| 131 | + write_int(data, item) |
| 132 | + |
| 133 | + |
| 134 | +def read_str_list(data: Buffer) -> list[str]: |
| 135 | + size = read_int(data) |
| 136 | + return [read_str(data) for _ in range(size)] |
| 137 | + |
| 138 | + |
| 139 | +def write_str_list(data: Buffer, value: Sequence[str]) -> None: |
| 140 | + write_int(data, len(value)) |
| 141 | + for item in value: |
| 142 | + write_str(data, item) |
| 143 | + |
| 144 | + |
| 145 | +def read_str_opt_list(data: Buffer) -> list[str | None]: |
| 146 | + size = read_int(data) |
| 147 | + return [read_str_opt(data) for _ in range(size)] |
| 148 | + |
| 149 | + |
| 150 | +def write_str_opt_list(data: Buffer, value: list[str | None]) -> None: |
| 151 | + write_int(data, len(value)) |
| 152 | + for item in value: |
| 153 | + write_str_opt(data, item) |
0 commit comments