|
1 | 1 | """Task dataclass: serializable envelope bundling a graph with arguments.""" |
2 | 2 |
|
| 3 | +import base64 |
| 4 | +import datetime as _dt |
3 | 5 | import json |
| 6 | +import pathlib |
4 | 7 | import uuid |
| 8 | +from decimal import Decimal |
5 | 9 | from typing import Any, Self |
6 | 10 | from dataclasses import field, dataclass |
7 | 11 |
|
8 | 12 | from pyfuse.core.errors import SignatureError |
9 | 13 | from pyfuse.core.signing import verify_signature, compute_signature |
10 | 14 |
|
11 | 15 | _OBJECT_SENTINEL = "__pyfuse_obj__" |
| 16 | +_BYTES_SENTINEL = "__pyfuse_bytes__" |
| 17 | +_BUILTIN_SENTINEL = "__pyfuse_builtin__" |
| 18 | + |
| 19 | + |
| 20 | +def _encode_builtin(o: object) -> dict[str, Any] | None: |
| 21 | + """Encode common stdlib types to a JSON-safe sentinel. |
| 22 | +
|
| 23 | + Returns ``None`` if *o* is not a recognised builtin -- the caller |
| 24 | + falls back to object-state serialization. |
| 25 | + """ |
| 26 | + # datetime is a subclass of date, so check it first. |
| 27 | + if isinstance(o, _dt.datetime): |
| 28 | + return {"type": "datetime", "value": o.isoformat()} |
| 29 | + if isinstance(o, _dt.date): |
| 30 | + return {"type": "date", "value": o.isoformat()} |
| 31 | + if isinstance(o, _dt.time): |
| 32 | + return {"type": "time", "value": o.isoformat()} |
| 33 | + if isinstance(o, _dt.timedelta): |
| 34 | + return {"type": "timedelta", "value": o.total_seconds()} |
| 35 | + if isinstance(o, Decimal): |
| 36 | + return {"type": "decimal", "value": str(o)} |
| 37 | + if isinstance(o, uuid.UUID): |
| 38 | + return {"type": "uuid", "value": o.hex} |
| 39 | + if isinstance(o, complex): |
| 40 | + return {"type": "complex", "value": [o.real, o.imag]} |
| 41 | + if isinstance(o, (set, frozenset)): |
| 42 | + kind = "frozenset" if isinstance(o, frozenset) else "set" |
| 43 | + return {"type": kind, "value": list(o)} |
| 44 | + if isinstance(o, pathlib.PurePath): |
| 45 | + return {"type": "path", "value": str(o), "cls": type(o).__name__} |
| 46 | + return None |
| 47 | + |
| 48 | + |
| 49 | +_PATH_CLASSES: dict[str, type[pathlib.PurePath]] = { |
| 50 | + "PurePath": pathlib.PurePath, |
| 51 | + "PurePosixPath": pathlib.PurePosixPath, |
| 52 | + "PureWindowsPath": pathlib.PureWindowsPath, |
| 53 | + "Path": pathlib.Path, |
| 54 | + "PosixPath": pathlib.PurePosixPath, |
| 55 | + "WindowsPath": pathlib.PureWindowsPath, |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +def _decode_builtin(info: dict[str, Any], namespace: dict[str, Any]) -> Any: |
| 60 | + """Reverse :func:`_encode_builtin`.""" |
| 61 | + kind = info.get("type") |
| 62 | + raw = info.get("value") |
| 63 | + if kind == "datetime": |
| 64 | + return _dt.datetime.fromisoformat(raw) |
| 65 | + if kind == "date": |
| 66 | + return _dt.date.fromisoformat(raw) |
| 67 | + if kind == "time": |
| 68 | + return _dt.time.fromisoformat(raw) |
| 69 | + if kind == "timedelta": |
| 70 | + return _dt.timedelta(seconds=raw) |
| 71 | + if kind == "decimal": |
| 72 | + return Decimal(raw) |
| 73 | + if kind == "uuid": |
| 74 | + return uuid.UUID(hex=raw) |
| 75 | + if kind == "complex": |
| 76 | + return complex(raw[0], raw[1]) |
| 77 | + if kind == "set": |
| 78 | + return {_resolve(v, namespace) for v in raw} |
| 79 | + if kind == "frozenset": |
| 80 | + return frozenset(_resolve(v, namespace) for v in raw) |
| 81 | + if kind == "path": |
| 82 | + # Try to honour the original class; fall back to a sensible |
| 83 | + # OS-portable default if the concrete subclass cannot be |
| 84 | + # instantiated on this platform. |
| 85 | + cls = _PATH_CLASSES.get(info.get("cls", ""), pathlib.PurePath) |
| 86 | + try: |
| 87 | + return cls(raw) |
| 88 | + except (NotImplementedError, TypeError): |
| 89 | + return pathlib.PurePath(raw) |
| 90 | + raise ValueError(f"Unknown builtin sentinel type: {kind!r}") |
12 | 91 |
|
13 | 92 |
|
14 | 93 | class _TaskEncoder(json.JSONEncoder): |
15 | 94 | """JSON encoder that serializes arbitrary objects via class name + __dict__.""" |
16 | 95 |
|
17 | 96 | def default(self, o: object) -> Any: |
| 97 | + if isinstance(o, (bytes, bytearray)): |
| 98 | + return { |
| 99 | + _BYTES_SENTINEL: { |
| 100 | + "data": base64.b64encode(bytes(o)).decode("ascii"), |
| 101 | + "type": type(o).__name__, |
| 102 | + } |
| 103 | + } |
| 104 | + builtin = _encode_builtin(o) |
| 105 | + if builtin is not None: |
| 106 | + return {_BUILTIN_SENTINEL: builtin} |
18 | 107 | if hasattr(o, "__dict__"): |
19 | 108 | state = o.__dict__ |
20 | 109 | elif hasattr(type(o), "__slots__"): |
@@ -62,6 +151,12 @@ def _resolve(value: Any, namespace: dict[str, Any]) -> Any: |
62 | 151 | return value |
63 | 152 | if len(value) == 1 and _OBJECT_SENTINEL in value: |
64 | 153 | return _reconstruct_object(value[_OBJECT_SENTINEL], namespace) |
| 154 | + if len(value) == 1 and _BYTES_SENTINEL in value: |
| 155 | + info = value[_BYTES_SENTINEL] |
| 156 | + raw = base64.b64decode(info["data"]) |
| 157 | + return bytearray(raw) if info.get("type") == "bytearray" else raw |
| 158 | + if len(value) == 1 and _BUILTIN_SENTINEL in value: |
| 159 | + return _decode_builtin(value[_BUILTIN_SENTINEL], namespace) |
65 | 160 | return {k: _resolve(v, namespace) for k, v in value.items()} |
66 | 161 |
|
67 | 162 |
|
|
0 commit comments