_pickle.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # These functions are referenced from the pickle archives produced by
  2. # ScriptModule.save()
  3. # These (`build_*`) functions used to be used by `pickler.cpp` to specify
  4. # the type of the list for certain special types, but now all lists get
  5. # a type attached and restored via `restore_type_tag` below. The legacy
  6. # functions should stick around for backwards-compatibility.
  7. from typing import Union
  8. def build_intlist(data: list[int]) -> list[int]:
  9. return data
  10. def build_tensorlist(data: list[object]) -> list[object]:
  11. return data
  12. def build_doublelist(data: list[float]) -> list[float]:
  13. return data
  14. def build_boollist(data: list[bool]) -> list[bool]:
  15. return data
  16. def build_tensor_from_id(data: Union[int, object]) -> Union[int, None]:
  17. if isinstance(data, int):
  18. # just the id, can't really do anything
  19. return data
  20. return None
  21. def restore_type_tag(value: object, type_str: str) -> object:
  22. # The type_ptr is used by the jit unpickler to restore the full static type
  23. # to container types like list when they are re-loaded, but this doesn't
  24. # matter for Python, so just return the plain value
  25. return value