File size: 689 Bytes
9c37cfa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | from pathlib import Path
import cloudpickle
import dill
ROOT = Path(__file__).resolve().parent
class BenignCanary:
def __reduce__(self):
# Relative marker path: resolves under the current working directory during load.
marker = Path("logs") / "serialization_canary_loaded.txt"
return (marker.write_text, ("serialization canary was loaded\n",))
obj = BenignCanary()
(ROOT / "canary_cloudpickle.pkl").write_bytes(cloudpickle.dumps(obj, protocol=5))
(ROOT / "canary_dill.pkl").write_bytes(dill.dumps(obj, protocol=5))
print("rewrote portable canaries")
for p in [ROOT / "canary_cloudpickle.pkl", ROOT / "canary_dill.pkl"]:
print(p.name, p.stat().st_size)
|