Parse the payload of a close frame. Args: data: Payload of the close frame. Raises: ProtocolError: If data is ill-formed. UnicodeDecodeError: If the reason isn't valid UTF-8.
(cls, data: BytesLike)
| 383 | |
| 384 | @classmethod |
| 385 | def parse(cls, data: BytesLike) -> Close: |
| 386 | """ |
| 387 | Parse the payload of a close frame. |
| 388 | |
| 389 | Args: |
| 390 | data: Payload of the close frame. |
| 391 | |
| 392 | Raises: |
| 393 | ProtocolError: If data is ill-formed. |
| 394 | UnicodeDecodeError: If the reason isn't valid UTF-8. |
| 395 | |
| 396 | """ |
| 397 | if isinstance(data, memoryview): |
| 398 | raise AssertionError("only compressed outgoing frames use memoryview") |
| 399 | if len(data) >= 2: |
| 400 | (code,) = struct.unpack("!H", data[:2]) |
| 401 | reason = data[2:].decode() |
| 402 | close = cls(code, reason) |
| 403 | close.check() |
| 404 | return close |
| 405 | elif len(data) == 0: |
| 406 | return cls(CloseCode.NO_STATUS_RCVD, "") |
| 407 | else: |
| 408 | raise ProtocolError("close frame too short") |
| 409 | |
| 410 | def serialize(self) -> bytes: |
| 411 | """ |
nothing calls this directly
no test coverage detected