mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Pull Request resolved: https://github.com/pytorch/pytorch/pull/156977 Approved by: https://github.com/XuehaiPan, https://github.com/jansel ghstack dependencies: #156975
28 lines
618 B
Python
28 lines
618 B
Python
"""
|
|
Python polyfills for struct
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import struct
|
|
from typing import Any
|
|
from typing_extensions import Buffer
|
|
|
|
from ..decorators import substitute_in_graph
|
|
|
|
|
|
__all__ = [
|
|
"pack",
|
|
"unpack",
|
|
]
|
|
|
|
|
|
@substitute_in_graph(struct.pack, can_constant_fold_through=True) # type: ignore[arg-type]
|
|
def pack(fmt: bytes | str, /, *v: Any) -> bytes:
|
|
return struct.pack(fmt, *v)
|
|
|
|
|
|
@substitute_in_graph(struct.unpack, can_constant_fold_through=True) # type: ignore[arg-type]
|
|
def unpack(format: bytes | str, buffer: Buffer, /) -> tuple[Any, ...]:
|
|
return struct.unpack(format, buffer)
|