mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
[struct] Add struct.pack
and struct.unpack
polyfills (#156977)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/156977 Approved by: https://github.com/XuehaiPan, https://github.com/jansel ghstack dependencies: #156975
This commit is contained in:
committed by
PyTorch MergeBot
parent
671e22a951
commit
f5314f89c8
@ -30,6 +30,7 @@ if TYPE_CHECKING:
|
||||
operator as operator,
|
||||
os as os,
|
||||
pytree as pytree,
|
||||
struct as struct,
|
||||
sys as sys,
|
||||
)
|
||||
|
||||
|
@ -19,6 +19,7 @@ POLYFILLED_MODULE_NAMES: tuple[str, ...] = (
|
||||
"operator",
|
||||
"os",
|
||||
"pytree",
|
||||
"struct",
|
||||
"sys",
|
||||
"fx",
|
||||
"tensor",
|
||||
|
27
torch/_dynamo/polyfills/struct.py
Normal file
27
torch/_dynamo/polyfills/struct.py
Normal file
@ -0,0 +1,27 @@
|
||||
"""
|
||||
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)
|
Reference in New Issue
Block a user