struct.py 618 B

123456789101112131415161718192021222324252627
  1. """
  2. Python polyfills for struct
  3. """
  4. from __future__ import annotations
  5. import struct
  6. from typing import Any
  7. from typing_extensions import Buffer
  8. from ..decorators import substitute_in_graph
  9. __all__ = [
  10. "pack",
  11. "unpack",
  12. ]
  13. @substitute_in_graph(struct.pack, can_constant_fold_through=True) # type: ignore[arg-type]
  14. def pack(fmt: bytes | str, /, *v: Any) -> bytes:
  15. return struct.pack(fmt, *v)
  16. @substitute_in_graph(struct.unpack, can_constant_fold_through=True) # type: ignore[arg-type]
  17. def unpack(format: bytes | str, buffer: Buffer, /) -> tuple[Any, ...]:
  18. return struct.unpack(format, buffer)