Load an encoded data structure from bytes. Args: encoded: Encoded data in bytes. Raises: DecodeError: If an error was encountered decoding the string. Returns: Decoded data.
(encoded: bytes)
| 167 | |
| 168 | |
| 169 | def load(encoded: bytes) -> object: |
| 170 | """Load an encoded data structure from bytes. |
| 171 | |
| 172 | Args: |
| 173 | encoded: Encoded data in bytes. |
| 174 | |
| 175 | Raises: |
| 176 | DecodeError: If an error was encountered decoding the string. |
| 177 | |
| 178 | Returns: |
| 179 | Decoded data. |
| 180 | """ |
| 181 | if not isinstance(encoded, bytes): |
| 182 | raise TypeError("must be bytes") |
| 183 | max_position = len(encoded) |
| 184 | position = 0 |
| 185 | |
| 186 | def get_byte() -> bytes: |
| 187 | """Get an encoded byte and advance position. |
| 188 | |
| 189 | Raises: |
| 190 | DecodeError: If the end of the data was reached |
| 191 | |
| 192 | Returns: |
| 193 | A bytes object with a single byte. |
| 194 | """ |
| 195 | nonlocal position |
| 196 | if position >= max_position: |
| 197 | raise DecodeError("More data expected") |
| 198 | character = encoded[position : position + 1] |
| 199 | position += 1 |
| 200 | return character |
| 201 | |
| 202 | def peek_byte() -> bytes: |
| 203 | """Get the byte at the current position, but don't advance position. |
| 204 | |
| 205 | Returns: |
| 206 | A bytes object with a single byte. |
| 207 | """ |
| 208 | return encoded[position : position + 1] |
| 209 | |
| 210 | def get_bytes(size: int) -> bytes: |
| 211 | """Get a number of bytes of encode data. |
| 212 | |
| 213 | Args: |
| 214 | size: Number of bytes to retrieve. |
| 215 | |
| 216 | Raises: |
| 217 | DecodeError: If there aren't enough bytes. |
| 218 | |
| 219 | Returns: |
| 220 | A bytes object. |
| 221 | """ |
| 222 | nonlocal position |
| 223 | bytes_data = encoded[position : position + size] |
| 224 | if len(bytes_data) != size: |
| 225 | raise DecodeError(b"Missing bytes in {bytes_data!r}") |
| 226 | position += size |
searching dependent graphs…